-
Notifications
You must be signed in to change notification settings - Fork 207
/
Copy pathccapi_message.h
283 lines (281 loc) · 11 KB
/
ccapi_message.h
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
#ifndef INCLUDE_CCAPI_CPP_CCAPI_MESSAGE_H_
#define INCLUDE_CCAPI_CPP_CCAPI_MESSAGE_H_
#include <chrono>
#include <vector>
#include "ccapi_cpp/ccapi_element.h"
#include "ccapi_cpp/ccapi_logger.h"
namespace ccapi {
/**
* A handle to a single message. Message objects are obtained from the getMessageList() function of the Event object. Each Message is associated with one or
* more correlation id values. The Message contents are represented as Elements and can be accessed via the getElementList() function. Each Message object
* consists of an Type attribute and a RecapType attribute. The exchange timestamp (if any) associated with the Messsage object can be retrieved via the
* getTime() function. The library timestamp can be retrieved via the getTimeReceived() function.
*/
class Message CCAPI_FINAL {
public:
enum class RecapType {
UNKNOWN,
SOLICITED, // A recap. For market depth, it represents the initial order book snapshot. For public trade, it represents the most recent historical trades.
// This is the first batch of data points.
NONE, // Normal data tick, not a recap. For market depth, it represents the updated order book state. For public trade, it represents the new trades. These
// are the batches of data points after the first batch.
};
static std::string recapTypeToString(RecapType recapType) {
std::string output;
switch (recapType) {
case RecapType::UNKNOWN:
output = "UNKNOWN";
break;
case RecapType::NONE:
output = "NONE";
break;
case RecapType::SOLICITED:
output = "SOLICITED";
break;
default:
CCAPI_LOGGER_FATAL(CCAPI_UNSUPPORTED_VALUE);
}
return output;
}
enum class Type {
UNKNOWN,
AUTHORIZATION_SUCCESS,
AUTHORIZATION_FAILURE,
MARKET_DATA_EVENTS_MARKET_DEPTH,
MARKET_DATA_EVENTS_TRADE,
MARKET_DATA_EVENTS_AGG_TRADE,
MARKET_DATA_EVENTS_CANDLESTICK,
EXECUTION_MANAGEMENT_EVENTS_ORDER_UPDATE,
EXECUTION_MANAGEMENT_EVENTS_PRIVATE_TRADE,
EXECUTION_MANAGEMENT_EVENTS_BALANCE_UPDATE,
EXECUTION_MANAGEMENT_EVENTS_POSITION_UPDATE,
SUBSCRIPTION_STARTED,
SUBSCRIPTION_FAILURE,
SESSION_CONNECTION_UP,
SESSION_CONNECTION_DOWN,
INCORRECT_STATE_FOUND,
CREATE_ORDER,
CANCEL_ORDER,
GET_ORDER,
GET_OPEN_ORDERS,
CANCEL_OPEN_ORDERS,
GET_ACCOUNTS,
GET_ACCOUNT_BALANCES,
GET_ACCOUNT_POSITIONS,
GET_RECENT_TRADES,
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,
RESPONSE_ERROR,
REQUEST_FAILURE,
GENERIC_ERROR,
CUSTOM,
FIX,
FIX_FAILURE,
GENERIC_PUBLIC_REQUEST,
GENERIC_PUBLIC_SUBSCRIPTION,
GENERIC_PRIVATE_REQUEST,
};
static std::string typeToString(Type type) {
std::string output;
switch (type) {
case Type::UNKNOWN:
output = "UNKNOWN";
break;
case Type::MARKET_DATA_EVENTS_MARKET_DEPTH:
output = "MARKET_DATA_EVENTS_MARKET_DEPTH";
break;
case Type::MARKET_DATA_EVENTS_TRADE:
output = "MARKET_DATA_EVENTS_TRADE";
break;
case Type::MARKET_DATA_EVENTS_AGG_TRADE:
output = "MARKET_DATA_EVENTS_AGG_TRADE";
break;
case Type::MARKET_DATA_EVENTS_CANDLESTICK:
output = "MARKET_DATA_EVENTS_CANDLESTICK";
break;
case Type::AUTHORIZATION_SUCCESS:
output = "AUTHORIZATION_SUCCESS";
break;
case Type::AUTHORIZATION_FAILURE:
output = "AUTHORIZATION_FAILURE";
break;
case Type::EXECUTION_MANAGEMENT_EVENTS_ORDER_UPDATE:
output = "EXECUTION_MANAGEMENT_EVENTS_ORDER_UPDATE";
break;
case Type::EXECUTION_MANAGEMENT_EVENTS_PRIVATE_TRADE:
output = "EXECUTION_MANAGEMENT_EVENTS_PRIVATE_TRADE";
break;
case Type::EXECUTION_MANAGEMENT_EVENTS_BALANCE_UPDATE:
output = "EXECUTION_MANAGEMENT_EVENTS_BALANCE_UPDATE";
break;
case Type::EXECUTION_MANAGEMENT_EVENTS_POSITION_UPDATE:
output = "EXECUTION_MANAGEMENT_EVENTS_POSITION_UPDATE";
break;
case Type::SUBSCRIPTION_STARTED:
output = "SUBSCRIPTION_STARTED";
break;
case Type::SUBSCRIPTION_FAILURE:
output = "SUBSCRIPTION_FAILURE";
break;
case Type::SESSION_CONNECTION_UP:
output = "SESSION_CONNECTION_UP";
break;
case Type::SESSION_CONNECTION_DOWN:
output = "SESSION_CONNECTION_DOWN";
break;
case Type::INCORRECT_STATE_FOUND:
output = "INCORRECT_STATE_FOUND";
break;
case Type::CREATE_ORDER:
output = "CREATE_ORDER";
break;
case Type::CANCEL_ORDER:
output = "CANCEL_ORDER";
break;
case Type::GET_ORDER:
output = "GET_ORDER";
break;
case Type::GET_OPEN_ORDERS:
output = "GET_OPEN_ORDERS";
break;
case Type::CANCEL_OPEN_ORDERS:
output = "CANCEL_OPEN_ORDERS";
break;
case Type::GET_ACCOUNTS:
output = "GET_ACCOUNTS";
break;
case Type::GET_ACCOUNT_BALANCES:
output = "GET_ACCOUNT_BALANCES";
break;
case Type::GET_ACCOUNT_POSITIONS:
output = "GET_ACCOUNT_POSITIONS";
break;
case Type::GET_RECENT_TRADES:
output = "GET_RECENT_TRADES";
break;
case Type::GET_HISTORICAL_TRADES:
output = "GET_HISTORICAL_TRADES";
break;
case Type::GET_RECENT_AGG_TRADES:
output = "GET_RECENT_AGG_TRADES";
break;
case Type::GET_HISTORICAL_AGG_TRADES:
output = "GET_HISTORICAL_AGG_TRADES";
break;
case Type::GET_RECENT_CANDLESTICKS:
output = "GET_RECENT_CANDLESTICKS";
break;
case Type::GET_HISTORICAL_CANDLESTICKS:
output = "GET_HISTORICAL_CANDLESTICKS";
break;
case Type::GET_MARKET_DEPTH:
output = "GET_MARKET_DEPTH";
break;
case Type::GET_SERVER_TIME:
output = "GET_SERVER_TIME";
break;
case Type::GET_INSTRUMENT:
output = "GET_INSTRUMENT";
break;
case Type::GET_INSTRUMENTS:
output = "GET_INSTRUMENTS";
break;
case Type::GET_BBOS:
output = "GET_BBOS";
break;
case Type::RESPONSE_ERROR:
output = "RESPONSE_ERROR";
break;
case Type::REQUEST_FAILURE:
output = "REQUEST_FAILURE";
break;
case Type::GENERIC_ERROR:
output = "GENERIC_ERROR";
break;
case Type::CUSTOM:
output = "CUSTOM";
break;
case Type::FIX:
output = "FIX";
break;
case Type::FIX_FAILURE:
output = "FIX_FAILURE";
break;
case Type::GENERIC_PUBLIC_REQUEST:
output = "GENERIC_PUBLIC_REQUEST";
break;
case Type::GENERIC_PUBLIC_SUBSCRIPTION:
output = "GENERIC_PUBLIC_SUBSCRIPTION";
break;
case Type::GENERIC_PRIVATE_REQUEST:
output = "GENERIC_PRIVATE_REQUEST";
break;
default:
CCAPI_LOGGER_FATAL(CCAPI_UNSUPPORTED_VALUE);
}
return output;
}
std::string toString() const {
std::string output = "Message [type = " + typeToString(type) + ", recapType = " + recapTypeToString(recapType) +
", time = " + UtilTime::getISOTimestamp(time) + ", timeReceived = " + UtilTime::getISOTimestamp(timeReceived) +
", elementList = " + ccapi::firstNToString(elementList, 10) + ", correlationIdList = " + ccapi::toString(correlationIdList) +
", secondaryCorrelationIdMap = " + ccapi::toString(secondaryCorrelationIdMap) + "]";
return output;
}
std::string toStringPretty(const int space = 2, const int leftToIndent = 0, const bool indentFirstLine = true) const {
std::string sl(leftToIndent, ' ');
std::string ss(leftToIndent + space, ' ');
std::string output = (indentFirstLine ? sl : "") + "Message [\n" + ss + "type = " + typeToString(type) + ",\n" + ss +
"recapType = " + recapTypeToString(recapType) + ",\n" + ss + "time = " + UtilTime::getISOTimestamp(time) + ",\n" + ss +
"timeReceived = " + UtilTime::getISOTimestamp(timeReceived) + ",\n" + ss +
"elementList = " + ccapi::firstNToStringPretty(elementList, 10, space, space + leftToIndent, false) + ",\n" + ss +
"correlationIdList = " + ccapi::toString(correlationIdList) + ",\n" + ss +
"secondaryCorrelationIdMap = " + ccapi::toString(secondaryCorrelationIdMap) + "\n" + sl + "]";
return output;
}
const std::vector<Element>& getElementList() const { return elementList; }
void setElementList(const std::vector<Element>& elementList) { this->elementList = elementList; }
void setElementList(std::vector<Element>& elementList) { this->elementList = std::move(elementList); }
const std::vector<std::string>& getCorrelationIdList() const { return correlationIdList; }
const std::map<std::string, std::string>& getSecondaryCorrelationIdMap() const { return secondaryCorrelationIdMap; }
void setCorrelationIdList(const std::vector<std::string>& correlationIdList) { this->correlationIdList = correlationIdList; }
void setSecondaryCorrelationIdMap(const std::map<std::string, std::string>& secondaryCorrelationIdMap) {
this->secondaryCorrelationIdMap = secondaryCorrelationIdMap;
}
// 'getTime' only works in C++. For other languages, please use 'getTimeISO'.
TimePoint getTime() const { return time; }
std::string getTimeISO() const { return UtilTime::getISOTimestamp(time); }
std::pair<long long, long long> getTimeUnix() const { return UtilTime::divide(time); }
std::pair<long long, long long> getTimePair() const { return UtilTime::divide(time); }
void setTime(TimePoint time) { this->time = time; }
RecapType getRecapType() const { return recapType; }
void setRecapType(RecapType recapType) { this->recapType = recapType; }
Type getType() const { return type; }
void setType(Type type) { this->type = type; }
// 'getTimeReceived' only works in C++. For other languages, please use 'getTimeReceivedISO'.
TimePoint getTimeReceived() const { return timeReceived; }
std::string getTimeReceivedISO() const { return UtilTime::getISOTimestamp(timeReceived); }
std::pair<long long, long long> getTimeReceivedUnix() const { return UtilTime::divide(timeReceived); }
std::pair<long long, long long> getTimeReceivedPair() const { return UtilTime::divide(timeReceived); }
void setTimeReceived(TimePoint timeReceived) { this->timeReceived = timeReceived; }
#ifndef CCAPI_EXPOSE_INTERNAL
private:
#endif
TimePoint time{std::chrono::seconds{0}};
TimePoint timeReceived{std::chrono::seconds{0}};
std::vector<Element> elementList;
std::vector<std::string> correlationIdList;
std::map<std::string, std::string> secondaryCorrelationIdMap;
Type type{Type::UNKNOWN};
RecapType recapType{RecapType::UNKNOWN};
};
} /* namespace ccapi */
#endif // INCLUDE_CCAPI_CPP_CCAPI_MESSAGE_H_