Miscelaenous improvements.

This commit is contained in:
Pedro Pinto committed 2020-08-10 13:46:59 -04:00
1 parent 52bc0f1a17
commit 78bc14348d
5 files changed
+63 -25

No files matched your search

+11 -14
View File
@@ -64,26 +64,23 @@ void dump_snapshots() {
}
int QuickTest::execute(std::vector<std::string> const &) {
//dump_snapshots();
// dump_snapshots();
using namespace bts::spartan::mdp3;
struct Obs : IChannelObserver {
void on_recovery_complete(
std::vector<InstrumentSnapshot const *> const &snaps) override {
std::cout << "Initial Snapshot\n";
for (auto is : snaps) {
if (is->security_id() == MDP3SecurityID{12181})
std::cout << *is << "\n";
}
}
std::vector<InstrumentSnapshot const *> const &) override {}
void on_real_time_event(MDP3Event const &ev) override {
for (auto is : ev.affected_securities) {
if (is->security_id() == MDP3SecurityID{12181}) {
std::cout << "Seq numbers: " << ev.first_packet_sn << " to "
<< ev.last_packet_sn << "\n";
std::cout << *is << "\n";
for (auto s : ev.affected_securities) {
if (s->order_changes().size() || s->fills().size()) {
std::cout << "symbol: " << s->definition().symbol() << "\n"
<< "seq_num: " << ev.first_packet_sn << " to "
<< ev.last_packet_sn << "\n"
<< "changes:\n"
<< s->order_changes() << "Fills:\n"
<< s->fills();
}
}
}
@@ -249,20 +249,28 @@ void InstrumentSnapshot::clear_order_changes() { me->order_changes.clear(); }
void InstrumentSnapshot::insert_order(MDP3Order const &order) {
bts_assert_d(me->orders.count(order.key) == 0);
me->orders.insert({order.key, order});
me->order_changes.emplace_back(OrderChange{order.key, OrderChange::added});
me->order_changes.emplace_back(OrderChange{order.key,
order.price,
order.quantity,
OrderChange::added});
}
void InstrumentSnapshot::update_order(MDP3Order const &order) {
auto it = me->orders.find(order.key);
bts_assert_d(it != me->orders.end());
it->second = order;
me->order_changes.emplace_back(OrderChange{order.key, OrderChange::updated});
me->order_changes.emplace_back(OrderChange{order.key,
order.price,
order.quantity,
OrderChange::updated});
}
void InstrumentSnapshot::remove_order(MDP3OrderKey id) {
bts_assert_d(me->orders.count(id));
me->orders.erase(id);
me->order_changes.emplace_back(OrderChange{id, OrderChange::removed});
me->order_changes.emplace_back(OrderChange{id,
{}, {},
OrderChange::removed});
}
Book InstrumentSnapshot::build_book_from_orders (api::Side side) const
@@ -30,6 +30,9 @@ class InstrumentSnapshot {
InstrumentDefinition definition() const;
void set_definition(InstrumentDefinition const&);
// Symbol (via definition)
const char* symbol() const {return definition().symbol();}
// Last packet that affected this security
size_t last_processed_sn() const ;
void set_last_msg_processed_sn(size_t);
@@ -69,15 +69,18 @@ struct MDP3Order {
struct OrderChange {
MDP3OrderKey key;
md::IntPrice price;
MDP3Quantity quantity;
enum ChangeType { added, removed, updated } change;
};
inline std::ostream &operator<<(std::ostream &s, OrderChange const &ch) {
s << " ok: " << ch.key.value() << " "
<< (ch.change == OrderChange::added
? "create"
: (ch.change == OrderChange::updated ? "update" : "delete"));
<< " price: " << ch.price.price()
<< " qty: " << ch.quantity.value()
<< " " << (ch.change == OrderChange::added
? "create"
: (ch.change == OrderChange::updated ? "update" : "delete"));
return s;
}
@@ -93,6 +96,12 @@ inline std::ostream &operator<<(std::ostream &s, MDP3Order const &order) {
return s;
}
inline std::ostream &operator<<(std::ostream &s, OrderChanges const &changes) {
for (auto c : changes)
s << " " << c << "\n";
return s;
}
using Orders = std::unordered_map<MDP3OrderKey, MDP3Order>;
using MDP3TradeEntryID =
utils::ValueWrapper<struct MDP3TradeEntryIDTag, uint32_t>;
@@ -103,6 +112,7 @@ struct MDP3Fill {
MDP3TradeEntryID trade_entry_id;
enum Aggressor { THIS, OTHER, IMPLIED } aggressor;
};
inline std::ostream &operator<<(std::ostream &s, MDP3Fill const &fill) {
s << " ok: " << fill.key.value() << " "
<< "price: " << fill.price.price() << " "
@@ -113,6 +123,11 @@ inline std::ostream &operator<<(std::ostream &s, MDP3Fill const &fill) {
}
using Fills = std::vector<MDP3Fill>;
inline std::ostream &operator<<(std::ostream &s, Fills const &fills) {
for (auto f : fills)
s << " " << f << "\n";
return s;
}
const auto MDP3_MAX_BOOK_DEPTH = 10;
const auto MDP3_MAX_IMPLIED_BOOK_DEPTH = 2;
@@ -89,13 +89,13 @@ RecoveryHandler::MaybeSnapshot RecoveryHandler::try_build_snapshot_and_reset() {
// Does the queue extend beyond the newest event of each snapshot? If
// if does not then we could run the risk of applying an event to a
// snapshot that already has it.
if (me->book_snapshot.newest_snapshot_seq_num() >=
if (me->book_snapshot.newest_snapshot_seq_num() >
me->incremental_queue.newest_packet_seq_num()) {
// Accumulate a few more
LOG(Spartan, Warn,
"Most recent packet in incremental queue ({}) is not newer than most "
"recent last processed packet in snapshot ({}). Will accumulate a "
"few more packets in incremental queue",
"Most recent packet in incremental queue ({}) is older than most "
"recent last processed packet in snapshot ({}). Will acumulate a "
"few more packets in incremental queue.",
me->incremental_queue.newest_packet_seq_num(),
me->book_snapshot.newest_snapshot_seq_num());
return {};
@@ -156,6 +156,7 @@ RecoveryHandler::MaybeSnapshot RecoveryHandler::try_build_snapshot_and_reset() {
RealTimeHandler rt_handler(null_handler);
auto pending_packets = me->incremental_queue.get_queued_packets_and_reset();
auto current_snapshot = sortable_snapshots.begin();
uint32_t last_applied_packet = 0;
for (auto &p : pending_packets) {
auto pv = p.view();
@@ -180,7 +181,21 @@ RecoveryHandler::MaybeSnapshot RecoveryHandler::try_build_snapshot_and_reset() {
LOG(Spartan, Info, "Appplying queued packet with sequence number {}",
pv.seq_num());
parse_packet(pv, rt_handler);
last_applied_packet = pv.seq_num();
}
for (; current_snapshot != sortable_snapshots.end(); current_snapshot++) {
auto &this_snapshot = *current_snapshot;
bts_assert(this_snapshot.last_processed_sn == last_applied_packet);
LOG(Spartan, Info,
"Added snapshot with last update time {} for security {} with "
"last processed sn {}",
this_snapshot.snapshot->definition().last_update_time().value(),
this_snapshot.snapshot->security_id().value(),
this_snapshot.last_processed_sn);
rt_handler.add_snapshot(std::move(this_snapshot.snapshot));
}
bts_assert(current_snapshot == sortable_snapshots.end());
// At this point we are caught up and all the instrument snapshots