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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
| #include "Ttransaction.h"
//=================================================================================================================================================================================
// Ttransaction
//=================================================================================================================================================================================
void Ttransaction::offset(double param, order_Units units)
{
std::string data_param, data_units;
if (units == order_Units::PRICE_UNITS)
{
data_param = get_correctPrice(param);
data_units = "PRICE_UNITS";
}
else
{
data_param = std::to_string(param);
data_units = "PERCENTS";
}
add_or_replace_field("OFFSET", data_param);
add_or_replace_field("OFFSET_UNITS", data_units);
}
void Ttransaction::spread(double param, order_Units units)
{
std::string data_param, data_units;
if (units == order_Units::PRICE_UNITS)
{
data_param = get_correctPrice(param);
data_units = "PRICE_UNITS";
}
else
{
data_param = std::to_string(param);
data_units = "PERCENTS";
}
add_or_replace_field("SPREAD", data_param);
add_or_replace_field("SPREAD_UNITS", data_units);
}
void Ttransaction::base_order_key(long param)
{
add_or_replace_field("BASE_ORDER_KEY", std::to_string(param));
}
void Ttransaction::use_base_order_balance(bool param)
{
add_or_replace_field("USE_BASE_ORDER_BALANCE", (param ? "YES" : "NO"));
}
void Ttransaction::activate_if_base_order_partly_fielled(bool param)
{
add_or_replace_field("ACTIVATE_IF_BASE_ORDER_PARTLY_FILLED", (param ? "YES" : "NO"));
}
void Ttransaction::base_contract(std::string param)
{
add_or_replace_field("BASE_CONTRACT", param);
}
void Ttransaction::mode(order_Mode param)
{
std::string data;
switch (param)
{
case order_Mode::_0: data = "0"; break;
case order_Mode::_1: data = "1"; break;
case order_Mode::_2: data = "2"; break;
default: data = ""; break;
}
add_or_replace_field("MODE", data);
}
void Ttransaction::first_order_number(long param)
{
add_or_replace_field("FIRST_ORDER_NUMBER", std::to_string(param));
}
void Ttransaction::first_order_new_quantity(int param)
{
if (param < 0) param = -param;
add_or_replace_field("FIRST_ORDER_NEW_QUANTITY", std::to_string(param));
}
void Ttransaction::first_order_new_price(double param)
{
add_or_replace_field("FIRST_ORDER_NEW_PRICE", get_correctPrice(param));
}
void Ttransaction::second_order_number(long param)
{
add_or_replace_field("SECOND_ORDER_NUMBER", std::to_string(param));
}
void Ttransaction::second_order_new_quantity(int param)
{
if (param < 0) param = -param;
add_or_replace_field("SECOND_ORDER_NEW_QUANTITY", std::to_string(param));
}
void Ttransaction::second_order_new_price(double param)
{
add_or_replace_field("SECOND_ORDER_NEW_PRICE", std::to_string(param));
}
void Ttransaction::kill_active_orders(bool param)
{
add_or_replace_field("KILL_ACTIVE_ORDERS", (param ? "YES" : "NO"));
}
void Ttransaction::neg_trade_operation(order_Operation param)
{
std::string data;
switch (param)
{
case order_Operation::B: data = "B"; break;
case order_Operation::S: data = "S"; break;
default: data = ""; break;
}
add_or_replace_field("NEG_TRADE_OPERATION", data);
}
void Ttransaction::neg_trade_number(std::string param)
{
add_or_replace_field("NEG_TRADE_NUMBER", param);
}
void Ttransaction::volumemn(std::string param)
{
add_or_replace_field("VOLUMEMN", param);
}
void Ttransaction::volumepl(std::string param)
{
add_or_replace_field("VOLUMEPL", param);
}
void Ttransaction::kfl(double param)
{
add_or_replace_field("KFL", std::to_string(param));
}
void Ttransaction::kgo(double param)
{
add_or_replace_field("KGO", std::to_string(param));
}
void Ttransaction::use_kgo(bool param)
{
add_or_replace_field("USE_KGO", (param ? "Y" : "N"));
}
void Ttransaction::check_limits(bool param)
{
add_or_replace_field("CHECK_LIMITS", (param ? "YES" : "NO"));
}
void Ttransaction::matchref(std::string param)
{
add_or_replace_field("MATCHREF", param);
}
void Ttransaction::correction(bool param)
{
add_or_replace_field("CORRECTION", (param ? "Y" : "N"));
}
std::string Ttransaction::get_correctPrice(double price)
{
price = round(price / price_step)*price_step;
std::string data = std::to_string(price);
std::string ans;
bool is_next_step = false;
std::string to_add;
int _dot = -1;
for (int i = 0; i < (int)data.size(); i++)
{
if(data.at(i) == '.' || data.at(i) == ',')
{
if (!is_next_step)
is_next_step = true;
continue;
}
if (is_next_step)
{
if (scale > 0)
to_add.push_back(data.at(i));
else
break;
}
else
ans.push_back(data.at(i));
}
if (scale > 0)
{
int to_add_size = to_add.size();
ans.push_back('.');
for (size_t i = 0; i < scale; i++)
{
if (i < to_add_size && to_add_size >0)
ans.push_back(to_add.at(i));
else
ans.push_back('0');
}
}
return ans;
}
void Ttransaction::add_or_replace_field(std::string fieldName, std::string arg)
{
if (arg.compare("") != 0)
{
bool insert = true;
for (size_t i = 0; i < param_keeper.size(); i++)
{
if (param_keeper[i].fieldName.compare(fieldName) == 0)
{
param_keeper[i].arg = arg;
insert = false;
}
}
if (insert)
{
param_value S;
S.fieldName = fieldName;
S.arg = arg;
param_keeper.push_back(S);
}
}
}
Ttransaction::Ttransaction(lua_State * L, DB_QueryParams params)
{
this->L = L;
reset_Table(params);
}
Ttransaction::~Ttransaction()
{
}
void Ttransaction::classcode()
{
add_or_replace_field("CLASSCODE", params.class_code);
}
void Ttransaction::seccode()
{
add_or_replace_field("SECCODE", params.sec_code);
}
void Ttransaction::classcode(std::string param)
{
add_or_replace_field("CLASSCODE", param);
}
void Ttransaction::seccode(std::string param)
{
add_or_replace_field("SECCODE", param);
}
void Ttransaction::action(order_Action param)
{
std::string data;
switch (param)
{
case order_Action::NEW_ORDER: data = "NEW_ORDER"; break;
case order_Action::NEW_NEG_DEAL: data = "NEW_NEG_DEAL"; break;
case order_Action::NEW_REPO_NEG_DEAL: data = "NEW_REPO_NEG_DEAL"; break;
case order_Action::NEW_EXT_REPO_NEG_DEAL: data = "NEW_EXT_REPO_NEG_DEAL"; break;
case order_Action::NEW_STOP_ORDER: data = "NEW_STOP_ORDER"; break;
case order_Action::KILL_ORDER: data = "KILL_ORDER"; break;
case order_Action::KILL_NEG_DEAL: data = "KILL_NEG_DEAL"; break;
case order_Action::KILL_STOP_ORDER: data = "KILL_STOP_ORDER"; break;
case order_Action::KILL_ALL_ORDERS: data = "KILL_ALL_ORDERS"; break;
case order_Action::KILL_ALL_STOP_ORDERS: data = "KILL_ALL_STOP_ORDERS"; break;
case order_Action::KILL_ALL_NEG_DEALS: data = "KILL_ALL_NEG_DEALS"; break;
case order_Action::KILL_ALL_FUTURES_ORDERS: data = "KILL_ALL_FUTURES_ORDERS"; break;
case order_Action::MOVE_ORDERS: data = "MOVE_ORDERS"; break;
case order_Action::NEW_QUOTE: data = "NEW_QUOTE"; break;
case order_Action::KILL_QUOTE: data = "KILL_QUOTE"; break;
case order_Action::NEW_REPORT: data = "NEW_REPORT"; break;
case order_Action::SET_FUT_LIMIT: data = "SET_FUT_LIMIT"; break;
default: data = ""; break;
}
add_or_replace_field("ACTION", data);
}
void Ttransaction::firm_ID(std::string param)
{
add_or_replace_field("FIRM_ID", param);
}
void Ttransaction::account()
{
add_or_replace_field("ACCOUNT", params.account);
}
void Ttransaction::account(std::string param)
{
add_or_replace_field("ACCOUNT", param);
}
void Ttransaction::client_code(std::string param)
{
add_or_replace_field("CLIENT_CODE", param);
}
void Ttransaction::type(order_Type param)
{
std::string data;
switch (param)
{
case order_Type::L: data = "L"; break;
case order_Type::M: data = "M"; break;
default: data = ""; break;
}
add_or_replace_field("TYPE", data);
}
void Ttransaction::market_maker_order(bool param)
{
add_or_replace_field("MARKET_MAKER_ORDER", (param ? "YES" : "NO"));
}
void Ttransaction::operation(order_Operation param)
{
std::string data;
switch (param)
{
case order_Operation::B: data = "B"; break;
case order_Operation::S: data = "S"; break;
default: data = ""; break;
}
add_or_replace_field("OPERATION", data);
}
void Ttransaction::execution_condition(order_Execution_condition param)
{
std::string data;
switch (param)
{
case order_Execution_condition::PUT_IN_QUEUE: data = "PUT_IN_QUEUE"; break;
case order_Execution_condition::FILL_OR_KILL: data = "FILL_OR_KILL"; break;
case order_Execution_condition::KILL_BALANCE: data = "KILL_BALANCE"; break;
default: data = ""; break;
}
add_or_replace_field("EXECUTION_CONDITION", data);
}
void Ttransaction::quantity(int param)
{
if (param < 0) param = -param;
add_or_replace_field("QUANTITY", std::to_string(param));
}
void Ttransaction::repovalue(double param)
{
add_or_replace_field("REPOVALUE", std::to_string(param));
}
void Ttransaction::start_discount(double param)
{
add_or_replace_field("START_DISCOUNT", std::to_string(param));
}
void Ttransaction::lower_discount(double param)
{
add_or_replace_field("LOWER_DISCOUNT", std::to_string(param));
}
void Ttransaction::upper_discount(double param)
{
add_or_replace_field("UPPER_DISCOUNT", std::to_string(param));
}
void Ttransaction::price(double param)
{
add_or_replace_field("PRICE", get_correctPrice(param));
}
void Ttransaction::stopprice(double param)
{
add_or_replace_field("STOPPRICE", get_correctPrice(param));
}
void Ttransaction::stop_order_kind(order_Stop_order_kind param)
{
std::string data;
switch (param)
{
case order_Stop_order_kind::SIMPLE_STOP_ORDER: data = "SIMPLE_STOP_ORDER"; break;
case order_Stop_order_kind::CONDITION_PRICE_BY_OTHER_SEC: data = "CONDITION_PRICE_BY_OTHER_SEC"; break;
case order_Stop_order_kind::WITH_LINKED_LIMIT_ORDER: data = "WITH_LINKED_LIMIT_ORDER"; break;
case order_Stop_order_kind::TAKE_PROFIT_STOP_ORDER: data = "TAKE_PROFIT_STOP_ORDER"; break;
case order_Stop_order_kind::TAKE_PROFIT_AND_STOP_LIMIT_ORDER: data = "TAKE_PROFIT_AND_STOP_LIMIT_ORDER"; break;
case order_Stop_order_kind::ACTIVATED_BY_ORDER_SIMPLE_STOP_ORDER: data = "ACTIVATED_BY_ORDER_SIMPLE_STOP_ORDER"; break;
case order_Stop_order_kind::ACTIVATED_BY_ORDER_TAKE_PROFIT_STOP_ORDER: data = "ACTIVATED_BY_ORDER_TAKE_PROFIT_STOP_ORDER"; break;
case order_Stop_order_kind::ACTIVATED_BY_ORDER_TAKE_PROFIT_AND_STOP_LIMIT_ORDER: data = "ACTIVATED_BY_ORDER_TAKE_PROFIT_AND_STOP_LIMIT_ORDER"; break;
default: data = ""; break;
}
add_or_replace_field("STOP_ORDER_KIND", data);
}
void Ttransaction::stopprice_classcode(std::string param)
{
add_or_replace_field("STOPPRICE_CLASSCODE", param);
}
void Ttransaction::stopprice_seccode(std::string param)
{
add_or_replace_field("STOPPRICE_SECCODE", param);
}
void Ttransaction::stopprice_condition(order_Stopprice_condition param)
{
std::string data;
switch (param)
{
case order_Stopprice_condition::less_or_equal: data = "<="; break;
case order_Stopprice_condition::more_or_ewual: data = ">="; break;
default: data = ""; break;
}
add_or_replace_field("STOPPRICE_CONDITION", data);
}
void Ttransaction::linked_order_price(double param)
{
add_or_replace_field("LINKED_ORDER_PRICE", get_correctPrice(param));
}
void Ttransaction::expiry_date(ExpiryDate param)
{
bool add = true;
std::string data;
switch (param.type)
{
case ExpiryType::GTC: data = "GTC"; break;
case ExpiryType::TODAY: data = "TODAY"; break;
default:
{
char DT[100];
if (std::strftime(DT, sizeof(DT), "%Y%m%d", ¶m.DT))
data = DT;
else
add = false;
}
break;
}
if (add)
add_or_replace_field("EXPIRY_DATE", data);
}
void Ttransaction::stopprice2(double param)
{
add_or_replace_field("STOPPRICE2", get_correctPrice(param));
}
void Ttransaction::market_stop_limit(bool param)
{
add_or_replace_field("MARKET_STOP_LIMIT", (param ? "YES" : "NO"));
}
void Ttransaction::market_take_profit(bool param)
{
add_or_replace_field("MARKET_TAKE_PROFIT", (param ? "YES" : "NO"));
}
void Ttransaction::is_active_in_time(bool param)
{
add_or_replace_field("IS_ACTIVE_IN_TIME", (param ? "YES" : "NO"));
}
void Ttransaction::active_from_time(order_TimeStruct param)
{
auto get_num = [](double num)
{
return (num < 10 ? "0" + std::to_string(num) : std::to_string(num));
};
std::string data = get_num(param.hour) + get_num(param.min) + get_num(param.sec);
add_or_replace_field("ACTIVE_FROM_TIME", data);
}
void Ttransaction::active_to_time(order_TimeStruct param)
{
auto get_num = [](double num)
{
return (num < 10 ? "0" + std::to_string(num) : std::to_string(num));
};
std::string data = get_num(param.hour) + get_num(param.min) + get_num(param.sec);
add_or_replace_field("ACTIVE_TO_TIME", data);
}
void Ttransaction::partner(std::string param)
{
add_or_replace_field("PARTNER", param);
}
void Ttransaction::order_key(long param)
{
add_or_replace_field("ORDER_KEY", std::to_string(param));
}
void Ttransaction::stop_order_key(long param)
{
add_or_replace_field("STOP_ORDER_KEY", std::to_string(param));
}
int Ttransaction::trans_id()
{
srand((unsigned int)time(NULL)); //seeding for the first time only!
int num = (1 + rand() % ((2147483647 + 1) - 1));
add_or_replace_field("TRANS_ID", std::to_string(num));
return num;
}
void Ttransaction::trans_id(int param)
{
if (param < 0) param = abs(param);
add_or_replace_field("TRANS_ID", std::to_string(param));
}
void Ttransaction::settle_code(std::string param)
{
add_or_replace_field("SETTLE_CODE", param);
}
void Ttransaction::price2(double param)
{
add_or_replace_field("PRICE2", get_correctPrice(param));
}
void Ttransaction::repoterm(std::string param)
{
add_or_replace_field("REPOTERM", param);
}
void Ttransaction::reporate(double param)
{
add_or_replace_field("REPORATE", std::to_string(param));
}
void Ttransaction::block_securities(bool param)
{
add_or_replace_field("BLOCK_SECURITIES", (param ? "YES" : "NO"));
}
void Ttransaction::refundrate(double param)
{
add_or_replace_field("REFUNDRATE", std::to_string(param));
}
void Ttransaction::comment(std::string param)
{
add_or_replace_field("COMMENT", param);
}
void Ttransaction::large_trade(bool param)
{
add_or_replace_field("LARGE_TRADE", (param ? "YES" : "NO"));
}
void Ttransaction::curr_code(std::string param)
{
add_or_replace_field("CURR_CODE", param);
}
void Ttransaction::for_account(order_For_account param)
{
std::string data;
switch (param)
{
case order_For_account::OWNOWN: data = "OWNOWN"; break;
case order_For_account::OWNCLI: data = "OWNCLI"; break;
case order_For_account::OWNDUP: data = "OWNDUP"; break;
case order_For_account::CLICLI: data = "CLICLI"; break;
default: data = ""; break;
}
add_or_replace_field("FOR_ACCOUNT", data);
}
void Ttransaction::settle_date(tm param)
{
char DT[100];
if (std::strftime(DT, sizeof(DT), "%Y%m%d", ¶m))
{
add_or_replace_field("SETTLE_DATE", DT);
}
}
void Ttransaction::kill_if_linked_order_partly_field(bool param)
{
add_or_replace_field("KILL_IF_LINKED_ORDER_PARTLY_FILLED", (param ? "YES" : "NO"));
}
std::string sendTransaction(Ttransaction & T)
{
std::string ans;
int length = (int)T.param_keeper.size();
lua_settop(T.L, 0);
lua_getglobal(T.L, "sendTransaction"); // 1 -2
lua_createtable(T.L, 0, length);
for (int i = 0; i < length; i++)
{
lua_pushfstring(T.L, T.param_keeper[i].arg.c_str());
lua_setfield(T.L, -2, T.param_keeper[i].fieldName.c_str());
}
lua_call(T.L, 1, 1);
if (lua_isnil(T.L, -1) || lua_tostring(T.L, -1) == "")
ans = "";
else
ans = lua_tostring(T.L, -1);
return ans;
}
void Ttransaction::reset_Table(DB_QueryParams params)
{
lua_settop(L, 0);
this->params = params;
if (param_keeper.size() > 0)
param_keeper.clear();
lua_getglobal(L, "getSecurityInfo");
lua_pushfstring(L, params.class_code.c_str());
lua_pushfstring(L, params.sec_code.c_str());
lua_call(L, 2, 1);
lua_getfield(L, -1, "scale");
scale = (int)lua_tonumber(L, -1);
lua_settop(L, 0);
lua_getglobal(L, "getParamEx");
lua_pushfstring(L, params.class_code.c_str());
lua_pushfstring(L, params.sec_code.c_str());
lua_pushfstring(L, "SEC_PRICE_STEP");
lua_call(L, 3, 1);
lua_getfield(L, -1, "param_value");
price_step = lua_tonumber(L, -1);
}
Ttransaction::Ttransaction(const Ttransaction & other)
{
this->L = other.L;
this->scale = other.scale;
this->price_step = other.price_step;
this->params = other.params;
this->param_keeper.reserve(other.param_keeper.size());
std::copy(other.param_keeper.begin(), other.param_keeper.end(), std::back_inserter(this->param_keeper));
} |