#include "server/sparkd/cmds/PrintHedgeOrders.h" #include "lib/api/services/OrderService.pb.h" #include "lib/persistence/SqliteStore.h" #include "lib/utils/PBUtils.h" namespace bts::cmds { PrintHedgeOrders::PrintHedgeOrders() : CLI::App( "Print out order ids for all hedge orders" "which were completed at least one second being created", "print-hedge-orders") { callback([&] { persistence::SqliteStore order_store("orders"); auto all_orders = order_store.retrieve_all()["orders"]; std::map> fill_times; persistence::SqliteStore fill_store("fills"); auto all_fills = fill_store.retrieve_all()["fills"]; for (auto const &kvp : all_fills) { api::orderman::Fill fill_rec; if (utils::pb::from_string( kvp.second, &fill_rec)) { auto ts = utils::pb::to_double_time(fill_rec.timestamp()); auto &p = fill_times[fill_rec.order_key()]; auto &min_time = p.second; if (min_time < ts) { p.first = kvp.first; min_time = ts; } } } std::cout << "order_key,time_to_first_fill,fill_id" << "\n"; api::orderman::OrderRecord rec; for (auto const &kvp : all_orders) if (utils::pb::from_string( kvp.second, &rec)) { auto &def = rec.def(); auto &state = rec.state(); if (rec.parent_key() && def.custom_def().has_limit() && def.sub_type() == api::orderman::OrderSubType::ORDER_SUB_TYPE_HEDGING_LEG) { auto create_ts = utils::pb::to_double_time(state.created()); auto &first_fill = fill_times[rec.key()]; auto delta = first_fill.second - create_ts; if (first_fill.first.size()) std::cout << kvp.first << "," << delta << "," << first_fill.first << "\n"; } } }); } } // namespace bts::cmds