#include "lib/domain_model/Security.pb.h" #include "lib/logging/logging.h" #include "lib/md/UserSymbol.h" #ifndef NO_CPP0X #include "PlatformLoader.h" #include "lib/config/config.h" #include "MemorySegmentUtils.h" #include "lib/utils/SharedList.h" // for cleanup_shared_memory #include "lib/config/protos/market_data.pb.h" #include "lib/config/protos/symbols.pb.h" #include #ifdef _LINUX #include #endif using namespace std; using namespace bts; using namespace bts::co2; using namespace bts::api; namespace bts { namespace md { PlatformLoader::PlatformLoader(const md::PlatformConfig &config) : _config(config), _platform(PLATFORM_UNSET) { bts_assert_msg( Platform_Parse(config.platform(), &_platform), "Invalid market data platform " + config.platform()); } PlatformLoader::~PlatformLoader() {} void cleanup_shared_memory( const std::set &symbols, Platform platform) { for (const auto &sym : symbols) { string name = hot_segment_name(sym.symbol(), sym.type(), sym.exchange(), platform); utils::SharedList::delete_segments(name); LOG(md, Info, "Cleaned up /run/shm/{}", name); name = cold_segment_name(sym.symbol(), sym.type(), sym.exchange(), platform); utils::SharedList::delete_segments(name); LOG(md, Info, "Cleaned up /run/shm/{}", name); } } // Does not return void PlatformLoader::run() { // Gather symbols to use set symbols; size_t count = get_system_md_symbols(_config, symbols); if (count == 0) { LOG(md, Error, "No Symbols configured for this publisher."); return; } // Quickly try to clean the shared memory segments before anyone gets a stale // one cleanup_shared_memory(symbols, _platform); switch (_platform) { case PLATFORM_MDP3ONIXS: break; case PLATFORM_MDP3EMUMBA: break; case PLATFORM_ICEONIXS: break; default: bts_assert(false); break; } bts_assert_msg(false, "Market data run should not return"); } /////////////////////////////// size_t get_system_md_symbols(set &result) { auto &config = config::get(); return get_system_md_symbols(config, result); } void add_symbols_to_result( const google::protobuf::RepeatedPtrField &symbols, Venue exchange, SecurityType type, std::set *result) { bts_assert(result); for (const auto &s : symbols) { UserSymbol u(s, exchange, type); result->insert(u); } } size_t get_system_md_symbols( api::Venue exchange, SecurityType type, std::set &result) { LOG(md, Info, "get_system_md_symbols called for exch: {} type: {}", Venue_Name(exchange), SecurityType_Name(type)); { auto const &e = config::get().glbx(); if (exchange == api::VENUE_UNSET || exchange == api::VENUE_GLBX) { if (type == api::SECURITYTYPE_FUTURE || type == api::SECURITYTYPE_UNSET) { add_symbols_to_result( e.future(), api::VENUE_GLBX, api::SECURITYTYPE_FUTURE, &result); } if (type == api::SECURITYTYPE_OPTION || type == api::SECURITYTYPE_UNSET) { add_symbols_to_result( e.option(), api::VENUE_GLBX, api::SECURITYTYPE_OPTION, &result); } if (type == api::SECURITYTYPE_SPREAD || type == api::SECURITYTYPE_UNSET) { add_symbols_to_result( e.spread(), api::VENUE_GLBX, api::SECURITYTYPE_SPREAD, &result); } } if (exchange == api::VENUE_BTS || exchange == api::VENUE_UNSET) { std::set all_symbols; all_symbols.insert(e.future().begin(), e.future().end()); all_symbols.insert(e.option().begin(), e.option().end()); all_symbols.insert(e.spread().begin(), e.spread().end()); for (const auto &symbol : all_symbols) { result.insert(UserSymbol( symbol, api::VENUE_BTS, api::SECURITYTYPE_SYNTHETIC)); } } } { auto const &e = config::get().ice(); if (exchange == api::VENUE_UNSET || exchange == api::VENUE_ICE) { if (type == api::SECURITYTYPE_FUTURE || type == api::SECURITYTYPE_UNSET) add_symbols_to_result( e.future(), api::VENUE_ICE, api::SECURITYTYPE_FUTURE, &result); if (type == api::SECURITYTYPE_OPTION || type == api::SECURITYTYPE_UNSET) add_symbols_to_result( e.option(), api::VENUE_ICE, api::SECURITYTYPE_OPTION, &result); if (type == api::SECURITYTYPE_SPREAD || type == api::SECURITYTYPE_UNSET) add_symbols_to_result( e.spread(), api::VENUE_ICE, api::SECURITYTYPE_SPREAD, &result); } if (exchange == api::VENUE_BTS || exchange == api::VENUE_UNSET) { std::set all_symbols; all_symbols.insert(e.future().begin(), e.future().end()); all_symbols.insert(e.option().begin(), e.option().end()); all_symbols.insert(e.spread().begin(), e.spread().end()); for (const auto &symbol : all_symbols) result.insert(UserSymbol( symbol, api::VENUE_BTS, api::SECURITYTYPE_SYNTHETIC)); } } { auto const &e = config::get().sim(); if (exchange == api::VENUE_UNSET || exchange == api::VENUE_SIM) { if (type == api::SECURITYTYPE_FUTURE || type == api::SECURITYTYPE_UNSET) add_symbols_to_result( e.future(), api::VENUE_SIM, api::SECURITYTYPE_FUTURE, &result); if (type == api::SECURITYTYPE_OPTION || type == api::SECURITYTYPE_UNSET) add_symbols_to_result( e.option(), api::VENUE_SIM, api::SECURITYTYPE_OPTION, &result); if (type == api::SECURITYTYPE_SPREAD || type == api::SECURITYTYPE_UNSET) add_symbols_to_result( e.spread(), api::VENUE_SIM, api::SECURITYTYPE_SPREAD, &result); } if (exchange == api::VENUE_BTS || exchange == api::VENUE_UNSET) { std::set all_symbols; all_symbols.insert(e.future().begin(), e.future().end()); all_symbols.insert(e.option().begin(), e.option().end()); all_symbols.insert(e.spread().begin(), e.spread().end()); for (const auto &symbol : all_symbols) result.insert(UserSymbol( symbol, api::VENUE_BTS, api::SECURITYTYPE_SYNTHETIC)); } } { auto const &e = config::get().simice(); if (exchange == api::VENUE_UNSET || exchange == api::VENUE_SIMICE) { if (type == api::SECURITYTYPE_FUTURE || type == api::SECURITYTYPE_UNSET) add_symbols_to_result( e.future(), api::VENUE_SIMICE, api::SECURITYTYPE_FUTURE, &result); if (type == api::SECURITYTYPE_OPTION || type == api::SECURITYTYPE_UNSET) add_symbols_to_result( e.option(), api::VENUE_SIMICE, api::SECURITYTYPE_OPTION, &result); if (type == api::SECURITYTYPE_SPREAD || type == api::SECURITYTYPE_UNSET) add_symbols_to_result( e.spread(), api::VENUE_SIMICE, api::SECURITYTYPE_SPREAD, &result); } if (exchange == api::VENUE_BTS || exchange == api::VENUE_UNSET) { std::set all_symbols; all_symbols.insert(e.future().begin(), e.future().end()); all_symbols.insert(e.option().begin(), e.option().end()); all_symbols.insert(e.spread().begin(), e.spread().end()); for (const auto &symbol : all_symbols) result.insert(UserSymbol( symbol, api::VENUE_BTS, api::SECURITYTYPE_SYNTHETIC)); } } { if (exchange == api::VENUE_DERIBIT || exchange == api::VENUE_UNSET) { auto const &e = config::get().deribit(); if (type == api::SECURITYTYPE_FUTURE || type == api::SECURITYTYPE_UNSET) { add_symbols_to_result( e.future(), api::VENUE_DERIBIT, api::SECURITYTYPE_FUTURE, &result); } if (type == api::SECURITYTYPE_OPTION || type == api::SECURITYTYPE_UNSET) { add_symbols_to_result( e.option(), api::VENUE_DERIBIT, api::SECURITYTYPE_OPTION, &result); } } } for (const auto &u : result) { LOG(md, Info, "get_system_md_symbols loaded sym: {} exch: {} type: {}", u.symbol(), Venue_Name(u.exchange()), SecurityType_Name(u.type())); } return result.size(); } size_t get_system_md_symbols( api::Venue exchange, SecurityType type, std::vector *result) { bts_assert(result); set result_set; size_t size = get_system_md_symbols(exchange, type, result_set); result->reserve(size); result->assign(result_set.begin(), result_set.end()); return size; } size_t get_system_md_symbols( const md::PlatformConfig &config, std::set &out_result) { Venue v; bts_assert(api::Venue_Parse(config.exchange(), &v)); SecurityType st; bts_assert(api::SecurityType_Parse(config.type(), &st)); return get_system_md_symbols(v, st, out_result); } std::set get_system_md_symbols() { std::set all_symbols; get_system_md_symbols(VENUE_UNSET, SECURITYTYPE_UNSET, all_symbols); return all_symbols; } extern "C" BTS_EXPORT Platform platform_for_exchange(Venue v) { const auto &pmap = config::get(); co2::Platform platform; if (v == VENUE_SIM) return PLATFORM_SIM; if (v == VENUE_ICE && co2::Platform_Parse(pmap.ice_platform(), &platform)) return platform; // md2_tests.cpp depends on this fall through to yield SIM as the GLBX // platform if (co2::Platform_Parse(pmap.glbx_platform(), &platform)) return platform; return PLATFORM_UNSET; } } // namespace md } // namespace bts #endif