#include "ExchangeSimApplication.h" #include "lib/md/SecurityDB.h" #include "IDGenerator.h" #include "lib/config/config.h" #include "lib/config/protos/ExchangeSimCfg.pb.h" #include "lib/domain_model/Security.h" #include "lib/logging/logging.h" #include "lib/md/MDConsumer.h" #include "lib/md/MDProducer.h" #include "lib/md/Shm/FixedSecurity.h" #include "lib/md/Shm/MDStructure.h" #include "lib/md/get_symbols.h" #include "lib/utils/Dispatcher.h" #include "lib/utils/datetime.h" #include "server/sparkd/exchange_sim/book/Order.h" #include "server/sparkd/exchange_sim/book/OrderMatcher.h" #include "server/sparkd/exchange_sim/book/book_transform.h" #include "server/sparkd/persistence/SqliteStore.h" #include #include #include #include #include namespace bts { namespace ex_sim { GCC_DIAG_OFF("-Wdeprecated") GCC_DIAG_OFF("-Wignored-qualifiers") api::Side convert(const FIX::Side &side) { switch (side) { case FIX::Side_BUY: return api::SIDE_BUY; case FIX::Side_SELL: return api::SIDE_SELL; default: throw std::logic_error("Unsupported Side, use buy or sell"); } } FIX::Side convert(api::Side side) { switch (side) { case api::SIDE_BUY: return FIX::Side(FIX::Side_BUY); case api::SIDE_SELL: return FIX::Side(FIX::Side_SELL); default: throw std::logic_error("Unsupported Side, use buy or sell"); } } api::orderman::NativeOrderType convert(const FIX::OrdType &ordType) { switch (ordType) { case FIX::OrdType_LIMIT: return api::orderman::NATIVE_ORDER_TYPE_LIMIT; default: throw std::logic_error("Unsupported Order Type, use limit"); } } FIX::OrdType convert(api::orderman::NativeOrderType type) { switch (type) { case api::orderman::NATIVE_ORDER_TYPE_LIMIT: return FIX::OrdType(FIX::OrdType_LIMIT); default: throw std::logic_error("Unsupported Order Type, use limit"); } } FIX::ManualOrderIndicator convert(const api::orderman::ManualOrderIndicator &moi) { switch (moi) { case api::orderman::MANUAL_ORDER_INDICATOR_YES: return FIX::ManualOrderIndicator(true); default: return FIX::ManualOrderIndicator(false); } } api::orderman::ManualOrderIndicator convert(const FIX::ManualOrderIndicator &moi) { if (moi.getValue()) return api::orderman::MANUAL_ORDER_INDICATOR_YES; else return api::orderman::MANUAL_ORDER_INDICATOR_NO; } struct ExchangeSimApplication::MDConsumerRec { md::MDConsumerHandle handle; api::Security security; utils::TimeStamp last_read; Book previous_md_book; }; struct ExchangeSimApplication::PImpl { PImpl() : producer(co2::PLATFORM_SIM), consumer(api::VENUE_GLBX, co2::PLATFORM_MDP3ONIXS), timer(utils::Dispatcher::background(), 200, [=]() { producer.heartbeat(); }), do_merge_with_md(config::get().merge_with_md()), store(new persistence::SqliteStore("order_book")) {} IDGenerator m_generator; md::MDProducer producer; md::MDConsumer consumer; std::map md_consumer_recs; std::map md_producer_handles; utils::DispatcherTimer timer; std::map matchers; bool do_merge_with_md; TimeStamp last_scan; std::unique_ptr store; std::mutex lock; }; ExchangeSimApplication::ExchangeSimApplication() : me(new PImpl()) { me->timer.start(); me->consumer.on_security( [this](api::Security const &s, md::MDConsumerHandle h) { on_new_security(s, h); }); } ExchangeSimApplication::~ExchangeSimApplication() {} void ExchangeSimApplication::onCreate(const FIX::SessionID &) {} void ExchangeSimApplication::onLogon(const FIX::SessionID &) {} void ExchangeSimApplication::onLogout(const FIX::SessionID &) {} void ExchangeSimApplication::toAdmin(FIX::Message &, const FIX::SessionID &) {} void ExchangeSimApplication::toApp( FIX::Message &, const FIX::SessionID &) throw(FIX::DoNotSend) {} void ExchangeSimApplication::fromAdmin( const FIX::Message &, const FIX::SessionID &) throw(FIX::FieldNotFound, FIX::IncorrectDataFormat, FIX::IncorrectTagValue, FIX::RejectLogon) {} void ExchangeSimApplication::fromApp( const FIX::Message &message, const FIX::SessionID &sessionID) throw(FIX::FieldNotFound, FIX::IncorrectDataFormat, FIX::IncorrectTagValue, FIX::UnsupportedMessageType) { std::lock_guard l(me->lock); crack(message, sessionID); } void ExchangeSimApplication::onMessage(const FIX42::NewOrderSingle &message, const FIX::SessionID &) { FIX::SenderCompID senderCompID; FIX::TargetCompID targetCompID; FIX::SenderSubID senderSubID; FIX::ClOrdID clOrdID; FIX::Account account; FIX::SecurityDesc sec_desc; FIX::Side side; FIX::OrdType ordType; FIX::Price price; FIX::OrderQty orderQty; FIX::TimeInForce timeInForce(FIX::TimeInForce_DAY); FIX::ManualOrderIndicator moi; message.getHeader().get(senderCompID); message.getHeader().get(targetCompID); message.getHeader().get(senderSubID); message.get(clOrdID); message.get(account); message.get(sec_desc); message.get(side); message.get(ordType); if (ordType == FIX::OrdType_LIMIT) message.get(price); message.get(orderQty); message.getFieldIfSet(timeInForce); message.getField(moi); LOG(ExSim, Info, "Got new order: {}", clOrdID.getString()); try { if (timeInForce != FIX::TimeInForce_DAY) throw std::logic_error("Unsupported TIF, use Day"); auto &matcher = me->matchers[sec_desc]; if (matcher.get_order(clOrdID)) throw std::logic_error("Duplicate order"); ex_sim::Order order; order.set_clordid (clOrdID); order.set_sec_desc(sec_desc); order.set_account(account); order.set_owner(senderCompID); order.set_target(targetCompID); order.set_sender_sub_id(senderSubID); order.set_side(convert(side)); order.set_type(convert(ordType)); order.set_price(price); order.set_quantity(orderQty); order.set_moi(convert(moi)); order.set_open_quantity(order.quantity()); order.set_executed_quantity(0); order.set_avg_executed_price(0); order.set_last_executed_price(0); order.set_last_executed_quantity(0); *order.mutable_create_time() = utils::pb::pb_now(); updateOrder(order, FIX::OrdStatus_NEW); std::vector updates; matcher.add(order, &updates); for (auto const &o : updates) fillOrder(o); update_md(sec_desc); } catch (std::exception &e) { rejectOrder(senderCompID, senderSubID, targetCompID, clOrdID, sec_desc, side, account, e.what()); } } void ExchangeSimApplication::onMessage( const FIX42::OrderCancelReplaceRequest &message, const FIX::SessionID &) { FIX::Price price; FIX::OrderQty orderQty; FIX::ClOrdID clOrdID; FIX::SecurityDesc sec_desc; FIX::Side side; FIX::SenderCompID senderCompID; FIX::SenderSubID senderSubID; FIX::TargetCompID targetCompID; FIX::Account account; message.getHeader().get(senderCompID); message.getHeader().get(senderSubID); message.getHeader().get(targetCompID); message.get(clOrdID); message.get(price); message.get(orderQty); message.get(sec_desc); message.get(side); message.get(account); LOG(ExSim, Info, "Got order change: {} {}@{}", clOrdID.getString(), orderQty, price); try { auto &matcher = me->matchers[sec_desc]; auto order = matcher.get_order(clOrdID); if (!order) throw std::logic_error("Unknown order"); else { auto order_cpy = *order; order_cpy.set_price (price); order_cpy.set_quantity(orderQty); updateOrder(order_cpy, FIX::OrdStatus_REPLACED); std::vector updates; matcher.update(clOrdID, Price(price), Quantity(orderQty), &updates); for (auto const &o : updates) fillOrder(o); update_md(sec_desc); } } catch (std::exception &e) { rejectOrder(senderCompID, senderSubID, targetCompID, clOrdID, sec_desc, side, account, e.what()); } } void ExchangeSimApplication::onMessage(const FIX42::OrderCancelRequest &message, const FIX::SessionID &) { FIX::OrigClOrdID origClOrdID; FIX::SecurityDesc sec_desc; FIX::Side side; message.get(origClOrdID); message.get(sec_desc); message.get(side); LOG(ExSim, Info, "Got order cancellation for: {}", origClOrdID.getString()); try { auto &matcher = me->matchers[sec_desc]; auto order = matcher.get_order(origClOrdID); if (!order) throw std::logic_error("Unknown order"); else { auto order_cpy = *order; order_cpy.set_open_quantity(0); updateOrder(order_cpy, FIX::OrdStatus_CANCELED); matcher.cancel(origClOrdID); update_md(sec_desc); } } catch (std::exception &) { } } void ExchangeSimApplication::rejectOrder(const Order &order) { updateOrder(order, FIX::OrdStatus_REJECTED); } void ExchangeSimApplication::fillOrder(const Order &order) { updateOrder(order, order.open_quantity() == 0 ? FIX::OrdStatus_FILLED : FIX::OrdStatus_PARTIALLY_FILLED); } void ExchangeSimApplication::updateOrder(const Order &order, char status) { FIX::TargetCompID targetCompID(order.owner()); FIX::SenderCompID senderCompID(order.target()); FIX::TargetSubID(order.sender_sub_id()); FIX42::ExecutionReport fixOrder; fixOrder.set(FIX::OrderID("12345678901234567")); fixOrder.set(FIX::ExecID(me->m_generator.genExecutionID())); fixOrder.set(FIX::ExecTransType(FIX::ExecTransType_NEW)); fixOrder.set(FIX::ExecType(status)); fixOrder.set(FIX::OrdStatus(status)); fixOrder.set(FIX::Account(order.account())); fixOrder.set(FIX::SecurityDesc(order.sec_desc())); fixOrder.set(FIX::Side(convert(order.side()))); fixOrder.set(FIX::LeavesQty(order.open_quantity())); fixOrder.set(FIX::CumQty(order.executed_quantity())); fixOrder.set(FIX::AvgPx(order.avg_executed_price())); fixOrder.set(FIX::ClOrdID(order.clordid())); fixOrder.set(FIX::OrderQty(order.quantity())); fixOrder.set(FIX::TimeInForce(FIX::TimeInForce_DAY)); fixOrder.set(FIX::OrdType(convert(order.type()))); fixOrder.set(FIX::Price(order.price())); fixOrder.set(FIX::OrderQty(order.quantity())); fixOrder.setField(convert(order.moi())); boost::posix_time::ptime now = boost::posix_time::second_clock::universal_time(); fixOrder.set(FIX::TransactTime( FIX::UtcTimeStamp(now.time_of_day().hours(), now.time_of_day().minutes(), now.time_of_day().seconds(), now.date().day(), now.date().month(), now.date().year()), true)); if (status == FIX::OrdStatus_FILLED || status == FIX::OrdStatus_PARTIALLY_FILLED) { fixOrder.set(FIX::LastShares(order.last_executed_quantity())); fixOrder.set(FIX::LastPx(order.last_executed_price())); } fixOrder.getHeader().set(FIX::TargetSubID(order.sender_sub_id())); try { FIX::Session::sendToTarget(fixOrder, senderCompID, targetCompID); } catch (FIX::SessionNotFound &) { } } void ExchangeSimApplication::rejectOrder( const FIX::SenderCompID &sender, const FIX::SenderSubID &sender_sub_id, const FIX::TargetCompID &target, const FIX::ClOrdID &clOrdID, const FIX::SecurityDesc &symbol, const FIX::Side &side, const std::string &account, const std::string &message) { FIX::TargetCompID targetCompID(sender.getValue()); FIX::SenderCompID senderCompID(target.getValue()); FIX42::ExecutionReport fixOrder; fixOrder.set(FIX::OrderID("12345678901234567")); fixOrder.set(FIX::ExecID(me->m_generator.genExecutionID())); fixOrder.set(FIX::ExecTransType(FIX::ExecTransType_NEW)); fixOrder.set(FIX::ExecType(FIX::ExecType_REJECTED)); fixOrder.set(FIX::OrdStatus(FIX::ExecType_REJECTED)); fixOrder.set(symbol); fixOrder.set(FIX::Account(account)); fixOrder.set(side); fixOrder.set(FIX::LeavesQty(0)); fixOrder.set(FIX::Price(0)); fixOrder.set(FIX::OrderQty(0)); fixOrder.set(FIX::CumQty(0)); fixOrder.set(FIX::LastShares(0)); fixOrder.set(FIX::AvgPx(0)); fixOrder.set(FIX::LastPx(0)); fixOrder.set(FIX::TimeInForce(FIX::TimeInForce_DAY)); fixOrder.set(clOrdID); fixOrder.set(FIX::Text(message)); fixOrder.set(FIX::OrdType(FIX::OrdType_LIMIT)); fixOrder.setField(FIX::ManualOrderIndicator(true)); fixOrder.getHeader().set(FIX::TargetSubID(sender_sub_id.getValue())); boost::posix_time::ptime now = boost::posix_time::second_clock::universal_time(); fixOrder.set(FIX::TransactTime( FIX::UtcTimeStamp(now.time_of_day().hours(), now.time_of_day().minutes(), now.time_of_day().seconds(), now.date().day(), now.date().month(), now.date().year()), true)); try { FIX::Session::sendToTarget(fixOrder, senderCompID, targetCompID); } catch (FIX::SessionNotFound &) { } } void ExchangeSimApplication::update_md(std::string const &sec_desc) { auto it = me->md_producer_handles.find(sec_desc); if (it != me->md_producer_handles.end()) { auto &handle = it->second; auto &matcher = me->matchers[sec_desc]; auto book = matcher.get_book(); auto price_factor = pow(10, handle.security().display_factor); auto n_bids = book.bids.size(); uint i = 0; auto it = book.bids.begin(); for (; i < std::min(md::COCONUT_MAX_DEPTH, (uint32_t)n_bids); i++, it++) { auto p = it->first.value() * price_factor; auto sz = it->second.value(); auto psz = md::PriceSize::from_double<7>(p, sz); LOG(ExSim, Diag, "Updating market data for {}. Depth: {} : Buy {}@{}", sec_desc, i, sz, p); handle.update_bid(utils::TimeStamp::now(), psz, i); } for (uint i = n_bids; i < md::COCONUT_MAX_DEPTH; i++) { auto psz = md::PriceSize::from_double<7>(os::nan(), 0); handle.update_bid(utils::TimeStamp::now(), psz, i); } auto n_asks = book.asks.size(); i = 0; it = book.asks.begin(); for (; i < std::min(md::COCONUT_MAX_DEPTH, (uint32_t)n_asks); i++, it++) { auto p = it->first.value() * price_factor; auto sz = it->second.value(); auto psz = md::PriceSize::from_double<7>(p, sz); LOG(ExSim, Diag, "Updating market data for {}. Depth: {} : Sell {}@{}", sec_desc, i, sz, p); handle.update_ask(utils::TimeStamp::now(), psz, i); } for (uint i = n_asks; i < md::COCONUT_MAX_DEPTH; i++) { auto psz = md::PriceSize::from_double<7>(os::nan(), 0); handle.update_ask(utils::TimeStamp::now(), psz, i); } auto new_trades = matcher.get_new_trades(); for (auto &t : new_trades) handle.add_trade(utils::TimeStamp::now(), md::PriceSize::from_double<7>(t.price.value() * price_factor, t.quantity.value()), datetime::ll_now()); } } void ExchangeSimApplication::on_new_security( api::Security const &s, md::MDConsumerHandle consumer_handle) { md::with_security_like(s, [this, s, consumer_handle](auto &fs) { md::convert_security(s, fs, true); md::MDProducerHandle handle = me->producer.add_security(fs); auto sec_desc = securities::descriptor(s); me->md_producer_handles.insert(make_pair(sec_desc, handle)); me->md_consumer_recs.insert( make_pair(sec_desc, MDConsumerRec{consumer_handle, s, utils::TimeStamp::now(), Book()})); }); } void ExchangeSimApplication::merge_with_md_book() { std::lock_guard l(me->lock); auto now = Timer::now(); if (sec_diff(now, me->last_scan) > 5) { me->last_scan = now; me->consumer.scan_securities(); } for (auto &kvp : me->md_consumer_recs) { auto &handle = kvp.second.handle; auto &sec = kvp.second.security; auto &ts = kvp.second.last_read; if (sec.has_synthetic()) { // } else if (me->do_merge_with_md) { md::PriceSize bids[md::COCONUT_MAX_DEPTH], asks[md::COCONUT_MAX_DEPTH]; if (handle.get_depth(ts, md::COCONUT_MAX_DEPTH, bids, asks) == md::HandleReadResult::SUCCESS) { Book previous_md_book = kvp.second.previous_md_book; Book current_md_book; for (size_t i = 0; i < md::COCONUT_MAX_DEPTH; i++) { auto b = securities::encode_exchange_price( sec, Price(bids[i].price())); if (!os::is_nan(b.value())) current_md_book.bids[b] = Quantity(bids[i].size()); auto a = securities::encode_exchange_price( sec, Price(asks[i].price())); if (!os::is_nan(a.value())) current_md_book.asks[a] = Quantity(asks[i].size()); } kvp.second.previous_md_book = current_md_book; LOG(ExSim, Diag, "Moving {} book \nfrom: {}\nto: {}", kvp.first, previous_md_book, current_md_book); merge_with_md_book(kvp.first, previous_md_book, current_md_book); } } } } void ExchangeSimApplication::merge_with_md_book(std::string const &sec_desc, Book const &src, Book const &dest) { auto &matcher = me->matchers[sec_desc]; auto src_book = matcher.get_book(); auto transform = compute_book_transform(src, dest); for (auto const &op : transform) { LOG(ExSim, Diag, "Adding synthetic book operation for {}. {} {} {}@{}", sec_desc, op.op == BookOp::ORDER ? std::string("Order") : std::string("Cancel"), op.side == api::SIDE_BUY ? std::string("Buy") : std::string("Sell"), op.quantity.value(), op.price.value()); switch (op.op) { case BookOp::ORDER: { ex_sim::Order order; order.set_clordid (fmt::format("synth-{}", me->m_generator.genOrderID())); order.set_sec_desc(sec_desc); order.set_account("SYNTH"); order.set_owner("SYNTH"); order.set_target("SYNTH"); order.set_sender_sub_id("SYNTH"); order.set_side(op.side); order.set_type(api::orderman::NATIVE_ORDER_TYPE_LIMIT); order.set_price(op.price.value()); order.set_quantity(op.quantity.value()); order.set_moi(api::orderman::MANUAL_ORDER_INDICATOR_NO); order.set_open_quantity(order.quantity()); order.set_executed_quantity(0); order.set_avg_executed_price(0); order.set_last_executed_price(0); order.set_last_executed_quantity(0); *order.mutable_create_time() = utils::pb::pb_now(); std::vector updates; matcher.add(order, &updates); for (auto const &o : updates) if (o.account() != "SYNTH") { LOG(ExSim, Diag, "Generating fill in response to md change for {}: {} {}@{}", sec_desc, o.side() == api::SIDE_BUY ? std::string("Buy") : std::string("Sell"), o.open_quantity(), o.price()); fillOrder(o); } break; } case BookOp::CANCEL: matcher.try_reduce_quantity_at_level(op.side,op.price,op.quantity,"SYNTH"); break; } } update_md(sec_desc); auto order_book = matcher.get_order_book("SYNTH"); order_book.set_sec_desc(sec_desc); persist_book(order_book); } void ExchangeSimApplication::persist_book(OrderBook const &b) { auto existing = me->store->retrieve_one("books",b.sec_desc()); auto val = utils::pb::to_string(b); if (existing.first) me->store->update("books", b.sec_desc(), val); else me->store->insert("books", b.sec_desc(), val); } GCC_DIAG_RESTORE() GCC_DIAG_RESTORE() } }