Shooting real order
This commit is contained in:
1 parent
145cf2b482
commit
7914ed4147
4 files changed
+260
-146
No files matched your search
@@ -240,9 +240,10 @@ extern "C" void init_sniper_plugin(bts::SparkServices& srv) {
|
||||
sniper.set_dynamic_order_type("Sniper");
|
||||
*sniper.add_param_desc() = param_string("File");
|
||||
*sniper.add_param_desc() = param_string("Products");
|
||||
*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("Throttle(ms)");
|
||||
*sniper.add_param_desc() = param_bool("BTS Keys in Files?");
|
||||
srv.register_dynamic_algo_order(sniper, []() { return std::make_unique<orderman::SniperAlgo>(); });
|
||||
}
|
||||
|
||||
@@ -6,15 +6,14 @@
|
||||
#include "lib/orderman/orderman_utils/orderman_utils.h"
|
||||
#include "lib/orderman/orders/algo/IAlgoServices.h"
|
||||
#include "lib/orderman/orders/spreader_orders/spreader/SpreaderUtils.h"
|
||||
#include "lib/orderman/actors/SODPosition.h"
|
||||
#include "lib/config/protos/spartan.pb.h"
|
||||
#include <boost/algorithm/string.hpp>
|
||||
#include <google/protobuf/timestamp.pb.h>
|
||||
#include <google/protobuf/util/time_util.h>
|
||||
|
||||
#include "lib/os/os.h"
|
||||
#include "lib/domain_model/Security.h"
|
||||
#include "lib/utils/PBUtils.h"
|
||||
#include "lib/utils/datetime.h"
|
||||
#include <google/protobuf/util/time_util.h>
|
||||
#include <boost/tokenizer.hpp>
|
||||
#include <stdint.h>
|
||||
#include <fstream>
|
||||
@@ -25,6 +24,7 @@ namespace orderman {
|
||||
using namespace api::orderman;
|
||||
using namespace google::protobuf;
|
||||
using namespace boost;
|
||||
using namespace google::protobuf::util;
|
||||
|
||||
struct SniperPolygon;
|
||||
|
||||
@@ -32,14 +32,89 @@ 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 ////////////////////////////////////////////
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// structure into which file is read, its not part of the state
|
||||
struct FilePolygon{
|
||||
struct Leg{
|
||||
const uint64_t file_leg_key = 0;
|
||||
const std::string security_desc;
|
||||
const api::Side leg_side = api::Side::SIDE_BUY;
|
||||
const uint16_t leg_qty = 0;
|
||||
// this is not stored in the file, its filled in later
|
||||
SecurityKey resolved_key = SecurityKey(0);
|
||||
};
|
||||
const uint32_t pid;
|
||||
const std::string p_name;
|
||||
std::vector<Leg> legs;
|
||||
void add_leg(uint64_t key, const std::string& security_desc,
|
||||
api::Side side, uint16_t qty){
|
||||
legs.push_back(Leg {key,security_desc,side,qty});
|
||||
}
|
||||
};
|
||||
|
||||
// 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){
|
||||
std::vector<string> toks;
|
||||
boost::split(toks,ln,boost::is_any_of(","));
|
||||
bts_assert(toks.size()==6);
|
||||
int pid = std::stoi(toks[0]);
|
||||
auto file_leg_key = std::stoull(toks[1]);
|
||||
std::string security_desc = toks[2];
|
||||
std::string side_txt = toks[3];
|
||||
api::Side side = side_txt.find("BUY") != std::string::npos ?
|
||||
api::Side::SIDE_BUY : api::Side::SIDE_SELL;
|
||||
int leg_qty = std::stoi(toks[4]);
|
||||
std::string poly_txt = toks[5];
|
||||
return std::make_tuple(
|
||||
static_cast<uint32_t>(pid),
|
||||
file_leg_key,
|
||||
security_desc,
|
||||
side,
|
||||
static_cast<uint16_t>(leg_qty),
|
||||
poly_txt);
|
||||
};
|
||||
std::map<uint32_t,FilePolygon> rv;
|
||||
std::ifstream file(file_n);
|
||||
std::string ln;
|
||||
size_t ln_no = 0;
|
||||
while (std::getline(file, ln)) {
|
||||
if(ln_no++ > 0 && !ln.empty()){
|
||||
auto[pid,lkey,lsec_desc,side,lqty,poly_txt] = parse_line(ln);
|
||||
FilePolygon* file_poly;
|
||||
auto it = rv.find(pid);
|
||||
if(it == rv.end()){
|
||||
rv.insert(std::pair(pid,FilePolygon{pid,poly_txt,{}}));
|
||||
it = rv.find(pid);
|
||||
}
|
||||
file_poly = &(it->second);
|
||||
file_poly->add_leg(lkey,lsec_desc,side,lqty);
|
||||
}
|
||||
}
|
||||
for(auto& p : rv){
|
||||
FilePolygon& file_poly = p.second;
|
||||
out_v.push_back(file_poly);
|
||||
LOG(OrderMan, Info, "Loaded polygon:{} n_legs:{}",
|
||||
file_poly.p_name, file_poly.legs.size());
|
||||
}
|
||||
}
|
||||
|
||||
// calculations
|
||||
|
||||
// All securities across all polygon legs have a representation here.
|
||||
struct SecData{
|
||||
const SecurityKey key;
|
||||
const api::Security& security;
|
||||
ChildTemplate* buy_template;
|
||||
ChildTemplate* sell_template;
|
||||
// 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;
|
||||
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,
|
||||
@@ -48,29 +123,14 @@ struct SecData{
|
||||
:
|
||||
key(key),
|
||||
security(security),
|
||||
buy_template(buy_template),
|
||||
sell_template(sell_template)
|
||||
{
|
||||
}
|
||||
buy_template(*buy_template),
|
||||
sell_template(*sell_template) { }
|
||||
void set_book(const Book& bk){
|
||||
last_book = current_book;
|
||||
current_book = bk;
|
||||
}
|
||||
};
|
||||
|
||||
struct WorkingOrder{
|
||||
OrderKey order_key = OrderKey(0);
|
||||
bool is_buy = true;
|
||||
Quantity filled_qty = Quantity(0);
|
||||
void reset (){
|
||||
order_key = OrderKey(0);
|
||||
filled_qty = Quantity(0);
|
||||
}
|
||||
bool is_working() const{
|
||||
return order_key != OrderKey(0);
|
||||
}
|
||||
};
|
||||
|
||||
struct PriceQty{
|
||||
// for the legs the buy and sell are wrt to the polygon
|
||||
Price buy_price = Price(os::nan());
|
||||
@@ -99,8 +159,8 @@ struct SniperLeg{
|
||||
SecData& sec_data;
|
||||
const uint16_t leg_qty;
|
||||
const api::Side leg_side;
|
||||
WorkingOrder order = {};
|
||||
PriceQty leg_price_qty = {};
|
||||
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;
|
||||
}
|
||||
@@ -108,28 +168,62 @@ struct SniperLeg{
|
||||
return std::make_tuple(leg_price_qty.price(side),
|
||||
leg_price_qty.qty(side));
|
||||
}
|
||||
std::string to_string() const{
|
||||
std::ostringstream buff;
|
||||
auto side_tx = side_txt(leg_side,false);
|
||||
buff << side_tx << sec_data.security.glbx().security_desc();
|
||||
return buff.str();
|
||||
}
|
||||
// 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::string to_string() const{
|
||||
std::ostringstream buff;
|
||||
for (auto& l : legs)
|
||||
buff << l.to_string() << " ";
|
||||
return buff.str();
|
||||
}
|
||||
std::tuple<Price,Quantity> price_qty(bool curr, api::Side side){
|
||||
|
||||
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){
|
||||
@@ -145,85 +239,34 @@ struct SniperAlgoParams{
|
||||
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;
|
||||
uint32_t throttle_ms;
|
||||
bool bts_keys_in_file;
|
||||
};
|
||||
|
||||
struct SniperAlgo::PImpl {
|
||||
SniperAlgoParams algo_params;
|
||||
IAlgoServices* algo_services = nullptr;
|
||||
// indexing :
|
||||
// sec_key -> books, order temp, polygons (with this sec_key as a leg)
|
||||
// polygon -> shared sec data (book, order temp)
|
||||
// order_key -> polygon (not resolved yet)
|
||||
// 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(){
|
||||
}
|
||||
};
|
||||
|
||||
struct FilePolygon{
|
||||
struct Leg{
|
||||
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);
|
||||
};
|
||||
const uint32_t pid;
|
||||
const std::string p_name;
|
||||
std::vector<Leg> legs;
|
||||
void add_leg(uint64_t key, api::Side side, uint16_t qty){
|
||||
legs.push_back(Leg {key,side,qty});
|
||||
}
|
||||
};
|
||||
|
||||
// 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){
|
||||
std::vector<string> toks;
|
||||
boost::split(toks,ln,boost::is_any_of(","));
|
||||
bts_assert(toks.size()==5);
|
||||
int pid = std::stoi(toks[0]);
|
||||
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;
|
||||
int leg_qty = std::stoi(toks[3]);
|
||||
std::string poly_txt = toks[4];
|
||||
return std::make_tuple(
|
||||
static_cast<uint32_t>(pid),
|
||||
file_leg_key,
|
||||
side,
|
||||
static_cast<uint16_t>(leg_qty),
|
||||
poly_txt);
|
||||
};
|
||||
std::map<uint32_t,FilePolygon> rv;
|
||||
std::ifstream file(file_n);
|
||||
std::string ln;
|
||||
size_t ln_no = 0;
|
||||
while (std::getline(file, ln)) {
|
||||
if(ln_no++ > 0){ // ignore header
|
||||
auto[pid,lkey,side,lqty,poly_txt] = parse_line(ln);
|
||||
FilePolygon* file_poly;
|
||||
auto it = rv.find(pid);
|
||||
if(it == rv.end()){
|
||||
rv.insert(std::pair(pid,FilePolygon{pid,poly_txt,{}}));
|
||||
it = rv.find(pid);
|
||||
}
|
||||
file_poly = &(it->second);
|
||||
file_poly->add_leg(lkey,side,lqty);
|
||||
}
|
||||
}
|
||||
for(auto& p : rv)
|
||||
out_v.push_back(p.second);
|
||||
}
|
||||
|
||||
// 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;
|
||||
@@ -237,8 +280,8 @@ bool is_shot_opp(api::Side poly_side, double poly_price, double min_edge){
|
||||
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.price_qty(true,side);
|
||||
auto[prev_p,prev_q] = p.price_qty(false,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
|
||||
@@ -278,12 +321,12 @@ bool _convert_params (const CustomOrderDefinition& cod,
|
||||
|
||||
auto file_name = get_if<std::string>(params, "File");
|
||||
auto products = get_if<std::string>(params, "Products");
|
||||
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 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() &&
|
||||
max_shot_qty.has_value() &&
|
||||
@@ -296,6 +339,8 @@ bool _convert_params (const CustomOrderDefinition& cod,
|
||||
for (auto& p : toks)
|
||||
out_p.product_list.insert(p);
|
||||
}
|
||||
out_p.shoot_real_orders = shoot_real_orders.has_value() &&
|
||||
shoot_real_orders.value();
|
||||
out_p.min_edge = min_edge.value();
|
||||
out_p.max_shot_qty = max_shot_qty.value();
|
||||
out_p.throttle_ms = throttle.value();
|
||||
@@ -344,9 +389,16 @@ void SniperAlgo::on_book(SecurityKey key, const Book& bk) {
|
||||
bts_assert(it != me->sec_map.end());
|
||||
auto& sec_data = it->second;
|
||||
sec_data.set_book(bk);
|
||||
// go through then snipers and analyze
|
||||
for (SniperPolygon* p_ptr : sec_data.polygons)
|
||||
_compute_sniper_opp(*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);
|
||||
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,
|
||||
@@ -356,7 +408,17 @@ void SniperAlgo::on_child_change(OrderKey key, const ChildState& st){
|
||||
key,
|
||||
static_cast<int>(st.status),
|
||||
st.total_fills,
|
||||
st.last_price);
|
||||
st.last_price);
|
||||
uint16_t n_working_polygons = 0;
|
||||
if(st.status != ChildStatus::WORKING){
|
||||
// find it in the polygon and update the state
|
||||
for(auto& p : me->polygons){
|
||||
p.mark_order_as_completed(key);
|
||||
if(p.has_working_order())
|
||||
n_working_polygons++;
|
||||
}
|
||||
}
|
||||
me->n_working_polygons = n_working_polygons;
|
||||
}
|
||||
void SniperAlgo::on_status(SecurityKey, co2::TradingStatus){
|
||||
}
|
||||
@@ -364,22 +426,19 @@ void SniperAlgo::on_timer(){
|
||||
}
|
||||
// private helpers ////////////////////////////////////////////////////////////
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// sets up all the state
|
||||
bool SniperAlgo::_onetime_setup(IBeforeLaunchAlgoServices& serv,
|
||||
const CustomOrderDefinition& cod){
|
||||
std::map<uint64_t,api::Security const*> exch_key_map;
|
||||
std::map<std::string,api::Security const*> desc_sec_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*{
|
||||
desc_sec_map[p->glbx().security_desc()] = p;
|
||||
// FilePolygon::Leg -> Security
|
||||
auto resolve_key = [&](FilePolygon::Leg& file_leg) {
|
||||
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());
|
||||
auto it = desc_sec_map.find(file_leg.security_desc);
|
||||
rv = (it != desc_sec_map.end()) ? it->second : nullptr;
|
||||
return rv;
|
||||
};
|
||||
// creates a SniperPolygon from a FilePolygon and
|
||||
@@ -393,34 +452,46 @@ bool SniperAlgo::_onetime_setup(IBeforeLaunchAlgoServices& serv,
|
||||
rv.legs.push_back(SniperLeg{sec_data,l.leg_qty,l.leg_side});
|
||||
}
|
||||
me->polygons.push_back(rv);
|
||||
return *(me->polygons.end()-1);
|
||||
};
|
||||
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<FilePolygon> polygons, resolved_polygons;
|
||||
read_polygons(me->algo_params.polygon_list_file, polygons);
|
||||
for (auto& p : polygons){
|
||||
std::vector<FilePolygon> all_read_polygons, resolved_polygons;
|
||||
read_polygons(me->algo_params.polygon_list_file, all_read_polygons);
|
||||
for (auto& p : all_read_polygons){
|
||||
bool succ = true;
|
||||
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.resolved_key,
|
||||
SecData(l.resolved_key,
|
||||
*sec_ptr,
|
||||
_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());
|
||||
if(sec_ptr != nullptr){
|
||||
if(prod_allowed(*sec_ptr)){
|
||||
auto sec_key = SecurityKey(sec_ptr->id());
|
||||
l.resolved_key = sec_key;
|
||||
if(me->sec_map.find(sec_key) == me->sec_map.end()){
|
||||
me->sec_map.emplace(
|
||||
sec_key,
|
||||
SecData(sec_key,
|
||||
*sec_ptr,
|
||||
_get_ord_template(serv,cod,sec_key,api::Side::SIDE_BUY),
|
||||
_get_ord_template(serv,cod,sec_key,api::Side::SIDE_SELL)));
|
||||
LOG(OrderMan, Info,
|
||||
"SniperAlgo::_onetime_setup successfully resolved "
|
||||
"exchange_key:{} bts_key:{} desc:{}",
|
||||
l.file_leg_key, sec_ptr->id(), l.security_desc);
|
||||
}
|
||||
}
|
||||
else{
|
||||
LOG(OrderMan, Warn,
|
||||
"SniperAlgo::_onetime_setup ignored leg with "
|
||||
"key:{} desc:{}", l.file_leg_key, l.security_desc);
|
||||
succ = false;
|
||||
}
|
||||
}
|
||||
else{
|
||||
LOG(OrderMan, Error,
|
||||
"SniperAlgo::_onetime_setup could not resolve key:{}",
|
||||
l.file_leg_key);
|
||||
"SniperAlgo::_onetime_setup could not resolve leg with "
|
||||
"key:{} desc:{}", l.file_leg_key, l.security_desc);
|
||||
succ = false;
|
||||
}
|
||||
}
|
||||
@@ -428,10 +499,10 @@ bool SniperAlgo::_onetime_setup(IBeforeLaunchAlgoServices& serv,
|
||||
resolved_polygons.push_back(p);
|
||||
}
|
||||
for (const auto& p : resolved_polygons){
|
||||
add_polygon(p);
|
||||
LOG(OrderMan, Info,
|
||||
const auto& sniper_polygon = add_polygon(p);
|
||||
LOG(OrderMan, Info,
|
||||
"SniperAlgo::_onetime_setup successfully resolved polygon:{}",
|
||||
p.p_name);
|
||||
sniper_polygon.to_string());
|
||||
}
|
||||
// 2-pass : adding polygons and then making the sec map point to them
|
||||
// (since references can be validated when elements are added to vectors)
|
||||
@@ -444,7 +515,17 @@ 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){
|
||||
@@ -452,8 +533,8 @@ void SniperAlgo::_compute_sniper_opp(SniperPolygon& p){
|
||||
bool legs_valid_price = true;
|
||||
for (auto& l : p.legs){
|
||||
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;
|
||||
// TODO push it to one place
|
||||
api::Side cross_side = get_leg_cross_side(poly_side,l);
|
||||
SpreaderUtils::traverse_depth_for_quantity(
|
||||
l.sec_data.current_book, // TODO: setting to use implied or not
|
||||
cross_side,
|
||||
@@ -465,15 +546,45 @@ void SniperAlgo::_compute_sniper_opp(SniperPolygon& p){
|
||||
legs_valid_price = legs_valid_price &&
|
||||
l_comp_qty.value()>=l.leg_qty;
|
||||
}
|
||||
//shot_price = legs_valid_price ? shot_price : os::nan();
|
||||
// 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);
|
||||
compute_shot(api::Side::SIDE_SELL);
|
||||
}
|
||||
void SniperAlgo::_fire_opps(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){
|
||||
for (auto& l : p.legs){
|
||||
ChildTemplate& order_template = l.order_template(side);
|
||||
auto[pr,qty] = l.price_qty(side);
|
||||
l.working_order =
|
||||
me->algo_services->launch_child(order_template,pr,qty);
|
||||
}
|
||||
me->last_shot_time = TimeUtil::GetCurrentTime();
|
||||
me->n_working_polygons++;
|
||||
};
|
||||
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(),
|
||||
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(),
|
||||
me->algo_params.min_edge))
|
||||
fire_order(*p,api::Side::SIDE_SELL);
|
||||
}
|
||||
else
|
||||
break;
|
||||
}
|
||||
}
|
||||
void SniperAlgo::_cxl_and_end (bool is_error, const std::string& err_msg){
|
||||
if(is_error)
|
||||
|
||||
@@ -10,6 +10,7 @@ namespace orderman {
|
||||
class ChildTemplate;
|
||||
class IBeforeLaunchAlgoServices;
|
||||
struct SniperPolygon;
|
||||
struct SecData;
|
||||
|
||||
class SniperAlgo : public IAlgoOrder {
|
||||
public:
|
||||
@@ -31,8 +32,9 @@ class SniperAlgo : public IAlgoOrder {
|
||||
private:
|
||||
IAlgoServices& _algo() const ;
|
||||
bool _onetime_setup(IBeforeLaunchAlgoServices& serv,
|
||||
const bts::api::orderman::CustomOrderDefinition& cod);
|
||||
const bts::api::orderman::CustomOrderDefinition& cod);
|
||||
void _compute_sniper_opp(SniperPolygon&);
|
||||
void _fire_opps(SecData& sec_data);
|
||||
// acts on md, cognizant of the state
|
||||
void _cxl_and_end (bool is_error, const std::string& err_msg);
|
||||
};
|
||||
|
||||
@@ -181,7 +181,7 @@ void FifoSecurityMatcher::_on_order_not_affecting_books(
|
||||
auto& ord = o.order;
|
||||
auto& opp_book = _book(_opp_side(ord.side));
|
||||
uint32_t new_ord_rem = total_quantity(ord).value();
|
||||
for (FifoPriceLevelMatcher &pl : opp_book) {
|
||||
for (const FifoPriceLevelMatcher &pl : opp_book) {
|
||||
if (_is_cross(ord, pl)){
|
||||
uint32_t level_qty =
|
||||
static_cast<uint32_t>(pl.total_quantity().value());
|
||||
|
||||
Reference in new issue
Block a user