-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.cpp
More file actions
72 lines (55 loc) · 2.43 KB
/
main.cpp
File metadata and controls
72 lines (55 loc) · 2.43 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
// Copyright (c) 2025 Devexperts LLC.
// SPDX-License-Identifier: MPL-2.0
#include <dxfeed_graal_cpp_api/api.hpp>
#include <fmt/chrono.h>
#include <fmt/format.h>
#include <fmt/std.h>
#include <iostream>
using namespace dxfcpp;
using namespace dxfcpp::literals;
using namespace std::literals;
// Demonstrates how to connect to an endpoint, subscribe to market data events,
// handle reconnections and re-subscribing.
int main() {
try {
const auto address = "demo.dxfeed.com:7300"; // The address of the DxFeed endpoint.
const auto symbol = "ETH/USD:GDAX"; // The symbol for which we want to receive quotes.
// Disable QD logging.
// Logging::init();
// Create a new endpoint and add a listener for state changes.
const auto endpoint = DXEndpoint::create();
endpoint->addStateChangeListener([](DXEndpoint::State oldState, DXEndpoint::State newState) {
std::cout << fmt::format("Connection state changed: {}->{}", DXEndpoint::stateToString(oldState),
DXEndpoint::stateToString(newState))
<< std::endl;
});
// Connect to the endpoint using the specified address.
endpoint->connect(address);
// Create a subscription for Quote events.
const auto subscription = endpoint->getFeed()->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(symbol);
// Wait for five seconds to allow some quotes to be received.
std::this_thread::sleep_for(5s);
// Disconnect from the endpoint.
endpoint->disconnect();
// Wait for another five seconds to ensure quotes stop coming in.
std::this_thread::sleep_for(5s);
// Reconnect to the endpoint.
// The subscription is automatically re-subscribed, and quotes start coming into the listener again.
// Another address can also be passed on.
endpoint->connect(address);
// Keep the application running indefinitely.
std::cin.get();
} catch (const RuntimeException &e) {
std::cerr << e << '\n';
return 1;
}
return 0;
}