cleanup, comments

This commit is contained in:
Taha Afzal committed 2020-04-26 23:10:16 -04:00
1 parent 8ccab66120
commit fd8b8e03ea
1 file changed
+44 -17
@@ -4,8 +4,10 @@
// at end of settle, order should dettach and complete (X)
// order going out logic (X)
// settlement compute logic (X)
// actual publish
// actual publish (X)
// no PTime
// check settle calc (X)
// check order shooting logic
#include "lib/orderman/orders/algo/algo_orders/TASCounterAlgo.h"
@@ -66,6 +68,7 @@ struct ContractAutoOrder{
};
struct AlgoData{
double tick_size;
Timestamp settle_start;
Timestamp settle_stop;
ContractMarketData t1_front,
@@ -80,7 +83,7 @@ struct AlgoData{
ContractAutoOrder t1_front_order,
t1_second_order,
t1_third_order;
DynamicOrderState out_state;
DynamicOrderState out_state;
};
struct TASCounterAlgo::PImpl {
@@ -102,10 +105,11 @@ bool _copy_settle_times (AlgoData&,const Map<std::string,DynamicOrderParam>&);
void _copy_order_data (AlgoData&,const Map<std::string,DynamicOrderParam>&);
bool _setup_data(IBeforeLaunchAlgoServices&, AlgoData&,
std::map<SecurityKey,ContractMarketData*>&);
long t1_minus_t2_usecs (const Timestamp&, const Timestamp&);
long t_minus_now_usecs (const google::protobuf::Timestamp&);
bool within_time_win (const Timestamp&, const Timestamp&,
const TickerUpdateEvent::Trade& t);
double round_to_nearest(double price, double tick_size);
long long t1_minus_t2_usecs (const Timestamp&, const Timestamp&);
long long t_minus_now_usecs (const google::protobuf::Timestamp&);
bool trade_within_time_win (const Timestamp&, const Timestamp&,
const TickerUpdateEvent::Trade& t);
bool now_within_time_win (const Timestamp&, const Timestamp&);
@@ -140,8 +144,8 @@ void TASCounterAlgo::do_update(const api::orderman::Update& u) {
if(me->state == Status::DONE)
return;
const auto& p = u.custom().dynamic().order_params();
// only order data change is allowed
_copy_order_data(me->data,p);
_copy_settle_times (me->data,p);
_check_and_send_order();
}
// callbacks //////////////////////////////////////////////////////////////////
@@ -224,7 +228,15 @@ void TASCounterAlgo::_compute_settles(){
AlgoData& d = me->data;
d.t1_front.estimated_settle = d.t1_front.vwap();
d.t1_second.estimated_settle = d.t2_front.vwap() - d.t1_front.vwap();
d.t1_third.estimated_settle = d.t3_front.vwap() - d.t1_front.vwap() - d.t1_second.vwap();
d.t1_third.estimated_settle = d.t3_front.vwap() - d.t1_front.vwap() -
d.t1_second.vwap();
// round to closest value
d.t1_front.estimated_settle = round_to_nearest(
d.t1_front.estimated_settle,me->data.tick_size);
d.t1_second.estimated_settle = round_to_nearest(
d.t1_second.estimated_settle,me->data.tick_size);
d.t1_third.estimated_settle = round_to_nearest(
d.t1_third.estimated_settle,me->data.tick_size);
}
// the decision to send an order can be caused by
@@ -299,7 +311,7 @@ bool TASCounterAlgo::_within_settle() const{
///////////////////////////////////////////////////////////////////////////////
// stateless conversion ///////////////////////////////////////////////////////
// statelss helpers
// stateless helpers
ChildTemplate* _create_order_template (IBeforeLaunchAlgoServices& serv,
SecurityKey key,
api::Side side){
@@ -313,14 +325,17 @@ ChildTemplate* _create_order_template (IBeforeLaunchAlgoServices& serv,
// copies output into the dynamic state for publishing
void _copy_output(AlgoData& d){
auto& m = *(d.out_state.mutable_custom_state());
auto secs_to_settle = t_minus_now_usecs(d.settle_start)/1000000;
// we can safely assume its within int32
auto secs_to_settle = static_cast<int>(t_minus_now_usecs(d.settle_start)/1000000);
// if its in settle (-ve value), its set to 0
secs_to_settle = std::max(secs_to_settle,0);
m["Seconds To Settle"].set_as_int32(secs_to_settle);
m["T1 Front Projected Settle"].set_as_double(d.t1_front.estimated_settle);
m["T1 Second Projected Settle"].set_as_double(d.t1_second.estimated_settle);
m["T1 Third Projected Settle"].set_as_double(d.t1_third.estimated_settle);
m["T2 Front Projected Settle"].set_as_double(d.t2_front.estimated_settle);
m["T2 Second Projected Settle"].set_as_double(d.t2_second.estimated_settle);
m["T3 Front Projected Settle"].set_as_double(d.t3_front.estimated_settle);
m["T2 Front VWAP"].set_as_double(d.t2_front.vwap());
m["T2 Second VWAP"].set_as_double(d.t2_second.vwap());
m["T3 Front VWAP"].set_as_double(d.t3_front.vwap());
}
// copies the settle time/period part of the input data
bool _copy_settle_times (AlgoData& d,
@@ -401,6 +416,7 @@ bool _setup_data(IBeforeLaunchAlgoServices& serv,
}
};
if (def.has_custom_def() && def.custom_def().has_dynamic()){
out_d.tick_size = serv.security().contract_spec().low_tick();
const auto& p = def.custom_def().dynamic().order_params();
// settlement time
if(!_copy_settle_times(out_d,p))
@@ -435,6 +451,17 @@ bool _setup_data(IBeforeLaunchAlgoServices& serv,
}
return false;
}
double round_to_nearest(double price, double tick_size){
if(os::is_nan(price))
return price;
auto buy_price = bts::securities::raw_round_to_quote_price(
price,
tick_size,
api::Side::SIDE_BUY);
auto sell_price = buy_price + tick_size;
return std::abs(price-buy_price) < std::abs(sell_price-price) ? buy_price :
sell_price;
}
// time helpers
// converts std::chrono::system_clock::time_point to google timestamp
google::protobuf::Timestamp to_ts (const bts::TimeStamp& ts){
@@ -443,11 +470,11 @@ google::protobuf::Timestamp to_ts (const bts::TimeStamp& ts){
auto usecs = ts_us_val.count();
return google::protobuf::util::TimeUtil::MillisecondsToTimestamp(usecs);
}
long t1_minus_t2_usecs (const Timestamp& t1,const Timestamp& t2){
long long t1_minus_t2_usecs (const Timestamp& t1,const Timestamp& t2){
return util::TimeUtil::TimestampToMicroseconds(t1) -
util::TimeUtil::TimestampToMicroseconds(t2);
}
long t_minus_now_usecs (const Timestamp& t){
long long t_minus_now_usecs (const Timestamp& t){
auto now = util::TimeUtil::GetCurrentTime();
return t1_minus_t2_usecs(t,now);
}
@@ -461,8 +488,8 @@ bool now_within_time_win (const Timestamp& beg,
const Timestamp& end){
return within_time_win(beg,end,util::TimeUtil::GetCurrentTime());
}
bool within_time_win (const Timestamp& beg, const Timestamp& end,
const TickerUpdateEvent::Trade& t){
bool trade_within_time_win (const Timestamp& beg, const Timestamp& end,
const TickerUpdateEvent::Trade& t){
auto ts = to_ts(t.ts);
return within_time_win(beg,end,ts);
}