Files
spark/src/lib/orderman/actors/SODPosition.cpp
T

196 lines
6.5 KiB
C++

#include "lib/orderman/actors/SODPosition.h"
#include "lib/logging/logging.h"
#include "lib/utils/bts_assert.h"
#include "lib/config/protos/toggles.pb.h"
#include "lib/config/protos/orders_fills_cleanup.pb.h"
#include "lib/config/config.h"
#include "lib/utils/PBUtils.h"
#include <sys/time.h>
#include <sstream>
namespace bts{
namespace orderman{
SODPosition::SODPosition()
:
_enabled (config::get<config::toggles::FillsSoftCleanup>().enabled()),
_sod_time(_get_fills_sod_time()){
LOG(persist, Info, "SODPosition enabled:{} sec:{}",
_enabled, _sod_time.tv_sec);
}
// returns the timeval for today's hh,mm,ss
// timeval is the microsecons from epoch)
timeval SODPosition::_to_timeval(uint16_t hh, uint16_t mm, uint16_t ss){
bts_assert(hh<24);
bts_assert(mm<60);
bts_assert(ss<60);
time_t secs;
time (&secs); // seconds since epoch (utc)
struct tm* ptm = gmtime (&secs);
ptm->tm_hour=hh;
ptm->tm_min=mm;
ptm->tm_sec=ss;
ptm->tm_isdst=-1;
secs=mktime(ptm);
timeval rv;
rv.tv_sec=secs;
rv.tv_usec=0;
return rv;
}
timeval SODPosition::_subtract_days(const timeval& tv, uint32_t n_days){
uint32_t secs_in_day=86400*n_days;
timeval rv=tv;
rv.tv_sec=(rv.tv_sec > secs_in_day) ? rv.tv_sec-secs_in_day : 0;
return rv;
}
timeval SODPosition::_get_fills_sod_time(){
const auto& sod_time = config::get<config::SODTime>();
timeval sod=_to_timeval(sod_time.sod_hh(),sod_time.sod_mm(),0);
timeval now;
gettimeofday(&now, NULL);
// if the server started before the sod for today
// we go back to yesterday
if(sod.tv_sec >= now.tv_sec)
sod=_subtract_days(sod);
return sod;
}
timeval SODPosition::get_orders_start_time(){
const auto& sod_time = config::get<config::SODTime>();
timeval sod=_to_timeval(sod_time.sod_hh(),sod_time.sod_mm(),0);
const auto& orders_cleanup = config::get<config::OrdersSoftCleanupCutoff>();
timeval now;
gettimeofday(&now, NULL);
// if the server started before the sod for today
// we go back to yesterday
if(sod.tv_sec >= now.tv_sec)
sod=_subtract_days(sod);
sod=_subtract_days(sod,orders_cleanup.extra_days());
return sod;
}
timeval SODPosition::get_order_cutoff_time(
const std::map<std::string,std::string>& order_map){
//returns true is t1 >= t2
auto compare_ts = [&](google::protobuf::Timestamp t1, google::protobuf::Timestamp t2) -> bool
{
if (t1.seconds() > t2.seconds())
return true;
else if (t1.seconds() < t2.seconds())
return false;
else
return t1.nanos() >= t2.nanos();
};
google::protobuf::Timestamp max_ts;
max_ts.set_seconds(0);
max_ts.set_nanos(0);
for (auto const &kvp : order_map){
OrderRecord trec;
if (utils::pb::from_string<api::orderman::OrderRecord>(kvp.second, &trec)){
const auto& ts = trec.state().created();
if(compare_ts(ts,max_ts))
(&max_ts)->CopyFrom(ts);
}
else
LOG(OrderMan, Error,
"Could not recover order {} from storage. "
"This should be investigated",
kvp.first);
}
timeval rv;
rv.tv_sec=max_ts.seconds();
rv.tv_usec=static_cast<double>(max_ts.nanos())/1000;
const auto& orders_cleanup = config::get<config::OrdersSoftCleanupCutoff>();
return _subtract_days(rv,orders_cleanup.extra_days());
}
std::string SODPosition::_sod_fill_id(const Fill& fill,api::Side side){
//sod:acct:long/short:sec
std::ostringstream oss;
oss << "sod:" << fill.account() << (side==api::Side::SIDE_BUY ? ":L:" : ":S:")
<< fill.security_key();
return oss.str();
}
void SODPosition::_merge_pos(Fill& pos, const Fill& fill){
bts_assert(pos.security_key()==fill.security_key());
bts_assert(pos.account()==fill.account());
bts_assert(pos.side()==fill.side());
double total_qty=pos.last_shares()+fill.last_shares();
double avg_price=(pos.avg_price()*pos.last_shares()+
fill.last_price()*fill.last_shares())/total_qty;
pos.set_avg_price(avg_price);
pos.set_last_price(avg_price);
pos.set_last_shares(total_qty);
}
void SODPosition::_zero_pos(Fill& pos, const Fill& fill, api::Side side){
pos.set_security_key(fill.security_key());
pos.set_account(fill.account());
pos.set_side(side);
pos.mutable_timestamp()->set_seconds(_sod_time.tv_sec);
pos.mutable_timestamp()->set_nanos(0);
pos.set_fill_kind(Fill::FILL_KIND_SOD);
pos.set_order_key(0);
pos.set_fill_key(_sod_fill_id(fill,side));
pos.set_last_shares(0);
}
bool SODPosition::add_fill_to_sod_pos(const Fill& fill){
if (_enabled && fill.timestamp().seconds() < _sod_time.tv_sec){
uint32_t seckey=fill.security_key();
const std::string& acct=fill.account();
auto acct_it=_sod_map.find(acct);
if(acct_it==_sod_map.end())
_sod_map[acct]=SecPosMap();
acct_it=_sod_map.find(fill.account());
bts_assert(acct_it!=_sod_map.end());
SecPosMap& sec_map=acct_it->second;
auto sec_it=sec_map.find(seckey);
if(sec_it==sec_map.end()){
Fill long_pos, short_pos;
_zero_pos(long_pos,fill,api::Side::SIDE_BUY);
_zero_pos(short_pos,fill,api::Side::SIDE_SELL);
sec_map[seckey]=std::pair<Fill,Fill>(long_pos,short_pos);
}
sec_it=sec_map.find(seckey);
bts_assert(sec_it!=sec_map.end());
std::pair<Fill,Fill>& pos=sec_it->second;
if(fill.side()==api::Side::SIDE_BUY)
_merge_pos(pos.first,fill);
else
_merge_pos(pos.second,fill);
return true;
}
else
return false;
}
void SODPosition::add_sod_pos_as_fill_events(std::vector<StoredFillEvent>& fe,
std::map<SecurityKey, const api::Security*> sec_map){
if(_enabled)
for (auto& acct_p : _sod_map){
for(auto& sec_p : acct_p.second){
Fill& long_pos=sec_p.second.first;
Fill& short_pos=sec_p.second.second;
bts_assert(long_pos.security_key()==short_pos.security_key());
auto sec_it=sec_map.find(SecurityKey(long_pos.security_key()));
if(sec_it==sec_map.end())
LOG(persist, Error,
"Could not recover fill for key:{} from storage. "
"This should be investigated",
long_pos.security_key());
else{
fe.emplace_back(long_pos,*sec_it->second);
fe.emplace_back(short_pos,*sec_it->second);
}
}
}
}
}}