Moving PositionServiceServer data into PositionMap (the Service Server was getting too long and confusing)
This commit is contained in:
1 parent
5cb1220d05
commit
72734bb15f
6 files changed
+252
-186
No files matched your search
@@ -12,6 +12,7 @@
|
||||
#include "lib/persistence/SqliteStore.h"
|
||||
#include "lib/utils/PBUtils.h"
|
||||
#include "lib/utils/Puid.h"
|
||||
#include "PositionMap.h"
|
||||
|
||||
#include <boost/algorithm/string.hpp>
|
||||
#include <boost/tokenizer.hpp>
|
||||
@@ -24,16 +25,15 @@ namespace bts {
|
||||
namespace position {
|
||||
|
||||
using namespace bts::api::position;
|
||||
using namespace bts::api::orderman;
|
||||
using namespace bts::md;
|
||||
|
||||
static auto const manual_fills_ns = "manual_fills";
|
||||
|
||||
struct PositionServiceServer::PImpl {
|
||||
bts::orderman::PositionMap state;
|
||||
bts::api::ServiceContext &context;
|
||||
bts::api::orderman::OrderService order_service;
|
||||
bts::api::orderman::OrderService order_service;
|
||||
PositionServiceBase::Position_dataStatefulPublisher pos_publisher;
|
||||
std::map<uint32_t, std::map<uint32_t, PositionUnit>> pos_map;
|
||||
int id;
|
||||
// manual fills management
|
||||
std::map<uint32_t, ManualFill> manual_fills;
|
||||
@@ -61,7 +61,7 @@ struct PositionServiceServer::PImpl {
|
||||
: // pos
|
||||
context(context),
|
||||
order_service(context.locate<bts::api::orderman::OrderService>()),
|
||||
pos_publisher(pos_publisher), pos_map(), id(0),
|
||||
pos_publisher(pos_publisher),
|
||||
// manual fills
|
||||
manual_fills(),
|
||||
manual_fills_store(
|
||||
@@ -83,27 +83,22 @@ PositionServiceServer::PositionServiceServer(bts::api::ServiceContext &context,
|
||||
make_pnl_data_stateful_publisher(),
|
||||
make_pnl_account_data_stateful_publisher(),
|
||||
make_manual_fills_stateful_publisher(), md_client)) {
|
||||
//_apply_sod_to_admin_fills();
|
||||
// incremental fill update
|
||||
me->order_service.subscribe_fills() |
|
||||
rxcpp::operators::subscribe<api::orderman::FillsUpdate>(
|
||||
[this](auto const &update) {
|
||||
for (const auto &f : update.removed())
|
||||
if (_valid_account_id(*f))
|
||||
me->pos_publisher.add_or_update(_on_fill(*f, false));
|
||||
for (const auto &f : update.added())
|
||||
if (_valid_account_id(*f))
|
||||
me->pos_publisher.add_or_update(_on_fill(*f, true));
|
||||
for (const auto &f : update.changed()) {
|
||||
if (_valid_account_id(*f.previous()))
|
||||
me->pos_publisher.add_or_update(
|
||||
_on_fill(*f.previous(), false));
|
||||
if (_valid_account_id(*f.current()))
|
||||
me->pos_publisher.add_or_update(
|
||||
_on_fill(*f.current(), true));
|
||||
}
|
||||
me->pos_publisher.flush();
|
||||
_compute_pnl_and_publish();
|
||||
});
|
||||
rxcpp::operators::subscribe<api::orderman::FillsUpdate>(
|
||||
[this](auto const &update) {
|
||||
for (const auto &f : update.removed())
|
||||
me->pos_publisher.add_or_update(_on_fill(*f, false));
|
||||
for (const auto &f : update.added())
|
||||
me->pos_publisher.add_or_update(_on_fill(*f, true));
|
||||
for (const auto &f : update.changed()) {
|
||||
me->pos_publisher.add_or_update(_on_fill(*f.previous(), false));
|
||||
me->pos_publisher.add_or_update(_on_fill(*f.current(), true));
|
||||
}
|
||||
me->pos_publisher.flush();
|
||||
_compute_pnl_and_publish();
|
||||
}
|
||||
);
|
||||
// admin data
|
||||
me->admin_service.subscribe_admin_data() |
|
||||
rxcpp::operators::subscribe<api::admin::Admin_dataUpdate>(
|
||||
@@ -135,148 +130,16 @@ PositionServiceServer::PositionServiceServer(bts::api::ServiceContext &context,
|
||||
me->context.post([this]() { _periodic_poll(); }, me->poll_period_ms);
|
||||
}
|
||||
|
||||
void PositionServiceServer::_apply_sod_to_admin_fills() {
|
||||
auto mf_key = [&](const ManualFill &mf) -> std::string {
|
||||
return std::to_string(mf.account_id()) +
|
||||
std::to_string(mf.security_key()) + std::to_string(mf.side());
|
||||
};
|
||||
auto agg_sod =
|
||||
[&](const std::vector<ManualFill> &mfs) -> std::pair<ManualFill, bool> {
|
||||
bts_assert(mfs.size() > 0);
|
||||
ManualFill rv;
|
||||
rv.set_record_id(0);
|
||||
bool has_sod = false;
|
||||
for (const auto &f : mfs) {
|
||||
has_sod = has_sod || f.fill_type() == ManualFill::ADMIN_SOD;
|
||||
if (rv.record_id() == 0)
|
||||
rv = f;
|
||||
else {
|
||||
uint32_t agg_qty = rv.quantity() + f.quantity();
|
||||
double avg_price =
|
||||
(rv.price() * rv.quantity() + f.price() * f.quantity()) /
|
||||
agg_qty;
|
||||
rv.set_quantity(agg_qty);
|
||||
rv.set_price(avg_price);
|
||||
}
|
||||
rv.set_fill_type(ManualFill::ADMIN_SOD);
|
||||
}
|
||||
return std::pair<ManualFill, bool>(rv, has_sod);
|
||||
};
|
||||
bts::orderman::SODPosition sod_pos;
|
||||
const auto &all = me->manual_fills_store->retrieve_all()[manual_fills_ns];
|
||||
std::map<std::string, std::vector<ManualFill>> sod_map;
|
||||
std::vector<ManualFill> day_mfs;
|
||||
for (const auto &p : all) {
|
||||
const auto &key = p.first;
|
||||
ManualFill mf;
|
||||
if (utils::pb::from_string<ManualFill>(p.second, &mf)) {
|
||||
if (mf.fill_type() == ManualFill::ADMIN_SOD) {
|
||||
auto &sod_mfs = sod_map[mf_key(mf)];
|
||||
sod_mfs.push_back(mf);
|
||||
} else
|
||||
day_mfs.push_back(mf);
|
||||
} else
|
||||
LOG(OrderMan, Error,
|
||||
"Could not recover manual fill {} from storage. "
|
||||
"This should be investigated",
|
||||
key);
|
||||
}
|
||||
// now go through the day fills and add them
|
||||
// to SOD if they are before the SOD cut off
|
||||
// (at usual system start all of them should)
|
||||
std::set<std::string> soded_day_mfs;
|
||||
for (const auto &d_mf : day_mfs) {
|
||||
bool is_sod = sod_pos.is_sod(d_mf.timestamp());
|
||||
if (is_sod) {
|
||||
auto &sod_mfs = sod_map[mf_key(d_mf)];
|
||||
sod_mfs.push_back(d_mf);
|
||||
// delete mf
|
||||
soded_day_mfs.insert(std::to_string(d_mf.record_id()));
|
||||
}
|
||||
}
|
||||
// remove all soded mfs
|
||||
me->manual_fills_store->remove(manual_fills_ns, soded_day_mfs);
|
||||
// now add or update sod records
|
||||
for (const auto &p : sod_map) {
|
||||
auto res = agg_sod(p.second);
|
||||
if (res.second) {
|
||||
// has sod, so update it
|
||||
} else {
|
||||
// new sod record, add it
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const PositionUnit &PositionServiceServer::_on_fill(const Fill &f,
|
||||
bool is_add) {
|
||||
PositionUnit &pu = _get_or_add(f);
|
||||
if (f.fill_kind() == bts::api::orderman::Fill::FILL_KIND_SOD)
|
||||
PositionUtilities::apply_fill(*(pu.mutable_sod()), f.side(),
|
||||
f.last_price(), f.last_shares(),
|
||||
f.fill_key(), is_add);
|
||||
else
|
||||
PositionUtilities::apply_fill(*(pu.mutable_day()), f.side(),
|
||||
f.last_price(), f.last_shares(),
|
||||
f.fill_key(), is_add);
|
||||
PositionUtilities::backwards_compat(pu);
|
||||
return pu;
|
||||
return me->state.on_fill(f,is_add);
|
||||
}
|
||||
|
||||
const PositionUnit &PositionServiceServer::_on_manual_fill(const ManualFill &mf,
|
||||
bool is_add) {
|
||||
PositionUnit &pu =
|
||||
_get_or_add(mf.account_id(), mf.security_key(), mf.account_name());
|
||||
std::string fill_id =
|
||||
std::string("manual:") + std::to_string(mf.record_id());
|
||||
switch (mf.fill_type()) {
|
||||
case ManualFill::LOCAL:
|
||||
PositionUtilities::apply_fill(*(pu.mutable_local()), mf.side(),
|
||||
mf.price(), mf.quantity(), fill_id, is_add);
|
||||
break;
|
||||
case ManualFill::ADMIN_SOD:
|
||||
PositionUtilities::apply_fill(*(pu.mutable_admin_sod()), mf.side(),
|
||||
mf.price(), mf.quantity(), fill_id, is_add);
|
||||
break;
|
||||
case ManualFill::ADMIN_DAY:
|
||||
PositionUtilities::apply_fill(*(pu.mutable_admin_day()), mf.side(),
|
||||
mf.price(), mf.quantity(), fill_id, is_add);
|
||||
break;
|
||||
case ManualFill_FillType_ManualFill_FillType_INT_MIN_SENTINEL_DO_NOT_USE_:
|
||||
case ManualFill_FillType_ManualFill_FillType_INT_MAX_SENTINEL_DO_NOT_USE_:
|
||||
break;
|
||||
}
|
||||
PositionUtilities::backwards_compat(pu);
|
||||
return pu;
|
||||
}
|
||||
PositionUnit &PositionServiceServer::_get_or_add(const Fill &f) {
|
||||
return _get_or_add(f.internal_account_id(), f.security_key(), f.account());
|
||||
return me->state.on_manual_fill(mf,is_add);
|
||||
}
|
||||
|
||||
PositionUnit &PositionServiceServer::_get_or_add(uint32_t account_id,
|
||||
uint32_t seckey,
|
||||
const std::string &account) {
|
||||
auto &pu = me->pos_map[account_id][seckey];
|
||||
if (pu.record_id() == 0) {
|
||||
// new PU
|
||||
pu.set_record_id(++me->id);
|
||||
pu.set_security_id(seckey);
|
||||
pu.set_account_id(account_id);
|
||||
pu.set_account(account);
|
||||
// zeroing out all pos's
|
||||
pu.mutable_day();
|
||||
pu.mutable_sod();
|
||||
pu.mutable_local();
|
||||
pu.mutable_admin_day();
|
||||
pu.mutable_admin_sod();
|
||||
}
|
||||
return pu;
|
||||
}
|
||||
|
||||
std::string PositionServiceServer::_fill_account(const Fill &fill) {
|
||||
return boost::trim_right_copy(fill.account());
|
||||
}
|
||||
bool PositionServiceServer::_valid_account_id(const Fill &fill) {
|
||||
return bts::admin::AdminServiceUtils::has_id(fill.internal_account_id());
|
||||
}
|
||||
void PositionServiceServer::_set_admin_data(
|
||||
const api::admin::AdminData &admin_data) {
|
||||
me->admin_data = admin_data;
|
||||
@@ -378,20 +241,20 @@ void PositionServiceServer::remove_manual_fill(
|
||||
}
|
||||
|
||||
void PositionServiceServer::set_base_position(
|
||||
const ::bts::api::position::BasePositionBatch &,
|
||||
const api::Response<::bts::api::position::PositionServiceResponse> &) {
|
||||
const BasePositionBatch& request,
|
||||
const api::Response<PositionServiceResponse>&) {
|
||||
me->state.set_base_position(request);
|
||||
}
|
||||
|
||||
void _recompute_all(){
|
||||
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
/// PNL helpers ///////////////////////////////////////////////////////////////
|
||||
void PositionServiceServer::_compute_pnl_and_publish() {
|
||||
std::vector<const PositionUnit *> in_pos;
|
||||
for (auto &acct_p : me->pos_map) {
|
||||
for (auto &sec_p : acct_p.second) {
|
||||
const PositionUnit &pu = sec_p.second;
|
||||
in_pos.push_back(&pu);
|
||||
}
|
||||
}
|
||||
me->state.get_pos(in_pos);
|
||||
std::vector<PNLUnit> pnl;
|
||||
std::vector<AccountPNL> account_pnl;
|
||||
me->pnl_helper.compute_pnl(in_pos, me->account_map, pnl, account_pnl);
|
||||
|
||||
Reference in new issue
Block a user