#include "FillStore.h" #include "lib/api/services/OrderService.pb.h" #include "lib/orderman/actors/SODPosition.h" #include "lib/orderman/events/NewExecutionReportEvent.h" #include "lib/orderman/events/NewSecuritiesEvent.h" #include "lib/orderman/events/OrderDefinitionUpdateEvent.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"; enum class FillStore::State { WAIT_SEC, READY }; struct FillStore::UnresolvedFills { std::vector fills; api::Security const *security; }; struct FillStore::PImpl { PImpl() : state(State::WAIT_SEC), store(new persistence::SqliteStore("fills")), known_orders(), fills_with_unresolved_accounts(), sod_pos_map() {} State state; 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; mercury::Actor::SubHandle stored_sec_sub; SODPosition sod_pos_map; }; FillStore::FillStore() : me(new PImpl()) {} void FillStore::init() { me->state = State::WAIT_SEC; me->stored_sec_sub = subscribe([this](auto const &update) { if (update.events) { if (me->state == State::WAIT_SEC) { LOG(OrderMan, Info, "Fill store received initial list of {} securities", update.events->size()); // don't need securities anymore, NewExecutionReportEvent // events will carry the security with them unsubscribe(me->stored_sec_sub); // Subscribe to NewExecutionReportEvent for new fills subscribe( [this](auto const &ev) { this->on_execution_report(ev); }); // index securities std::map sec_idx; for (auto const &ev : *(update.events)) { auto sec_ptr = ev.security; sec_idx[SecurityKey(sec_ptr->id())] = sec_ptr; } // 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 = sec_idx[SecurityKey(fill.security_key())]; if (sec) { 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, Diag, "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, sec_idx); publish_batch(me->fill_stream, stored_batch); // subscribe to order updates so we can resolve // account ids for future execution reports subscribe([this](auto const &ev) { this->on_order_definition_update(ev); }); me->state = State::READY; } } }); } 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); 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()); } 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()); 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