165 lines
5.8 KiB
C++
165 lines
5.8 KiB
C++
#include "PositionUtilities.h"
|
|
#include "lib/api/services/PositionService.server.pb.h"
|
|
#include "lib/api/services/OrderService.pb.h"
|
|
#include "lib/api/services/OrderService.client.pb.h"
|
|
#include "lib/logging/logging.h"
|
|
|
|
namespace bts {
|
|
namespace position{
|
|
|
|
using namespace bts::api::position;
|
|
using namespace bts::api::orderman;
|
|
|
|
void PositionUtilities::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,"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,"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;
|
|
}
|
|
}
|
|
}
|
|
|
|
void PositionUtilities::apply_fill (PositionEntry& pos,
|
|
api::Side side,
|
|
double price,
|
|
double qty,
|
|
const std::string& fill_id,
|
|
bool is_add){
|
|
double tot_avg_price, tot_qty;
|
|
if(side==api::Side::SIDE_BUY){
|
|
avg_price_qty(pos.long_avg_price(),
|
|
pos.long_pos(),
|
|
price,
|
|
qty,
|
|
is_add,
|
|
fill_id,
|
|
tot_avg_price,
|
|
tot_qty);
|
|
pos.set_long_avg_price(tot_avg_price);
|
|
pos.set_long_pos(tot_qty);
|
|
}
|
|
else{
|
|
avg_price_qty(pos.short_avg_price(),
|
|
pos.short_pos(),
|
|
price,
|
|
qty,
|
|
is_add,
|
|
fill_id,
|
|
tot_avg_price,
|
|
tot_qty);
|
|
pos.set_short_avg_price(tot_avg_price);
|
|
pos.set_short_pos(tot_qty);
|
|
}
|
|
}
|
|
|
|
void PositionUtilities::apply_fill (PositionEntry& pos,
|
|
const Fill& f,
|
|
bool is_add){
|
|
PositionUtilities::apply_fill(pos,
|
|
f.side(),
|
|
f.last_price(),
|
|
f.last_shares(),
|
|
f.fill_key(),
|
|
is_add);
|
|
}
|
|
|
|
void PositionUtilities::apply_manual_fill (
|
|
PositionEntry& pos,
|
|
const ManualFill& mf,
|
|
bool is_add){
|
|
PositionUtilities::apply_fill(pos,
|
|
mf.side(),
|
|
mf.price(),
|
|
mf.quantity(),
|
|
"",
|
|
is_add);
|
|
}
|
|
|
|
void PositionUtilities::total_position(const PositionUnit& p,
|
|
uint32_t& total_long,
|
|
uint32_t& total_short){
|
|
total_long = p.sod().long_pos() + p.day().long_pos() +
|
|
p.admin_day().long_pos() + p.admin_sod().long_pos();
|
|
total_short = p.sod().short_pos() + p.day().short_pos() +
|
|
p.admin_day().short_pos() + p.admin_sod().short_pos();
|
|
}
|
|
|
|
PositionEntry PositionUtilities::add_positions (
|
|
const std::vector<PositionEntry const*>& v){
|
|
double long_pos = 0, long_price = 0, short_pos = 0, short_price = 0;
|
|
for (auto ptr : v){
|
|
const auto& p = *ptr;
|
|
long_pos += p.long_pos();
|
|
long_price += p.long_pos()*p.long_avg_price();
|
|
short_pos += p.short_pos();
|
|
short_price += p.short_pos()*p.short_avg_price();
|
|
}
|
|
long_price = long_pos > 0 ? long_price/long_pos : 0.0;
|
|
short_price = short_pos > 0 ? short_price/short_pos : 0.0;
|
|
bts::api::position::PositionEntry rv;
|
|
rv.set_long_pos(long_pos);
|
|
rv.set_long_avg_price(long_price);
|
|
rv.set_short_pos(short_pos);
|
|
rv.set_short_avg_price(short_price);
|
|
return rv;
|
|
}
|
|
|
|
// copies from the new PositionEntry fields
|
|
// to the doubles (this is a temporary function)
|
|
void PositionUtilities::backwards_compat(PositionUnit& p){
|
|
std::vector<bts::api::position::PositionEntry const*> v;
|
|
// day
|
|
v.push_back(&p.day());
|
|
v.push_back(&p.admin_day());
|
|
v.push_back(&p.local());
|
|
bts::api::position::PositionEntry day = add_positions(v);
|
|
p.set_day_long_pos(day.long_pos());
|
|
p.set_day_long_avg_price(day.long_avg_price());
|
|
p.set_day_short_pos(day.short_pos());
|
|
p.set_day_short_avg_price(day.short_avg_price());
|
|
// sod
|
|
v.clear();
|
|
v.push_back(&p.sod());
|
|
v.push_back(&p.admin_sod());
|
|
bts::api::position::PositionEntry sod = add_positions(v);
|
|
p.set_sod_long_pos(sod.long_pos());
|
|
p.set_sod_long_avg_price(sod.long_avg_price());
|
|
p.set_sod_short_pos(sod.short_pos());
|
|
p.set_sod_short_avg_price(sod.short_avg_price());
|
|
}
|
|
|
|
double PositionUtilities::compute_pnl_entry (const PositionEntry& p,
|
|
double cpp,
|
|
double md){
|
|
return std::abs(p.long_pos()) * cpp *
|
|
(md - p.long_avg_price()) +
|
|
std::abs(p.short_pos()) * cpp *
|
|
(p.short_avg_price() - md);
|
|
}
|
|
|
|
}} |