#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 "lib/utils/overloaded.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)) {} // returns the sod type of a non sod type fill auto sod_fill_kind (const api::orderman::Fill& fill){ auto rv = Fill::FILL_KIND_SOD; switch (fill.fill_kind()){ case Fill::FILL_KIND_STANDARD: rv = Fill::FILL_KIND_SOD; break; case Fill::FILL_KIND_LEG: rv = Fill::FILL_KIND_SOD_SPREAD_LEG; break; case Fill::FILL_KIND_STRIP_LEG: rv = Fill::FILL_KIND_SOD_STRIP_LEG; break; default: LOG(persist, Error, "Stored fill kinds can not be SOD"); rv = Fill::FILL_KIND_SOD; break; } return rv; } auto leg_fill_kind (const api::Security& parent, const api::Security& child){ if(securities::is_strip(parent)) return Fill::FILL_KIND_STRIP_LEG; else{ // parent is a spread if(securities::is_spread_of_strips(parent)){ // this has 3 levels strip spread -> strip -> outright // first level is an ordinary leg and second is a strip leg. // since we dont get the level passed we have to // use the following heurestics if(securities::is_strip(child)) return Fill::FILL_KIND_LEG; else return Fill::FILL_KIND_STRIP_LEG; } } return Fill::FILL_KIND_LEG; } 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())){ auto fill_kind = sod_fill_kind(fill); fill.set_fill_kind(fill_kind); } auto sec = me->oracle.get_security(SecurityKey(fill.security_key())); if (sec) { auto fk = kvp.first; me->known_fills.insert(fk); if (!config::get().enabled() || !securities::is_expired(*sec)) { 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; auto fk = fmt::format("{}:{}", er.order_key.value(), to_str(er.exchange_fill_id)); auto is_known = me->known_fills.count(fk); if (er.exec_type == api::orderman::EXEC_TYPE_PARTIAL_FILL || er.exec_type == api::orderman::EXEC_TYPE_FILL) { api::orderman::Fill fill; if (!is_known) { fill.set_order_key(er.order_key.value()); if (er.leg_security) { auto leg_type = leg_fill_kind(*er.security,*er.leg_security); fill.set_fill_kind(leg_type); 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_status(api::orderman::Fill::FILL_STATUS_ACTIVE); if(er.exec_type == api::orderman::EXEC_TYPE_TRADE_CORRECT) fill.set_fill_status(api::orderman::Fill::FILL_STATUS_CORRECTED); fill.set_fill_key(fk); auto exchange_ts = std::visit(overloaded { [](const orderman::ExchangeTimeStampStr& ts) { return utils::pb::to_timestamp( datetime::parse_FIX_timestamp(to_str(ts))); }, [](ExchangeTimeStamp64 ts) { google::protobuf::Timestamp gts; auto seconds = ts.value() / 1E9; gts.set_seconds(seconds); gts.set_nanos(ts.value() - (seconds * 1E9)); return gts; } }, 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 } } // 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