This repository was archived by the owner on Apr 1, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGetCandles.cpp
More file actions
79 lines (65 loc) · 1.92 KB
/
GetCandles.cpp
File metadata and controls
79 lines (65 loc) · 1.92 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
#include <iostream>
#include <memory>
#include <string>
#include <boost/asio.hpp>
#include <boost/json.hpp>
#include <xapi/Xapi.hpp>
boost::asio::awaitable<void> printCandles(xapi::XStationClientStream &stream)
{
co_await stream.getCandles("US100");
// Use getKeepAlive, to keep the connection alive
co_await stream.getKeepAlive();
// Print 5 candle results from the stream
int counter = 0;
while (counter < 5)
{
auto result = co_await stream.listen();
// Check for result type, as you`ll get keep alive messages
// every 3 seconds and candle messages every minute
if (result["command"] == "candle")
{
std::cout << boost::json::serialize(result) << std::endl;
counter += 1;
}
}
co_await stream.stopCandles("US100");
co_await stream.stopKeepAlive();
co_return;
}
boost::asio::awaitable<void> run(boost::asio::io_context &context)
{
const boost::json::object accountCredentials = {
{"accountId", "accountId"},
{"password", "password"},
{"accountType", "demo"}
};
xapi::XStationClient user(context, accountCredentials);
try
{
co_await user.login();
auto userStream = user.getClientStream();
co_await userStream.open();
co_await printCandles(userStream);
co_await userStream.close();
co_await user.logout();
}
catch (xapi::exception::ConnectionClosed &e)
{
std::cout << "Connection failed: " << e.what() << std::endl;
}
catch (xapi::exception::LoginFailed &e)
{
std::cout << "Logging failed: " << e.what() << std::endl;
}
catch (std::exception &e)
{
std::cout << e.what() << std::endl;
}
}
int main(int argc, char const *argv[])
{
boost::asio::io_context context;
boost::asio::co_spawn(context, run(context), boost::asio::detached);
context.run();
return 0;
}