diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/algos/Sniper.cpp b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/algos/Sniper.cpp index 8f8e606be..a2d8d1167 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/algos/Sniper.cpp +++ b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/algos/Sniper.cpp @@ -21,6 +21,7 @@ struct Sniper::Target { Polygon const polygon; std::string const descriptor; double value = Worthless; + MDP3Quantity quantity = MDP3Quantity{0}; void mark_as_worthless() { value = Worthless; } @@ -31,6 +32,7 @@ struct Sniper::Shot { Target const ⌖ MDP3TimeStamp observed_ts; double value_at_observation_ts; + MDP3Quantity quantity; std::unordered_map> fills; }; @@ -56,19 +58,22 @@ struct Sniper::PImpl { Sniper::Sniper() : me(new PImpl()) {} Sniper::~Sniper() {} +int param_or_default(protos::SimRunOptions const &options, + std::string const &name, int default_value) { + auto it = options.parameters().find(name); + if (it != options.parameters().end()) + return std::stoi(it->second); + else + return default_value; +} + void Sniper::init(ISimServices &s, protos::SimRunOptions const &options) { me->sim = &s; me->options = options; - auto it = options.parameters().find("MinOrdersAtTop"); - if (it != options.parameters().end()) - me->min_orders_at_top = std::stoi(it->second); - else - me->min_orders_at_top = 1; - it = options.parameters().find("Qty"); - if (it != options.parameters().end()) - me->order_qty = std::stoi(it->second); - else - me->order_qty = 1; + + me->min_orders_at_top = param_or_default(options, "MinOrdersAtTop", 1); + me->order_qty = param_or_default(options, "Qty", 1); + me->state = State::WATCH; me->books.clear(); me->targets.clear(); @@ -104,14 +109,20 @@ void Sniper::on_notification(AlgoNotification const &n) { void Sniper::eval(std::vector const &targets) { for (auto &t : targets) { t->value = 0; + int qty = me->order_qty; for (auto &l : t->polygon.parts) { auto mkt_side = securities::other_side(side_from_ratio(l.ratio)); auto &mkt = me->books[l.sec_id].book.mbp_side(mkt_side); - if (mkt[0].n_orders < me->min_orders_at_top) { + auto this_qty = mkt[0].quantity.value(); + auto this_ratio = this_qty / std::abs(l.ratio); + if (this_ratio < qty) + qty = this_ratio; + if (qty == 0 || mkt[0].n_orders < me->min_orders_at_top) { t->mark_as_worthless(); break; } else { auto cash = me->sim->get_sec(l.sec_id).to_cash(mkt[0].price); + t->quantity.value() = qty; t->value += -l.ratio * cash; } } @@ -121,9 +132,10 @@ void Sniper::eval(std::vector const &targets) { Sniper::MaybeShot Sniper::pick(std::vector &targets) { std::sort(targets.begin(), targets.end(), [](Target const *ptr1, Target const *ptr2) { - if (std::abs(ptr1->value - ptr2->value) > - securities::market_epsilon()) - return ptr1->value > ptr2->value; + auto val1 = ptr1->value * ptr1->quantity.value(); + auto val2 = ptr2->value * ptr2->quantity.value(); + if (std::abs(val1 - val2) > securities::market_epsilon()) + return val1 > val2; else return ptr1->polygon.parts.size() < ptr2->polygon.parts.size(); @@ -132,9 +144,14 @@ Sniper::MaybeShot Sniper::pick(std::vector &targets) { if (targets[0]->has_value()) { for (auto t : targets) if (t->has_value()) - me->sim->log("opportunity", t->descriptor, t->value); + me->sim->log("opportunity", t->descriptor, t->value, + t->quantity.value(), t->value * t->quantity.value()); if (me->order_qty) - return Shot{*targets[0], me->sim->now(), targets[0]->value, {}}; + return Shot{*targets[0], + me->sim->now(), + targets[0]->value, + targets[0]->quantity, + {}}; else return {}; } else @@ -161,7 +178,7 @@ void Sniper::fire(Shot const &s) { order_change.order.security = p.sec_id; order_change.order.price = worst_price; order_change.order.quantity = - MDP3Quantity{me->order_qty * std::abs(p.ratio)}; + MDP3Quantity{s.quantity.value() * std::abs(p.ratio)}; order_change.order.side = part_side; order_change.change = OrderChange::added; me->sim->send(order_change); @@ -229,9 +246,11 @@ void Sniper::operator()(const OrderUpdate &update) { } PandL pandl(me->sim->instruments()); me->sim->log("shot_outcome", in_flight.target.descriptor, - in_flight.observed_ts.value(), - in_flight.value_at_observation_ts, me->sim->now().value(), - pandl.net_cash(all_fills)); + in_flight.quantity.value(), + in_flight.value_at_observation_ts * in_flight.quantity.value(), + pandl.net_cash(all_fills), + in_flight.observed_ts.value() , + me->sim->now().value()); me->sim->log_pandl(); me->in_flight.reset(); transition(State::WATCH);