SPARK-1516 sod pos id's
This commit is contained in:
1 parent
7ff42506af
commit
5aad335023
3 files changed
+78
-43
No files matched your search
@@ -5,6 +5,7 @@
|
||||
#include "server/sparkd/orderman/events/NewSecuritiesEvent.h"
|
||||
#include "server/sparkd/orderman/events/StoredFillEvent.h"
|
||||
#include "server/sparkd/persistence/SqliteStore.h"
|
||||
#include "SODPositionMap.h"
|
||||
|
||||
#include <sys/time.h>
|
||||
|
||||
@@ -17,7 +18,8 @@ enum class FillStore::State { WAIT_SEC, READY };
|
||||
|
||||
struct FillStore::PImpl {
|
||||
PImpl()
|
||||
: state(State::WAIT_SEC), store(new persistence::SqliteStore("fills")) {}
|
||||
: state(State::WAIT_SEC), store(new persistence::SqliteStore("fills")),
|
||||
sod_pos_map() {}
|
||||
|
||||
State state;
|
||||
std::unique_ptr<persistence::IStore> store;
|
||||
@@ -25,6 +27,7 @@ struct FillStore::PImpl {
|
||||
known_fills;
|
||||
mercury::Stream<StoredFillEvent> fill_stream;
|
||||
mercury::Actor::SubHandle stored_sec_sub;
|
||||
SODPosition sod_pos_map;
|
||||
};
|
||||
|
||||
FillStore::FillStore() : me(new PImpl()) {}
|
||||
@@ -68,7 +71,8 @@ void FillStore::init() {
|
||||
if (sec) {
|
||||
auto fk = kvp.first;
|
||||
auto fill = std::make_unique<api::orderman::Fill>(rec);
|
||||
stored_batch.emplace_back(*fill.get(),*sec);
|
||||
if (!me->sod_pos_map.add_fill_to_sod_pos(*fill))
|
||||
stored_batch.emplace_back(*fill.get(),*sec);
|
||||
me->known_fills[fk] = std::move(fill);
|
||||
}
|
||||
else
|
||||
@@ -81,7 +85,7 @@ void FillStore::init() {
|
||||
"Could not recover fill {} from storage. "
|
||||
"This should be investigated",
|
||||
kvp.first);
|
||||
|
||||
me->sod_pos_map.add_sod_pos_as_fill_events(stored_batch,sec_idx);
|
||||
me->fill_stream = make_stream<StoredFillEvent>();
|
||||
publish_batch(me->fill_stream, stored_batch);
|
||||
me->state = State::READY;
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#include "lib/logging/logging.h"
|
||||
#include "lib/utils/bts_assert.h"
|
||||
#include <sys/time.h>
|
||||
#include <sstream>
|
||||
|
||||
namespace bts{
|
||||
namespace orderman{
|
||||
@@ -36,7 +37,7 @@ timeval SODPosition::_subtract_day(const timeval& tv){
|
||||
}
|
||||
|
||||
timeval SODPosition::_get_sod_time(){
|
||||
uint16_t hh=21,mm=30; //TODO get it fro config (1640 central)
|
||||
uint16_t hh=21,mm=30; //TODO get it from config (1630 central)
|
||||
timeval sod=_to_timeval(hh,mm,0);
|
||||
timeval now;
|
||||
gettimeofday(&now, NULL);
|
||||
@@ -45,6 +46,14 @@ timeval SODPosition::_get_sod_time(){
|
||||
return sod;
|
||||
}
|
||||
|
||||
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());
|
||||
@@ -54,52 +63,69 @@ void SODPosition::_merge_pos(Fill& pos, const Fill& fill){
|
||||
double avg_price=(pos.avg_price()*pos.last_shares()+
|
||||
fill.avg_price()*fill.last_shares())/total_qty;
|
||||
pos.set_avg_price(avg_price);
|
||||
pos.set_last_price(total_qty);
|
||||
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.set_fill_kind(Fill::SOD_POS);
|
||||
}
|
||||
|
||||
void SODPosition::add_fill_to_sod_pos(const Fill& fill){
|
||||
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());
|
||||
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);
|
||||
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::SOD_POS);
|
||||
pos.set_order_key(0);
|
||||
pos.set_fill_key(_sod_fill_id(fill,side));
|
||||
}
|
||||
|
||||
std::vector<const Fill*> SODPosition::sod_pos(){
|
||||
std::vector<const Fill*> rv;
|
||||
bool SODPosition::add_fill_to_sod_pos(const Fill& fill){
|
||||
if (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){
|
||||
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());
|
||||
rv.push_back(&long_pos);
|
||||
rv.push_back(&short_pos);
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
|
||||
}}
|
||||
@@ -1,6 +1,9 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "lib/api/services/OrderService.pb.h"
|
||||
#include "server/sparkd/mercury/actors/Actor.h"
|
||||
#include "server/sparkd/orderman/events/StoredFillEvent.h"
|
||||
|
||||
using namespace bts::api::orderman;
|
||||
|
||||
@@ -18,14 +21,16 @@ private:
|
||||
private:
|
||||
// converts the timeval to todat:hh:mm:ss
|
||||
static timeval _to_timeval(uint16_t hh, uint16_t mm, uint16_t ss);
|
||||
static timeval _subtract_day(const timeval& tv);
|
||||
static timeval _subtract_day(const timeval& tv);
|
||||
timeval _get_sod_time();
|
||||
static std::string _sod_fill_id(const Fill& fill,api::Side side);
|
||||
void _merge_pos(Fill& pos, const Fill& fill);
|
||||
void _zero_pos(Fill& pos, const Fill& fill, api::Side side);
|
||||
void _zero_pos(Fill& pos, const Fill& fill, api::Side side);
|
||||
public:
|
||||
SODPosition();
|
||||
void add_fill_to_sod_pos(const Fill& fill);
|
||||
std::vector<const Fill*> sod_pos();
|
||||
bool add_fill_to_sod_pos(const Fill& fill);
|
||||
void add_sod_pos_as_fill_events(std::vector<StoredFillEvent>& fill_events,
|
||||
std::map<SecurityKey, const api::Security*> sec_map);
|
||||
};
|
||||
|
||||
}}
|
||||
Reference in new issue
Block a user