-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathsimple.cpp
More file actions
83 lines (77 loc) · 2.86 KB
/
simple.cpp
File metadata and controls
83 lines (77 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <csignal> // sig_atomic_t
#include <cstdint>
#include <databento/constants.hpp>
#include <databento/dbn.hpp>
#include <databento/enums.hpp>
#include <databento/live.hpp>
#include <databento/live_threaded.hpp>
#include <databento/log.hpp>
#include <databento/record.hpp>
#include <databento/symbol_map.hpp>
#include <databento/with_ts_out.hpp>
#include <iostream>
#include <memory>
static std::sig_atomic_t volatile gSignal;
int main() {
databento::PitSymbolMap symbol_mappings;
auto log_receiver =
std::make_unique<databento::ConsoleLogReceiver>(databento::LogLevel::Debug);
auto client = databento::LiveBuilder{}
.SetLogReceiver(log_receiver.get())
.SetSendTsOut(true)
.SetKeyFromEnv()
.SetDataset(databento::Dataset::GlbxMdp3)
.BuildThreaded();
// Set up signal handler for Ctrl+C
std::signal(SIGINT, [](int signal) { gSignal = signal; });
std::vector<std::string> symbols{"ESZ5", "ESZ5 C6200", "ESZ5 P5500"};
client.Subscribe(symbols, databento::Schema::Definition, databento::SType::RawSymbol);
client.Subscribe(symbols, databento::Schema::Mbo, databento::SType::RawSymbol);
auto metadata_callback = [](databento::Metadata&& metadata) {
std::cout << metadata << '\n';
};
auto record_callback = [&symbol_mappings](const databento::Record& rec) {
using databento::RType;
switch (rec.RType()) {
case RType::Mbo: {
auto ohlcv = rec.Get<databento::WithTsOut<databento::MboMsg>>();
std::cout << "Received tick for " << symbol_mappings[ohlcv.rec.hd.instrument_id]
<< " with ts_out " << ohlcv.ts_out.time_since_epoch().count() << ": "
<< ohlcv.rec << '\n';
break;
}
case RType::InstrumentDef: {
std::cout << "Received definition: " << rec.Get<databento::InstrumentDefMsg>()
<< '\n';
break;
}
case RType::SymbolMapping: {
auto mapping = rec.Get<databento::SymbolMappingMsg>();
symbol_mappings.OnSymbolMapping(mapping);
break;
}
case RType::System: {
const auto& system_msg = rec.Get<databento::SystemMsg>();
if (!system_msg.IsHeartbeat()) {
std::cout << "Received system msg: " << system_msg.Msg() << '\n';
}
break;
}
case RType::Error: {
std::cerr << "Received error from gateway: "
<< rec.Get<databento::ErrorMsg>().Err() << '\n';
break;
}
default: {
std::cerr << "Received unknown record with rtype " << std::hex
<< static_cast<std::uint16_t>(rec.RType()) << '\n';
}
}
return databento::KeepGoing::Continue;
};
client.Start(metadata_callback, record_callback);
while (::gSignal == 0) {
std::this_thread::sleep_for(std::chrono::milliseconds{100});
}
return 0;
}