diff --git a/src/lib/orderman/orderman_utils/orderman_utils.h b/src/lib/orderman/orderman_utils/orderman_utils.h index d31713d99..0192872ee 100644 --- a/src/lib/orderman/orderman_utils/orderman_utils.h +++ b/src/lib/orderman/orderman_utils/orderman_utils.h @@ -66,6 +66,10 @@ inline std::optional get_if(const api::orderman::DynamicOrderParam& p) { return p.value_case() == api::orderman::DynamicOrderParam::kAsBool ? std::optional{p.as_bool()} : std::nullopt; } template <> +inline std::optional get_if(const api::orderman::DynamicOrderParam& p) { + return p.value_case() == api::orderman::DynamicOrderParam::kAsString ? std::optional{p.as_string()} : std::nullopt; +} +template <> inline std::optional get_if(const api::orderman::DynamicOrderParam& p) { return p.value_case() == api::orderman::DynamicOrderParam::kAsOrderDef ? std::optional{p.as_order_def()} : std::nullopt; } diff --git a/src/lib/orderman/orders/algo/algo_orders/RegisterDorman.cpp b/src/lib/orderman/orders/algo/algo_orders/RegisterDorman.cpp index bf0f3213a..3853115ae 100644 --- a/src/lib/orderman/orders/algo/algo_orders/RegisterDorman.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/RegisterDorman.cpp @@ -242,7 +242,8 @@ extern "C" void init_sniper_plugin(bts::SparkServices& srv) { *sniper.add_param_desc() = param_string("Products"); *sniper.add_param_desc() = param_double("Min Edge"); *sniper.add_param_desc() = param_uint32("Max Shot Qty"); - *sniper.add_param_desc() = param_uint32("Throttle(ms)"); + *sniper.add_param_desc() = param_uint32("Throttle(ms)"); + *sniper.add_param_desc() = param_bool("BTS Keys in Files?"); srv.register_dynamic_algo_order(sniper, []() { return std::make_unique(); }); } diff --git a/src/lib/orderman/orders/algo/algo_orders/SniperAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/SniperAlgo.cpp index 96e92ac4d..602eea8ee 100644 --- a/src/lib/orderman/orders/algo/algo_orders/SniperAlgo.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/SniperAlgo.cpp @@ -58,14 +58,6 @@ struct SecData{ } }; -struct PriceQty{ - // for the legs the buy and sell are wrt to the polygon - Price buy_price = Price(os::nan()); - Quantity buy_qty = Quantity(0); - Quantity sell_qty = Quantity(0); - Price sell_price = Price(os::nan()); -}; - struct WorkingOrder{ OrderKey order_key = OrderKey(0); bool is_buy = true; @@ -79,6 +71,30 @@ struct WorkingOrder{ } }; +struct PriceQty{ + // for the legs the buy and sell are wrt to the polygon + Price buy_price = Price(os::nan()); + Quantity buy_qty = Quantity(0); + Quantity sell_qty = Quantity(0); + Price sell_price = Price(os::nan()); + Price& price_ref(api::Side side){ + return side == api::Side::SIDE_BUY ? buy_price : sell_price; + } + Quantity& qty_ref(api::Side side){ + return side == api::Side::SIDE_BUY ? buy_qty : sell_qty; + } + Price price(api::Side side) const{ + return side == api::Side::SIDE_BUY ? buy_price : sell_price; + } + Quantity qty(api::Side side) const{ + return side == api::Side::SIDE_BUY ? buy_qty : sell_qty; + } + void set (api::Side side, Price p, Quantity q){ + price_ref(side) = p; + qty_ref(side) = q; + } +}; + struct SniperLeg{ SecData& sec_data; const uint16_t leg_qty; @@ -88,10 +104,9 @@ struct SniperLeg{ int mult() const{ return leg_side == api::Side::SIDE_BUY?leg_qty:-1*leg_qty; } - std::tuple price_qty(api::Side side){ - return side == api::Side::SIDE_BUY ? - std::make_tuple(leg_price_qty.buy_price,leg_price_qty.buy_qty) : - std::make_tuple(leg_price_qty.sell_price,leg_price_qty.sell_qty); + std::tuple price_qty(api::Side side) const{ + return std::make_tuple(leg_price_qty.price(side), + leg_price_qty.qty(side)); } std::string to_string() const{ std::ostringstream buff; @@ -113,9 +128,15 @@ struct SniperPolygon{ } std::tuple price_qty(bool curr, api::Side side){ auto& pq = curr ? curr_output : prev_output; - return side == api::Side::SIDE_BUY ? - std::make_tuple(pq.buy_price,pq.buy_qty) : - std::make_tuple(pq.sell_price,pq.sell_qty); + return std::make_tuple(pq.price(side),pq.qty(side)); + } + std::string leg_markets(api::Side side) const{ + std::ostringstream buff; + for (auto& l : legs){ + auto[p,q] = l.price_qty(side); + buff << p.value() << " " << q.value() << " "; + } + return buff.str(); } }; @@ -129,6 +150,7 @@ struct SniperAlgoParams{ uint16_t max_shot_qty; // min time bw 2 sniper fires uint32_t throttle_ms; + bool bts_keys_in_file; }; struct SniperAlgo::PImpl { @@ -146,19 +168,21 @@ struct SniperAlgo::PImpl { struct FilePolygon{ struct Leg{ - SecurityKey leg_sec_key = SecurityKey(0); - api::Side leg_side = api::Side::SIDE_BUY; - uint16_t leg_qty = 0; + const uint64_t file_leg_key = 0; + const api::Side leg_side = api::Side::SIDE_BUY; + const uint16_t leg_qty = 0; + // this is not stored in the file, filled later + SecurityKey resolved_key = SecurityKey(0); }; - uint32_t pid; - std::string p_name; + const uint32_t pid; + const std::string p_name; std::vector legs; - void add_leg(SecurityKey key, api::Side side, uint16_t qty){ + void add_leg(uint64_t key, api::Side side, uint16_t qty){ legs.push_back(Leg {key,side,qty}); } }; -// read polygons from the file and puts them in the second arg +// reads polygons from the file and puts them in the second arg void read_polygons (const std::string& file_n, std::vector& out_v){ auto parse_line = [](std::string& ln){ @@ -166,7 +190,7 @@ void read_polygons (const std::string& file_n, boost::split(toks,ln,boost::is_any_of(",")); bts_assert(toks.size()==5); int pid = std::stoi(toks[0]); - int leg_sec_key = std::stoi(toks[1]); + auto file_leg_key = std::stoull(toks[1]); std::string side_txt = toks[2]; api::Side side = side_txt.find("BUY") != std::string::npos ? api::Side::SIDE_BUY : api::Side::SIDE_SELL; @@ -174,7 +198,7 @@ void read_polygons (const std::string& file_n, std::string poly_txt = toks[4]; return std::make_tuple( static_cast(pid), - SecurityKey(leg_sec_key), + file_leg_key, side, static_cast(leg_qty), poly_txt); @@ -200,38 +224,51 @@ void read_polygons (const std::string& file_n, out_v.push_back(p.second); } -void log_opp(SniperPolygon& p){ +bool is_shot_opp(api::Side poly_side, double poly_price, double min_edge){ + if(os::is_nan(poly_price)) + return false; + if(poly_side == api::Side::SIDE_SELL) + return bts::securities::gr_than_eq(poly_price,min_edge); + else + return bts::securities::gr_than_eq(-1 * std::abs(min_edge), + poly_price); +} + +void log_polygon_eval(SniperPolygon& p, double min_edge){ auto log_to_file = [&](api::Side side){ - // opp/no-opp, pid, polygon, side, p,q -> p,q, for each leg (price, d_qty, i_qty, side) - std:: string leg_markets = ""; + // opp/no-opp, pid, polygon, side, p,q -> p,q, for each leg (price, d_qty, i_qty, side) auto[curr_p,curr_q] = p.price_qty(true,side); auto[prev_p,prev_q] = p.price_qty(false,side); - std::string new_opp = curr_q.value() > 0 ? "Opp Created" : "Opp Vanished"; - LOG(OrderMan, Warn, "Sniper:{} pid:{} poly:{} side:{} " - "{},{} to {},{} legs: {}", - new_opp, - p.pid, - p.to_string(), - side_txt(side), - curr_p, curr_q, prev_p, prev_q, - leg_markets); + auto is_curr_opp = is_shot_opp(side,curr_p.value(),min_edge), + is_prev_opp = is_shot_opp(side,prev_p.value(),min_edge); + // logs only if the opp changes + if(is_curr_opp != is_prev_opp){ + std::string new_opp = is_curr_opp ? "Sniper Opp Created" : + "Sniper Opp Vanished"; + std::string leg_markets = p.leg_markets(side); + LOG(OrderMan, Warn, "{},{},{},{},{},{},{},{},{}", + new_opp, + p.pid, + p.to_string(), + side_txt(side), + prev_p, prev_q,curr_p,curr_q, + leg_markets); + } }; - // logs if its an opp and 1. it was non-opp before, or changed - if(p.curr_output.buy_qty.value() != p.prev_output.buy_qty.value()) - log_to_file(api::Side::SIDE_BUY); - else if(p.curr_output.sell_qty.value() != p.prev_output.sell_qty.value()) - log_to_file(api::Side::SIDE_SELL); + log_to_file(api::Side::SIDE_BUY); + log_to_file(api::Side::SIDE_SELL); } ChildTemplate* _get_ord_template (IBeforeLaunchAlgoServices& serv, - const CustomOrderDefinition& cod, + const CustomOrderDefinition&, SecurityKey key, api::Side side){ - OrderDefinition rv = serv.definition(); - rv.set_security_key(key.value()); - rv.set_side(side); - rv.mutable_custom_def()->CopyFrom(cod); - return &(serv.make_child_template(rv)); + OrderDefinition order_def = serv.definition(); + order_def.set_security_key (key.value()); + order_def.set_side(side); + order_def.mutable_custom_def()->mutable_limit()->MergeFrom ( + api::orderman::LimitOrderDefinition()); + return &(serv.make_child_template(order_def)); } bool _convert_params (IBeforeLaunchAlgoServices&, @@ -239,25 +276,32 @@ bool _convert_params (IBeforeLaunchAlgoServices&, SniperAlgoParams& out_p){ const DynamicOrderDefinition& def = cod.dynamic(); const auto& params = def.order_params(); + auto file_name = get_if(params, "File"); auto products = get_if(params, "Products"); auto min_edge = get_if(params, "Min Edge"); auto max_shot_qty = get_if(params, "Max Shot Qty"); auto throttle = get_if(params, "Throttle(ms)"); + auto bts_keys_in_files = get_if(params, "BTS Keys in Files?"); + + if(file_name.has_value() && - products.has_value() && min_edge.has_value() && max_shot_qty.has_value() && throttle.has_value()){ - out_p.polygon_list_file = std::string(file_name.value()); - std::vector toks; - std::string products_str = products.value(); - boost::split(toks,products_str,boost::is_any_of(",")); - for (auto& p : toks) - out_p.product_list.insert(p); + out_p.polygon_list_file = file_name.value(); + if(products.has_value()){ + std::vector toks; + std::string products_str = products.value(); + boost::split(toks,products_str,boost::is_any_of(",")); + for (auto& p : toks) + out_p.product_list.insert(p); + } out_p.min_edge = min_edge.value(); out_p.max_shot_qty = max_shot_qty.value(); - out_p.throttle_ms = throttle.value(); + out_p.throttle_ms = throttle.value(); + out_p.bts_keys_in_file = bts_keys_in_files.has_value() ? + bts_keys_in_files.value() : true; return true; } return false; @@ -270,10 +314,9 @@ OrderOpResult SniperAlgo::do_before_launch(IBeforeLaunchAlgoServices& serv){ auto& def = serv.definition(); if (def.has_custom_def() && def.custom_def().has_dynamic() && - _convert_params(serv,def.custom_def(),me->algo_params)){ - _onetime_setup(serv,def.custom_def()); + _convert_params(serv,def.custom_def(),me->algo_params) && + _onetime_setup(serv,def.custom_def())) return {}; - } return {OrderError::ORDER_ERROR_INVALID_PARAMETER, std::string("Parameters are not well formed")}; } @@ -297,10 +340,8 @@ void SniperAlgo::on_book(SecurityKey key, const Book& bk) { auto& sec_data = it->second; sec_data.set_book(bk); // go through then snipers and analyze - for (SniperPolygon* p_ptr : sec_data.polygons){ - auto& p = *p_ptr; - _compute_sniper_opp(p); - } + for (SniperPolygon* p_ptr : sec_data.polygons) + _compute_sniper_opp(*p_ptr); } void SniperAlgo::on_child_change(OrderKey key, const ChildState& st){ LOG(OrderMan, Info, @@ -318,45 +359,75 @@ void SniperAlgo::on_timer(){ } // private helpers //////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// -void SniperAlgo::_onetime_setup(IBeforeLaunchAlgoServices& serv, +bool SniperAlgo::_onetime_setup(IBeforeLaunchAlgoServices& serv, const CustomOrderDefinition& cod){ + std::map exch_key_map; + for(auto p : serv.known_securities()) + if(p->has_glbx()) + exch_key_map[p->glbx().exchange_id()] = p; + auto resolve_key = [&](FilePolygon::Leg& file_leg) -> api::Security const*{ + api::Security const* rv = nullptr; + if (me->algo_params.bts_keys_in_file) + rv = serv.lookup_security(SecurityKey(file_leg.file_leg_key)); + else{ + auto it = exch_key_map.find(file_leg.file_leg_key); + rv = (it != exch_key_map.end()) ? it->second : nullptr; + } + if(rv != nullptr) + file_leg.resolved_key = SecurityKey(rv->id()); + return rv; + }; + // creates a SniperPolygon from a FilePolygon and + // adds it to the state auto add_polygon = [&](const auto& file_poly){ SniperPolygon rv; for (const auto& l : file_poly.legs){ - auto it = me->sec_map.find(l.leg_sec_key); + auto it = me->sec_map.find(l.resolved_key); bts_assert(it != me->sec_map.end()); SecData& sec_data = it->second; rv.legs.push_back(SniperLeg{sec_data,l.leg_qty,l.leg_side}); } me->polygons.push_back(rv); }; + auto prod_allowed = [&](const api::Security& sec){ + const auto& prods = me->algo_params.product_list; + return prods.size() == 0 || prods.find(sec.symbol()) != prods.end(); + }; std::vector polygons, resolved_polygons; read_polygons(me->algo_params.polygon_list_file, polygons); - // TODO: get this from somewhere - for (const auto& p : polygons){ + for (auto& p : polygons){ bool succ = true; - for (const auto& l : p.legs){ - auto* sec_ptr = serv.lookup_security(l.leg_sec_key); - if(sec_ptr != nullptr){ - if(me->sec_map.find(l.leg_sec_key) == me->sec_map.end()) + for (auto& l : p.legs){ + auto* sec_ptr = resolve_key(l); + if(sec_ptr != nullptr && prod_allowed(*sec_ptr)){ + if(me->sec_map.find(l.resolved_key) == me->sec_map.end()){ me->sec_map.emplace( - l.leg_sec_key, - SecData(l.leg_sec_key, + l.resolved_key, + SecData(l.resolved_key, *sec_ptr, - _get_ord_template(serv,cod,l.leg_sec_key,api::Side::SIDE_BUY), - _get_ord_template(serv,cod,l.leg_sec_key,api::Side::SIDE_SELL))); + _get_ord_template(serv,cod,l.resolved_key,api::Side::SIDE_BUY), + _get_ord_template(serv,cod,l.resolved_key,api::Side::SIDE_SELL))); + LOG(OrderMan, Info, + "SniperAlgo::_onetime_setup successfully resolved sec_key:{}", + sec_ptr->id()); + } } else{ - LOG(OrderMan, Error, "SniperAlgo::_onetime_setup " - "could not resolve key:{}", l.leg_sec_key); + LOG(OrderMan, Error, + "SniperAlgo::_onetime_setup could not resolve key:{}", + l.file_leg_key); succ = false; } } if(succ) resolved_polygons.push_back(p); } - for (const auto& p : resolved_polygons) + for (const auto& p : resolved_polygons){ add_polygon(p); + LOG(OrderMan, Info, + "SniperAlgo::_onetime_setup successfully resolved polygon:{}", + p.p_name); + } // 2-pass : adding polygons and then making the sec map point to them // (since references can be validated when elements are added to vectors) @@ -366,49 +437,38 @@ void SniperAlgo::_onetime_setup(IBeforeLaunchAlgoServices& serv, for (auto& p : me->polygons) for(auto& l : p.legs) l.sec_data.polygons.push_back(&p); + return me->polygons.size() > 0; } -void SniperAlgo::_compute_sniper_opp(SniperPolygon& p){ + +void SniperAlgo::_compute_sniper_opp(SniperPolygon& p){ p.prev_output = p.curr_output; - // as is - { - double buy_price = 0; + auto compute_shot = [&](api::Side poly_side){ + double shot_price = 0; + bool legs_valid_price = true; for (auto& l : p.legs){ - // TODO: setting to use implied or not + Price l_comp_price; Quantity l_comp_qty; + api::Side cross_side = poly_side == api::Side::SIDE_BUY ? + SpreaderUtils::invert_side(l.leg_side) : l.leg_side; SpreaderUtils::traverse_depth_for_quantity( - l.sec_data.current_book, - SpreaderUtils::invert_side(l.leg_side), - Quantity(l.leg_qty), // TODO times min qty - l.leg_price_qty.buy_price, - l.leg_price_qty.buy_qty); - buy_price += l.mult()*l.leg_price_qty.buy_price.value(); + l.sec_data.current_book, // TODO: setting to use implied or not + cross_side, + Quantity(l.leg_qty), // TODO times min qty + l_comp_price, + l_comp_qty); + l.leg_price_qty.set(poly_side,l_comp_price,l_comp_qty); + shot_price += l.mult()*l_comp_price.value(); + legs_valid_price = legs_valid_price && + l_comp_qty.value()>=l.leg_qty; } - p.curr_output.buy_price = Price(buy_price); - bool buy_shot = bts::securities::gr_than_eq( - -1.0*me->algo_params.min_edge, - buy_price); - // TODO, shot qty hardocded at 1 - p.curr_output.buy_qty = buy_shot ? Quantity(1) : Quantity(0); - } - // opp - // if buy_shot is true. sell_shot can not be true (otherwise markts are crossed) - { - double sell_price = 0; - for (auto& l : p.legs){ - SpreaderUtils::traverse_depth_for_quantity( - l.sec_data.current_book, - l.leg_side, - Quantity(l.leg_qty), // TODO times min qty - l.leg_price_qty.sell_price, - l.leg_price_qty.sell_qty); - sell_price += l.mult()*l.leg_price_qty.sell_price.value(); - } - p.curr_output.sell_price = Price(sell_price); - bool sell_shot = bts::securities::gr_than_eq( - sell_price, - me->algo_params.min_edge); - p.curr_output.sell_qty = sell_shot ? Quantity(1) : Quantity(0); - } - log_opp(p); + //shot_price = legs_valid_price ? shot_price : os::nan(); + // TODO, shot qty hardcoded at 1 + p.curr_output.set(poly_side,Price(shot_price), + (legs_valid_price ? Quantity(1) : Quantity(0))); + + }; + compute_shot(api::Side::SIDE_BUY); + compute_shot(api::Side::SIDE_SELL); + log_polygon_eval(p,me->algo_params.min_edge); } void SniperAlgo::_cxl_and_end (bool is_error, const std::string& err_msg){ if(is_error) diff --git a/src/lib/orderman/orders/algo/algo_orders/SniperAlgo.h b/src/lib/orderman/orders/algo/algo_orders/SniperAlgo.h index 1d294cad1..5511db43e 100644 --- a/src/lib/orderman/orders/algo/algo_orders/SniperAlgo.h +++ b/src/lib/orderman/orders/algo/algo_orders/SniperAlgo.h @@ -30,7 +30,7 @@ class SniperAlgo : public IAlgoOrder { std::unique_ptr me; private: IAlgoServices& _algo() const ; - void _onetime_setup(IBeforeLaunchAlgoServices& serv, + bool _onetime_setup(IBeforeLaunchAlgoServices& serv, const bts::api::orderman::CustomOrderDefinition& cod); void _compute_sniper_opp(SniperPolygon&); // acts on md, cognizant of the state diff --git a/src/lib/orderman/orders/spreader_orders/spreader/SpreaderUtils.h b/src/lib/orderman/orders/spreader_orders/spreader/SpreaderUtils.h index 683a75832..6e039ee36 100644 --- a/src/lib/orderman/orders/spreader_orders/spreader/SpreaderUtils.h +++ b/src/lib/orderman/orders/spreader_orders/spreader/SpreaderUtils.h @@ -152,7 +152,7 @@ public: } if (cum_quantity_u == 0){ out_quantity = Quantity(0); - out_price = Price(0); + out_price = Price(os::nan()); } else{ out_quantity = Quantity(cum_quantity_u);