-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_userStream.cpp
More file actions
132 lines (124 loc) · 4.08 KB
/
example_userStream.cpp
File metadata and controls
132 lines (124 loc) · 4.08 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
#include <json/json.h>
#include <map>
#include <string>
#include <vector>
#include "binance_api.h"
#include "binance_websocket.h"
#define API_KEY "api key"
#define SECRET_KEY "secret_key"
// Some code to make terminal to have colors
#define KGRN "\033[0;32;32m"
#define KCYN "\033[0;36m"
#define KRED "\033[0;32;31m"
#define KYEL "\033[1;33m"
#define KBLU "\033[0;32;34m"
#define KCYN_L "\033[1;36m"
#define KBRN "\033[0;33m"
#define RESET "\033[0m"
std::map<std::string, std::map<std::string, double>> userBalance;
//---------------------------
void print_userBalance()
{
std::map<std::string, std::map<std::string, double>>::iterator it_i;
std::cout << "==================================" << std::endl;
for (it_i = userBalance.begin(); it_i != userBalance.end(); it_i++)
{
std::string symbol = (*it_i).first;
std::map<std::string, double> balance = (*it_i).second;
std::cout << "Symbol :" << symbol << ", ";
printf("Free : %.08f, ", balance["f"]);
printf("Locked : %.08f\n", balance["l"]);
}
}
//----------------------------------
int ws_userStream_OnData(Json::Value &json_result)
{
int i;
std::string action = json_result["e"].asString();
if (action == "executionReport")
{
std::string executionType = json_result["x"].asString();
std::string orderStatus = json_result["X"].asString();
std::string reason = json_result["r"].asString();
std::string symbol = json_result["s"].asString();
std::string side = json_result["S"].asString();
std::string orderType = json_result["o"].asString();
std::string orderId = json_result["i"].asString();
std::string price = json_result["p"].asString();
std::string qty = json_result["q"].asString();
if (executionType == "NEW")
{
if (orderStatus == "REJECTED")
{
printf("%sOrder Failed! Reason: %s\n%s", KRED, reason.c_str(), RESET);
}
printf("%s\n\n%s %s %s %s(%s) %s %s\n\n%s",
KGRN,
symbol.c_str(),
side.c_str(),
orderType.c_str(),
orderId.c_str(),
orderStatus.c_str(),
price.c_str(),
qty.c_str(),
RESET);
}
else
{
printf("%s\n\n%s %s %s %s %s\n\n%s",
KBLU,
symbol.c_str(),
side.c_str(),
executionType.c_str(),
orderType.c_str(),
orderId.c_str(),
RESET);
}
}
else if (action == "outboundAccountInfo")
{
for (i = 0; i < json_result["B"].size(); i++)
{
std::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();
}
return 0;
}
int ws_klines_onData(Json::Value &json_result)
{
printf("Event type:%s symbol:%s\n",
json_result["e"].asString().c_str(),
json_result["s"].asString().c_str());
return 0;
}
int main()
{
std::string api_key = API_KEY;
std::string secret_key = SECRET_KEY;
binance_cpp::core::BinanceAPI::Init(api_key, secret_key);
Json::Value result;
binance_cpp::financial_trading::spot_trading::account_endpoints::
AccountInformation::GetAccountInformation(5000, result);
for (int i = 0; i < result["balances"].size(); i++)
{
std::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();
binance_cpp::financial_trading::spot_trading::user_data_stream_endpoints::
UserDataStream::CreateListenKey(result);
std::cout << result << std::endl;
std::string ws_path = std::string("/ws/");
ws_path.append(result["listenKey"].asString());
BinanceCPP_websocket::connect_endpoint(ws_userStream_OnData, ws_path.c_str());
BinanceCPP_websocket::enter_event_loop();
return 0;
}