Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- result_t<std::vector<result_t<order_t>>> connector::new_batch_orders(const std::vector<order_t>& orders) {
- std::vector<api::new_order_request_t> request(orders.size());
- result_t<std::vector<result_t<order_t>>> result;
- for (size_t i = 0; i < orders.size(); ++i) {
- const order_t& order = orders[i];
- request[i].symbol = order.symbol->symbol;
- request[i].side = e_side_to_str(order.side);
- request[i].type = e_type_to_str(order.type);
- if (order.type == e_type::MARKET) {
- if (order.side == e_side::BUY) {
- if (order.amount.initialized()) {
- request[i].quote_order_qty = order.amount.to_string();
- } else if (order.quantity.initialized()) {
- book_t cur = local_get_book(order.symbol).value;
- double price = 0;
- if (cur.ask_price.initialized()) {
- price = cur.ask_price.to_double();
- } else {
- result.code = e_market_error::NO_EXIST_OPPONENT_ORDER;
- return result;
- }
- request[i].quote_order_qty = order.symbol->make_amount(price * order.quantity.to_double()).to_string();
- } else {
- result.code = e_market_error::AMOUNT_NULL_INVALID;
- return result;
- }
- } else {
- if (order.quantity.initialized()) {
- request[i].quantity = order.quantity.to_string();
- } else if (order.amount.initialized()) {
- book_t cur = local_get_book(order.symbol).value;
- double price = 0;
- if (cur.bid_price.initialized()) {
- price = cur.bid_price.to_double();
- } else {
- result.code = e_market_error::NO_EXIST_OPPONENT_ORDER;
- return result;
- }
- request[i].quantity = order.symbol->make_quantity(order.amount.to_double() / price).to_string();
- } else {
- result.code = e_market_error::AMOUNT_NULL_INVALID;
- return result;
- }
- }
- } else {
- request[i].price = order.price.to_string();
- if (order.quantity.initialized()) {
- request[i].quantity = order.quantity.to_string();
- } else if (order.amount.initialized()) {
- request[i].quantity = order.symbol->make_quantity(order.amount.to_double() / order.price.to_double()).to_string();
- } else {
- result.code = e_market_error::AMOUNT_NULL_INVALID;
- return result;
- }
- }
- }
- result_t<std::vector<result_t<api::new_order_response_t>>> response = api_.new_batch_orders(std::move(request));
- if (response.code != e_market_error::OK) {
- result.code = response.code;
- return result;
- }
- result.value = std::vector<result_t<order_t>>(response.value.size()); // my code!
- for (size_t i = 0; i < response.value.size(); ++i) {
- const order_t& order = orders[i];
- if (response.value[i].code != e_market_error::OK) {
- result.value[i].code = response.value[i].code;
- } else {
- result.value[i].value.symbol = order.symbol;
- result.value[i].value.side = response.value[i].value.side;
- result.value[i].value.type = response.value[i].value.type;
- result.value[i].value.price = order.price;
- result.value[i].value.quantity = order.quantity;
- result.value[i].value.amount = order.amount;
- result.value[i].value.update_time = response.value[i].value.transact_time;
- }
- }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement