159 lines
5.4 KiB
C++
159 lines
5.4 KiB
C++
#include "PositionServiceServer.h"
|
|
#include "lib/api/services/OrderService.pb.h"
|
|
#include "lib/api/services/OrderService.client.pb.h"
|
|
#include "lib/api/public/ServiceContext.h"
|
|
#include "lib/logging/logging.h"
|
|
#include "lib/utils/PBUtils.h"
|
|
|
|
#include <boost/algorithm/string.hpp>
|
|
#include <boost/tokenizer.hpp>
|
|
#include <fmt/format.h>
|
|
#include <map>
|
|
#include <set>
|
|
#include <utility>
|
|
|
|
namespace bts {
|
|
namespace position{
|
|
|
|
using namespace bts::api::position;
|
|
using namespace bts::api::orderman;
|
|
|
|
struct PositionServiceServer::PImpl {
|
|
PositionServiceBase::Position_dataStatefulPublisher publisher;
|
|
bts::api::orderman::OrderService order_service;
|
|
std::map<std::string, std::map<uint32_t, PositionUnit>> state;
|
|
int id;
|
|
|
|
PImpl(bts::api::ServiceContext& context,
|
|
const PositionServiceBase::Position_dataStatefulPublisher& publisher)
|
|
:
|
|
publisher(publisher),
|
|
order_service(context.locate<bts::api::orderman::OrderService>()),
|
|
state(),
|
|
id(0)
|
|
{
|
|
}
|
|
};
|
|
|
|
PositionServiceServer::PositionServiceServer(bts::api::ServiceContext& context)
|
|
:
|
|
PositionServiceBase (context),
|
|
me(new PImpl(context,
|
|
make_position_data_stateful_publisher())){
|
|
me->order_service.subscribe_fills() |
|
|
rxcpp::operators::subscribe<api::orderman::FillsUpdate>(
|
|
[this](auto const& update) {
|
|
|
|
for (const auto& f : update.removed())
|
|
me->publisher.add_or_update(_on_fill(*f,false));
|
|
for (const auto& f : update.added())
|
|
me->publisher.add_or_update(_on_fill(*f,true));
|
|
me->publisher.flush();
|
|
});
|
|
}
|
|
|
|
const PositionUnit& PositionServiceServer::_on_fill (const Fill& f,
|
|
bool is_add){
|
|
std::string fill_account=_fill_account(f);
|
|
PositionUnit& pu=_get_or_add(fill_account,f.security_key());
|
|
double avg_price, tot_qty;
|
|
if(f.fill_kind()==bts::api::orderman::Fill::FILL_KIND_SOD){
|
|
if(f.side()==api::Side::SIDE_BUY){
|
|
_avg_price_qty(pu.sod_long_avg_price(),pu.sod_long_pos(),
|
|
f.avg_price(),f.last_shares(),
|
|
is_add,f.fill_key(),
|
|
avg_price, tot_qty);
|
|
pu.set_sod_long_avg_price(avg_price);
|
|
pu.set_sod_long_pos(tot_qty);
|
|
}
|
|
else{
|
|
_avg_price_qty(pu.sod_short_avg_price(),pu.sod_short_pos(),
|
|
f.avg_price(),f.last_shares(),
|
|
is_add,f.fill_key(),
|
|
avg_price, tot_qty);
|
|
pu.set_sod_short_avg_price(avg_price);
|
|
pu.set_sod_short_pos(tot_qty);
|
|
}
|
|
}
|
|
else{
|
|
if(f.side()==api::Side::SIDE_BUY){
|
|
_avg_price_qty(pu.day_long_avg_price(),pu.day_long_pos(),
|
|
f.avg_price(),f.last_shares(),
|
|
is_add,f.fill_key(),
|
|
avg_price, tot_qty);
|
|
pu.set_day_long_avg_price(avg_price);
|
|
pu.set_day_long_pos(tot_qty);
|
|
}
|
|
else{
|
|
_avg_price_qty(pu.day_short_avg_price(),pu.day_short_pos(),
|
|
f.avg_price(),f.last_shares(),
|
|
is_add,f.fill_key(),
|
|
avg_price, tot_qty);
|
|
pu.set_day_short_avg_price(avg_price);
|
|
pu.set_day_short_pos(tot_qty);
|
|
}
|
|
}
|
|
return pu;
|
|
}
|
|
|
|
PositionUnit& PositionServiceServer::_get_or_add (const std::string& acct,
|
|
uint32_t seckey){
|
|
auto acct_it=me->state.find(acct);
|
|
if(acct_it==me->state.end())
|
|
me->state[acct]=std::map<uint32_t,PositionUnit>();
|
|
acct_it=me->state.find(acct);
|
|
bts_assert(acct_it!=me->state.end());
|
|
|
|
std::map<uint32_t,PositionUnit>& sec_map=acct_it->second;
|
|
auto sec_it=sec_map.find(seckey);
|
|
if(sec_it==sec_map.end()){
|
|
PositionUnit n;
|
|
n.set_record_id(++me->id);
|
|
n.set_account(acct);
|
|
n.set_security_id(seckey);
|
|
sec_map[seckey]=n;
|
|
}
|
|
sec_it=sec_map.find(seckey);
|
|
bts_assert(sec_it!=sec_map.end());
|
|
return sec_it->second;
|
|
}
|
|
|
|
void PositionServiceServer::_avg_price_qty(double base_p,
|
|
double base_q,
|
|
double fill_p,
|
|
double fill_q,
|
|
bool is_add,
|
|
const std::string& fill_id,
|
|
double& avg_price,
|
|
double& tot_qty){
|
|
if(fill_q<0){
|
|
LOG(OrderMan,Error,"PositionServiceServer::_avg_price_qty "
|
|
"fill_id:{} fill_q:{} is_add:{}",
|
|
fill_id, fill_q, is_add);
|
|
fill_q=0;
|
|
}
|
|
if(is_add){
|
|
tot_qty=base_q+fill_q;
|
|
avg_price = (base_p*base_q+fill_p*fill_q)/tot_qty;
|
|
}
|
|
else{
|
|
if(fill_q>=base_q){
|
|
tot_qty=0;
|
|
avg_price=0;
|
|
if(fill_q>base_q)
|
|
LOG(OrderMan,Error,"PositionServiceServer::_avg_price_qty "
|
|
"fill_id:{} base_qty:{} fill_qty:{}",
|
|
fill_id, base_q, fill_q);
|
|
}
|
|
else{
|
|
tot_qty=base_q-fill_q;
|
|
avg_price = (base_p*base_q-fill_p*fill_q)/tot_qty;
|
|
}
|
|
}
|
|
}
|
|
std::string PositionServiceServer::_fill_account(const Fill& fill){
|
|
return boost::trim_right_copy(fill.account());
|
|
}
|
|
PositionServiceServer::~PositionServiceServer(){
|
|
}
|
|
}} |