-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorder.cpp
501 lines (433 loc) · 16.5 KB
/
order.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
#include "order.h"
TOrder::TOrder(Telegram& telegram, std::vector<std::string>& symbols_list, callback_event event_handler) : m_telegram{telegram},
m_symbols_list(symbols_list),
m_event_handler{event_handler}
{
m_view.insert(std::make_pair("symbol", std::make_pair("пара: ", "")));
m_view.insert(std::make_pair("newClientOrderId", std::make_pair("имя: ", "")));
m_view.insert(std::make_pair("side", std::make_pair("операция: ", "")));
m_view.insert(std::make_pair("quantity", std::make_pair("количество: ", "")));
m_view.insert(std::make_pair("price", std::make_pair("цена: ", "")));
m_view.insert(std::make_pair("stopPrice", std::make_pair("стоп - цена: ", "")));
m_view.insert(std::make_pair("type", std::make_pair("тип: ", "")));
m_view.insert(std::make_pair("timeInForce", std::make_pair("вид: ", "")));
}
void TOrder::del()
{
m_state = OS_Delete;
m_telegram.set_input_mode(Telegram::IM_Order);
engine(std::string());
}
void TOrder::list()
{
m_state = OS_List;
m_telegram.set_input_mode(Telegram::IM_Order);
engine(std::string());
}
void TOrder::start()
{
m_state = OS_Start;
m_telegram.set_input_mode(Telegram::IM_Order);
engine(std::string());
}
void TOrder::prev()
{
int st = static_cast<int>(m_state) - 1;
m_state = static_cast<State>(st);
}
void TOrder::print()
{
std::string str;
str = "------------------------------\n";
str += std::string("НОВЫЙ ОРДЕР") + '\n';
str += "------------------------------\n";
for(const auto& line : m_view)
str += line.second.first + line.second.second + '\n';
str += "------------------------------\n";
m_telegram.send_message(m_telegram.chat_id(), str);
}
void TOrder::reset_view()
{
for(auto& line : m_view)
line.second.second.clear();
}
std::string TOrder::first_arg(const std::string& str)
{
std::vector<std::string> args;
boost::algorithm::split(args, str, boost::is_any_of(" "));
return args.empty() ? "" : args.at(0);
}
bool TOrder::input_double(const std::string val, double& field, const std::string& name, const std::string& emsg)
{
bool result{true};
try
{
field = boost::lexical_cast<double>(val);
m_view[name].second = val;
}
catch (const boost::bad_lexical_cast &e)
{
m_telegram.send_message(m_telegram.chat_id(),
emsg,
"",
Telegram::Replay);
result = false;
}
return result;
}
void TOrder::engine(const std::string& data)
{
switch(m_state)
{
case OS_Start:
if(m_symbols_list.empty())
{
m_telegram.set_input_mode(Telegram::IM_Command);
return;
}
reset_view();
m_telegram.send_message(m_telegram.chat_id(),
"Введите имя ордера",
"",
Telegram::Replay);
set_state(OS_Name);
return;
case OS_Name:
if(!m_repeat)
{
m_order.newClientOrderId = data;
m_view["newClientOrderId"].second = data;
print();
}
else
{
m_repeat = false;
}
if(m_symbols_list.size() > 1)
{
for(const auto& sit: m_symbols_list)
keys.push_back(sit);
m_keyboard.add_keys(keys);
m_telegram.send_message(m_telegram.chat_id(),
"Выбор валюты:",
"",
Telegram::Keyboard,
m_keyboard.keyboard());
set_state(OS_Symbol);
}
else
{
m_order.symbol = *m_symbols_list.begin() + "USDT";
m_view["symbol"].second = m_order.symbol;
set_state(OS_InputType);
break;
}
return;
case OS_Symbol:
m_order.symbol = first_arg(data) + "USDT";
m_view["symbol"].second = m_order.symbol;
set_state(OS_InputType);
break;
case OS_InputType:
keys.clear();
m_keyboard.clear();
keys.push_back("LIMIT");
keys.push_back("MARKET");
m_keyboard.add_keys(keys);
m_telegram.send_message(m_telegram.chat_id(),
"Тип ордера?",
"",
Telegram::Keyboard,
m_keyboard.keyboard());
set_state(OS_Type);
return;
case OS_Type:
if(first_arg(data) == "$h")
{
help();
return;
}
m_order.type = BncOrder::string_to_type(first_arg(data));
m_view["type"].second = first_arg(data);
keys.clear();
m_keyboard.clear();
keys.push_back("BUY - покупка");
keys.push_back("SELL - продажа");
m_keyboard.add_keys(keys);
m_telegram.send_message(m_telegram.chat_id(),
"Тип операции",
"",
Telegram::Keyboard,
m_keyboard.keyboard());
set_state(OS_Side);
return;
case OS_Side:
if(!m_repeat)
{
m_order.side = BncOrder::string_to_side(first_arg(data));
m_view["side"].second = first_arg(data);
}
else
{
m_repeat = false;
}
print();
m_telegram.send_message(m_telegram.chat_id(),
"Введите количество",
"",
Telegram::Replay);
set_state(OS_Quantity);
return;
case OS_Quantity:
if(!input_double(data, m_order.quantity, "quantity", "Ошибка ввода количества"))
return;
print();
if(m_order.type == BncOrder::MARKET)
{
set_state(OS_OrderReady);
break;
}
if(m_order.type == BncOrder::LIMIT)
// || m_order.type == BncOrder::STOP_LOSS_LIMIT
// || m_order.type == BncOrder::TAKE_PROFIT_LIMIT
// || m_order.type == BncOrder::LIMIT_MAKER)
{
set_state(OS_Price);
m_telegram.send_message(m_telegram.chat_id(),
"Введите цену",
"",
Telegram::Replay);
}
else
{
set_state(OS_StopPrice);
m_telegram.send_message(m_telegram.chat_id(),
"Введите стоп - цену",
"",
Telegram::Replay);
}
return;
case OS_StopPrice:
if(!m_repeat)
{
if(!input_double(data, m_order.stopPrice, "stopPrice", "Ошибка ввода стоп-цены"))
return;
print();
}
else
{
m_repeat = false;
}
if(m_order.type == BncOrder::STOP_LOSS_LIMIT
|| m_order.type == BncOrder::TAKE_PROFIT_LIMIT)
{
set_state(OS_Price);
m_telegram.send_message(m_telegram.chat_id(),
"Введите цену",
"",
Telegram::Replay);
}
else
{
set_state(OS_TimeInForce);
keys.clear();
m_keyboard.clear();
keys.push_back("GTC");
keys.push_back("IOC");
keys.push_back("FOK");
keys.push_back("$h ОПИСАНИЕ");
m_keyboard.add_keys(keys);
m_telegram.send_message(m_telegram.chat_id(),
"Время действия ордера?",
"",
Telegram::Keyboard,
m_keyboard.keyboard());
}
return;
case OS_Price:
if(!m_repeat)
{
if(!input_double(data, m_order.price, "price", "Ошибка ввода цены"))
return;
print();
}
else
{
m_repeat = false;
}
/*
if(m_correction_mode)
{
set_state(OS_WaitCorrect);
break;
}
*/
if(m_order.type == BncOrder::LIMIT)
//|| m_order.type == BncOrder::STOP_LOSS_LIMIT
//|| m_order.type == BncOrder::TAKE_PROFIT_LIMIT)
{
set_state(OS_TimeInForce);
keys.clear();
m_keyboard.clear();
keys.push_back("GTC");
keys.push_back("IOC");
keys.push_back("FOK");
m_keyboard.add_keys(keys);
m_telegram.send_message(m_telegram.chat_id(),
"Время действия ордера?",
"",
Telegram::Keyboard,
m_keyboard.keyboard());
}
else
{
}
return;
case OS_TimeInForce:
if(first_arg(data) == "$h")
{
help();
return;
}
m_order.timeInForce = BncOrder::string_to_tif(first_arg(data));
m_view["timeInForce"].second = first_arg(data);
print();
set_state(OS_OrderReady);
break;
case OS_OrderReady:
keys.clear();
m_keyboard.clear();
keys.push_back("$e Выставить");
//keys.push_back("$b Назад");
keys.push_back("$c Отмена");
m_keyboard.add_keys(keys);
m_telegram.send_message(m_telegram.chat_id(),
"Ордер готов",
"",
Telegram::Keyboard,
m_keyboard.keyboard());
set_state(OS_OrderResult);
return;
case OS_OrderResult:
if("$e" == first_arg(data))
{
m_telegram.send_message(m_telegram.chat_id(), std::string("Ордер ") + m_order.newClientOrderId + " отправлен");
m_event_handler(Events::EVENT_ORD_MANUAL, boost::variant<OrderResult>(OST_Execute));
m_telegram.set_input_mode(Telegram::IM_Command);
}
else if("$b" == first_arg(data))
{
}
else if("$c" == first_arg(data))
{
m_telegram.send_message(m_telegram.chat_id(), std::string("Ордер ") + m_order.newClientOrderId + " отменен");
m_event_handler(Events::EVENT_ORD_MANUAL, boost::variant<OrderResult>(OST_Cancel));
m_telegram.set_input_mode(Telegram::IM_Command);
}
else
{
}
m_telegram.set_input_mode(Telegram::IM_Command);
return;
case OS_Delete:
if(m_symbols_list.empty())
{
m_telegram.set_input_mode(Telegram::IM_Command);
return;
}
m_telegram.send_message(m_telegram.chat_id(),
"Введите имя ордера для удаления",
"",
Telegram::Replay);
set_state(OS_DelName);
return;
case OS_DelName:
m_order.newClientOrderId = data;
if(m_symbols_list.size() > 1)
{
keys.clear();
m_keyboard.clear();
for(const auto& sit: m_symbols_list)
keys.push_back(sit);
m_keyboard.add_keys(keys);
m_telegram.send_message(m_telegram.chat_id(),
"Выбор валюты:",
"",
Telegram::Keyboard,
m_keyboard.keyboard());
set_state(OS_DelSymbol);
return;
}
else
{
m_data = *m_symbols_list.begin();
set_state(OS_DelSymbol);
break;
}
case OS_DelSymbol:
m_order.symbol = data + "USDT";
set_state(OS_DelReady);
break;
case OS_DelReady:
m_event_handler(Events::EVENT_ORD_DELETE, boost::variant<bnc_order_t>(m_order));
m_telegram.set_input_mode(Telegram::IM_Command);
return;
case OS_List:
if(m_symbols_list.size() > 1)
{
keys.clear();
m_keyboard.clear();
for(const auto& sit: m_symbols_list)
keys.push_back(sit);
m_keyboard.add_keys(keys);
m_telegram.send_message(m_telegram.chat_id(),
"Выбор валюты:",
"",
Telegram::Keyboard,
m_keyboard.keyboard());
set_state(OS_ListSymbol);
return;
}
else
{
m_data = *m_symbols_list.begin();
set_state(OS_ListSymbol);
break;
}
case OS_ListSymbol:
set_state(OS_Idle);
m_event_handler(Events::EVENT_ORD_REQ_LIST, boost::variant<std::string>(data/* + "USDT"*/));
m_telegram.set_input_mode(Telegram::IM_Command);
break;
default:
return;
}
engine(m_data);
}
void TOrder::help()
{
std::string help_msg;
switch(m_state)
{
case OS_Type:
help_msg += "LIMIT - \n";
help_msg += "MARKET - подажа или покупка по рыночной цене\n";
help_msg += "STOP_LOSS - исполнятся по рынку, как только будет достигнута стоп - цена\n";
help_msg += "STOP_LOSS_LIMIT\n";
help_msg += "TAKE_PROFIT - При достижении стоп-цены будет выполнен MARKET ордер.\n";
help_msg += "TAKE_PROFIT_LIMIT\n";
help_msg += "LIMIT_MAKER - отклонятся, если ордер при выставлении может выполниться по рынку\n";
break;
case OS_TimeInForce:
help_msg += "GTC - ордер будет висеть до тех пор, пока его не отменят.\n";
help_msg += "IOC - Будет куплено то количество, которое можно купить немедленно. Все, что не удалось купить, будет отменено.\n";
help_msg += "FOK - Либо будет куплено все указанное количество немедленно, либо не будет куплено вообще ничего, ордер отменится.\n";
break;
default:
return;
}
m_telegram.send_message(m_telegram.chat_id(), help_msg);
set_state(m_prev_state);
m_repeat = true;
engine(std::string());
}