- Notifications
You must be signed in to change notification settings - Fork 138
Expand file tree
/
Copy pathexample_userStream.cpp
More file actions
Latest commit
140 lines (93 loc) · 3.45 KB
/
Copy pathexample_userStream.cpp
File metadata and controls
140 lines (93 loc) · 3.45 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include<map>
#include<vector>
#include<string>
#include"binacpp.h"
#include"binacpp_websocket.h"
#include<json/json.h>
usingnamespacestd;
#defineAPI_KEY"api key"
#defineSECRET_KEY"secret_key"
// Some code to make terminal to have colors
#defineKGRN"\033[0;32;32m"
#defineKCYN"\033[0;36m"
#defineKRED"\033[0;32;31m"
#defineKYEL"\033[1;33m"
#defineKBLU"\033[0;32;34m"
#defineKCYN_L"\033[1;36m"
#defineKBRN"\033[0;33m"
#defineRESET"\033[0m"
map < string, map <string,double> > userBalance;
//---------------
voidprint_userBalance() {
map < string, map<string,double> >::iterator it_i;
cout << "==================================" << endl;
for ( it_i = userBalance.begin() ; it_i != userBalance.end() ; it_i++ ) {
string symbol = (*it_i).first;
map <string,double> balance = (*it_i).second;
cout << "Symbol :" << symbol << ", ";
printf("Free : %.08f, ", balance["f"] );
printf("Locked : %.08f " , balance["l"] );
cout << "" << endl;
}
}
//---------------
intws_userStream_OnData( Json::Value &json_result ) {
int i;
string action = json_result["e"].asString();
if ( action == "executionReport" ) {
string executionType = json_result["x"].asString();
string orderStatus = json_result["X"].asString();
string reason = json_result["r"].asString();
string symbol = json_result["s"].asString();
string side = json_result["S"].asString();
string orderType = json_result["o"].asString();
string orderId = json_result["i"].asString();
string price = json_result["p"].asString();
string qty = json_result["q"].asString();
if ( executionType == "NEW" ) {
if ( orderStatus == "REJECTED" ) {
printf(KRED"Order Failed! Reason: %s\n"RESET, reason.c_str() );
}
printf(KGRN"\n\n%s %s %s %s(%s) %s %s\n\n"RESET, symbol.c_str() , side.c_str() , orderType.c_str() , orderId.c_str() , orderStatus.c_str(), price.c_str(), qty.c_str() );
return0;
}
printf(KBLU"\n\n%s %s %s %s %s\n\n"RESET, symbol.c_str() , side.c_str() , executionType.c_str() , orderType.c_str() , orderId.c_str() );
} elseif ( action == "outboundAccountInfo" ) {
// Update user balance
for ( i = 0 ; i < json_result["B"].size() ; i++ ) {
string symbol = json_result["B"][i]["a"].asString();
userBalance[symbol]["f"] = atof( json_result["B"][i]["f"].asString().c_str() );
userBalance[symbol]["l"] = atof( json_result["B"][i]["f"].asString().c_str() );
}
print_userBalance();
}
}
//---------------------------
/*
To compile, type
make example_userStream
*/
//--------------------------
intmain() {
Json::Value result;
long recvWindow = 10000;
string api_key = API_KEY;
string secret_key = SECRET_KEY;
BinaCPP::init( api_key , secret_key );
// User Balance
BinaCPP::get_account( recvWindow , result );
for ( int i = 0 ; i < result["balances"].size() ; i++ ) {
string symbol = result["balances"][i]["asset"].asString();
userBalance[symbol]["f"] = atof( result["balances"][i]["free"].asString().c_str() );
userBalance[symbol]["l"] = atof( result["balances"][i]["locked"].asString().c_str() );
}
print_userBalance();
// User data stream
BinaCPP::start_userDataStream(result );
cout << result << endl;
string ws_path = string("/ws/");
ws_path.append( result["listenKey"].asString() );
BinaCPP_websocket::init();
BinaCPP_websocket::connect_endpoint( ws_userStream_OnData , ws_path.c_str() );
BinaCPP_websocket::enter_event_loop();
}