#include "ExchangeSimApplication.h" #include #include #include #include #include #include #include #include #include "lib/config/config.h" #include "lib/config/protos/exchange_sim_cfg.pb.h" #include "lib/config/protos/ports.pb.h" #include "lib/domain_model/Security.pb.h" #include "lib/logging/logging.h" #include "lib/os/os.h" #include "lib/domain_model/Security.pb.h" #include "lib/md/IntPrice.h" #include "lib/md/MBOProcessor.h" #include "lib/md/relay/RelayClient.h" #include "lib/md/relay/RelayRecorder.h" #include "lib/utils/bts_chrono.h" #include #include namespace bts { namespace sim { auto cfg = "[DEFAULT]\n" "ConnectionType=acceptor\n" "SocketAcceptPort=$EXCHANGE_SIM_FIX_PORT\n" "SocketReuseAddress=Y\n" "StartTime=06:02:00\n" "EndTime=06:01:00\n" "FileLogPath=$BTS_DATA_PATH/exchange_sim/$SIM_VENUE/log\n" "FileStorePath=$BTS_DATA_PATH/exchange_sim/$SIM_VENUE/store\n" "HeartBtInt=30\n" "[SESSION]\n" "BeginString=FIX.4.2\n" "SenderCompID=BTS\n" "TargetCompID=SPARK\n" "UseDataDictionary=N\n" "ValidateUserDefinedFields=N\n" "ValidateFieldsOutOfOrder=N\n"; md::PriceSize coerce_precision(const co2::PriceSizeMessage &p) { md::IntPrice raw = {p.mantissa(), (int8_t)p.exponent()}; auto price = md::IntPrice_from_double(raw.price()); return {price, p.size()}; } struct MDPlayer : md::IRelayWriter { MDPlayer( IMDPlayerObserver &obs, std::map &known_securities) : obs(obs), known_securities(known_securities), forward_implied(config::get().forward_implied()) { } void write(const co2::MDReply &data, bool *) override { for (auto const &ch : data.channels()) for (auto const &cr : ch.updates()) { auto it = known_securities.find(cr.key()); api::Security const *sec_ptr(nullptr); if (it == known_securities.end()) { bts_assert(cr.has_security()); sec_ptr = &cr.security(); known_securities[cr.key()] = *sec_ptr; obs.on_new_security(*sec_ptr); } else sec_ptr = &it->second; auto const &book = cr.direct(); md::PriceSize bids[md::COCONUT_MAX_DEPTH], asks[md::COCONUT_MAX_DEPTH]; size_t i = 0; for (auto &b : book.bids()) bids[i++] = md::PriceSize( {b.mantissa(), (int8_t)b.exponent()}, b.size()); size_t n = 0; for (auto &a : book.asks()) asks[n++] = md::PriceSize( {a.mantissa(), (int8_t)a.exponent()}, a.size()); std::array implied_bids, implied_asks; bool has_implied = false; { size_t x = 0; for (const auto &b : cr.implied().bids()) { implied_bids[x] = coerce_precision(b); x += 1; has_implied = true; } } { size_t x = 0; for (const auto &b : cr.implied().asks()) { implied_asks[x] = coerce_precision(b); x += 1; has_implied = true; } } if (has_implied && forward_implied) { obs.on_depth(*sec_ptr, bids, asks, &implied_bids, &implied_asks); } else { obs.on_depth(*sec_ptr, bids, asks, nullptr, nullptr); } if (cr.has_misc()) { const auto &s = cr.misc(); obs.on_high(*sec_ptr, s.session_high()); obs.on_low(*sec_ptr, s.session_low()); obs.on_open(*sec_ptr, s.open_price()); obs.on_close(*sec_ptr, s.close_price()); if (cr.misc().has_settlement()) obs.on_settlement( *sec_ptr, {s.settlement().mantissa(), (int8_t)s.settlement().exponent()}); } } } IMDPlayerObserver &obs; std::map &known_securities; bool forward_implied; }; int start_exchange_sim(bts::api::Venue venue, md::MBOWriter *mbo) { try { std::string t_name; os::get_thread_name(&t_name); LOG(md, Info, "ExchangeSim::start_exchange_sim t_id:{} t_name:{}", os::get_thread_id(), t_name); auto str_settings = std::string(cfg); auto venue_string = boost::to_lower_copy(api::Venue_Name(venue)); boost::replace_all(str_settings, "$BTS_DATA_PATH", os::bts_data_path()); boost::replace_all(str_settings, "$SIM_VENUE", venue_string); std::string port_string; switch (venue) { case api::VENUE_SIM: port_string = std::to_string(config::get().glbx_sim_fix_port()); break; case api::VENUE_SIMICE: port_string = std::to_string(config::get().ice_sim_fix_port()); break; default: bts_assert_msg(false, "Not a valid sim venue"); } boost::replace_all(str_settings, "$EXCHANGE_SIM_FIX_PORT", port_string); std::istringstream str{str_settings}; FIX::SessionSettings settings(str); ex_sim::ExchangeSimApplication application(venue, mbo); FIX::FileStoreFactory storeFactory(settings); FIX::FileLogFactory logFactory(settings); FIX::SocketAcceptor acceptor( application, storeFactory, settings, logFactory); acceptor.start(); std::map known_securities; const auto &conf = config::get(); while (true) { MDPlayer player(application, known_securities); switch (conf.source_case()) { case config::ExchangeSim::SOURCE_NOT_SET: case config::ExchangeSim::kReplayUrl: md::replay_url_with_fallback( conf.replay_url(), player, config::get().naggle_ms()); break; case config::ExchangeSim::kReplayFile: md::replay_file( conf.replay_file(), player, config::get().naggle_ms(), true); break; case config::ExchangeSim::kLocalRelay: { md::local_relay(player); } break; } } acceptor.stop(); return 0; } catch (std::exception &e) { std::cout << e.what() << std::endl; return 1; } } } // namespace sim } // namespace bts