diff --git a/externals b/externals index 6b8c745fb..67a79263b 160000 --- a/externals +++ b/externals @@ -1 +1 @@ -Subproject commit 6b8c745fb2725ad92fc0292b7836e0838e7e1679 +Subproject commit 67a79263ba116a7646beb8368cef015530bea027 diff --git a/src/lib/orderman/actors/SODPosition.h b/src/lib/orderman/actors/SODPosition.h index 270fd5150..e515d598c 100644 --- a/src/lib/orderman/actors/SODPosition.h +++ b/src/lib/orderman/actors/SODPosition.h @@ -20,7 +20,7 @@ class SODPosition{ private: std::map _sod_map; const bool _enabled; - const timeval _sod_time; + const timeval _sod_time; private: // converts today:hh:mm:ss to timeval static timeval _to_timeval(uint16_t hh, uint16_t mm, uint16_t ss); diff --git a/src/server/sparkd/position/PositionMap.cpp b/src/server/sparkd/position/PositionMap.cpp new file mode 100644 index 000000000..8d158e5af --- /dev/null +++ b/src/server/sparkd/position/PositionMap.cpp @@ -0,0 +1,156 @@ +#include "PositionMap.h" +#include "lib/config/config.h" +#include "lib/config/protos/toggles.pb.h" +#include "lib/logging/logging.h" +#include "lib/utils/PBUtils.h" +#include "lib/utils/bts_assert.h" +#include "lib/api/service_helpers/PositionUtilities.h" +#include "lib/orderman/actors/SODPosition.h" +#include +#include +#include + +namespace bts { +namespace orderman { + +using namespace bts::position; +using namespace bts::api::orderman; +using namespace bts::api::position; + +PositionMap::PositionMap() +: +_pos_id(0){ +} + +const PositionUnit& PositionMap::on_fill (const Fill& f, + bool is_add){ + PositionUnit &pu = _get_or_add(f.internal_account_id(), + f.security_key()); + if(!is_add) + _fill_map.erase(f.fill_key()); + else + _fill_map[f.fill_key()] = f; + auto fill_type = _fill_type(f); + switch (fill_type){ + case FillType::BASE: + break; + case FillType::SOD: + PositionUtilities::apply_fill(*(pu.mutable_sod()), + f.side(), + f.last_price(), + f.last_shares(), + f.fill_key(), + is_add); + break; + case FillType::DAY: + PositionUtilities::apply_fill(*(pu.mutable_day()), + f.side(), + f.last_price(), + f.last_shares(), + f.fill_key(), + is_add); + break; + } + PositionUtilities::backwards_compat(pu); + return pu; + } + + const PositionUnit& PositionMap::on_manual_fill ( + const ManualFill& mf, + bool is_add){ + PositionUnit &pu =_get_or_add(mf.account_id(), + mf.security_key()); + 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; + default: + break; + } + PositionUtilities::backwards_compat(pu); + return pu; +} + +void PositionMap::set_base_position(const BasePositionBatch& request){ + _base_pos_map.clear(); + for (const auto& bp : request.positions()){ + BasePositionEntry& e = _base_pos_map[bp.account_id()][bp.security_id()]; + PositionUtilities::apply_fill(e.pos, + bp.side(), + bp.avg_price(), + bp.quantity(), + "", + true); + } +} + +PositionUnit& PositionMap::_get_or_add(uint32_t account_id, uint32_t seckey){ + auto &pu = _pos_map[account_id][seckey]; + if (pu.record_id() == 0) { + // new PU + pu.set_record_id(++_pos_id); + pu.set_security_id(seckey); + pu.set_account_id(account_id); + // zeroing out all pos's + pu.mutable_day(); + pu.mutable_sod(); + pu.mutable_local(); + pu.mutable_admin_day(); + pu.mutable_admin_sod(); + } + return pu; +} + +PositionMap::FillType PositionMap::_fill_type(const Fill& f) const{ + // is this right, what if there is no entry in base pos ? + PositionMap::BasePositionEntry const* bp = + _get_base_position(f.internal_account_id(),f.security_key()); + if(bp != nullptr && f.timestamp().seconds() < bp->current_as_of.tv_sec) + return FillType::BASE; + else{ + bts::orderman::SODPosition sod_pos; + return sod_pos.is_sod(f.timestamp()) ? FillType::SOD : FillType::DAY; + } +} + +void PositionMap::get_pos (std::vector in_pos){ + for (auto &acct_p : _pos_map) { + for (auto &sec_p : acct_p.second) { + const PositionUnit &pu = sec_p.second; + in_pos.push_back(&pu); + } + } +} + +void PositionMap::_reset(){ + _pos_map.clear(); + for (auto& acct_p : _base_pos_map) { + uint32_t account_id = acct_p.first; + for (auto& sec_p : acct_p.second) { + uint32_t sec_key = sec_p.first; + const BasePositionEntry& bpe = sec_p.second; + PositionUnit& pu = _get_or_add(account_id, sec_key); + pu.mutable_sod()->CopyFrom(bpe.pos); + } + } + for (auto& fill_p : _fill_map) + on_fill(fill_p.second,true); + // TODO manual fills +} + +PositionMap::BasePositionEntry const* PositionMap::_get_base_position ( + uint32_t account_id, + uint32_t sec_key) const{ + auto acct_it = _base_pos_map.find(account_id); + if(acct_it != _base_pos_map.end()){ + auto sec_it = acct_it->second.find(sec_key); + if (sec_it != acct_it->second.end()) + return &(sec_it->second); + } + return nullptr; +} + +}} \ No newline at end of file diff --git a/src/server/sparkd/position/PositionMap.h b/src/server/sparkd/position/PositionMap.h new file mode 100644 index 000000000..9556c1552 --- /dev/null +++ b/src/server/sparkd/position/PositionMap.h @@ -0,0 +1,51 @@ + +#pragma once + +#include "lib/api/services/OrderService.pb.h" +#include "lib/api/services/PositionService.server.pb.h" +#include "lib/api/services/OrderService.client.pb.h" +#include +#include + +using namespace bts::api::orderman; + +namespace google { namespace protobuf { class timestamp;}} + +namespace bts{ +namespace orderman{ + +class PositionMap{ +private: + enum FillType{ + BASE, + SOD, + DAY + }; + struct BasePositionEntry{ + timeval current_as_of; + api::position::PositionEntry pos; + }; + int _pos_id; + std::map _fill_map; + std::map> _pos_map; + std::map> _base_pos_map; +private: + FillType _fill_type(const bts::api::orderman::Fill &fill) const; + bts::api::position::PositionUnit& _get_or_add(uint32_t account_id, + uint32_t seckey); + void _reset(); + PositionMap::BasePositionEntry const* _get_base_position (uint32_t account_id, + uint32_t sec_key) const; +public: + PositionMap(); + const bts::api::position::PositionUnit& on_manual_fill ( + const bts::api::position::ManualFill& mf, + bool is_add); + const bts::api::position::PositionUnit& on_fill ( + const bts::api::orderman::Fill& f, + bool is_add); + void set_base_position(const bts::api::position::BasePositionBatch& request); + void get_pos (std::vector in_pos); +}; + +}} diff --git a/src/server/sparkd/position/PositionServiceServer.cpp b/src/server/sparkd/position/PositionServiceServer.cpp index 6fc49f4d3..e5fd8fd17 100644 --- a/src/server/sparkd/position/PositionServiceServer.cpp +++ b/src/server/sparkd/position/PositionServiceServer.cpp @@ -12,6 +12,7 @@ #include "lib/persistence/SqliteStore.h" #include "lib/utils/PBUtils.h" #include "lib/utils/Puid.h" +#include "PositionMap.h" #include #include @@ -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> pos_map; int id; // manual fills management std::map manual_fills; @@ -61,7 +61,7 @@ struct PositionServiceServer::PImpl { : // pos context(context), order_service(context.locate()), - 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( - [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( + [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( @@ -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 &mfs) -> std::pair { - 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(rv, has_sod); - }; - bts::orderman::SODPosition sod_pos; - const auto &all = me->manual_fills_store->retrieve_all()[manual_fills_ns]; - std::map> sod_map; - std::vector day_mfs; - for (const auto &p : all) { - const auto &key = p.first; - ManualFill mf; - if (utils::pb::from_string(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 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&) { + me->state.set_base_position(request); +} + +void _recompute_all(){ + } /////////////////////////////////////////////////////////////////////////////// /// PNL helpers /////////////////////////////////////////////////////////////// void PositionServiceServer::_compute_pnl_and_publish() { std::vector 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 pnl; std::vector account_pnl; me->pnl_helper.compute_pnl(in_pos, me->account_map, pnl, account_pnl); diff --git a/src/server/sparkd/position/PositionServiceServer.h b/src/server/sparkd/position/PositionServiceServer.h index e42d016ce..ab48fe0ec 100644 --- a/src/server/sparkd/position/PositionServiceServer.h +++ b/src/server/sparkd/position/PositionServiceServer.h @@ -35,32 +35,28 @@ class PositionServiceServer : public bts::api::position::PositionServiceBase { private: struct PImpl; std::unique_ptr me; - - private: - // at startup, the admin day fills with < sod time - // are moved to SOD. this mosdifies the DB, and - // then the rest of the code takes over without - // knowing about this DB work - void _apply_sod_to_admin_fills(); + enum FillType{ + BASE, + SOD, + DAY + }; + struct BasePositionEntry{ + timeval current_as_of; + api::position::PositionEntry pos; + }; // this is the function that aggrgates the // position. is_add is to indicate if a row // is added or removed const bts::api::position::PositionUnit & _on_fill(const bts::api::orderman::Fill &fill, bool is_add); const bts::api::position::PositionUnit & - _on_manual_fill(const api::position::ManualFill &fill, bool is_add); - // given an account and key, finds (or adds if absent) - // the PositionUnit entry - bts::api::position::PositionUnit & - _get_or_add(const bts::api::orderman::Fill &fill); - bts::api::position::PositionUnit & - _get_or_add(uint32_t account_id, uint32_t seckey, const std::string &); + _on_manual_fill(const api::position::ManualFill &fill, bool is_add); void _periodic_poll(); void _compute_pnl_and_publish(); - static std::string _fill_account(const bts::api::orderman::Fill &fill); - static bool _valid_account_id(const bts::api::orderman::Fill &fill); void _set_admin_data(const api::admin::AdminData &admin_data); - + //FillType _fill_type(const bts::api::orderman::Fill &fill) const; + // this is when we have to reset the + void _recompute_all(); public: PositionServiceServer(bts::api::ServiceContext &context, md::MDClient &);