Sniper 1.0 completed
This commit is contained in:
1 parent
7914ed4147
commit
9b8ebe32d4
3 files changed
+291
-263
No files matched your search
@@ -26,16 +26,129 @@ using namespace google::protobuf;
|
||||
using namespace boost;
|
||||
using namespace google::protobuf::util;
|
||||
|
||||
struct SniperPolygon;
|
||||
// All securities that exist across all polygon legs have a representation
|
||||
// here. This contains all the common security data (books, order templates),
|
||||
// as well as pointers to all polygons that contain this security as one of
|
||||
// the legs. Stored in the PImpl state in an unordered_map
|
||||
struct SecData{
|
||||
const SecurityKey key;
|
||||
const api::Security& security;
|
||||
// shared across all polygons
|
||||
ChildTemplate& buy_template;
|
||||
ChildTemplate& sell_template;
|
||||
// keeping last 2 books to see book evolution (not used currently)
|
||||
Book current_book;
|
||||
Book last_book;
|
||||
// all polygons that have a leg with this security are in this vector
|
||||
std::vector<SniperPolygon*> polygons;
|
||||
SecData (SecurityKey key,
|
||||
const api::Security& security,
|
||||
ChildTemplate* buy_template,
|
||||
ChildTemplate* sell_template)
|
||||
:
|
||||
key(key),
|
||||
security(security),
|
||||
buy_template(*buy_template),
|
||||
sell_template(*sell_template) { }
|
||||
void set_book(const Book& bk){
|
||||
last_book = current_book;
|
||||
current_book = bk;
|
||||
}
|
||||
};
|
||||
|
||||
std::string side_txt(api::Side s, bool use_buy_sell_txt = true){
|
||||
return use_buy_sell_txt ? (s == api::Side::SIDE_BUY ? "BUY" : "SELL") :
|
||||
(s == api::Side::SIDE_BUY ? "+" : "-");
|
||||
}
|
||||
// reading polygons from from file ////////////////////////////////////////////
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Conveniance struct to package price/qty on both sides
|
||||
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());
|
||||
void set (api::Side side, Price p, Quantity q){
|
||||
if(side == api::Side::SIDE_BUY){
|
||||
buy_price = p;
|
||||
buy_qty = q;
|
||||
}
|
||||
else{
|
||||
sell_price = p;
|
||||
sell_qty = q;
|
||||
}
|
||||
}
|
||||
std::tuple<Price,Quantity> price_qty(api::Side side) const{
|
||||
return side == api::Side::SIDE_BUY ?
|
||||
std::make_tuple(buy_price, buy_qty) :
|
||||
std::make_tuple(sell_price, sell_qty);
|
||||
}
|
||||
};
|
||||
|
||||
// structure into which file is read, its not part of the state
|
||||
struct SniperLeg{
|
||||
SecData& sec_data;
|
||||
const uint16_t leg_qty;
|
||||
const api::Side leg_side;
|
||||
std::optional<OrderKey> working_order = std::nullopt;
|
||||
// the buy and sell side of the cross_value for the leg
|
||||
// corresponds to the polygon's buy or sell side (not to
|
||||
// the leg's), ie the price/qty for the leg when the polygon
|
||||
// is being biought (as defined) or sold (opposite)
|
||||
PriceQty cross_value = {};
|
||||
};
|
||||
|
||||
// Polygon representation, legs and its evaluation
|
||||
struct SniperPolygon{
|
||||
uint32_t pid;
|
||||
std::vector<SniperLeg> legs;
|
||||
// last 2 computed values
|
||||
PriceQty prev_cross_value, curr_cross_value;
|
||||
|
||||
bool has_working_order() const{
|
||||
for(const auto& l : legs)
|
||||
if(l.working_order.has_value())
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
// If the a polygon leg has a working order with this
|
||||
// key it is marked completed.
|
||||
void mark_order_as_completed(OrderKey k){
|
||||
for (auto& l : legs)
|
||||
if(l.working_order.has_value() && l.working_order == k){
|
||||
l.working_order = std::nullopt;
|
||||
return;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
struct SniperAlgoParams{
|
||||
// file from where to read the polygons
|
||||
std::string polygon_list_file;
|
||||
// all legs of a polygon have to be present in this list
|
||||
std::set<std::string> product_list;
|
||||
bool shoot_real_orders;
|
||||
double min_edge; // in price (TODO, should be in ticks)
|
||||
// max sniper shot qty
|
||||
uint16_t max_shot_qty;
|
||||
// min time bw 2 sniper fires
|
||||
uint32_t throttle_ms;
|
||||
};
|
||||
|
||||
struct SniperAlgo::PImpl {
|
||||
SniperAlgoParams algo_params;
|
||||
IAlgoServices* algo_services = nullptr;
|
||||
// indexing : the SecData has pointers to all the polygons
|
||||
// that have include that security
|
||||
// The SniperPolygon has references to the SecData for each leg
|
||||
// (SecData has all the security specific data - order templates,
|
||||
// books etc)
|
||||
std::unordered_map<SecurityKey,SecData> sec_map;
|
||||
std::vector<SniperPolygon> polygons;
|
||||
google::protobuf::Timestamp last_shot_time =
|
||||
TimeUtil::GetCurrentTime();
|
||||
const uint16_t max_no_working_polygons = 1;
|
||||
uint16_t n_working_polygons = 0;
|
||||
PImpl(){
|
||||
}
|
||||
};
|
||||
|
||||
// Conveniance structure to read the files into. They are discarded after the
|
||||
// the file is read into the state
|
||||
struct FilePolygon{
|
||||
struct Leg{
|
||||
const uint64_t file_leg_key = 0;
|
||||
@@ -54,7 +167,7 @@ struct FilePolygon{
|
||||
}
|
||||
};
|
||||
|
||||
// reads polygons from the file and puts them in the second arg
|
||||
// Function reads polygons from the file and puts them in the second arg
|
||||
void read_polygons (const std::string& file_n,
|
||||
std::vector<FilePolygon>& out_v){
|
||||
auto parse_line = [](std::string& ln){
|
||||
@@ -102,218 +215,6 @@ void read_polygons (const std::string& file_n,
|
||||
}
|
||||
}
|
||||
|
||||
// calculations
|
||||
|
||||
// All securities across all polygon legs have a representation here.
|
||||
struct SecData{
|
||||
const SecurityKey key;
|
||||
const api::Security& security;
|
||||
// shared across all polygons
|
||||
ChildTemplate& buy_template;
|
||||
ChildTemplate& sell_template;
|
||||
// keeping last 2 books to see book evolution (not used currently)
|
||||
Book current_book;
|
||||
Book last_book;
|
||||
// all polygons that have a leg with this security are in this vector
|
||||
std::vector<SniperPolygon*> polygons;
|
||||
SecData (SecurityKey key,
|
||||
const api::Security& security,
|
||||
ChildTemplate* buy_template,
|
||||
ChildTemplate* sell_template)
|
||||
:
|
||||
key(key),
|
||||
security(security),
|
||||
buy_template(*buy_template),
|
||||
sell_template(*sell_template) { }
|
||||
void set_book(const Book& bk){
|
||||
last_book = current_book;
|
||||
current_book = bk;
|
||||
}
|
||||
};
|
||||
|
||||
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;
|
||||
const api::Side leg_side;
|
||||
std::optional<OrderKey> working_order = std::nullopt;
|
||||
PriceQty leg_price_qty = {};
|
||||
int mult() const{
|
||||
return leg_side == api::Side::SIDE_BUY?leg_qty:-1*leg_qty;
|
||||
}
|
||||
std::tuple<Price,Quantity> price_qty(api::Side side) const{
|
||||
return std::make_tuple(leg_price_qty.price(side),
|
||||
leg_price_qty.qty(side));
|
||||
}
|
||||
// this returns the order template for the leg wrt the
|
||||
// polygon side, poly_side == BUY meaning as the polygon
|
||||
// is defined, poly_side == SELL meaning opposite
|
||||
// -----------------------------
|
||||
// poly_side | leg_side | result
|
||||
// -----------------------------
|
||||
// BUY BUY BUY
|
||||
// BUY SELL SELL
|
||||
// SELL BUY SELL
|
||||
// SELL SELL BUY
|
||||
ChildTemplate& order_template(api::Side poly_side){
|
||||
auto order_side = (poly_side == api::Side::SIDE_BUY) ?
|
||||
leg_side : SpreaderUtils::invert_side(leg_side);
|
||||
return (order_side == api::Side::SIDE_BUY) ?
|
||||
sec_data.buy_template : sec_data.sell_template;
|
||||
}
|
||||
};
|
||||
|
||||
struct SniperPolygon{
|
||||
uint32_t pid;
|
||||
std::vector<SniperLeg> legs;
|
||||
PriceQty prev_output, curr_output;
|
||||
|
||||
std::tuple<Price,Quantity> cross_value(bool curr, api::Side side){
|
||||
auto& pq = curr ? curr_output : prev_output;
|
||||
return std::make_tuple(pq.price(side),pq.qty(side));
|
||||
}
|
||||
bool has_working_order() const{
|
||||
for(const auto& l : legs)
|
||||
if(l.working_order.has_value())
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
// if the polygon has a working order with this key
|
||||
// it is marked as completed
|
||||
void mark_order_as_completed(OrderKey k){
|
||||
for (auto& l : legs)
|
||||
if(l.working_order.has_value() && l.working_order == k){
|
||||
l.working_order = std::nullopt;
|
||||
return;
|
||||
}
|
||||
}
|
||||
// for logging
|
||||
std::string to_string() const{
|
||||
std::ostringstream buff;
|
||||
for (auto& l : legs){
|
||||
auto side_tx = side_txt(l.leg_side,false);
|
||||
auto qty_tx = l.leg_qty == 1 ? "" : std::to_string(l.leg_qty);
|
||||
auto sec_tx = l.sec_data.security.glbx().security_desc();
|
||||
if (l.sec_data.security.has_spread())
|
||||
sec_tx = "(" + sec_tx + ")";
|
||||
buff << side_tx << qty_tx << sec_tx << " ";
|
||||
}
|
||||
return buff.str();
|
||||
}
|
||||
// for logging
|
||||
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();
|
||||
}
|
||||
};
|
||||
|
||||
struct SniperAlgoParams{
|
||||
// file from where to read the polygons
|
||||
std::string polygon_list_file;
|
||||
// all legs of a polygon have to be present in this list
|
||||
std::set<std::string> product_list;
|
||||
bool shoot_real_orders;
|
||||
double min_edge; // in price (TODO, should be in ticks)
|
||||
// max sniper shot qty
|
||||
uint16_t max_shot_qty;
|
||||
// min time bw 2 sniper fires
|
||||
uint32_t throttle_ms;
|
||||
bool bts_keys_in_file;
|
||||
};
|
||||
|
||||
struct SniperAlgo::PImpl {
|
||||
SniperAlgoParams algo_params;
|
||||
IAlgoServices* algo_services = nullptr;
|
||||
// indexing : the SecData has pointers to all the polygons
|
||||
// that have include that security
|
||||
// The SniperPolygon has references to the SecData for each leg
|
||||
// (SecData has all the security specific data - order templates,
|
||||
// books etc)
|
||||
std::unordered_map<SecurityKey,SecData> sec_map;
|
||||
std::vector<SniperPolygon> polygons;
|
||||
google::protobuf::Timestamp last_shot_time =
|
||||
TimeUtil::GetCurrentTime();
|
||||
const uint16_t max_no_working_polygons = 1;
|
||||
uint16_t n_working_polygons = 0;
|
||||
PImpl(){
|
||||
}
|
||||
};
|
||||
|
||||
// TODO put all calculation like functions at one place
|
||||
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.0 * 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)
|
||||
auto[curr_p,curr_q] = p.cross_value(true,side);
|
||||
auto[prev_p,prev_q] = p.cross_value(false,side);
|
||||
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);
|
||||
}
|
||||
};
|
||||
log_to_file(api::Side::SIDE_BUY);
|
||||
log_to_file(api::Side::SIDE_SELL);
|
||||
}
|
||||
|
||||
ChildTemplate* _get_ord_template (IBeforeLaunchAlgoServices& serv,
|
||||
const CustomOrderDefinition&,
|
||||
SecurityKey key,
|
||||
api::Side side){
|
||||
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 (const CustomOrderDefinition& cod,
|
||||
SniperAlgoParams& out_p){
|
||||
const DynamicOrderDefinition& def = cod.dynamic();
|
||||
@@ -325,7 +226,6 @@ bool _convert_params (const CustomOrderDefinition& cod,
|
||||
auto min_edge = get_if<double>(params, "Min Edge");
|
||||
auto max_shot_qty = get_if<uint32_t>(params, "Max Shot Qty");
|
||||
auto throttle = get_if<uint32_t>(params, "Throttle(ms)");
|
||||
auto bts_keys_in_files = get_if<bool>(params, "BTS Keys in Files?");
|
||||
|
||||
if(file_name.has_value() &&
|
||||
min_edge.has_value() &&
|
||||
@@ -344,16 +244,138 @@ bool _convert_params (const CustomOrderDefinition& cod,
|
||||
out_p.min_edge = min_edge.value();
|
||||
out_p.max_shot_qty = max_shot_qty.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;
|
||||
}
|
||||
|
||||
ChildTemplate* _get_ord_template (IBeforeLaunchAlgoServices& serv,
|
||||
const CustomOrderDefinition&,
|
||||
SecurityKey key,
|
||||
api::Side side){
|
||||
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));
|
||||
}
|
||||
|
||||
// Trading Logic //////////////////////////////////////////////////////////////
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// Returns true if it a good shot (ie for sells, poly_cross_price >= min_edge
|
||||
// and poly_cross_price <= -1*min_edge. So if we are looking for $0.05, selling
|
||||
// it for >= 0.05 or buying it for <= -0.05 would be an opportunity)
|
||||
bool is_shot_opp(api::Side poly_side, double poly_cross_price, double min_edge){
|
||||
if(os::is_nan(poly_cross_price))
|
||||
return false;
|
||||
if(poly_side == api::Side::SIDE_SELL)
|
||||
return bts::securities::gr_than_eq(poly_cross_price,min_edge);
|
||||
else
|
||||
return bts::securities::gr_than_eq(-1.0 * min_edge,
|
||||
poly_cross_price);
|
||||
}
|
||||
|
||||
// Given a polygon trade side, finds the leg trade side.
|
||||
// (Buying the polygon, ie polygon_side == BUY, is trading the legs
|
||||
// as the polygon is defined and SELLING the polygon means
|
||||
// trading the legs opposite to how the legs are defined)
|
||||
api::Side get_leg_cross_market_side(api::Side polygon_side,
|
||||
const SniperLeg& leg){
|
||||
// -----------------------------
|
||||
// poly_side | leg_side | result
|
||||
// -----------------------------
|
||||
// BUY BUY SELL
|
||||
// BUY SELL BUY
|
||||
// SELL BUY BUY
|
||||
// SELL SELL SELL
|
||||
return polygon_side == api::Side::SIDE_BUY ?
|
||||
SpreaderUtils::invert_side(leg.leg_side) : leg.leg_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,
|
||||
SniperLeg& leg){
|
||||
// -----------------------------
|
||||
// poly_side | leg_side | result
|
||||
// -----------------------------
|
||||
// BUY BUY BUY
|
||||
// BUY SELL SELL
|
||||
// SELL BUY SELL
|
||||
// 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;
|
||||
}
|
||||
|
||||
int leg_mult(const SniperLeg& leg){
|
||||
return leg.leg_side == api::Side::SIDE_BUY ? leg.leg_qty : -1*leg.leg_qty;
|
||||
}
|
||||
|
||||
// logging/strings ////////////////////////////////////////////////////////////
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
std::string side_txt(api::Side s, bool use_buy_sell_txt = true){
|
||||
return use_buy_sell_txt ? (s == api::Side::SIDE_BUY ? "BUY" : "SELL") :
|
||||
(s == api::Side::SIDE_BUY ? "+" : "-");
|
||||
}
|
||||
|
||||
std::string polygon_text(const SniperPolygon& p){
|
||||
std::ostringstream buff;
|
||||
for (auto& l : p.legs){
|
||||
auto side_tx = side_txt(l.leg_side,false);
|
||||
auto qty_tx = l.leg_qty == 1 ? "" : std::to_string(l.leg_qty);
|
||||
auto sec_tx = l.sec_data.security.glbx().security_desc();
|
||||
if (l.sec_data.security.has_spread())
|
||||
sec_tx = "(" + sec_tx + ")";
|
||||
buff << side_tx << qty_tx << sec_tx << " ";
|
||||
}
|
||||
return buff.str();
|
||||
}
|
||||
// Called when sniper is in log mode
|
||||
void log_polygon_eval(const SniperPolygon& p, double min_edge){
|
||||
auto leg_markets_txt = [&](api::Side side){
|
||||
std::ostringstream buff;
|
||||
for (auto& l : p.legs){
|
||||
auto[p,q] = l.cross_value.price_qty(side);
|
||||
buff << p.value() << " " << q.value() << " ";
|
||||
}
|
||||
return buff.str();
|
||||
};
|
||||
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)
|
||||
auto[curr_p,curr_q] = p.curr_cross_value.price_qty(side);
|
||||
auto[prev_p,prev_q] = p.prev_cross_value.price_qty(side);
|
||||
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 = leg_markets_txt(side);
|
||||
LOG(OrderMan, Warn, "{},{},{},{},{},{},{},{},{}",
|
||||
new_opp,
|
||||
p.pid,
|
||||
polygon_text(p),
|
||||
side_txt(side),
|
||||
prev_p, prev_q,curr_p,curr_q,
|
||||
leg_markets);
|
||||
}
|
||||
};
|
||||
log_to_file(api::Side::SIDE_BUY);
|
||||
log_to_file(api::Side::SIDE_SELL);
|
||||
}
|
||||
|
||||
SniperAlgo::SniperAlgo() : me (new PImpl()){}
|
||||
SniperAlgo::~SniperAlgo() {}
|
||||
|
||||
// IAlgo functions ////////////////////////////////////////////////////////////
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
OrderOpResult SniperAlgo::do_before_launch(IBeforeLaunchAlgoServices& serv){
|
||||
auto& def = serv.definition();
|
||||
if (def.has_custom_def() &&
|
||||
@@ -364,18 +386,21 @@ OrderOpResult SniperAlgo::do_before_launch(IBeforeLaunchAlgoServices& serv){
|
||||
return {OrderError::ORDER_ERROR_INVALID_PARAMETER,
|
||||
std::string("Parameters are not well formed")};
|
||||
}
|
||||
|
||||
void SniperAlgo::do_launch(IAlgoServices& algo_services){
|
||||
me->algo_services = &algo_services;
|
||||
me->algo_services->subscribe_timer();
|
||||
for (auto kp : me->sec_map)
|
||||
me->algo_services->subscribe_book(kp.first);
|
||||
}
|
||||
|
||||
void SniperAlgo::do_launch_attached(IAlgoServices& algo_services,
|
||||
OrderKey,
|
||||
ChildState){
|
||||
me->algo_services = &algo_services;
|
||||
_cxl_and_end(true, "Sniper Algo can not be attached to working orders");
|
||||
}
|
||||
|
||||
void SniperAlgo::do_update(const Update& u){
|
||||
SniperAlgoParams p;
|
||||
if(_convert_params(u.custom(),p)){
|
||||
@@ -384,22 +409,24 @@ void SniperAlgo::do_update(const Update& u){
|
||||
me->algo_params.throttle_ms = p.throttle_ms;
|
||||
}
|
||||
}
|
||||
|
||||
void SniperAlgo::on_book(SecurityKey key, const Book& bk) {
|
||||
auto it = me->sec_map.find(key);
|
||||
bts_assert(it != me->sec_map.end());
|
||||
auto& sec_data = it->second;
|
||||
sec_data.set_book(bk);
|
||||
for (SniperPolygon* p_ptr : sec_data.polygons)
|
||||
_compute_sniper_opp(*p_ptr);
|
||||
_compute_polygon_cross_value(*p_ptr);
|
||||
// The polygons are computed and then its decided which
|
||||
// one(s) to shoot (for now, not really, we just shoot
|
||||
// the ones that we arbitrarily encounter first in the vector)
|
||||
if(me->algo_params.shoot_real_orders)
|
||||
_fire_opps(sec_data);
|
||||
_fire_orders(sec_data);
|
||||
else
|
||||
for (SniperPolygon* p_ptr : sec_data.polygons)
|
||||
log_polygon_eval(*p_ptr,me->algo_params.min_edge);
|
||||
}
|
||||
|
||||
void SniperAlgo::on_child_change(OrderKey key, const ChildState& st){
|
||||
LOG(OrderMan, Info,
|
||||
"SniperAlgo::on_child_change key:{} child_key:{} status:{} "
|
||||
@@ -420,10 +447,13 @@ void SniperAlgo::on_child_change(OrderKey key, const ChildState& st){
|
||||
}
|
||||
me->n_working_polygons = n_working_polygons;
|
||||
}
|
||||
|
||||
void SniperAlgo::on_status(SecurityKey, co2::TradingStatus){
|
||||
}
|
||||
|
||||
void SniperAlgo::on_timer(){
|
||||
}
|
||||
|
||||
// private helpers ////////////////////////////////////////////////////////////
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
@@ -502,7 +532,7 @@ bool SniperAlgo::_onetime_setup(IBeforeLaunchAlgoServices& serv,
|
||||
const auto& sniper_polygon = add_polygon(p);
|
||||
LOG(OrderMan, Info,
|
||||
"SniperAlgo::_onetime_setup successfully resolved polygon:{}",
|
||||
sniper_polygon.to_string());
|
||||
polygon_text(sniper_polygon));
|
||||
}
|
||||
// 2-pass : adding polygons and then making the sec map point to them
|
||||
// (since references can be validated when elements are added to vectors)
|
||||
@@ -515,56 +545,52 @@ bool SniperAlgo::_onetime_setup(IBeforeLaunchAlgoServices& serv,
|
||||
l.sec_data.polygons.push_back(&p);
|
||||
return me->polygons.size() > 0;
|
||||
}
|
||||
api::Side get_leg_cross_side(api::Side polygon_side, const SniperLeg& leg){
|
||||
// -----------------------------
|
||||
// poly_side | leg_side | result
|
||||
// -----------------------------
|
||||
// BUY BUY SELL
|
||||
// BUY SELL BUY
|
||||
// SELL BUY BUY
|
||||
// SELL SELL SELL
|
||||
return polygon_side == api::Side::SIDE_BUY ?
|
||||
SpreaderUtils::invert_side(leg.leg_side) : leg.leg_side;
|
||||
}
|
||||
void SniperAlgo::_compute_sniper_opp(SniperPolygon& p){
|
||||
p.prev_output = p.curr_output;
|
||||
auto compute_shot = [&](api::Side poly_side){
|
||||
// Computes the "cross value" of a polygon ie sum(ratio*legs' cross value)
|
||||
// and stores it in polygons's curr_cross_value
|
||||
void SniperAlgo::_compute_polygon_cross_value(SniperPolygon& p){
|
||||
p.prev_cross_value = p.curr_cross_value;
|
||||
auto compute_cross_value_side = [&](api::Side poly_side){
|
||||
double shot_price = 0;
|
||||
bool legs_valid_price = true;
|
||||
for (auto& l : p.legs){
|
||||
Price l_comp_price; Quantity l_comp_qty;
|
||||
// TODO push it to one place
|
||||
api::Side cross_side = get_leg_cross_side(poly_side,l);
|
||||
api::Side cross_side = get_leg_cross_market_side(poly_side,l);
|
||||
// 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, // TODO: setting to use implied or not
|
||||
l.sec_data.current_book,
|
||||
cross_side,
|
||||
Quantity(l.leg_qty), // TODO times min qty
|
||||
Quantity(l.leg_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();
|
||||
l.cross_value.set(poly_side,l_comp_price,l_comp_qty);
|
||||
shot_price += leg_mult(l)*l_comp_price.value();
|
||||
legs_valid_price = legs_valid_price &&
|
||||
l_comp_qty.value()>=l.leg_qty;
|
||||
l_comp_qty.value()>=l.leg_qty
|
||||
&& !os::is_nan(l_comp_price.value());
|
||||
}
|
||||
// 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)));
|
||||
|
||||
p.curr_cross_value.set(
|
||||
poly_side,
|
||||
legs_valid_price ? Price(shot_price) : Price(os::nan()),
|
||||
legs_valid_price ? Quantity(1) : Quantity(0));
|
||||
};
|
||||
compute_shot(api::Side::SIDE_BUY);
|
||||
compute_shot(api::Side::SIDE_SELL);
|
||||
compute_cross_value_side(api::Side::SIDE_BUY);
|
||||
compute_cross_value_side(api::Side::SIDE_SELL);
|
||||
}
|
||||
void SniperAlgo::_fire_opps(SecData& sec_data){
|
||||
|
||||
// For a security key, iterates through all the pollygons
|
||||
// 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){
|
||||
auto msecs_since_last_order =
|
||||
TimeUtil::TimestampToMilliseconds(TimeUtil::GetCurrentTime()) -
|
||||
TimeUtil::TimestampToMilliseconds(me->last_shot_time);
|
||||
if(msecs_since_last_order < me->algo_params.throttle_ms)
|
||||
return;
|
||||
auto fire_order = [&](SniperPolygon& p, api::Side side){
|
||||
auto fire_order = [&](SniperPolygon& p, api::Side poly_side){
|
||||
for (auto& l : p.legs){
|
||||
ChildTemplate& order_template = l.order_template(side);
|
||||
auto[pr,qty] = l.price_qty(side);
|
||||
ChildTemplate& order_template = get_leg_order_template(poly_side,l);
|
||||
auto[pr,qty] = l.cross_value.price_qty(poly_side);
|
||||
l.working_order =
|
||||
me->algo_services->launch_child(order_template,pr,qty);
|
||||
}
|
||||
@@ -574,11 +600,11 @@ void SniperAlgo::_fire_opps(SecData& sec_data){
|
||||
for(SniperPolygon* p : sec_data.polygons){
|
||||
if(me->n_working_polygons < me->max_no_working_polygons){
|
||||
if(is_shot_opp(api::Side::SIDE_BUY,
|
||||
p->curr_output.buy_price.value(),
|
||||
p->curr_cross_value.buy_price.value(),
|
||||
me->algo_params.min_edge))
|
||||
fire_order(*p,api::Side::SIDE_BUY);
|
||||
else if(is_shot_opp(api::Side::SIDE_SELL,
|
||||
p->curr_output.sell_price.value(),
|
||||
p->curr_cross_value.sell_price.value(),
|
||||
me->algo_params.min_edge))
|
||||
fire_order(*p,api::Side::SIDE_SELL);
|
||||
}
|
||||
|
||||
@@ -33,8 +33,8 @@ private:
|
||||
IAlgoServices& _algo() const ;
|
||||
bool _onetime_setup(IBeforeLaunchAlgoServices& serv,
|
||||
const bts::api::orderman::CustomOrderDefinition& cod);
|
||||
void _compute_sniper_opp(SniperPolygon&);
|
||||
void _fire_opps(SecData& sec_data);
|
||||
void _compute_polygon_cross_value(SniperPolygon&);
|
||||
void _fire_orders(SecData& sec_data);
|
||||
// acts on md, cognizant of the state
|
||||
void _cxl_and_end (bool is_error, const std::string& err_msg);
|
||||
};
|
||||
|
||||
@@ -182,6 +182,8 @@ void FifoSecurityMatcher::_on_order_not_affecting_books(
|
||||
auto& opp_book = _book(_opp_side(ord.side));
|
||||
uint32_t new_ord_rem = total_quantity(ord).value();
|
||||
for (const FifoPriceLevelMatcher &pl : opp_book) {
|
||||
if(!pl.populated())
|
||||
continue;
|
||||
if (_is_cross(ord, pl)){
|
||||
uint32_t level_qty =
|
||||
static_cast<uint32_t>(pl.total_quantity().value());
|
||||
|
||||
Reference in new issue
Block a user