From feff24bf10184e5fc4d03c355da31dcdb290e0e0 Mon Sep 17 00:00:00 2001 From: Taha Afzal Date: Thu, 23 Apr 2026 03:41:42 +0000 Subject: [PATCH] Sim execution works with live cme md now --- .../mdp3_onixs/ScriptedMdp3Feeder.cpp | 185 ++++++++++++++++++ .../platforms/mdp3_onixs/ScriptedMdp3Feeder.h | 39 ++++ 2 files changed, 224 insertions(+) create mode 100644 src/app_sparkd/sparkd/md/platforms/mdp3_onixs/ScriptedMdp3Feeder.cpp create mode 100644 src/app_sparkd/sparkd/md/platforms/mdp3_onixs/ScriptedMdp3Feeder.h diff --git a/src/app_sparkd/sparkd/md/platforms/mdp3_onixs/ScriptedMdp3Feeder.cpp b/src/app_sparkd/sparkd/md/platforms/mdp3_onixs/ScriptedMdp3Feeder.cpp new file mode 100644 index 000000000..80ad04b89 --- /dev/null +++ b/src/app_sparkd/sparkd/md/platforms/mdp3_onixs/ScriptedMdp3Feeder.cpp @@ -0,0 +1,185 @@ +#include "ScriptedMdp3Feeder.h" + +#include "spark/api/domain/Venue.pb.h" +#include "spark/api/domain/SecurityType.pb.h" +#include "spark/md/IntPrice.h" + +#include +#include +#include +#include +#include + +#include +#include + +namespace bts::md { + +namespace { + +class ScriptedSecurity : public onixs::Security { + public: + explicit ScriptedSecurity(uint32_t id) : onixs::Security(id) {} + + using onixs::Security::asset; + using onixs::Security::group; + using onixs::Security::symbol; + + void sequence(std::uint32_t value) { seqNumber(value); } +}; + +struct ScriptedPacketHeader { + onixs::SequenceNumber seqNumber = 0; + onixs::Timestamp sendingTime; +}; + +PriceSize make_level(double price, float size) { + if (size == 0) { + return PriceSize(); + } + return PriceSize(IntPrice_from_double<7>(price), size); +} + +onixs::Decimal to_onixs_decimal(const PriceSize &level) { + // MDP3Listener applies the security display_factor when converting OnixS + // prices, so the scripted price must be expressed in pre-display-factor + // exchange units here. For ES with display_factor=-2, 7000.00 becomes + // mantissa 700000 with exponent 0. + return onixs::Decimal( + static_cast(std::llround(level.price() * 100.0)), + 0); +} + +using DirectBookPtr = std::unique_ptr; + +DirectBookPtr make_direct_book(const ScriptedMdp3Feeder::DirectBookUpdate &update) { + DirectBookPtr book( + onixs::DirectBookFactory::construct(COCONUT_MAX_DEPTH), + &onixs::DirectBookFactory::destruct); + std::size_t bid_depth = 0; + std::size_t ask_depth = 0; + while (bid_depth < update.bids.size() && !update.bids[bid_depth].is_nan() && + update.bids[bid_depth].size() != 0) { + ++bid_depth; + } + while (ask_depth < update.asks.size() && !update.asks[ask_depth].is_nan() && + update.asks[ask_depth].size() != 0) { + ++ask_depth; + } + book->depth(static_cast(std::max(bid_depth, ask_depth))); + book->bids().resize(static_cast(bid_depth)); + book->offers().resize(static_cast(ask_depth)); + for (std::size_t i = 0; i < bid_depth; ++i) { + book->bids()[i] = onixs::DirectPriceLevel( + to_onixs_decimal(update.bids[i]), + static_cast(update.bids[i].size()), + 1); + } + for (std::size_t i = 0; i < ask_depth; ++i) { + book->offers()[i] = onixs::DirectPriceLevel( + to_onixs_decimal(update.asks[i]), + static_cast(update.asks[i].size()), + 1); + } + return book; +} + +onixs::Handler &scripted_handler() { + static onixs::HandlerSettings settings; + static bool initialized = false; + if (!initialized) { + settings.channel(1); + initialized = true; + } + static onixs::Handler handler(settings); + return handler; +} + +ScriptedSecurity &scripted_security() { + static ScriptedSecurity security(1000001); + static bool initialized = false; + if (!initialized) { + security.symbol(onixs::StrRef("ESM6", 4)); + security.group(onixs::StrRef("ES", 2)); + security.asset(onixs::StrRef("ES", 2)); + initialized = true; + } + return security; +} + +} // namespace + +ScriptedMdp3Feeder::ScriptedMdp3Feeder(MDP3Listener &listener) + : _listener(listener) {} + +void ScriptedMdp3Feeder::publish_hard_coded_future() const { + FixedFutureSecurity security; + fill_hard_coded_future(security); + _listener.add_security(security, co2::TRADINGSTATUS_NORMAL); +} + +void ScriptedMdp3Feeder::publish_hard_coded_direct_book() const { + publish_hard_coded_direct_book(1, 0.0); +} + +void ScriptedMdp3Feeder::publish_hard_coded_direct_book( + std::uint32_t sequence, double mid_price_shift) const { + const auto update = hard_coded_direct_book(sequence, mid_price_shift); + auto book = make_direct_book(update); + auto &handler = scripted_handler(); + auto &security = scripted_security(); + security.sequence(sequence); + + ScriptedPacketHeader packet_header; + packet_header.seqNumber = sequence; + onixs::PacketArgs packet(&packet_header, sizeof(packet_header)); + packet.receiveTime(onixs::Timestamp(update.packet_time.to_uint64())); + + _listener.onPacket(handler, packet); + _listener.onBookUpdate(handler, security, *book); + _listener.onEndOfPacket(handler, packet); +} + +void ScriptedMdp3Feeder::publish_hard_coded_values() const { + publish_hard_coded_future(); + publish_hard_coded_direct_book(); +} + +void ScriptedMdp3Feeder::fill_hard_coded_future(FixedFutureSecurity &security) { + security.key = 1000001; + security.numeric_id = 1000001; + security.type = api::SECURITYTYPE_FUTURE; + security.exchange = api::Venue::VENUE_GLBX; + security.symbol("ES"); + security.alt_symbol("ES"); + security.security_desc("ESM6"); + security.currency("USD"); + security.display_factor = -2; + security.low_tick = 25.0; + security.high_tick = 25.0; + security.price_break = 0.0; + security.cash_per_point = 50.0; + security.expiration_year = 2026; + security.expiration_month = 6; + security.expiration_day = 19; +} + +ScriptedMdp3Feeder::DirectBookUpdate +ScriptedMdp3Feeder::hard_coded_direct_book( + std::uint32_t sequence, double mid_price_shift) { + DirectBookUpdate update; + const double best_bid = 7000.0 + mid_price_shift; + const double best_ask = 7001.0 + mid_price_shift; + update.exchange_security_id = 1000001; + update.bids[0] = make_level(best_bid, 10); + update.bids[1] = make_level(best_bid - 0.25, 10); + update.bids[2] = make_level(best_bid - 0.50, 10); + update.asks[0] = make_level(best_ask, 10); + update.asks[1] = make_level(best_ask + 0.25, 10); + update.asks[2] = make_level(best_ask + 0.50, 10); + update.packet_time = utils::TimeStamp::now(); + update.sequence = sequence; + return update; +} + +} // namespace bts::md diff --git a/src/app_sparkd/sparkd/md/platforms/mdp3_onixs/ScriptedMdp3Feeder.h b/src/app_sparkd/sparkd/md/platforms/mdp3_onixs/ScriptedMdp3Feeder.h new file mode 100644 index 000000000..ce8e2d2b2 --- /dev/null +++ b/src/app_sparkd/sparkd/md/platforms/mdp3_onixs/ScriptedMdp3Feeder.h @@ -0,0 +1,39 @@ +#pragma once + +#include "MDP3Listener.h" +#include "spark/md/PriceSize.h" +#include "spark/utils/TimeStamp.h" + +#include +#include + + +namespace bts::md { + +class ScriptedMdp3Feeder { + public: + struct DirectBookUpdate { + uint32_t exchange_security_id = 0; + std::array bids = {}; + std::array asks = {}; + utils::TimeStamp packet_time = utils::TimeStamp::now(); + uint32_t sequence = 1; + }; + + explicit ScriptedMdp3Feeder(MDP3Listener &listener); + + void publish_hard_coded_future() const; + void publish_hard_coded_direct_book() const; + void publish_hard_coded_direct_book(std::uint32_t sequence, + double mid_price_shift) const; + void publish_hard_coded_values() const; + + static void fill_hard_coded_future(FixedFutureSecurity &security); + static DirectBookUpdate hard_coded_direct_book( + std::uint32_t sequence = 1, double mid_price_shift = 0.0); + + private: + MDP3Listener &_listener; +}; + +} // namespace bts::md