-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
82 lines (63 loc) · 2.71 KB
/
main.cpp
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
// Copyright (c) 2024 Devexperts LLC.
// SPDX-License-Identifier: MPL-2.0
#include <dxfeed_graal_cpp_api/api.hpp>
#include <chrono>
#include <mutex>
#include <random>
#include <string>
#include <fmt/chrono.h>
#include <fmt/format.h>
#include <fmt/ostream.h>
#include <fmt/std.h>
using namespace dxfcpp;
using namespace dxfcpp::literals;
using namespace std::literals;
std::random_device rd; // a seed source for the random number engine
std::mt19937 gen(rd()); // mersenne_twister_engine seeded with rd()
// Demonstrates how to connect to endpoint requires authentication token, subscribe to market data events, and handle
// periodic token updates.
int main() {
try {
const auto address = "demo.dxfeed.com:7300";
// Disable QD logging.
// Logging::init();
auto generateToken = [] {
std::uniform_int_distribution<std::size_t> lengthDistrib(4u, 9u);
std::uniform_int_distribution<> letterDistrib('a', 'z');
std::string token(lengthDistrib(gen), ' ');
for (char &i : token) {
i = static_cast<char>(letterDistrib(gen));
}
return token;
};
// Add a listener for state changes to the default application-wide singleton instance of DXEndpoint.
DXEndpoint::getInstance()->addStateChangeListener([](DXEndpoint::State oldState, DXEndpoint::State newState) {
std::cout << fmt::format("Connection state changed: {}->{}", DXEndpoint::stateToString(oldState),
DXEndpoint::stateToString(newState))
<< std::endl;
});
// Set up a timer to periodically update the token and reconnect every 10 seconds.
// The first connection will be made immediately.
// After reconnection, all existing subscriptions will be re-subscribed automatically.
auto updateTokenTimer = dxfcpp::Timer::schedule(
[address, &generateToken] {
DXEndpoint::getInstance()->connect(fmt::format("{}[login=entitle:{}]", address, generateToken()));
},
0, 10s);
// Create a subscription for Quote events.
auto subscription = DXFeed::getInstance()->createSubscription(Quote::TYPE);
subscription->addEventListener<Quote>([](const auto "es) {
// Event listener that prints each received event.
for (const auto "e : quotes) {
std::cout << quote << std::endl;
}
});
// Add the specified symbol to the subscription.
subscription->addSymbols("ETH/USD:GDAX");
std::cin.get();
} catch (const RuntimeException &e) {
std::cerr << e << '\n';
return 1;
}
return 0;
}