SPARK-1516 sod pos aggregation done

This commit is contained in:
Taha Afzal committed 2018-07-27 20:33:49 -04:00
1 parent 3c7313b65a
commit 9ca4f85ea1
4 files changed
+99

No files matched your search

+1
View File
@@ -140,6 +140,7 @@ message Fill {
enum FillKind {
FILL_KIND_STANDARD = 0;
FILL_KIND_LEG = 1;
SOD_POS = 2;
}
FillKind fill_kind = 10;
}
@@ -0,0 +1,65 @@
#include "SODPositionMap.h"
#include "lib/logging/logging.h"
#include "lib/utils/bts_assert.h"
namespace bts{
namespace orderman{
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());
pos.set_last_price(fill.last_price());
double total_qty=pos.last_shares()+fill.last_shares();
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);
}
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);
}
std::vector<const Fill*> SODPosition::sod_pos(){
std::vector<const Fill*> rv;
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);
}
}
return rv;
}
}}
@@ -0,0 +1,25 @@
#pragma once
#include "lib/api/services/OrderService.pb.h"
using namespace bts::api::orderman;
namespace bts{
namespace orderman{
typedef std::map<uint32_t,std::pair<api::orderman::Fill,api::orderman::Fill>>
SecPosMap;
class SODPosition{
private:
std::map<std::string,SecPosMap> _sod_map;
std::map<uint32_t, const api::Security*> _sec_ptr_map;
private:
void _merge_pos(Fill& pos, const Fill& fill);
void _zero_pos(Fill& pos, const Fill& fill, api::Side side);
public:
void add_fill_to_sod_pos(const Fill& fill);
std::vector<const Fill*> sod_pos();
};
}}
@@ -1,4 +1,5 @@
#include "server/sparkd/orderman/orders/spreader_orders/tests/tests_common.h"
#include <time.h>
namespace bts {
@@ -2902,5 +2903,12 @@ TEST_CASE ("scratch_pad", "[spreader]"){
REQUIRE(rv_1.i==0);
REQUIRE(rvuser_1.rv.i==100);
}
TEST_CASE ("scratch_pad_2", "[spreader]"){
time_t rawtime;
time (&rawtime);
struct tm* ptm = gmtime (&rawtime);
LOG(OrderMan, Info, "hrs:{} mins:{} sec:{}", ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
}
}}