160 lines
6.0 KiB
C++
160 lines
6.0 KiB
C++
#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/OrderStateUpdateEvent.h"
|
|
#include "lib/orderman/events/StoredFillEvent.h"
|
|
#include "lib/persistence/SqliteStore.h"
|
|
#include "lib/utils/PBUtils.h"
|
|
|
|
#include <sys/time.h>
|
|
|
|
namespace bts {
|
|
namespace orderman {
|
|
|
|
auto const fills_ns = "fills";
|
|
|
|
struct FillStore::UnresolvedFills {
|
|
std::vector<api::orderman::Fill> 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<persistence::IStore> store;
|
|
std::unordered_set<std::string> known_fills;
|
|
std::unordered_map<OrderKey, api::orderman::OrderDefinition const *>
|
|
known_orders;
|
|
std::unordered_map<OrderKey, UnresolvedFills> fills_with_unresolved_accounts;
|
|
mercury::Stream<StoredFillEvent> 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<NewExecutionReportEvent>(
|
|
[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<StoredFillEvent> 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<api::orderman::Fill>(kvp.second,
|
|
&fill)) {
|
|
|
|
auto sec = me->oracle.get_security(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, 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<StoredFillEvent>();
|
|
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 updates so we can resolve
|
|
// account ids for future execution reports
|
|
subscribe<OrderStateUpdateEvent>(
|
|
[this](auto const &ev) { this->on_order_state_update(ev); });
|
|
}
|
|
|
|
FillStore::~FillStore() {}
|
|
|
|
void FillStore::on_execution_report(
|
|
const mercury::Update<NewExecutionReportEvent> &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 order_def = me->known_orders[er.order_key];
|
|
if (order_def)
|
|
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_state_update(
|
|
mercury::Update<OrderStateUpdateEvent> const &u) {
|
|
if (u.events)
|
|
for (auto const &ev : *u.events) {
|
|
me->known_orders[ev.key] = ev.definition;
|
|
|
|
// 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.definition->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
|