-
Notifications
You must be signed in to change notification settings - Fork 232
Expand file tree
/
Copy pathccapi_request.h
More file actions
320 lines (274 loc) · 12.3 KB
/
ccapi_request.h
File metadata and controls
320 lines (274 loc) · 12.3 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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#pragma once
#include <condition_variable>
#include <map>
#include <mutex>
#include <string>
#include "ccapi_cpp/ccapi_macro.h"
#include "ccapi_cpp/ccapi_util_private.h"
// We use macros instead of static constants in the Request class so that SWIG can properly generate C# bindings
#define CCAPI_REQUEST_OPERATION_TYPE_CUSTOM 0x100
#define CCAPI_REQUEST_OPERATION_TYPE_GENERIC_PUBLIC_REQUEST 0x200
#define CCAPI_REQUEST_OPERATION_TYPE_GENERIC_PRIVATE_REQUEST 0x300
#define CCAPI_REQUEST_OPERATION_TYPE_FIX 0x400
#define CCAPI_REQUEST_OPERATION_TYPE_MARKET_DATA 0x500
#define CCAPI_REQUEST_OPERATION_TYPE_EXECUTION_MANAGEMENT 0x600
#define CCAPI_REQUEST_OPERATION_TYPE_EXECUTION_MANAGEMENT_ORDER CCAPI_REQUEST_OPERATION_TYPE_EXECUTION_MANAGEMENT
#define CCAPI_REQUEST_OPERATION_TYPE_EXECUTION_MANAGEMENT_ACCOUNT 0x700
namespace ccapi {
/**
* A single request. Request objects are created using Request constructors. They are used with Session::sendRequest() or Session::sendRequestByWebsocket() or
* Session::sendRequestByFix(). The Request object contains the parameters for a single request. Once a Request has been created its fields can be further
* modified using the convenience functions appendParam() or appendFixParam() or setParamList() or setParamListFix(). A correlation id can be used as the unique
* identifier to tag all data associated with this request.
*/
class Request {
public:
enum class Operation {
UNKNOWN = 0,
CUSTOM = CCAPI_REQUEST_OPERATION_TYPE_CUSTOM,
GENERIC_PUBLIC_REQUEST = CCAPI_REQUEST_OPERATION_TYPE_GENERIC_PUBLIC_REQUEST,
GENERIC_PRIVATE_REQUEST = CCAPI_REQUEST_OPERATION_TYPE_GENERIC_PRIVATE_REQUEST,
FIX = CCAPI_REQUEST_OPERATION_TYPE_FIX,
GET_RECENT_TRADES = CCAPI_REQUEST_OPERATION_TYPE_MARKET_DATA,
GET_HISTORICAL_TRADES,
GET_RECENT_AGG_TRADES,
GET_HISTORICAL_AGG_TRADES,
GET_RECENT_CANDLESTICKS,
GET_HISTORICAL_CANDLESTICKS,
GET_MARKET_DEPTH,
GET_SERVER_TIME,
GET_INSTRUMENT,
GET_INSTRUMENTS,
GET_BBOS,
GET_TICKERS,
CREATE_ORDER = CCAPI_REQUEST_OPERATION_TYPE_EXECUTION_MANAGEMENT_ORDER,
CANCEL_ORDER,
GET_ORDER,
GET_OPEN_ORDERS,
CANCEL_OPEN_ORDERS,
GET_ACCOUNTS = CCAPI_REQUEST_OPERATION_TYPE_EXECUTION_MANAGEMENT_ACCOUNT,
GET_ACCOUNT_BALANCES,
GET_ACCOUNT_POSITIONS,
};
static std::string operationToString(Operation operation) {
std::string output;
switch (operation) {
case Operation::CUSTOM:
output = "CUSTOM";
break;
case Operation::UNKNOWN:
output = "UNKNOWN";
break;
case Operation::GENERIC_PUBLIC_REQUEST:
output = "GENERIC_PUBLIC_REQUEST";
break;
case Operation::GENERIC_PRIVATE_REQUEST:
output = "GENERIC_PRIVATE_REQUEST";
break;
case Operation::FIX:
output = "FIX";
break;
case Operation::GET_RECENT_TRADES:
output = "GET_RECENT_TRADES";
break;
case Operation::GET_HISTORICAL_TRADES:
output = "GET_HISTORICAL_TRADES";
break;
case Operation::GET_RECENT_AGG_TRADES:
output = "GET_RECENT_AGG_TRADES";
break;
case Operation::GET_HISTORICAL_AGG_TRADES:
output = "GET_HISTORICAL_AGG_TRADES";
break;
case Operation::GET_RECENT_CANDLESTICKS:
output = "GET_RECENT_CANDLESTICKS";
break;
case Operation::GET_HISTORICAL_CANDLESTICKS:
output = "GET_HISTORICAL_CANDLESTICKS";
break;
case Operation::GET_MARKET_DEPTH:
output = "GET_MARKET_DEPTH";
break;
case Operation::GET_SERVER_TIME:
output = "GET_SERVER_TIME";
break;
case Operation::GET_INSTRUMENT:
output = "GET_INSTRUMENT";
break;
case Operation::GET_INSTRUMENTS:
output = "GET_INSTRUMENTS";
break;
case Operation::GET_BBOS:
output = "GET_BBOS";
break;
case Operation::GET_TICKERS:
output = "GET_TICKERS";
break;
case Operation::CREATE_ORDER:
output = "CREATE_ORDER";
break;
case Operation::CANCEL_ORDER:
output = "CANCEL_ORDER";
break;
case Operation::GET_ORDER:
output = "GET_ORDER";
break;
case Operation::GET_OPEN_ORDERS:
output = "GET_OPEN_ORDERS";
break;
case Operation::CANCEL_OPEN_ORDERS:
output = "CANCEL_OPEN_ORDERS";
break;
case Operation::GET_ACCOUNTS:
output = "GET_ACCOUNTS";
break;
case Operation::GET_ACCOUNT_BALANCES:
output = "GET_ACCOUNT_BALANCES";
break;
case Operation::GET_ACCOUNT_POSITIONS:
output = "GET_ACCOUNT_POSITIONS";
break;
default:
CCAPI_LOGGER_FATAL(std::string(CCAPI_UNSUPPORTED_VALUE) + " " + std::to_string(static_cast<int>(operation)));
}
return output;
}
explicit Request(Operation operation = Operation::UNKNOWN, const std::string& exchange = "", const std::string& instrument = "",
const std::string& correlationId = "", const std::map<std::string, std::string>& credential = {})
: operation(operation), exchange(exchange), instrument(instrument), correlationId(correlationId), credential(credential) {
if (operation == Operation::CUSTOM) {
this->serviceName = CCAPI_UNKNOWN;
} else if (operation == Operation::GENERIC_PUBLIC_REQUEST) {
this->serviceName = CCAPI_MARKET_DATA;
} else if (operation == Operation::GENERIC_PRIVATE_REQUEST) {
this->serviceName = CCAPI_EXECUTION_MANAGEMENT;
} else if (operation == Operation::FIX) {
this->serviceName = CCAPI_FIX;
} else {
this->serviceName = static_cast<int>(operation) >= CCAPI_REQUEST_OPERATION_TYPE_EXECUTION_MANAGEMENT ? CCAPI_EXECUTION_MANAGEMENT : CCAPI_MARKET_DATA;
}
if (this->correlationId.empty()) {
this->correlationId = UtilString::generateRandomString(CCAPI_CORRELATION_ID_GENERATED_LENGTH);
}
}
std::string toString() const {
std::map<std::string, std::string> shortCredential;
for (const auto& x : credential) {
shortCredential.insert(std::make_pair(x.first, UtilString::firstNCharacter(x.second, CCAPI_CREDENTIAL_DISPLAY_LENGTH)));
}
std::string output =
"Request [operation = " + operationToString(operation) + ", exchange = " + exchange + ", marginType = " + marginType + ", instrument = " + instrument +
", serviceName = " + serviceName + ", correlationId = " + correlationId +
(this->serviceName == CCAPI_FIX ? ", paramListFix = " + ccapi::toString(paramListFix) : ", paramList = " + ccapi::toString(paramList)) +
", credential = " + ccapi::toString(shortCredential) + ", timeSent = " + UtilTime::getISOTimestamp(timeSent) + ", index = " + ccapi::toString(index) +
", localIpAddress = " + localIpAddress + ", baseUrl = " + baseUrl + "]";
return output;
}
const std::string& getCorrelationId() const { return correlationId; }
const std::string& getExchange() const { return exchange; }
const std::string& getMarginType() const { return marginType; }
const std::string& getInstrument() const { return instrument; }
const std::map<std::string, std::string>& getCredential() const { return credential; }
const std::string& getServiceName() const { return serviceName; }
void appendParam(const std::map<std::string, std::string>& param) { this->paramList.push_back(param); }
void appendFixParam(const std::vector<std::pair<int, std::string>>& param) { this->paramListFix.push_back(param); }
void appendParamListFix(const std::vector<std::vector<std::pair<int, std::string>>>& paramList) {
this->paramListFix.insert(std::end(this->paramListFix), std::begin(paramList), std::end(paramList));
}
void setParamListFix(const std::vector<std::vector<std::pair<int, std::string>>>& paramListFix) { this->paramListFix = paramListFix; }
Operation getOperation() const { return operation; }
const std::vector<std::map<std::string, std::string>>& getParamList() const { return paramList; }
const std::vector<std::vector<std::pair<int, std::string>>>& getParamListFix() const { return paramListFix; }
void setParamList(const std::vector<std::map<std::string, std::string>>& paramList) { this->paramList = paramList; }
std::map<std::string, std::string> getFirstParamWithDefault(const std::map<std::string, std::string> defaultValue = {}) const {
if (this->paramList.empty()) {
return defaultValue;
} else {
return this->paramList.front();
}
}
// 'getTimeSent' only works in C++. For other languages, please use 'getTimeSentISO'.
TimePoint getTimeSent() const { return timeSent; }
std::string getTimeSentISO() const { return UtilTime::getISOTimestamp(timeSent); }
std::pair<long long, long long> getTimeSentPair() const { return UtilTime::divide(timeSent); }
void setTimeSent(TimePoint timeSent) { this->timeSent = timeSent; }
int getIndex() const { return index; }
const std::string& getLocalIpAddress() const { return localIpAddress; }
const std::string& getBaseUrl() const { return baseUrl; }
const std::string& getHost() const { return host; }
const std::string& getPort() const { return port; }
void setExchange(const std::string& exchange) { this->exchange = exchange; }
void setInstrument(const std::string& instrument) { this->instrument = instrument; }
void setIndex(int index) { this->index = index; }
void setCredential(const std::map<std::string, std::string>& credential) { this->credential = credential; }
void setCorrelationId(const std::string& correlationId) { this->correlationId = correlationId; }
void setMarginType(const std::string& marginType) { this->marginType = marginType; }
void setLocalIpAddress(const std::string& localIpAddress) { this->localIpAddress = localIpAddress; }
void setBaseUrl(const std::string& baseUrl) {
this->baseUrl = baseUrl;
this->setBaseUrlParts();
}
void setBaseUrlParts() {
auto splitted1 = UtilString::split(this->baseUrl, "://");
if (splitted1.size() >= 2) {
auto splitted2 = UtilString::split(UtilString::split(splitted1.at(1), "/").at(0), ":");
this->host = splitted2.at(0);
if (splitted2.size() == 2) {
this->port = splitted2.at(1);
} else {
if (splitted1.at(0) == "https" || splitted1.at(0) == "wss") {
this->port = CCAPI_HTTPS_PORT_DEFAULT;
} else {
this->port = CCAPI_HTTP_PORT_DEFAULT;
}
}
}
}
std::string generateNextClientOrderId() {
static int64_t lastClientOrderIdUnixTimestampInSeconds = 0;
static int64_t lastClientOrderIdSequenceNumber = 0;
const auto& now = UtilTime::now();
const auto& unixTimestampInSeconds = std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch()).count();
if (lastClientOrderIdUnixTimestampInSeconds != unixTimestampInSeconds) {
lastClientOrderIdUnixTimestampInSeconds = unixTimestampInSeconds;
lastClientOrderIdSequenceNumber = 0;
} else {
++lastClientOrderIdSequenceNumber;
}
std::string nextClientOrderId;
if (this->exchange == CCAPI_EXCHANGE_NAME_BINANCE) {
nextClientOrderId += "x-";
nextClientOrderId += CCAPI_BINANCE_API_LINK_ID;
nextClientOrderId += "-";
} else if (this->exchange == CCAPI_EXCHANGE_NAME_BINANCE_USDS_FUTURES || this->exchange == CCAPI_EXCHANGE_NAME_BINANCE_COIN_FUTURES) {
nextClientOrderId += "x-";
nextClientOrderId += CCAPI_BINANCE_USDS_FUTURES_API_LINK_ID;
nextClientOrderId += "-";
} else if (this->exchange == CCAPI_EXCHANGE_NAME_HUOBI) {
nextClientOrderId += CCAPI_HTX_BROKER_ID;
nextClientOrderId += "-";
}
nextClientOrderId += std::to_string(lastClientOrderIdUnixTimestampInSeconds);
nextClientOrderId += UtilString::leftPadTo(std::to_string(lastClientOrderIdSequenceNumber), CCAPI_EM_CLIENT_ORDER_ID_SEQUENCE_NUMBER_PAD_TO_LENGTH, '0');
return nextClientOrderId;
}
#ifndef CCAPI_EXPOSE_INTERNAL
private:
#endif
Operation operation{Operation::UNKNOWN};
std::string exchange;
std::string marginType;
std::string instrument;
std::string serviceName;
std::string correlationId;
std::vector<std::map<std::string, std::string>> paramList;
std::map<std::string, std::string> credential;
std::vector<std::vector<std::pair<int, std::string>>> paramListFix;
TimePoint timeSent{std::chrono::seconds{0}};
int index{};
std::string localIpAddress;
std::string baseUrl;
std::string host;
std::string port;
};
} /* namespace ccapi */