forked from antonred/SmartCOM3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestRobot.cpp
391 lines (369 loc) · 9.91 KB
/
TestRobot.cpp
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#include "TestRobot.h"
TestRobot::TestRobot(const char *server, unsigned short port, const char *login, const char *password)
{
printf("TestRobot::TestRobot()\n");
symbols["SBER"] = "";
symbols["VTBR"] = "";
symbols["GAZP"] = "";
symbols["ROSN"] = "";
std::string version = "UNKNOWN";
ErrorCode ercode = GetClientVersionString(&version);
printf("TestRobot::GetClientVersionString() %s SmartCOM3 dll version: %s\n",
GetErrorCodeString(ercode), version.c_str());
SetLogPath("C:\\logs"); // to store logs in C:\ on Windows you must have administrator permission
SetLogLevel(4);
SetAsyncConnectionMode(true);
SetCalcPosition(true);
SetDisconnectTimeout(3);
ercode = ConfigureLibrary();
if (ercode != ErrorCode_Success) {
printf("TestRobot::Connect() ConfigureLibrary error\n");
exit(1);
}
ercode = Connect(server, port, login, password);
// for demo server, if bad user name or password, lib calls Disconnected(reason) with zero length of reason string
// but if real - correctly calls Disconnected(reason) with "Bad user name or password"
if (ercode == ErrorCode_Success) {
printf("TestRobot::Connect() Connecting to %s:%d with login %s, please wait...\n", server, port, login);
} else {
printf("TestRobot::Connect() Connection error: %s. "
"Possibly log path (C:\\logs) doesn't exist or "
"you don't have write permission.\n", GetErrorCodeString(ercode));
exit(1);
}
printf("TestRobot::TestRobot() OK\n\n");
}
TestRobot::~TestRobot()
{
printf("TestRobot::~TestRobot()\n");
bool connected;
IsConnected(&connected);
if (connected) {
printf("TestRobot::~TestRobot() Disconnecting...\n");
Disconnect();
} else {
printf("TestRobot::~TestRobot() Not connected\n");
}
printf("TestRobot::~TestRobot() OK\n");
}
/* SmartCOM3 events implementation */
void TestRobot::Connected()
{
printf("\nTestRobot::Connected()\n");
printf("TestRobot::GetSymbols() %s\n", GetErrorCodeString(GetSymbols()));
printf("TestRobot::GetPortfolioList() %s\n", GetErrorCodeString(GetPortfolioList()));
}
void TestRobot::Disconnected(const char *reason)
{
printf("TestRobot::Disconnected(%s)\n", reason);
}
void TestRobot::UpdateQuote(
const char *symbol,
time_t datetime,
double open,
double high,
double low,
double close,
double last,
double volume,
double size,
double bid,
double ask,
double bidsize,
double asksize,
double open_int,
double go_buy,
double go_sell,
double go_base,
double go_base_backed,
double high_limit,
double low_limit,
TradingStatus trading_status,
double volat,
double theor_price)
{
printf("TestRobot::UpdateQuote(%s) %s ask %.5f bid %.5f status %d - %s\n",
symbol, GetDatetimeString(datetime).c_str(), ask, bid, trading_status, GetTradingStatusString(trading_status));
}
void TestRobot::UpdateBidAsk(
const char *symbol,
long row,
long nrows,
double bid,
double bidsize,
double ask,
double asksize)
{
if (row == 0) printf("TestRobot::UpdateBidAsk() %s %ld:%ld %.5f %.f %.5f %.f\n",
symbol, row, nrows, bid, bidsize, ask, asksize);
}
void TestRobot::AddTick(
const char *symbol,
time_t datetime,
double price,
double volume,
const char *tradeno,
OrderAction action)
{
printf("TestRobot::AddTick(%s) %s %.5f %.f %s %s\n",
symbol, GetDatetimeString(datetime).c_str(), price, volume, tradeno, GetOrderActionString(action));
}
void TestRobot::AddBar(
long row,
long nrows,
const char *symbol,
BarInterval interval,
time_t datetime,
double open,
double high,
double low,
double close,
double volume,
double open_int)
{
if (row == 0 || row == nrows - 1)
printf("TestRobot::AddBar() %ld:%ld %s %s %s %.5f %.5f %.5f %.5f %.f %.f\n",
row, nrows, symbol,
GetBarIntervalString(interval),
GetDatetimeString(datetime).c_str(),
open, high, low, close, volume, open_int);
}
void TestRobot::SetPortfolio(
const char *portfolio,
double cash,
double leverage,
double comission,
double saldo)
{
printf("TestRobot::SetPortfolio() %s %.2f %.f %.2f %.2f\n",
portfolio, cash, leverage, comission, saldo);
}
void TestRobot::AddTrade(
const char *portfolio,
const char *symbol,
const char *orderid,
double price,
double amount,
time_t datetime,
const char *tradeno)
{
printf("TestRobot::AddTrade() %s %s %s %.5f %.f %s %sf\n",
portfolio, symbol, orderid, price, amount,
GetDatetimeString(datetime).c_str(),
tradeno);
}
void TestRobot::UpdateOrder(
const char *portfolio,
const char *symbol,
OrderState state,
OrderAction action,
OrderType type,
OrderValidity validity,
double price,
double amount,
double stop,
double filled,
time_t datetime,
const char *orderid,
const char *orderno,
long status_mask,
long cookie)
{
printf("TestRobot::UpdateOrder() %s %s %s %s %s %s %.5f %.f %.5f %.f %s %s %s %ld %ld\n",
portfolio, symbol,
GetOrderStateString(state),
GetOrderActionString(action),
GetOrderTypeString(type),
GetOrderValidityString(validity),
price, amount, stop, filled,
GetDatetimeString(datetime).c_str(),
orderid, orderno, status_mask, cookie);
}
void TestRobot::UpdatePosition(
const char *portfolio,
const char *symbol,
double avprice,
double amount,
double planned)
{
printf("TestRobot::UpdatePosition() %s %s %.f %.f %.f\n",
portfolio, symbol, avprice, amount, planned);
}
void TestRobot::AddTickHistory(
long row,
long nrows,
const char *symbol,
time_t datetime,
double price,
double volume,
const char *tradeno,
OrderAction action)
{
printf("TestRobot::AddTickHistory() %ld:%ld %s %s %.5f %.f %s %s\n",
row, nrows, symbol,
GetDatetimeString(datetime).c_str(),
price, volume, tradeno,
GetOrderActionString(action));
}
void TestRobot::AddSymbol(
long row,
long nrows,
const char *symbol,
const char *short_name,
const char *int_name,
const char *type,
long decimals,
long lot_size,
double punkt,
double step,
const char *sec_ext_id,
const char *sec_exch_name,
time_t expiry_date,
double days_before_expiry,
double strike)
{
auto it = symbols.find(symbol);
if (it != symbols.end()) it->second = short_name;
if (row == nrows - 1) {
printf("TestRobot::AddSymbol() received all of %ld symbols\n", nrows);
for (auto &pair : symbols) {
printf("TestRobot::AddSymbol() added symbol '%s' with short_name '%s'\n",
pair.first.c_str(), pair.second.c_str());
}
//printf("TestRobot::ListenQuotes(%s) %s\n", symbols.begin()->first.c_str(), GetErrorCodeString(ListenQuotes(symbols.begin()->first.c_str())));
//printf("TestRobot::ListenBidAsks(%s) %s\n", symbols.begin()->first.c_str(), GetErrorCodeString(ListenBidAsks(symbols.begin()->first.c_str())));
printf("TestRobot::ListenTicks(%s) %s\n", symbols.begin()->first.c_str(), GetErrorCodeString(ListenTicks(symbols.begin()->first.c_str())));
}
}
void TestRobot::OrderSucceeded(
long cookie,
const char *orderid)
{
printf("TestRobot::OrderSucceeded() %ld %s\n", cookie, orderid);
}
void TestRobot::OrderFailed(
long cookie,
const char *orderid,
const char *reason)
{
printf("TestRobot::OrderFailed() %ld %s %s\n", cookie, orderid, reason);
}
void TestRobot::AddPortfolio(
long row,
long nrows,
const char *portfolioName,
const char *portfolioExch,
PortfolioStatus portfolioStatus)
{
printf("TestRobot::AddPortfolio(%ld/%ld) name %s exchage %s status %s\n",
row, nrows, portfolioName, portfolioExch, GetPortfolioStatusString(portfolioStatus));
}
void TestRobot::SetSubscribtionCheckResult(long result)
{
printf("TestRobot::SetSubscribtionCheckResult() %ld\n", result);
}
void TestRobot::SetMyTrade(
long row,
long nrows,
const char *portfolio,
const char *symbol,
time_t datetime,
double price,
double volume,
const char *tradeno,
OrderAction buysell,
const char *orderno)
{
printf("TestRobot::SetMyTrade() %ld:%ld %s %s %s %.5f %.f %s %s %s\n",
row, nrows, portfolio, symbol,
GetDatetimeString(datetime).c_str(),
price, volume, tradeno,
GetOrderActionString(buysell),
orderno);
}
void TestRobot::SetMyOrder(
long row,
long nrows,
const char *portfolio,
const char *symbol,
OrderState state,
OrderAction action,
OrderType type,
OrderValidity validity,
double price,
double amount,
double stop,
double filled,
time_t datetime,
const char *id,
const char *no,
long cookie)
{
printf("TestRobot::SetMyOrder() %ld:%ld %s %s %s %s %s %s %.5f %.f %.5f %.f %s %s %s %ld\n",
row, nrows, portfolio, symbol,
GetOrderStateString(state),
GetOrderActionString(action),
GetOrderTypeString(type),
GetOrderValidityString(validity),
price, amount, stop, filled,
GetDatetimeString(datetime).c_str(),
id, no, cookie);
}
void TestRobot::SetMyClosePos(
long row,
long nrows,
const char *portfolio,
const char *symbol,
double amount,
double price_buy,
double price_sell,
time_t postime,
const char *order_open,
const char *order_close)
{
printf("TestRobot::SetMyClosePos() %ld:%ld %s %s %.f %.5f %.5f %s %s %s\n",
row, nrows - 1, portfolio, symbol, amount, price_buy, price_sell,
GetDatetimeString(postime).c_str(), order_open, order_close);
}
void TestRobot::OrderCancelSucceeded(const char *orderid)
{
printf("TestRobot::OrderCancelSucceeded(%s)\n", orderid);
}
void TestRobot::OrderCancelFailed(const char *orderid)
{
printf("TestRobot::OrderCancelFailed(%s)\n", orderid);
}
void TestRobot::OrderMoveSucceeded(const char *orderid)
{
printf("TestRobot::OrderMoveSucceeded(%s)\n", orderid);
}
void TestRobot::OrderMoveFailed(const char *orderid)
{
printf("TestRobot::OrderMoveFailed(%s)\n", orderid);
}
int main(int argc, char **argv)
{
SetConsoleOutputCP(CP_UTF8);
if (argc != 5) {
printf("Usage: SmartCOM3.exe <server> <port> <login> <password>\n");
printf("E.g.: SmartCOM3.exe mxdemo.ittrade.ru 8443 LOGIN PASSWORD\n");
return 0;
}
const char *server = argv[1];
unsigned int port = atoi(argv[2]);
assert(port > 0 && port <= 65535);
const char *login = argv[3];
const char *password = argv[4];
TestRobot *robot = nullptr;
try {
robot = new TestRobot(server,port,login,password);
} catch (std::runtime_error &er) {
printf("Robot error: %s\n", er.what());
return 0;
}
printf("Press ENTER to exit\n");
getchar();
delete robot;
TestRobot::UninitializeApartments();
return 0;
}