Advertisement
den4ik2003

Untitled

Feb 8th, 2024
539
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.83 KB | None | 0 0
  1. result_t<std::vector<result_t<order_t>>> connector::new_batch_orders(const std::vector<order_t>& orders) {
  2.     std::vector<api::new_order_request_t> request(orders.size());
  3.     result_t<std::vector<result_t<order_t>>> result;
  4.     for (size_t i = 0; i < orders.size(); ++i) {
  5.         const order_t& order = orders[i];
  6.         request[i].symbol = order.symbol->symbol;
  7.         request[i].side = e_side_to_str(order.side);
  8.         request[i].type = e_type_to_str(order.type);
  9.         if (order.type == e_type::MARKET) {
  10.             if (order.side == e_side::BUY) {
  11.                 if (order.amount.initialized()) {
  12.                     request[i].quote_order_qty = order.amount.to_string();
  13.                 } else if (order.quantity.initialized()) {
  14.                     book_t cur = local_get_book(order.symbol).value;
  15.                     double price = 0;
  16.                     if (cur.ask_price.initialized()) {
  17.                         price = cur.ask_price.to_double();
  18.                     } else {
  19.                         result.code = e_market_error::NO_EXIST_OPPONENT_ORDER;
  20.                         return result;
  21.                     }
  22.                     request[i].quote_order_qty = order.symbol->make_amount(price * order.quantity.to_double()).to_string();
  23.                 } else {
  24.                     result.code = e_market_error::AMOUNT_NULL_INVALID;
  25.                     return result;
  26.                 }
  27.             } else {
  28.                 if (order.quantity.initialized()) {
  29.                     request[i].quantity = order.quantity.to_string();
  30.                 } else if (order.amount.initialized()) {
  31.                     book_t cur = local_get_book(order.symbol).value;
  32.                     double price = 0;
  33.                     if (cur.bid_price.initialized()) {
  34.                         price = cur.bid_price.to_double();
  35.                     } else {
  36.                         result.code = e_market_error::NO_EXIST_OPPONENT_ORDER;
  37.                         return result;
  38.                     }
  39.                     request[i].quantity = order.symbol->make_quantity(order.amount.to_double() / price).to_string();
  40.                 } else {
  41.                     result.code = e_market_error::AMOUNT_NULL_INVALID;
  42.                     return result;
  43.                 }
  44.             }
  45.         } else {
  46.             request[i].price = order.price.to_string();
  47.             if (order.quantity.initialized()) {
  48.                 request[i].quantity = order.quantity.to_string();
  49.             } else if (order.amount.initialized()) {
  50.                 request[i].quantity = order.symbol->make_quantity(order.amount.to_double() / order.price.to_double()).to_string();
  51.             } else {
  52.                 result.code = e_market_error::AMOUNT_NULL_INVALID;
  53.                 return result;
  54.             }
  55.         }
  56.     }
  57.     result_t<std::vector<result_t<api::new_order_response_t>>> response = api_.new_batch_orders(std::move(request));
  58.     if (response.code != e_market_error::OK) {
  59.         result.code = response.code;
  60.         return result;
  61.     }
  62.     result.value = std::vector<result_t<order_t>>(response.value.size()); // my code!
  63.     for (size_t i = 0; i < response.value.size(); ++i) {
  64.         const order_t& order = orders[i];
  65.         if (response.value[i].code != e_market_error::OK) {
  66.             result.value[i].code = response.value[i].code;
  67.         } else {
  68.             result.value[i].value.symbol = order.symbol;
  69.             result.value[i].value.side = response.value[i].value.side;
  70.             result.value[i].value.type = response.value[i].value.type;
  71.             result.value[i].value.price = order.price;
  72.             result.value[i].value.quantity = order.quantity;
  73.             result.value[i].value.amount = order.amount;
  74.             result.value[i].value.update_time = response.value[i].value.transact_time;
  75.         }
  76.     }
  77.     return result;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement