Доброго времени Суток.
Так как данный ресурс мне довольно сильно помог, и так как у меня появились неплохие свои наработки, которые могут быть полезны Вам, то я решил отдать "дань" данному ресурсу и посодействовать его развитию, поделившись с Вами одной из таких наработок.
ПРЕДУПРЕЖДЕНИЕ:
1) Код НЕ потокобезопасный и по этому требуется использовать разделяемые ресурсы для работы со стеком Луа (если проект многопоточный).
2) Предоставляется "Как Есть" и не несу не какой ответственности за Ваше использование кода
3) Если найдете недочеты или баги, то просьба сообщить тут. (поправим)
Код написан на плюсах и собственно разделен на 2 файла (заголовочный и реализации). Данная обертка функционирует следующим образом:
1) При использовании функций, автоматически формируется массив с полями таблицы. Если вдруг поле менялось дважды то оно просто меняет свое значение в массиве. Так же таблица сама нормализует цены и все прочие... Все команды вынесены в перечисления и по этому удобны для использования.
2) Вызывается функция std::string sentTransaction (Ttransaction &T) - принимающая в себя сформированную таблицу. Данная функция заносит в Луа все необходимые данные, вызывает lua_call и получает в качестве ответа сообщение об ошибке которое возвращает стандартная ф-ция lua_call.
Иными словами функционал таблицы и функции максимально схож с функционалом таблицы и функции из руководства пользователя луа.
Далее пойдет реализация:
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 | #include <string> #include<vector> #include <ctime> #include <iterator> #include <windows.h> #define LUA_LIB #define LUA_BUILD_AS_DLL extern "C" { #include "Lua/lauxlib.h" #include "Lua/lua.h" #include "Lua/luaconf.h" } enum class ExpiryType { GTC = 0, TODAY = 1, DT = 2 }; struct ExpiryDate { tm DT; ExpiryType type; }; struct DB_QueryParams { std::string class_code; std::string sec_code; std::string account; }; enum class order_Action { NEW_ORDER,//новая заявка NEW_NEG_DEAL, //новая заявка на внебиржевую сделку NEW_REPO_NEG_DEAL, //новая заявка на сделку РЕПО NEW_EXT_REPO_NEG_DEAL, //новая заявка на сделку модифицированного РЕПО (РЕПО-М) NEW_STOP_ORDER, //новая стоп-заявка KILL_ORDER, //снять заявку KILL_NEG_DEAL, //снять заявку на внебиржевую сделку или заявку на сделку РЕПО KILL_STOP_ORDER, //снять стоп-заявку KILL_ALL_ORDERS, //снять все заявки из торговой системы KILL_ALL_STOP_ORDERS, //снять все стоп-заявки KILL_ALL_NEG_DEALS, //снять все заявки на внебиржевые сделки и заявки на сделки РЕПО KILL_ALL_FUTURES_ORDERS, //снять все заявки на рынке FORTS MOVE_ORDERS, //переставить заявки на рынке FORTS NEW_QUOTE, //новая безадресная заявка KILL_QUOTE, //снять безадресную заявку NEW_REPORT, //новая заявка-отчет о подтверждении транзакций в режимах РПС и РЕПО SET_FUT_LIMIT, //новое ограничение по фьючерсному счету }; enum class order_Type { L, //limit M //market }; enum class order_Operation { B, //bue S //sell }; enum class order_Execution_condition { PUT_IN_QUEUE, //поставить в очередь (по умолчанию) FILL_OR_KILL, //немедленно или отклонить KILL_BALANCE, //снять остаток }; enum class order_Stop_order_kind { SIMPLE_STOP_ORDER, //стоп-лимит CONDITION_PRICE_BY_OTHER_SEC, //с условием по другой бумаге WITH_LINKED_LIMIT_ORDER, //со связанной заявкой TAKE_PROFIT_STOP_ORDER, //тэйк-профит TAKE_PROFIT_AND_STOP_LIMIT_ORDER, //тэйк-профит и стоп-лимит ACTIVATED_BY_ORDER_SIMPLE_STOP_ORDER, //стоп-лимит по исполнению заявки ACTIVATED_BY_ORDER_TAKE_PROFIT_STOP_ORDER, //тэйк-профит по исполнению заявки ACTIVATED_BY_ORDER_TAKE_PROFIT_AND_STOP_LIMIT_ORDER //тэйк-профит и стоп-лимит по исполнению заявки }; enum class order_Stopprice_condition { less_or_equal, more_or_ewual }; struct order_TimeStruct { int hour; int min; int sec; }; enum class order_For_account { OWNOWN, //от своего имени, за свой счет OWNCLI, //от своего имени, за счет клиента OWNDUP, //от своего имени, за счет доверительного управления CLICLI, //от имени клиента, за счет клиента }; enum class order_Units { PERCENTS, //в процентах (шаг изменения – одна сотая процента) PRICE_UNITS // в параметрах цены(шаг изменения равен шагу цены по данному инструменту) }; enum class order_Mode { _0, //оставить количество в заявках без изменения, _1, //изменить количество в заявках на новые, _2 //при несовпадении новых количеств с текущим хотя бы в одной заявке, обе заявки снимаются }; struct Ttransaction { public: Ttransaction(lua_State *L, DB_QueryParams params); Ttransaction(const Ttransaction &other); ~Ttransaction(); void classcode(); void seccode(); void classcode(std::string param); void seccode(std::string param); void action(order_Action param); void firm_ID(std::string param); void account(); void account(std::string param); void client_code(std::string param); // 20 symb - max void type(order_Type param); void market_maker_order(bool param); void operation(order_Operation param); void execution_condition(order_Execution_condition param); void quantity(int param); void repovalue(double param); void start_discount(double param); void lower_discount(double param); void upper_discount(double param); void price(double param); void stopprice(double param); void stop_order_kind(order_Stop_order_kind param); void stopprice_classcode(std::string param); void stopprice_seccode(std::string param); void stopprice_condition(order_Stopprice_condition param); void linked_order_price(double param); void expiry_date(ExpiryDate param); void stopprice2(double param); void market_stop_limit(bool param); void market_take_profit(bool param); void is_active_in_time(bool param); void active_from_time(order_TimeStruct param); void active_to_time(order_TimeStruct param); void partner(std::string param); void order_key(long param); void stop_order_key(long param); int trans_id(); void trans_id(int param); void settle_code(std::string param); void price2(double param); void repoterm(std::string param); void reporate(double param); void block_securities(bool param); void refundrate(double param); void comment(std::string param); void large_trade(bool param); void curr_code(std::string param); void for_account(order_For_account param); void settle_date(tm param); void kill_if_linked_order_partly_field(bool param); void offset(double param, order_Units units); void spread(double param, order_Units units); void base_order_key(long param); void use_base_order_balance(bool param); void activate_if_base_order_partly_fielled(bool param); void base_contract(std::string param); void mode(order_Mode param); void first_order_number(long param); void first_order_new_quantity(int param); void first_order_new_price(double param); void second_order_number(long param); void second_order_new_quantity(int param); void second_order_new_price(double param); void kill_active_orders(bool param); void neg_trade_operation(order_Operation param); void neg_trade_number(std::string param); void volumemn(std::string param); void volumepl(std::string param); void kfl(double param); void kgo(double param); void use_kgo(bool param); void check_limits(bool param); void matchref(std::string param); void correction(bool param); //void reset_Table(int args_num); void reset_Table(DB_QueryParams params); private: struct param_value { std::string fieldName; std::string arg; }; lua_State *L; int scale; double price_step; std::vector<param_value> param_keeper; DB_QueryParams params; friend std::string sendTransaction(Ttransaction& T); std::string get_correctPrice(double price); void add_or_replace_field(std::string fieldName, std::string arg); // проверяет есть ли в fieldRows, если есть то заменяет, если нет то добовляет новое и добавляет в fieldRows }; |
#include <string> #include<vector> #include <ctime> #include <iterator> #include <windows.h> #define LUA_LIB #define LUA_BUILD_AS_DLL extern "C" { #include "Lua/lauxlib.h" #include "Lua/lua.h" #include "Lua/luaconf.h" } enum class ExpiryType { GTC = 0, TODAY = 1, DT = 2 }; struct ExpiryDate { tm DT; ExpiryType type; }; struct DB_QueryParams { std::string class_code; std::string sec_code; std::string account; }; enum class order_Action { NEW_ORDER,//новая заявка NEW_NEG_DEAL, //новая заявка на внебиржевую сделку NEW_REPO_NEG_DEAL, //новая заявка на сделку РЕПО NEW_EXT_REPO_NEG_DEAL, //новая заявка на сделку модифицированного РЕПО (РЕПО-М) NEW_STOP_ORDER, //новая стоп-заявка KILL_ORDER, //снять заявку KILL_NEG_DEAL, //снять заявку на внебиржевую сделку или заявку на сделку РЕПО KILL_STOP_ORDER, //снять стоп-заявку KILL_ALL_ORDERS, //снять все заявки из торговой системы KILL_ALL_STOP_ORDERS, //снять все стоп-заявки KILL_ALL_NEG_DEALS, //снять все заявки на внебиржевые сделки и заявки на сделки РЕПО KILL_ALL_FUTURES_ORDERS, //снять все заявки на рынке FORTS MOVE_ORDERS, //переставить заявки на рынке FORTS NEW_QUOTE, //новая безадресная заявка KILL_QUOTE, //снять безадресную заявку NEW_REPORT, //новая заявка-отчет о подтверждении транзакций в режимах РПС и РЕПО SET_FUT_LIMIT, //новое ограничение по фьючерсному счету }; enum class order_Type { L, //limit M //market }; enum class order_Operation { B, //bue S //sell }; enum class order_Execution_condition { PUT_IN_QUEUE, //поставить в очередь (по умолчанию) FILL_OR_KILL, //немедленно или отклонить KILL_BALANCE, //снять остаток }; enum class order_Stop_order_kind { SIMPLE_STOP_ORDER, //стоп-лимит CONDITION_PRICE_BY_OTHER_SEC, //с условием по другой бумаге WITH_LINKED_LIMIT_ORDER, //со связанной заявкой TAKE_PROFIT_STOP_ORDER, //тэйк-профит TAKE_PROFIT_AND_STOP_LIMIT_ORDER, //тэйк-профит и стоп-лимит ACTIVATED_BY_ORDER_SIMPLE_STOP_ORDER, //стоп-лимит по исполнению заявки ACTIVATED_BY_ORDER_TAKE_PROFIT_STOP_ORDER, //тэйк-профит по исполнению заявки ACTIVATED_BY_ORDER_TAKE_PROFIT_AND_STOP_LIMIT_ORDER //тэйк-профит и стоп-лимит по исполнению заявки }; enum class order_Stopprice_condition { less_or_equal, more_or_ewual }; struct order_TimeStruct { int hour; int min; int sec; }; enum class order_For_account { OWNOWN, //от своего имени, за свой счет OWNCLI, //от своего имени, за счет клиента OWNDUP, //от своего имени, за счет доверительного управления CLICLI, //от имени клиента, за счет клиента }; enum class order_Units { PERCENTS, //в процентах (шаг изменения – одна сотая процента) PRICE_UNITS // в параметрах цены(шаг изменения равен шагу цены по данному инструменту) }; enum class order_Mode { _0, //оставить количество в заявках без изменения, _1, //изменить количество в заявках на новые, _2 //при несовпадении новых количеств с текущим хотя бы в одной заявке, обе заявки снимаются }; struct Ttransaction { public: Ttransaction(lua_State *L, DB_QueryParams params); Ttransaction(const Ttransaction &other); ~Ttransaction(); void classcode(); void seccode(); void classcode(std::string param); void seccode(std::string param); void action(order_Action param); void firm_ID(std::string param); void account(); void account(std::string param); void client_code(std::string param); // 20 symb - max void type(order_Type param); void market_maker_order(bool param); void operation(order_Operation param); void execution_condition(order_Execution_condition param); void quantity(int param); void repovalue(double param); void start_discount(double param); void lower_discount(double param); void upper_discount(double param); void price(double param); void stopprice(double param); void stop_order_kind(order_Stop_order_kind param); void stopprice_classcode(std::string param); void stopprice_seccode(std::string param); void stopprice_condition(order_Stopprice_condition param); void linked_order_price(double param); void expiry_date(ExpiryDate param); void stopprice2(double param); void market_stop_limit(bool param); void market_take_profit(bool param); void is_active_in_time(bool param); void active_from_time(order_TimeStruct param); void active_to_time(order_TimeStruct param); void partner(std::string param); void order_key(long param); void stop_order_key(long param); int trans_id(); void trans_id(int param); void settle_code(std::string param); void price2(double param); void repoterm(std::string param); void reporate(double param); void block_securities(bool param); void refundrate(double param); void comment(std::string param); void large_trade(bool param); void curr_code(std::string param); void for_account(order_For_account param); void settle_date(tm param); void kill_if_linked_order_partly_field(bool param); void offset(double param, order_Units units); void spread(double param, order_Units units); void base_order_key(long param); void use_base_order_balance(bool param); void activate_if_base_order_partly_fielled(bool param); void base_contract(std::string param); void mode(order_Mode param); void first_order_number(long param); void first_order_new_quantity(int param); void first_order_new_price(double param); void second_order_number(long param); void second_order_new_quantity(int param); void second_order_new_price(double param); void kill_active_orders(bool param); void neg_trade_operation(order_Operation param); void neg_trade_number(std::string param); void volumemn(std::string param); void volumepl(std::string param); void kfl(double param); void kgo(double param); void use_kgo(bool param); void check_limits(bool param); void matchref(std::string param); void correction(bool param); //void reset_Table(int args_num); void reset_Table(DB_QueryParams params); private: struct param_value { std::string fieldName; std::string arg; }; lua_State *L; int scale; double price_step; std::vector<param_value> param_keeper; DB_QueryParams params; friend std::string sendTransaction(Ttransaction& T); std::string get_correctPrice(double price); void add_or_replace_field(std::string fieldName, std::string arg); // проверяет есть ли в fieldRows, если есть то заменяет, если нет то добовляет новое и добавляет в fieldRows };
Теперь реализация данного заголовочного файла:
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)); } |
#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)); }
В качестве примера использования, выложу открытие простого рыночного ордера с использованием данной таблицы:
C++:
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 | #include "Ttransaction.h" BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) { return TRUE; } void msg_ToQuik(lua_State *L, std::string msg, int num) { lua_settop(L, 0); lua_getglobal(L, "message"); lua_pushfstring(L, msg.c_str()); lua_pushnumber(L, num); lua_call(L, 2, 0); } void market_order(lua_State *L, DB_QueryParams params, order_Operation operation, double borderPrice, int qty) { Ttransaction T(L, params); T.trans_id(); T.account(); T.classcode(); T.seccode(); T.action(order_Action::NEW_ORDER); T.type(borderPrice == 0 ? order_Type::M : order_Type::L); T.operation(operation); T.price(borderPrice); T.quantity(qty); std::string res = sendTransaction(T); if (res.compare("") != 0) { msg_ToQuik(L, res, 3); } } int _main(lua_State *L) { DB_QueryParams params; params.account = "NL0011100043"; params.class_code = "QJSIM"; params.sec_code = "ROSN"; market_order(L, params, order_Operation::S, 313.15, 1); return 0; } static struct luaL_reg ls_lib[] = { { NULL, NULL } }; extern "C" LUALIB_API int luaopen_Project2(lua_State *L) { lua_pushcfunction(L, _main); lua_setglobal(L, "main"); return 0; } |
#include "Ttransaction.h" BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) { return TRUE; } void msg_ToQuik(lua_State *L, std::string msg, int num) { lua_settop(L, 0); lua_getglobal(L, "message"); lua_pushfstring(L, msg.c_str()); lua_pushnumber(L, num); lua_call(L, 2, 0); } void market_order(lua_State *L, DB_QueryParams params, order_Operation operation, double borderPrice, int qty) { Ttransaction T(L, params); T.trans_id(); T.account(); T.classcode(); T.seccode(); T.action(order_Action::NEW_ORDER); T.type(borderPrice == 0 ? order_Type::M : order_Type::L); T.operation(operation); T.price(borderPrice); T.quantity(qty); std::string res = sendTransaction(T); if (res.compare("") != 0) { msg_ToQuik(L, res, 3); } } int _main(lua_State *L) { DB_QueryParams params; params.account = "NL0011100043"; params.class_code = "QJSIM"; params.sec_code = "ROSN"; market_order(L, params, order_Operation::S, 313.15, 1); return 0; } static struct luaL_reg ls_lib[] = { { NULL, NULL } }; extern "C" LUALIB_API int luaopen_Project2(lua_State *L) { lua_pushcfunction(L, _main); lua_setglobal(L, "main"); return 0; }
Lua:
1 | require("ProjectName"); |
require("ProjectName");