#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/actors/SODPosition.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 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(), sod_pos_map(), 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; SODPosition sod_pos_map; 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()); for (auto const &kvp : all) if (utils::pb::from_string(kvp.second, &fill)) { 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; if (!me->sod_pos_map.add_fill_to_sod_pos(fill)) stored_batch.emplace_back(fill, *sec); me->known_fills.insert(fk); } } 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()); me->sod_pos_map.add_sod_pos_as_fill_events(stored_batch, me->oracle); 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 || er.exec_type == api::orderman::EXEC_TYPE_TRADE_CORRECT) { 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_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); struct timeval tv; gettimeofday(&tv, NULL); fill.mutable_timestamp()->set_seconds(tv.tv_sec); fill.mutable_timestamp()->set_nanos(tv.tv_usec * 1000); 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 (auto const &ev : *u.events) { auto &er = *ev.data; auto efi = to_str(er.exchange_fill_id); if(std::count(efi.begin(), efi.end(), ':') > 0) { auto cpos = efi.find_last_of(':'); efi = efi.substr(0, cpos); ExchangeFillID temp; str_to(efi, &temp); efi = to_str(temp); } if(efi == "") continue; auto fk = fmt::format("{}:{}", er.order_key.value(), efi); auto is_known = me->known_fills.count(fk); if(er.exec_type == api::orderman::EXEC_TYPE_TRADE_CANCEL) { auto data = me->store->retrieve_one(fills_ns, fk); Fill existing_fill; if(is_known && data.first && utils::pb::from_string(data.second, &existing_fill)) { existing_fill.set_fill_status(api::orderman::Fill::FILL_STATUS_BUSTED); auto fv = utils::pb::to_string(existing_fill); me->store->update(fills_ns, fk, fv); publish(me->fill_stream, existing_fill, *er.security); } else LOG(OrderMan, Warn, "Received bust for unknown fill {}", fk); } else if(er.exec_type == api::orderman::EXEC_TYPE_TRADE_CORRECT) { auto data = me->store->retrieve_one(fills_ns, fk); Fill existing_fill; if(is_known && data.first && utils::pb::from_string(data.second, &existing_fill)) { existing_fill.set_fill_status(api::orderman::Fill::FILL_STATUS_CORRECTED); existing_fill.set_last_price(er.last_price.value()); existing_fill.set_avg_price(er.avg_price.value()); existing_fill.set_side(er.side); existing_fill.set_last_shares(er.last_shares.value()); auto fv = utils::pb::to_string(existing_fill); me->store->update(fills_ns, fk, fv); publish(me->fill_stream, existing_fill, *er.security); } else LOG(OrderMan, Warn, "Received correction for unknown fill {}", fk); } } // 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