Merge branch 'spartan_tas' of bitbucket.org:bluetradesys/spark into spartan_tas
This commit is contained in:
commit
317da795ec
4 files changed
+479
No files matched your search
@@ -259,6 +259,10 @@ inline bool test_price_equality(double price_1, double price_2,
|
||||
inline bool gr_than_eq(double p1, double p2, double tick_size) {
|
||||
return test_price_equality(p1, p2, tick_size) || p1 >= p2;
|
||||
}
|
||||
// p1 >= p2
|
||||
inline bool gr_than_eq(double p1, double p2) {
|
||||
return test_price_equality(p1, p2, market_epsilon()) || p1 >= p2;
|
||||
}
|
||||
inline bool gr_than(double p1, double p2, double tick_size) {
|
||||
return gr_than_eq(p1, p2, tick_size) &&
|
||||
!test_price_equality(p1, p2, tick_size);
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#include "TASAlgo.h"
|
||||
#include "TASCounterAlgo.h"
|
||||
#include "OnSignalAlgo.h"
|
||||
#include "SniperAlgo.h"
|
||||
|
||||
using namespace bts;
|
||||
using namespace api::orderman;
|
||||
@@ -234,6 +235,17 @@ extern "C" void init_on_signal_plugin(bts::SparkServices& srv) {
|
||||
srv.register_dynamic_algo_order(on_signal, []() { return std::make_unique<orderman::OnSignalAlgo>(); });
|
||||
}
|
||||
|
||||
extern "C" void init_sniper_plugin(bts::SparkServices& srv) {
|
||||
DynamicOrderDescriptor sniper;
|
||||
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_double("Min Edge");
|
||||
*sniper.add_param_desc() = param_uint32("Max Shot Qty");
|
||||
*sniper.add_param_desc() = param_uint32("Throttle(ms)");
|
||||
srv.register_dynamic_algo_order(sniper, []() { return std::make_unique<orderman::SniperAlgo>(); });
|
||||
}
|
||||
|
||||
extern "C" void init_spark_plugin(bts::SparkServices &srv) {
|
||||
auto& dyn_algo_config = config::get<config::toggles::DynamicAlgos>();
|
||||
if(dyn_algo_config.enable_dorman())
|
||||
@@ -248,6 +260,7 @@ extern "C" void init_spark_plugin(bts::SparkServices &srv) {
|
||||
init_limit_cover(srv);
|
||||
if (dyn_algo_config.enable_onsignal())
|
||||
init_on_signal_plugin(srv);
|
||||
init_sniper_plugin(srv);
|
||||
}
|
||||
|
||||
}}}
|
||||
@@ -0,0 +1,421 @@
|
||||
#include "lib/orderman/orders/algo/algo_orders/SniperAlgo.h"
|
||||
|
||||
#include "lib/domain_model/Security.h"
|
||||
#include "lib/orderman/order_factory/IOrderFactory.h"
|
||||
#include "lib/orderman/orderman_utils/Book.h"
|
||||
#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 "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>
|
||||
|
||||
namespace bts {
|
||||
namespace orderman {
|
||||
|
||||
using namespace api::orderman;
|
||||
using namespace google::protobuf;
|
||||
using namespace boost;
|
||||
|
||||
struct SniperPolygon;
|
||||
|
||||
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 ? "+" : "-");
|
||||
}
|
||||
|
||||
struct SecData{
|
||||
const SecurityKey key;
|
||||
const api::Security& security;
|
||||
ChildTemplate* buy_template;
|
||||
ChildTemplate* sell_template;
|
||||
Book current_book;
|
||||
Book last_book;
|
||||
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());
|
||||
};
|
||||
|
||||
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 SniperLeg{
|
||||
SecData& sec_data;
|
||||
const uint16_t leg_qty;
|
||||
const api::Side leg_side;
|
||||
WorkingOrder order = {};
|
||||
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){
|
||||
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::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();
|
||||
}
|
||||
};
|
||||
|
||||
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){
|
||||
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);
|
||||
}
|
||||
};
|
||||
|
||||
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;
|
||||
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 :
|
||||
// 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)
|
||||
std::unordered_map<SecurityKey,SecData> sec_map;
|
||||
std::vector<SniperPolygon> polygons;
|
||||
PImpl(){
|
||||
}
|
||||
};
|
||||
|
||||
struct FilePolygon{
|
||||
struct Leg{
|
||||
SecurityKey leg_sec_key = SecurityKey(0);
|
||||
api::Side leg_side = api::Side::SIDE_BUY;
|
||||
uint16_t leg_qty = 0;
|
||||
};
|
||||
uint32_t pid;
|
||||
std::string p_name;
|
||||
std::vector<Leg> legs;
|
||||
void add_leg(SecurityKey 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
|
||||
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]);
|
||||
int leg_sec_key = std::stoi(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),
|
||||
SecurityKey(leg_sec_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);
|
||||
}
|
||||
|
||||
void log_opp(SniperPolygon& p){
|
||||
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 = "";
|
||||
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);
|
||||
};
|
||||
// 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);
|
||||
}
|
||||
|
||||
ChildTemplate* _get_ord_template (IBeforeLaunchAlgoServices& serv,
|
||||
const CustomOrderDefinition& cod,
|
||||
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));
|
||||
}
|
||||
|
||||
bool _convert_params (IBeforeLaunchAlgoServices&,
|
||||
const CustomOrderDefinition& cod,
|
||||
SniperAlgoParams& out_p){
|
||||
const DynamicOrderDefinition& def = cod.dynamic();
|
||||
const auto& params = def.order_params();
|
||||
auto file_name = get_if<std::string>(params, "File");
|
||||
auto products = get_if<std::string>(params, "Products");
|
||||
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)");
|
||||
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<string> 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();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
SniperAlgo::SniperAlgo() : me (new PImpl()){}
|
||||
SniperAlgo::~SniperAlgo() {}
|
||||
|
||||
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());
|
||||
return {};
|
||||
}
|
||||
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&){
|
||||
}
|
||||
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);
|
||||
// go through then snipers and analyze
|
||||
for (SniperPolygon* p_ptr : sec_data.polygons){
|
||||
auto& p = *p_ptr;
|
||||
_compute_sniper_opp(p);
|
||||
}
|
||||
}
|
||||
void SniperAlgo::on_child_change(OrderKey key, const ChildState& st){
|
||||
LOG(OrderMan, Info,
|
||||
"SniperAlgo::on_child_change key:{} child_key:{} status:{} "
|
||||
"total_fills:{} last_price:{}",
|
||||
me->algo_services->order_key(),
|
||||
key,
|
||||
static_cast<int>(st.status),
|
||||
st.total_fills,
|
||||
st.last_price);
|
||||
}
|
||||
void SniperAlgo::on_status(SecurityKey, co2::TradingStatus){
|
||||
}
|
||||
void SniperAlgo::on_timer(){
|
||||
}
|
||||
// private helpers ////////////////////////////////////////////////////////////
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
void SniperAlgo::_onetime_setup(IBeforeLaunchAlgoServices& serv,
|
||||
const CustomOrderDefinition& cod){
|
||||
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);
|
||||
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);
|
||||
};
|
||||
std::vector<FilePolygon> polygons, resolved_polygons;
|
||||
read_polygons(me->algo_params.polygon_list_file, polygons);
|
||||
// TODO: get this from somewhere
|
||||
for (const 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())
|
||||
me->sec_map.emplace(
|
||||
l.leg_sec_key,
|
||||
SecData(l.leg_sec_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)));
|
||||
}
|
||||
else{
|
||||
LOG(OrderMan, Error, "SniperAlgo::_onetime_setup "
|
||||
"could not resolve key:{}", l.leg_sec_key);
|
||||
succ = false;
|
||||
}
|
||||
}
|
||||
if(succ)
|
||||
resolved_polygons.push_back(p);
|
||||
}
|
||||
for (const auto& p : resolved_polygons)
|
||||
add_polygon(p);
|
||||
// 2-pass : adding polygons and then making the sec map point to them
|
||||
// (since references can be validated when elements are added to vectors)
|
||||
|
||||
// polygons already have ref's to sec's so the assumption here is adding
|
||||
// polygon pointers to sec-data does not invalidate the ref's
|
||||
|
||||
for (auto& p : me->polygons)
|
||||
for(auto& l : p.legs)
|
||||
l.sec_data.polygons.push_back(&p);
|
||||
}
|
||||
void SniperAlgo::_compute_sniper_opp(SniperPolygon& p){
|
||||
p.prev_output = p.curr_output;
|
||||
// as is
|
||||
{
|
||||
double buy_price = 0;
|
||||
for (auto& l : p.legs){
|
||||
// TODO: setting to use implied or not
|
||||
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();
|
||||
}
|
||||
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);
|
||||
}
|
||||
void SniperAlgo::_cxl_and_end (bool is_error, const std::string& err_msg){
|
||||
if(is_error)
|
||||
me->algo_services->terminate_error(err_msg);
|
||||
else
|
||||
me->algo_services->complete();
|
||||
}
|
||||
|
||||
} // namespace orderman
|
||||
} // namespace bts
|
||||
@@ -0,0 +1,41 @@
|
||||
#pragma once
|
||||
|
||||
#include "lib/orderman/orders/algo/IAlgoOrder.h"
|
||||
|
||||
namespace bts {namespace api {namespace orderman{class CustomOrderDefinition;}}}
|
||||
|
||||
namespace bts {
|
||||
namespace orderman {
|
||||
|
||||
class ChildTemplate;
|
||||
class IBeforeLaunchAlgoServices;
|
||||
struct SniperPolygon;
|
||||
|
||||
class SniperAlgo : public IAlgoOrder {
|
||||
public:
|
||||
SniperAlgo();
|
||||
~SniperAlgo();
|
||||
OrderOpResult do_before_launch(IBeforeLaunchAlgoServices &) override;
|
||||
void do_launch(IAlgoServices &) override;
|
||||
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_child_change(OrderKey, ChildState const &) override;
|
||||
void on_status(SecurityKey, co2::TradingStatus) override;
|
||||
void on_timer() override;
|
||||
|
||||
private:
|
||||
enum class State;
|
||||
struct PImpl;
|
||||
std::unique_ptr<PImpl> me;
|
||||
private:
|
||||
IAlgoServices& _algo() const ;
|
||||
void _onetime_setup(IBeforeLaunchAlgoServices& serv,
|
||||
const bts::api::orderman::CustomOrderDefinition& cod);
|
||||
void _compute_sniper_opp(SniperPolygon&);
|
||||
// acts on md, cognizant of the state
|
||||
void _cxl_and_end (bool is_error, const std::string& err_msg);
|
||||
};
|
||||
|
||||
} // namespace orderman
|
||||
} // namespace bts
|
||||
Reference in new issue
Block a user