Added stubs for base position service.
This commit is contained in:
1 parent
37ba9f7b0d
commit
5cb1220d05
9 files changed
+333
-233
No files matched your search
@@ -1,17 +1,17 @@
|
||||
#include "PositionServiceServer.h"
|
||||
#include "lib/api/services/OrderService.pb.h"
|
||||
#include "lib/api/services/OrderService.client.pb.h"
|
||||
#include "lib/api/services/AdminService.pb.h"
|
||||
#include "lib/api/services/AdminService.client.pb.h"
|
||||
#include "lib/api/public/ServiceContext.h"
|
||||
#include "lib/logging/logging.h"
|
||||
#include "lib/utils/PBUtils.h"
|
||||
#include "lib/persistence/SqliteStore.h"
|
||||
#include "lib/utils/Puid.h"
|
||||
#include "PositionServicePNLHelper.h"
|
||||
#include "lib/api/service_helpers/PositionUtilities.h"
|
||||
#include "lib/api/public/ServiceContext.h"
|
||||
#include "lib/api/service_helpers/AdminServiceUtils.h"
|
||||
#include "lib/api/service_helpers/PositionUtilities.h"
|
||||
#include "lib/api/services/AdminService.client.pb.h"
|
||||
#include "lib/api/services/AdminService.pb.h"
|
||||
#include "lib/api/services/OrderService.client.pb.h"
|
||||
#include "lib/api/services/OrderService.pb.h"
|
||||
#include "lib/logging/logging.h"
|
||||
#include "lib/orderman/actors/SODPosition.h"
|
||||
#include "lib/persistence/SqliteStore.h"
|
||||
#include "lib/utils/PBUtils.h"
|
||||
#include "lib/utils/Puid.h"
|
||||
|
||||
#include <boost/algorithm/string.hpp>
|
||||
#include <boost/tokenizer.hpp>
|
||||
@@ -21,7 +21,7 @@
|
||||
#include <utility>
|
||||
|
||||
namespace bts {
|
||||
namespace position{
|
||||
namespace position {
|
||||
|
||||
using namespace bts::api::position;
|
||||
using namespace bts::api::orderman;
|
||||
@@ -30,14 +30,14 @@ using namespace bts::md;
|
||||
static auto const manual_fills_ns = "manual_fills";
|
||||
|
||||
struct PositionServiceServer::PImpl {
|
||||
bts::api::ServiceContext& context;
|
||||
bts::api::ServiceContext &context;
|
||||
bts::api::orderman::OrderService order_service;
|
||||
PositionServiceBase::Position_dataStatefulPublisher pos_publisher;
|
||||
std::map<uint32_t, std::map<uint32_t, PositionUnit>> pos_map;
|
||||
int id;
|
||||
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;
|
||||
std::unique_ptr<persistence::IStore> manual_fills_store ;
|
||||
std::unique_ptr<persistence::IStore> manual_fills_store;
|
||||
PositionServiceBase::Manual_fillsStatefulPublisher manual_fills_publisher;
|
||||
// pnl
|
||||
PositionServiceBase::Pnl_dataStatefulPublisher pnl_publisher;
|
||||
@@ -47,78 +47,75 @@ struct PositionServiceServer::PImpl {
|
||||
// admin data needed for pnl
|
||||
bts::api::admin::AdminService admin_service;
|
||||
api::admin::AdminData admin_data;
|
||||
std::map<std::string,uint32_t> account_map;
|
||||
std::map<std::string, uint32_t> account_map;
|
||||
|
||||
PImpl(bts::api::ServiceContext& context,
|
||||
const PositionServiceBase::Position_dataStatefulPublisher& pos_publisher,
|
||||
const PositionServiceBase::Pnl_dataStatefulPublisher& pnl_publisher,
|
||||
const PositionServiceBase::Pnl_account_dataStatefulPublisher& pnl_account_publisher,
|
||||
const PositionServiceBase::Manual_fillsStatefulPublisher &manual_fills_publisher,
|
||||
bts::md::MDClient& md_client)
|
||||
:
|
||||
// pos
|
||||
context(context),
|
||||
order_service(context.locate<bts::api::orderman::OrderService>()),
|
||||
pos_publisher(pos_publisher),
|
||||
pos_map(),
|
||||
id(0),
|
||||
// manual fills
|
||||
manual_fills(),
|
||||
manual_fills_store(std::make_unique<persistence::SqliteStore> ("manual_fills")),
|
||||
manual_fills_publisher(manual_fills_publisher),
|
||||
// pnl
|
||||
pnl_publisher(pnl_publisher),
|
||||
pnl_account_publisher(pnl_account_publisher),
|
||||
pnl_helper(md_client),
|
||||
poll_period_ms(300),
|
||||
// admin
|
||||
admin_service(context.locate<bts::api::admin::AdminService>()),
|
||||
admin_data(),
|
||||
account_map()
|
||||
{
|
||||
}
|
||||
PImpl(
|
||||
bts::api::ServiceContext &context,
|
||||
const PositionServiceBase::Position_dataStatefulPublisher &pos_publisher,
|
||||
const PositionServiceBase::Pnl_dataStatefulPublisher &pnl_publisher,
|
||||
const PositionServiceBase::Pnl_account_dataStatefulPublisher
|
||||
&pnl_account_publisher,
|
||||
const PositionServiceBase::Manual_fillsStatefulPublisher
|
||||
&manual_fills_publisher,
|
||||
bts::md::MDClient &md_client)
|
||||
: // pos
|
||||
context(context),
|
||||
order_service(context.locate<bts::api::orderman::OrderService>()),
|
||||
pos_publisher(pos_publisher), pos_map(), id(0),
|
||||
// manual fills
|
||||
manual_fills(),
|
||||
manual_fills_store(
|
||||
std::make_unique<persistence::SqliteStore>("manual_fills")),
|
||||
manual_fills_publisher(manual_fills_publisher),
|
||||
// pnl
|
||||
pnl_publisher(pnl_publisher),
|
||||
pnl_account_publisher(pnl_account_publisher), pnl_helper(md_client),
|
||||
poll_period_ms(300),
|
||||
// admin
|
||||
admin_service(context.locate<bts::api::admin::AdminService>()),
|
||||
admin_data(), account_map() {}
|
||||
};
|
||||
|
||||
PositionServiceServer::PositionServiceServer(bts::api::ServiceContext& context,
|
||||
bts::md::MDClient& md_client)
|
||||
:
|
||||
PositionServiceBase (context),
|
||||
me(new PImpl(context,
|
||||
make_position_data_stateful_publisher(),
|
||||
make_pnl_data_stateful_publisher(),
|
||||
make_pnl_account_data_stateful_publisher(),
|
||||
make_manual_fills_stateful_publisher(),
|
||||
md_client))
|
||||
{
|
||||
PositionServiceServer::PositionServiceServer(bts::api::ServiceContext &context,
|
||||
bts::md::MDClient &md_client)
|
||||
: PositionServiceBase(context),
|
||||
me(new PImpl(context, make_position_data_stateful_publisher(),
|
||||
make_pnl_data_stateful_publisher(),
|
||||
make_pnl_account_data_stateful_publisher(),
|
||||
make_manual_fills_stateful_publisher(), md_client)) {
|
||||
//_apply_sod_to_admin_fills();
|
||||
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));
|
||||
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();
|
||||
});
|
||||
// admin data
|
||||
me->admin_service.subscribe_admin_data() |
|
||||
rxcpp::operators::subscribe<api::admin::Admin_dataUpdate>(
|
||||
[this](auto const& update) {
|
||||
for (const auto& r : update.added())
|
||||
{_set_admin_data(*r);}
|
||||
for (const auto& r : update.changed())
|
||||
{_set_admin_data(*r.current());}
|
||||
rxcpp::operators::subscribe<api::admin::Admin_dataUpdate>(
|
||||
[this](auto const &update) {
|
||||
for (const auto &r : update.added()) {
|
||||
_set_admin_data(*r);
|
||||
}
|
||||
for (const auto &r : update.changed()) {
|
||||
_set_admin_data(*r.current());
|
||||
}
|
||||
});
|
||||
// manual fills
|
||||
// manual fills
|
||||
auto all = me->manual_fills_store->retrieve_all()[manual_fills_ns];
|
||||
LOG(persist, Info, "Finished loading all {} manual fills", all.size());
|
||||
ManualFill rec;
|
||||
@@ -126,9 +123,8 @@ me(new PImpl(context,
|
||||
if (utils::pb::from_string<ManualFill>(kvp.second, &rec)) {
|
||||
me->manual_fills.insert({rec.record_id(), rec});
|
||||
me->manual_fills_publisher.add_or_update(rec);
|
||||
me->pos_publisher.add_or_update(_on_manual_fill(rec,true));
|
||||
}
|
||||
else {
|
||||
me->pos_publisher.add_or_update(_on_manual_fill(rec, true));
|
||||
} else {
|
||||
LOG(OrderMan, Error,
|
||||
"Could not recover manual fill {} from storage. "
|
||||
"This should be investigated",
|
||||
@@ -136,175 +132,165 @@ me(new PImpl(context,
|
||||
}
|
||||
me->manual_fills_publisher.flush();
|
||||
me->pos_publisher.flush();
|
||||
me->context.post([this](){_periodic_poll();},me->poll_period_ms);
|
||||
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());
|
||||
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);
|
||||
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){
|
||||
for (const auto &f : mfs) {
|
||||
has_sod = has_sod || f.fill_type() == ManualFill::ADMIN_SOD;
|
||||
if(rv.record_id() == 0)
|
||||
if (rv.record_id() == 0)
|
||||
rv = f;
|
||||
else{
|
||||
else {
|
||||
uint32_t agg_qty = rv.quantity() + f.quantity();
|
||||
double avg_price =
|
||||
(rv.price()*rv.quantity() + f.price()*f.quantity())/agg_qty;
|
||||
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);
|
||||
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];
|
||||
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;
|
||||
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)];
|
||||
if (mf.fill_type() == ManualFill::ADMIN_SOD) {
|
||||
auto &sod_mfs = sod_map[mf_key(mf)];
|
||||
sod_mfs.push_back(mf);
|
||||
}
|
||||
else
|
||||
} else
|
||||
day_mfs.push_back(mf);
|
||||
}
|
||||
else
|
||||
} else
|
||||
LOG(OrderMan, Error,
|
||||
"Could not recover manual fill {} from storage. "
|
||||
"This should be investigated", key);
|
||||
"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){
|
||||
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)];
|
||||
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);
|
||||
me->manual_fills_store->remove(manual_fills_ns, soded_day_mfs);
|
||||
// now add or update sod records
|
||||
for (const auto& p : sod_map){
|
||||
for (const auto &p : sod_map) {
|
||||
auto res = agg_sod(p.second);
|
||||
if(res.second){
|
||||
if (res.second) {
|
||||
// has sod, so update it
|
||||
}
|
||||
else{
|
||||
} 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);
|
||||
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::apply_fill(*(pu.mutable_day()), f.side(),
|
||||
f.last_price(), f.last_shares(),
|
||||
f.fill_key(), is_add);
|
||||
PositionUtilities::backwards_compat(pu);
|
||||
return pu;
|
||||
}
|
||||
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;
|
||||
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());
|
||||
PositionUnit &PositionServiceServer::_get_or_add(const Fill &f) {
|
||||
return _get_or_add(f.internal_account_id(), f.security_key(), f.account());
|
||||
}
|
||||
|
||||
PositionUnit& PositionServiceServer::_get_or_add (uint32_t account_id,
|
||||
uint32_t seckey,
|
||||
const std::string& account){
|
||||
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();
|
||||
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){
|
||||
std::string PositionServiceServer::_fill_account(const Fill &fill) {
|
||||
return boost::trim_right_copy(fill.account());
|
||||
}
|
||||
bool PositionServiceServer::_valid_account_id (const Fill& fill){
|
||||
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){
|
||||
void PositionServiceServer::_set_admin_data(
|
||||
const api::admin::AdminData &admin_data) {
|
||||
me->admin_data = admin_data;
|
||||
for (const auto& a : admin_data.accounts())
|
||||
for (const auto &a : admin_data.accounts())
|
||||
me->account_map[a.name()] = a.id();
|
||||
}
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
/// Manual Fills API //////////////////////////////////////////////////////////
|
||||
void PositionServiceServer::create_manual_fill(
|
||||
const ManualFillBatch &batch,
|
||||
const api::Response<PositionServiceResponse> &r)
|
||||
{
|
||||
const ManualFillBatch &batch,
|
||||
const api::Response<PositionServiceResponse> &r) {
|
||||
PositionServiceResponse resp;
|
||||
for (auto mf : batch.manual_fill()) {
|
||||
auto id = utils::gen_id ("manual_fills");
|
||||
auto id = utils::gen_id("manual_fills");
|
||||
mf.set_record_id(id);
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv, NULL);
|
||||
@@ -312,12 +298,11 @@ void PositionServiceServer::create_manual_fill(
|
||||
mf.mutable_timestamp()->set_nanos(tv.tv_usec * 1000);
|
||||
|
||||
me->manual_fills.insert({mf.record_id(), mf});
|
||||
me->manual_fills_store->insert(manual_fills_ns,
|
||||
std::to_string(id),
|
||||
me->manual_fills_store->insert(manual_fills_ns, std::to_string(id),
|
||||
utils::pb::to_string(mf));
|
||||
resp.add_response()->set_ok(true);
|
||||
me->manual_fills_publisher.add_or_update(mf);
|
||||
me->pos_publisher.add_or_update(_on_manual_fill(mf,true));
|
||||
me->pos_publisher.add_or_update(_on_manual_fill(mf, true));
|
||||
}
|
||||
r(resp);
|
||||
me->manual_fills_publisher.flush();
|
||||
@@ -325,35 +310,32 @@ void PositionServiceServer::create_manual_fill(
|
||||
}
|
||||
|
||||
void PositionServiceServer::update_manual_fill(
|
||||
const ManualFillBatch &batch,
|
||||
const api::Response<PositionServiceResponse> &r)
|
||||
{
|
||||
const ManualFillBatch &batch,
|
||||
const api::Response<PositionServiceResponse> &r) {
|
||||
PositionServiceResponse resp;
|
||||
for (auto mf : batch.manual_fill()) {
|
||||
auto *this_resp = resp.add_response();
|
||||
auto id = std::to_string(mf.record_id());
|
||||
|
||||
auto p = me->manual_fills_store->retrieve_one(manual_fills_ns,id);
|
||||
auto p = me->manual_fills_store->retrieve_one(manual_fills_ns, id);
|
||||
auto found = p.first;
|
||||
if (!found) {
|
||||
this_resp->set_ok(false);
|
||||
this_resp->set_msg("Could not find record");
|
||||
}
|
||||
else {
|
||||
me->manual_fills_store->update(manual_fills_ns,
|
||||
id,
|
||||
} else {
|
||||
me->manual_fills_store->update(manual_fills_ns, id,
|
||||
utils::pb::to_string(mf));
|
||||
this_resp->set_ok(true);
|
||||
|
||||
// Retrieve old record so we can remove it from position
|
||||
auto it = me->manual_fills.find(mf.record_id());
|
||||
bts_assert(it!= me->manual_fills.end());
|
||||
bts_assert(it != me->manual_fills.end());
|
||||
auto &old = it->second;
|
||||
// remove old
|
||||
me->pos_publisher.add_or_update(_on_manual_fill(old,false));
|
||||
me->pos_publisher.add_or_update(_on_manual_fill(old, false));
|
||||
// add new
|
||||
me->manual_fills.insert({mf.record_id(), mf});
|
||||
me->pos_publisher.add_or_update(_on_manual_fill(mf,true));
|
||||
me->pos_publisher.add_or_update(_on_manual_fill(mf, true));
|
||||
me->manual_fills_publisher.add_or_update(mf);
|
||||
}
|
||||
}
|
||||
@@ -363,31 +345,29 @@ void PositionServiceServer::update_manual_fill(
|
||||
}
|
||||
|
||||
void PositionServiceServer::remove_manual_fill(
|
||||
const ManualFillBatch &batch,
|
||||
const api::Response<PositionServiceResponse> &r)
|
||||
{
|
||||
const ManualFillBatch &batch,
|
||||
const api::Response<PositionServiceResponse> &r) {
|
||||
PositionServiceResponse resp;
|
||||
for (auto mf : batch.manual_fill()) {
|
||||
auto *this_resp = resp.add_response();
|
||||
auto id = std::to_string(mf.record_id());
|
||||
|
||||
auto p = me->manual_fills_store->retrieve_one(manual_fills_ns,id);
|
||||
auto p = me->manual_fills_store->retrieve_one(manual_fills_ns, id);
|
||||
auto found = p.first;
|
||||
if (!found) {
|
||||
this_resp->set_ok(false);
|
||||
this_resp->set_msg("Could not find record");
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
// Retrieve old record so we can remove it from position
|
||||
auto it = me->manual_fills.find(mf.record_id());
|
||||
bts_assert(it != me->manual_fills.end());
|
||||
auto &old = it->second;
|
||||
me->pos_publisher.add_or_update(_on_manual_fill(old,false));
|
||||
me->pos_publisher.add_or_update(_on_manual_fill(old, false));
|
||||
me->manual_fills.erase(it);
|
||||
|
||||
std::set<std::string> to_r ;
|
||||
std::set<std::string> to_r;
|
||||
to_r.insert(id);
|
||||
me->manual_fills_store->remove(manual_fills_ns,to_r);
|
||||
me->manual_fills_store->remove(manual_fills_ns, to_r);
|
||||
this_resp->set_ok(true);
|
||||
me->manual_fills_publisher.erase(mf.record_id());
|
||||
}
|
||||
@@ -397,32 +377,37 @@ void PositionServiceServer::remove_manual_fill(
|
||||
me->manual_fills_publisher.flush();
|
||||
}
|
||||
|
||||
void PositionServiceServer::set_base_position(
|
||||
const ::bts::api::position::BasePositionBatch &,
|
||||
const api::Response<::bts::api::position::PositionServiceResponse> &) {
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
/// 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;
|
||||
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);
|
||||
}
|
||||
}
|
||||
std::vector<PNLUnit> pnl;
|
||||
std::vector<AccountPNL> account_pnl;
|
||||
me->pnl_helper.compute_pnl(in_pos,me->account_map,pnl,account_pnl);
|
||||
for (const auto& u : pnl)
|
||||
me->pnl_helper.compute_pnl(in_pos, me->account_map, pnl, account_pnl);
|
||||
for (const auto &u : pnl)
|
||||
me->pnl_publisher.add_or_update(u);
|
||||
for (const auto& u : account_pnl)
|
||||
for (const auto &u : account_pnl)
|
||||
me->pnl_account_publisher.add_or_update(u);
|
||||
me->pnl_publisher.flush();
|
||||
me->pnl_account_publisher.flush();
|
||||
}
|
||||
|
||||
void PositionServiceServer::_periodic_poll(){
|
||||
void PositionServiceServer::_periodic_poll() {
|
||||
_compute_pnl_and_publish();
|
||||
me->context.post([this](){_periodic_poll();},me->poll_period_ms);
|
||||
me->context.post([this]() { _periodic_poll(); }, me->poll_period_ms);
|
||||
}
|
||||
|
||||
PositionServiceServer::~PositionServiceServer(){
|
||||
}
|
||||
}}
|
||||
PositionServiceServer::~PositionServiceServer() {}
|
||||
} // namespace position
|
||||
} // namespace bts
|
||||
Reference in new issue
Block a user