#include "FillStore.h" #include "lib/api/services/OrderService.pb.h" #include "lib/config/config.h" #include "lib/config/protos/toggles.pb.h" #include "lib/domain_model/Security.h" #include "lib/orderman/events/NewExecutionReportEvent.h" #include "lib/orderman/events/OrderDefinitionUpdateEvent.h" #include "lib/orderman/events/OrderStateUpdateEvent.h" #include "lib/orderman/events/StoredFillEvent.h" #include "lib/persistence/SqliteStore.h" #include "lib/utils/PBUtils.h" #include "lib/orderman/actors/SODPosition.h" #include #include namespace bts { namespace orderman { auto const fills_ns = "fills"; struct FillStore::UnresolvedFills { std::vector fills; api::Security const *security; }; struct FillStore::PImpl { PImpl(md::SecurityOracle &oracle) : store(new persistence::SqliteStore("fills")), known_orders(), fills_with_unresolved_accounts(), oracle(oracle) {} std::unique_ptr store; std::unordered_set known_fills; std::unordered_map known_orders; std::unordered_map fills_with_unresolved_accounts; mercury::Stream fill_stream; md::SecurityOracle oracle; }; FillStore::FillStore(md::SecurityOracle o) : me(new PImpl(o)) {} void FillStore::init() { // Subscribe to NewExecutionReportEvent for new fills subscribe( [this](auto const &ev) { this->on_execution_report(ev); }); // Load stored fills, resolve their securities and publish the lot api::orderman::Fill fill; std::vector stored_batch; LOG(persist, Info, "Loading load all fills"); auto all = me->store->retrieve_all()[fills_ns]; LOG(persist, Info, "Finished loading all {} fills", all.size()); SODPosition sod_helper; for (auto const &kvp : all) if (utils::pb::from_string(kvp.second, &fill)) { if (sod_helper.is_sod(fill.timestamp())) fill.set_fill_kind(Fill::FILL_KIND_SOD); auto sec = me->oracle.get_security(SecurityKey(fill.security_key())); if (sec) { if (config::get().enabled() && securities::is_expired(*sec)) continue; else { auto fk = kvp.first; me->known_fills.insert(fk); stored_batch.emplace_back(fill, *sec); } } else LOG(persist, Error, "Stored fill {} contains unresolved security {}. ", kvp.first, fill.security_key()); } else LOG(persist, Error, "Could not recover fill {} from storage. " "This should be investigated", kvp.first); me->fill_stream = make_stream(); LOG(OrderMan, Info, "Fill Store published {} fills recoved from disk out of {} " "total fills", stored_batch.size(), all.size()); publish_batch(me->fill_stream, stored_batch); // subscribe to order definition updates so we can resolve // account ids for future execution reports subscribe( [this](auto const &ev) { this->on_order_definition_update(ev); }); } // namespace orderman FillStore::~FillStore() {} void FillStore::on_execution_report( const mercury::Update &u) { if (u.events) { for (auto const &ev : *u.events) { auto &er = *ev.data; if (er.exec_type == api::orderman::EXEC_TYPE_PARTIAL_FILL || er.exec_type == api::orderman::EXEC_TYPE_FILL) { auto fk = fmt::format("{}:{}", er.order_key.value(), to_str(er.exchange_fill_id)); auto is_known = me->known_fills.count(fk); api::orderman::Fill fill; if (!is_known) { fill.set_order_key(er.order_key.value()); if (er.leg_security) { fill.set_fill_kind(api::orderman::Fill::FILL_KIND_LEG); fill.set_security_key(er.leg_security->id()); } else { fill.set_fill_kind(api::orderman::Fill::FILL_KIND_STANDARD); fill.set_security_key(er.security->id()); } fill.set_account(to_str(er.account_name)); fill.set_last_price(er.last_price.value()); fill.set_avg_price(er.avg_price.value()); fill.set_side(er.side); fill.set_last_shares(er.last_shares.value()); fill.set_fill_key(fk); auto exchange_ts = utils::pb::to_timestamp( datetime::parse_FIX_timestamp(to_str(er.transact_time))); *fill.mutable_timestamp() = exchange_ts; auto it = me->known_orders.find(er.order_key); if (it != me->known_orders.end()) { auto &order_def = it->second; fill.set_account(order_def.account()); fill.set_internal_account_id(order_def.account_id()); } else me->fills_with_unresolved_accounts[er.order_key] .fills.push_back(fill); auto fv = utils::pb::to_string(fill); me->store->insert(fills_ns, fk, fv); publish(me->fill_stream, fill, *er.security); } else LOG(OrderMan, Info, "Discarding known execution report for fill {}", fk); } // if partial_fill or fill } // for loop } // if u.events } void FillStore::on_order_definition_update( mercury::Update const &u) { if (u.events) for (auto const &ev : *u.events) { me->known_orders[ev.key] = *ev.def; // see if we can resolve the accounts for any fills that we // might have already received for this order auto &unresolved_fills = me->fills_with_unresolved_accounts[ev.key]; for (auto &f : unresolved_fills.fills) { f.set_account(ev.def->account()); f.set_internal_account_id(ev.def->account_id()); auto fv = utils::pb::to_string(f); me->store->update(fills_ns, f.fill_key(), fv); publish(me->fill_stream, f, *unresolved_fills.security); } unresolved_fills.fills.clear(); // end fill resolution } } } // namespace orderman } // namespace bts