#include "server/sparkd/fill_models/MBOBook.h" namespace bts { namespace md { // FillOp bool FillOp::is_done() { return _price_level == _book_end; } void FillOp::fill_against(const ExchangeOrderSnapshot &exch_order, uint32_t fill_qty) { while (fill_qty > 0 && _ensure_working(exch_order)) { if (_curr_fill == nullptr) { // todo: assert (the purpose of _ensure_working is to prepare // curr_fill for manipulation) } if (_curr_fill->is_filled()) { // todo: assert (curr_fill should be immediately reset when it is // 'filled') } if (_curr_fill->filled_qty == 0) { } fill_qty -= _curr_fill.add_partial_fill(_curr_order().price, fill_qty); } } const OrderStatus& FillOp::_curr_order() { return _order->second; } // requirements to 'steal' fill from the exchange order: // 1) transaction time 'at or before' that of the exch order. // 2) price 'better than or equal to' that of the exchange order // (i.e., >= for bids, <= for asks). bool FillOp::_can_steal_fill(const ExchangeOrderSnapshot &exch_order, const OrderStatus &order) { return order.transaction_time.to_uint64() <= exch_order.transaction_time.to_uint64() && (exch_order.is_bid ? order.price.price() >= exch_order.state->price.price() : order.price.price() <= exch_order.state->price.price()); } bool FillOp::_ensure_working(const ExchangeOrderSnapshot& exch_order) { if (_curr_fill != nullptr && _curr_fill->working_qty() > 0) return true; // God forbid, something terrible happens and ++_price_level never reaches _book_end // limit the num of loops to an arbitrary large number int num_loops = 0; while (true) { if (++num_loops > 16384) { // todo: log a warning/error (something bad must have happened) break; } while (_order != _order_end) { if (_order->second.is_working() && _can_steal_fill(exch_order, _order->second)) { auto p = _fills.insert_or_assign(_order->second.id, FillData(_order->second.size, _order->second.filled_quantity)); if (p.second) { // todo: maybe assert (order should not already have a corresponding fill item) } _curr_fill = &(p.first->second); return true; } _order++; } if (++_price_level == _book_end) { break; } _order = _price_level->second.orders.begin(); } return false; } // uint32_t MBOBook::_assign_fills(const OrderStatus& target_order, uint32_t // filled_qty, OrderAtPriceIter order_it, OrderAtPriceIter order_end, // PriceNodeIter price_node_it, PriceNodeIter price_node_end) { if (order_it == // order_end) { if (price_node_it == price_node_end) return filled_qty; // //} // int32_t working_qty = target_order.working_quantity(); // while (filled_qty < working_qty) { // if (order_it == order_end) { // if (++price_node_it == price_node_end) // break; // price_node_it->second.orders.begin(); //} //} // return filled_qty; //} void MBOBook::assign_fills( TimeStamp transaction_time, std::map::iterator working_orders_it, std::map::iterator working_orders_end, std::vector> &fills_out, std::vector trades) { if (working_orders_it == working_orders_it) return; FillData curr_fill; for (auto tit = trades.begin(); tit != trades.end(); tit++) { auto exch_order_it = _cache.find(tit->order_id); if (exch_order_it != _cache.end()) { SnapshotOrder state; TimeStamp exch_order_transaction_time; if (exch_order_it->second.try_match_order( transaction_time, *state, *exch_order_transaction_time)) { if (exch_order_transaction_time.to_uint64() > working_orders_it->first) { uint32_t wkg_qty = working_orders_it->second.working_quantity(); } } } } } void MBOBook::_assign_fills(const OrderStatus &target_order, const std::vector &trades, std::vector> &fills) { } void MBOBook::_assign_fills( TimeStamp transaction_time, WorkingBookIter worders_it, WorkingBookIter worders_end, std::vector> &fills_out, std::vector::iterator trades_it, std::vector::iterator trades_end) { if (trades_it == trades_end || worders_it == worders_end) return; FillData fill_data; worders_it->second.orders.begin(); do { FillData curr_fill; ExchangeOrderSnapshot exch_order; if (_find_exchange_order(transaction_time, trades_it->order_id, exch_order)) { } trades_it++; } while (trades_it != trades_end); } bool MBOBook::_find_exchange_order(TimeStamp transaction_time, ExchangeOrderKey order_id, ExchangeOrderSnapshot &result) { auto order_hist = _cache.find(order_id); if (order_hist == _cache.end()) { return false; } auto state_it = order_hist->second.lower_bound(transaction_time); if (state_it == order_hist->second.begin()) return false; auto priority = --state_it->second.priority; while (state_it != order_hist->second.begin() && --state_it->second.priority == priority) ; result = state_it->second; return true; } bool MBOBook::_find_working_order( TimeStamp transaction_time, IntPrice price, bool is_bid, std::map::iterator &it) { auto working_orders = is_bid ? _working_bids : _working_asks; auto price_level_it = working_orders.begin(); if (price_level_it == working_orders.end()) return false; it = working_orders.lower_bound(transaction_time); if (it == working_orders.begin()) return false; it--; return true; } // void MBOBook::_assign_fills(const OrderStatus& target_order, } // namespace md } // namespace bts } // namespace bts