-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_depthCache.cpp
More file actions
120 lines (101 loc) · 2.98 KB
/
example_depthCache.cpp
File metadata and controls
120 lines (101 loc) · 2.98 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
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
#include <json/json.h>
#include <map>
#include <string>
#include <vector>
#include "binance_api.h"
#include "binance_websocket.h"
std::map<std::string, std::map<double, double>> depthCache;
int lastUpdateId;
//------------------------------
void print_depthCache()
{
std::map<std::string, std::map<double, double>>::iterator it_i;
for (it_i = depthCache.begin(); it_i != depthCache.end(); it_i++)
{
std::string bid_or_ask = (*it_i).first;
std::cout << bid_or_ask << std::endl;
std::cout << "Price Qty" << std::endl;
std::map<double, double>::reverse_iterator it_j;
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);
}
}
}
//-------------
int ws_depth_onData(Json::Value &json_result)
{
int i;
int new_updateId = json_result["u"].asInt();
if (new_updateId > lastUpdateId)
{
for (i = 0; i < json_result["b"].size(); i++)
{
double price = atof(json_result["b"][i][0].asString().c_str());
double qty = atof(json_result["b"][i][1].asString().c_str());
if (qty == 0.0)
{
depthCache["bids"].erase(price);
}
else
{
depthCache["bids"][price] = qty;
}
}
for (i = 0; i < json_result["a"].size(); i++)
{
double price = atof(json_result["a"][i][0].asString().c_str());
double qty = atof(json_result["a"][i][1].asString().c_str());
if (qty == 0.0)
{
depthCache["asks"].erase(price);
}
else
{
depthCache["asks"][price] = qty;
}
}
lastUpdateId = new_updateId;
}
print_depthCache();
return 0;
}
//---------------------------
/*
To compile, type
make example_depthCache
*/
//--------------------------
int main()
{
Json::Value result;
long recvWindow = 10000;
// Market Depth
int i;
std::string symbol = "BNBBTC";
binance_cpp::core::BinanceAPI::Init();
binance_cpp::financial_trading::spot_trading::market_data_endpoints::
OrderBook::GetOrderBook(symbol.c_str(), 20, result);
// Initialize the lastUpdateId
lastUpdateId = result["lastUpdateId"].asInt64();
for (int i = 0; i < result["asks"].size(); i++)
{
double price = atof(result["asks"][i][0].asString().c_str());
double qty = atof(result["asks"][i][1].asString().c_str());
depthCache["asks"][price] = qty;
}
for (int i = 0; i < result["bids"].size(); i++)
{
double price = atof(result["bids"][i][0].asString().c_str());
double qty = atof(result["bids"][i][1].asString().c_str());
depthCache["bids"][price] = qty;
}
print_depthCache();
BinanceCPP_websocket::init();
BinanceCPP_websocket::connect_endpoint(ws_depth_onData, "/ws/bnbbtc@depth");
BinanceCPP_websocket::enter_event_loop();
}