Added payup ticks to sniper and logic to alternate bw main book and lookahead book

This commit is contained in:
Taha Afzal committed 2020-12-08 21:47:25 -05:00
1 parent 64b42aef99
commit fd8bd63f55
3 files changed
+45 -15

No files matched your search

@@ -243,7 +243,8 @@ extern "C" void init_sniper_plugin(bts::SparkServices& srv) {
*sniper.add_param_desc() = param_bool("Shoot Real Orders");
*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("Leg Payup Ticks");
*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<orderman::SniperAlgo>(); });
}
@@ -37,7 +37,10 @@ struct SecData{
ChildTemplate& buy_template;
ChildTemplate& sell_template;
// keeping last 2 books to see book evolution (not used currently)
Book current_book, last_book;
private:
Book current_book, last_book;
std::optional<Book> look_ahead_book = std::nullopt;
public:
// all polygons that have a leg with this security are in this vector
std::vector<SniperPolygon*> polygons;
SecData (SecurityKey key,
@@ -50,8 +53,14 @@ struct SecData{
buy_template(*buy_template),
sell_template(*sell_template) { }
void set_book(const Book& bk){
look_ahead_book = std::nullopt;
last_book = current_book;
current_book = bk;
current_book = bk;
}
const Book& get_current_book(){
if(look_ahead_book.has_value())
return look_ahead_book.value();
return current_book;
}
};
@@ -130,6 +139,7 @@ struct SniperAlgoParams{
double min_edge; // in price (TODO, should be in ticks)
// max sniper shot qty
uint16_t max_shot_qty;
uint16_t leg_payup_ticks;
// min time bw 2 sniper fires
uint32_t throttle_ms;
};
@@ -231,11 +241,13 @@ bool _convert_params (const CustomOrderDefinition& cod,
auto shoot_real_orders = get_if<bool>(params, "Shoot Real Orders");
auto min_edge = get_if<double>(params, "Min Edge");
auto max_shot_qty = get_if<uint32_t>(params, "Max Shot Qty");
auto leg_payup_ticks = get_if<uint32_t>(params, "Leg Payup Ticks");
auto throttle = get_if<uint32_t>(params, "Throttle(ms)");
if(file_name.has_value() &&
min_edge.has_value() &&
max_shot_qty.has_value() &&
leg_payup_ticks.has_value() &&
throttle.has_value()){
out_p.polygon_list_file = file_name.value();
if(products.has_value()){
@@ -249,6 +261,7 @@ bool _convert_params (const CustomOrderDefinition& cod,
shoot_real_orders.value();
out_p.min_edge = min_edge.value();
out_p.max_shot_qty = max_shot_qty.value();
out_p.leg_payup_ticks = leg_payup_ticks.value();
out_p.throttle_ms = throttle.value();
return true;
}
@@ -303,7 +316,8 @@ api::Side get_leg_cross_market_side(api::Side polygon_side,
// Returns the order template for the leg wrt the
// polygon side, once again poly_side == BUY means as the polygon
// is defined and poly_side == SELL meaning opposite
ChildTemplate& get_leg_order_template(api::Side polygon_side,
std::tuple<ChildTemplate&,api::Side> get_leg_order_template(
api::Side polygon_side,
SniperLeg& leg){
// -----------------------------
// poly_side | leg_side | result
@@ -314,14 +328,22 @@ ChildTemplate& get_leg_order_template(api::Side polygon_side,
// SELL SELL BUY
auto order_side = (polygon_side == api::Side::SIDE_BUY) ?
leg.leg_side : SpreaderUtils::invert_side(leg.leg_side);
return (order_side == api::Side::SIDE_BUY) ?
leg.sec_data.buy_template : leg.sec_data.sell_template;
ChildTemplate& rv = (order_side == api::Side::SIDE_BUY) ?
leg.sec_data.buy_template : leg.sec_data.sell_template;
return std::tuple<ChildTemplate&,api::Side>(rv,order_side);
}
int leg_mult(const SniperLeg& leg){
return leg.leg_side == api::Side::SIDE_BUY ? leg.leg_qty : -1*leg.leg_qty;
}
Price get_payup_price(Price price, uint16_t payup_ticks,
const api::Security& sec, api::Side side){
int mult = side == api::Side::SIDE_BUY ? 1 : -1;
return Price(price.value() +
(mult * payup_ticks * sec.contract_spec().low_tick()));
}
// logging/strings ////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
@@ -349,7 +371,7 @@ void log_polygon_eval(OrderKey ok, const SniperPolygon& p, double min_edge){
for (auto& l : p.legs){
auto[p,q] = l.cross_value.price_qty(side);
buff << p.value() << " " << q.value() << ",";
auto& bk_side = l.sec_data.current_book.side(
auto& bk_side = l.sec_data.get_current_book().side(
get_leg_cross_market_side(side,l));
for(const auto& psz : bk_side)
buff << psz.price() << " " << psz.direct_size() << " " <<
@@ -443,6 +465,10 @@ void SniperAlgo::on_book(SecurityKey key, const Book& bk) {
log_polygon_eval(me->order_key, *p_ptr,me->algo_params.min_edge);
}
void SniperAlgo::on_trade_summary_signal(SecurityKey, api::Side /* aggressor */,
Price /* min_p */, Price /* max_p */){
}
void SniperAlgo::on_child_change(OrderKey key, const ChildState& st){
LOG(OrderMan, Info,
"SniperAlgo::on_child_change key:{} child_key:{} status:{} "
@@ -577,7 +603,7 @@ void SniperAlgo::_compute_polygon_cross_value(SniperPolygon& p){
// parameters like whether to use implied markets or not,
// number of orders etc can be incorporated here
SpreaderUtils::traverse_depth_for_quantity(
l.sec_data.current_book,
l.sec_data.get_current_book(),
cross_side,
Quantity(l.leg_qty),
l_comp_price,
@@ -597,7 +623,7 @@ void SniperAlgo::_compute_polygon_cross_value(SniperPolygon& p){
compute_cross_value_side(api::Side::SIDE_SELL);
}
// For a security key, iterates through all the pollygons
// For a security key, iterates through all the polygons
// and shoots orders if the contraints allow it it
// (right now it is naive and does it in an abrbitrary manner0
void SniperAlgo::_fire_orders(SecData& sec_data){
@@ -608,12 +634,13 @@ void SniperAlgo::_fire_orders(SecData& sec_data){
return;
auto fire_order = [&](SniperPolygon& p, api::Side poly_side){
for (auto& l : p.legs){
auto[pr,qty] = l.cross_value.price_qty(poly_side);
LOG(OrderMan, Diag, "{} {}", pr, qty);
// TODO: commenting out shooting orders till code is reviewed
// ChildTemplate& order_template = get_leg_order_template(poly_side,l);
// l.working_order =
// me->algo_services->launch_child(order_template,pr,qty);
auto[pr,qty] = l.cross_value.price_qty(poly_side);
auto[order_template,side] = get_leg_order_template(poly_side,l);
auto payup_pr = get_payup_price(pr, me->algo_params.leg_payup_ticks,
l.sec_data.security, side);
LOG(OrderMan, Diag, "pr:{} payup_pr:{} qty:{}", pr, payup_pr, qty);
l.working_order =
me->algo_services->launch_child(order_template,payup_pr,qty);
}
me->last_shot_time = TimeUtil::GetCurrentTime();
me->n_working_polygons++;
@@ -21,6 +21,8 @@ class SniperAlgo : public IAlgoOrder {
void do_launch_attached(IAlgoServices &, OrderKey, ChildState) override;
void do_update(api::orderman::Update const &) override;
void on_book(SecurityKey, Book const &) override;
void on_trade_summary_signal(SecurityKey, api::Side /* aggressor */,
Price /* min_p */, Price /* max_p */) override;
void on_child_change(OrderKey, ChildState const &) override;
void on_status(SecurityKey, co2::TradingStatus) override;
void on_timer() override;