-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_klines.cpp
More file actions
89 lines (73 loc) · 2.67 KB
/
example_klines.cpp
File metadata and controls
89 lines (73 loc) · 2.67 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
84
85
86
87
88
89
#include <json/json.h>
#include <map>
#include <string>
#include <vector>
#include "binance_api.h"
#include "binance_websocket.h"
std::map<long, std::map<std::string, double>> klinesCache;
//------------------
void print_klinesCache()
{
std::map<long, std::map<std::string, double>>::iterator it_i;
std::cout << "==================================" << std::endl;
for (it_i = klinesCache.begin(); it_i != klinesCache.end(); it_i++)
{
long start_of_candle = (*it_i).first;
std::map<std::string, double> candle_obj = (*it_i).second;
std::cout << "s:" << start_of_candle << ",";
std::cout << "o:" << candle_obj["o"] << ",";
std::cout << "h:" << candle_obj["h"] << ",";
std::cout << "l:" << candle_obj["l"] << ",";
std::cout << "c:" << candle_obj["c"] << ",";
std::cout << "v:" << candle_obj["v"];
std::cout << " " << std::endl;
}
}
//-------------
int ws_klines_onData(Json::Value &json_result)
{
long start_of_candle = json_result["k"]["t"].asInt64();
klinesCache[start_of_candle]["o"] =
atof(json_result["k"]["o"].asString().c_str());
klinesCache[start_of_candle]["h"] =
atof(json_result["k"]["h"].asString().c_str());
klinesCache[start_of_candle]["l"] =
atof(json_result["k"]["l"].asString().c_str());
klinesCache[start_of_candle]["c"] =
atof(json_result["k"]["c"].asString().c_str());
klinesCache[start_of_candle]["v"] =
atof(json_result["k"]["v"].asString().c_str());
print_klinesCache();
return 0;
}
//---------------------------
/*
To compile, type
make example_klines
*/
//--------------------------
int main()
{
Json::Value result;
long recvWindow = 10000;
binance_cpp::core::BinanceAPI::Init();
// Klines/CandleStick
binance_cpp::financial_trading::spot_trading::market_data_endpoints::
TradeData::GetKlinesCandlestickData("BNBBTC", "1h", 10, 0, 0, result);
for (int i = 0; i < result.size(); i++)
{
long start_of_candle = result[i][0].asInt64();
klinesCache[start_of_candle]["o"] = atof(result[i][1].asString().c_str());
klinesCache[start_of_candle]["h"] = atof(result[i][2].asString().c_str());
klinesCache[start_of_candle]["l"] = atof(result[i][3].asString().c_str());
klinesCache[start_of_candle]["c"] = atof(result[i][4].asString().c_str());
klinesCache[start_of_candle]["v"] = atof(result[i][5].asString().c_str());
}
print_klinesCache();
// Klines/Candlestick update via websocket
BinanceCPP_websocket::init();
BinanceCPP_websocket::connect_endpoint(ws_klines_onData,
"/ws/bnbbtc@kline_1m");
BinanceCPP_websocket::enter_event_loop();
return 0;
}