-
Notifications
You must be signed in to change notification settings - Fork 43
/
example.cpp
173 lines (135 loc) · 6.05 KB
/
example.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <iostream>
#include <json/json.h>
#include <map>
#include <string>
#include <vector>
#include "binance.h"
#include "binance_logger.h"
#include "binance_websocket.h"
using namespace binance;
using namespace std;
void print_depthCache(map < string, map <double,double> > depthCache) {
map < string, map <double,double> >::iterator it_i;
for ( it_i = depthCache.begin() ; it_i != depthCache.end() ; it_i++ ) {
string bid_or_ask = (*it_i).first ;
cout << bid_or_ask << endl ;
cout << "Price Qty" << endl ;
map <double,double>::reverse_iterator it_j;
cout << "==================================" << endl;
for ( it_j = depthCache[bid_or_ask].rbegin() ; it_j != depthCache[bid_or_ask].rend() ; it_j++ ) {
double price = (*it_j).first;
double qty = (*it_j).second;
printf("%.08f %.08f\n", price, qty );
}
cout << "==================================" << endl;
}
}
void print_klinesCache(map<long, map<string, double> > klinesCache)
{
for (map<long, map<string, double> >::iterator it_i = klinesCache.begin();
it_i != klinesCache.end(); it_i++)
{
long start_of_candle = (*it_i).first;
map <string,double> candle_obj = (*it_i).second;
cout << "s:" << start_of_candle << ",";
cout << "o:" << candle_obj["o"] << ",";
cout << "h:" << candle_obj["h"] << ",";
cout << "l:" << candle_obj["l"] << ",";
cout << "c:" << candle_obj["c"] << ",";
cout << "v:" << candle_obj["v"] ;
cout << " " << endl;
}
cout << "==================================" << endl;
}
int ws_klines_onData(Json::Value& json_result)
{
map<long, map<string, double> > klinesCache;
map < string, map <double,double> > depthCache;
if (json_result["lastUpdateId"].isNumeric() && json_result["bids"].isArray() && json_result["asks"].isArray()) {
int i;
for ( i = 0 ; i < json_result["bids"].size() ; i++ ) {
double price = atof( json_result["bids"][i][0].asString().c_str());
double qty = atof( json_result["bids"][i][1].asString().c_str());
if ( qty == 0.0 ) {
depthCache["bids"].erase(price);
} else {
depthCache["bids"][price] = qty;
}
}
for ( i = 0 ; i < json_result["asks"].size() ; i++ ) {
double price = atof( json_result["asks"][i][0].asString().c_str());
double qty = atof( json_result["asks"][i][1].asString().c_str());
if ( qty == 0.0 ) {
depthCache["asks"].erase(price);
} else {
depthCache["asks"][price] = qty;
}
}
print_depthCache(depthCache);
}else if(json_result["e"].asString() == "kline"){
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());
cout << "=============="<< json_result["s"].asString() <<"====================" << endl;
print_klinesCache(klinesCache);
}else if(json_result["e"].asString() == "24hrMiniTicker"){
cout << "=====24hrMiniTicker\\Begin========="<< json_result["s"].asString() <<"=======24hrMiniTicker\\Begin" << endl;
cout << json_result << endl;
cout << "=====24hrMiniTicker\\End========="<< json_result["s"].asString() <<"=======24hrMiniTicker\\End====" << endl;
}
return 0;
}
int main()
{
Logger::set_debug_level(1);
Logger::set_debug_logfp(stderr);
Json::Value result;
Server server;
Market market(server);
// Klines / CandleStick
BINANCE_ERR_CHECK(market.getKlines(result, "BNBUSDT", "1w", 0, 0, 10));
map<long, map<string, double> > klinesCache;
for (Json::Value::ArrayIndex 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(klinesCache);
while (true){
Websocket::init();
sleep (1);
Websocket::connect_endpoint(ws_klines_onData, "/ws/poebtc@depth20@1000ms");
Websocket::connect_endpoint(ws_klines_onData, "/ws/batbtc@depth20@1000ms");
Websocket::connect_endpoint(ws_klines_onData, "/ws/xtzusdt@depth20@1000ms");
Websocket::connect_endpoint(ws_klines_onData, "/ws/bnbusdt@depth20@1000ms");
Websocket::connect_endpoint(ws_klines_onData, "/ws/algobnb@depth20@1000ms");
Websocket::connect_endpoint(ws_klines_onData, "/ws/poebtc@kline_1m");
Websocket::connect_endpoint(ws_klines_onData, "/ws/batbtc@kline_1m");
Websocket::connect_endpoint(ws_klines_onData, "/ws/xtzusdt@kline_1m");
Websocket::connect_endpoint(ws_klines_onData, "/ws/bnbusdt@kline_1m");
Websocket::connect_endpoint(ws_klines_onData, "/ws/algobnb@kline_1m");
Websocket::connect_endpoint(ws_klines_onData, "/ws/xtzbtc@depth20@1000ms");
Websocket::connect_endpoint(ws_klines_onData, "/ws/xtzbtc@miniTicker");
Websocket::connect_endpoint(ws_klines_onData, "/ws/xrpbtc@depth20@1000ms");
Websocket::connect_endpoint(ws_klines_onData, "/ws/xrpbtc@miniTicker");
Websocket::connect_endpoint(ws_klines_onData, "/ws/eosusdt@depth20@1000ms");
Websocket::connect_endpoint(ws_klines_onData, "/ws/eosusdt@miniTicker");
Websocket::connect_endpoint(ws_klines_onData, "/ws/algousdt@depth20@1000ms");
Websocket::connect_endpoint(ws_klines_onData, "/ws/algousdt@miniTicker");
Websocket::connect_endpoint(ws_klines_onData, "/ws/xrpbnb@depth20@1000ms");
Websocket::connect_endpoint(ws_klines_onData, "/ws/xrpbnb@miniTicker");
Websocket::connect_endpoint(ws_klines_onData, "/ws/ethusdt@depth20@1000ms");
Websocket::connect_endpoint(ws_klines_onData, "/ws/ethusdt@miniTicker");
Websocket::enter_event_loop();
cout << "error exiting enter_event_loop and we will try again after 5sec" << endl;
sleep(5);
}
return 0;
}