From ddaf029cc7b4ffbcbd79423df01bf9c156dff661 Mon Sep 17 00:00:00 2001 From: Taha Afzal Date: Wed, 14 Aug 2019 21:45:24 -0400 Subject: [PATCH] Cleaned up the SODPosition file and added scaffolding for tests --- src/lib/orderman/actors/SODPosition.cpp | 99 ++----------------- src/lib/orderman/actors/SODPosition.h | 7 +- src/server/sparkd/position/PositionMap.cpp | 17 +++- src/server/sparkd/position/PositionMap.h | 3 + .../sparkd/position/tests/PositionTests.cpp | 64 +++--------- 5 files changed, 40 insertions(+), 150 deletions(-) diff --git a/src/lib/orderman/actors/SODPosition.cpp b/src/lib/orderman/actors/SODPosition.cpp index 196c7e2f2..a258da53f 100644 --- a/src/lib/orderman/actors/SODPosition.cpp +++ b/src/lib/orderman/actors/SODPosition.cpp @@ -14,10 +14,14 @@ namespace bts { namespace orderman { SODPosition::SODPosition() - : _enabled(config::get().enabled()), - _sod_time(_get_fills_sod_time()) { - LOG(persist, Info, "SODPosition enabled:{} sec:{}", _enabled, - _sod_time.tv_sec); +: +SODPosition(_get_fills_sod_time()) { +} + +SODPosition::SODPosition(timeval sod_time) +: +_sod_time(sod_time){ + LOG(persist, Info, "SODPosition::cons sod_time:{}", sod_time.tv_sec); } // returns the timeval for today's hh,mm,ss @@ -117,93 +121,8 @@ std::string SODPosition::_sod_fill_id(const Fill &fill, api::Side side) { return oss.str(); } -void SODPosition::_merge_pos(Fill &pos, const Fill &fill) { - bts_assert(pos.security_key() == fill.security_key()); - bts_assert(pos.account() == fill.account()); - bts_assert(pos.side() == fill.side()); - double total_qty = pos.last_shares() + fill.last_shares(); - double avg_price = (pos.avg_price() * pos.last_shares() + - fill.last_price() * fill.last_shares()) / - total_qty; - pos.set_avg_price(avg_price); - pos.set_last_price(avg_price); - pos.set_last_shares(total_qty); -} - -void SODPosition::_zero_pos(Fill &pos, const Fill &fill, api::Side side) { - pos.set_security_key(fill.security_key()); - pos.set_account(fill.account()); - pos.set_internal_account_id(fill.internal_account_id()); - pos.set_side(side); - pos.mutable_timestamp()->set_seconds(_sod_time.tv_sec); - pos.mutable_timestamp()->set_nanos(0); - pos.set_fill_kind(Fill::FILL_KIND_SOD); - pos.set_order_key(0); - pos.set_fill_key(_sod_fill_id(fill, side)); - pos.set_last_shares(0); -} - -bool SODPosition::add_fill_to_sod_pos(const Fill &fill) { - if (_enabled && fill.timestamp().seconds() < _sod_time.tv_sec) { - if (!bts::admin::AdminServiceUtils::has_id(fill.internal_account_id())){ - LOG(persist, Error, "SODPosition::add_fill_to_sod_pos Could not find " - "account_id for fill:{} account:{}", - fill.fill_key(), fill.account()); - return false; - } - uint32_t seckey = fill.security_key(), - account_id = fill.internal_account_id(); - auto acct_it = _sod_map.find(account_id); - if (acct_it == _sod_map.end()) - _sod_map[account_id] = SecPosMap(); - acct_it = _sod_map.find(account_id); - bts_assert(acct_it != _sod_map.end()); - SecPosMap &sec_map = acct_it->second; - auto sec_it = sec_map.find(seckey); - if (sec_it == sec_map.end()) { - Fill long_pos, short_pos; - _zero_pos(long_pos, fill, api::Side::SIDE_BUY); - _zero_pos(short_pos, fill, api::Side::SIDE_SELL); - sec_map[seckey] = std::pair(long_pos, short_pos); - } - sec_it = sec_map.find(seckey); - bts_assert(sec_it != sec_map.end()); - std::pair &pos = sec_it->second; - if (fill.side() == api::Side::SIDE_BUY) - _merge_pos(pos.first, fill); - else - _merge_pos(pos.second, fill); - return true; - } - else - return false; -} - -void SODPosition::add_sod_pos_as_fill_events( - std::vector &fe, - md::SecurityOracle& oracle) { - if (_enabled) - for (auto &acct_p : _sod_map) { - for (auto &sec_p : acct_p.second) { - Fill &long_pos = sec_p.second.first; - Fill &short_pos = sec_p.second.second; - bts_assert(long_pos.security_key() == short_pos.security_key()); - auto sec = oracle.get_security(SecurityKey(long_pos.security_key())); - if (!sec) - LOG(persist, Error, - "Could not recover fill for key:{} from storage. " - "This should be investigated", - long_pos.security_key()); - else { - fe.emplace_back(long_pos, *sec); - fe.emplace_back(short_pos, *sec); - } - } - } -} - bool SODPosition::is_sod(const google::protobuf::Timestamp& fill_time) const{ - return _enabled && fill_time.seconds() < _sod_time.tv_sec; + return fill_time.seconds() < _sod_time.tv_sec; } } // namespace orderman diff --git a/src/lib/orderman/actors/SODPosition.h b/src/lib/orderman/actors/SODPosition.h index d8eef996b..1b186ea68 100644 --- a/src/lib/orderman/actors/SODPosition.h +++ b/src/lib/orderman/actors/SODPosition.h @@ -18,8 +18,6 @@ SecPosMap; class SODPosition{ private: - std::map _sod_map; - const bool _enabled; const timeval _sod_time; private: // converts today:hh:mm:ss to timeval @@ -27,16 +25,13 @@ private: static timeval _subtract_days(const timeval& tv, uint32_t n_days=1); static timeval _get_fills_sod_time(); static std::string _sod_fill_id(const Fill& fill,api::Side side); - void _merge_pos(Fill& pos, const Fill& fill); - void _zero_pos(Fill& pos, const Fill& fill, api::Side side); public: SODPosition(); + SODPosition(timeval); // all inactive orders before this time are not published static timeval get_orders_start_time(); static timeval get_order_cutoff_time(const std::map& ordermap); bool add_fill_to_sod_pos(const Fill& fill); - void add_sod_pos_as_fill_events(std::vector& fill_events, - md::SecurityOracle&); bool is_sod (const google::protobuf::Timestamp& fill_time) const; }; diff --git a/src/server/sparkd/position/PositionMap.cpp b/src/server/sparkd/position/PositionMap.cpp index 9d2b58eb3..69da1f210 100644 --- a/src/server/sparkd/position/PositionMap.cpp +++ b/src/server/sparkd/position/PositionMap.cpp @@ -28,8 +28,7 @@ _fill_map(), _manual_fill_map(), _pos_map(), _base_pos_ts(), -_base_pos_map() -{ +_base_pos_map(){ } const PositionUnit& PositionMap::on_fill (const Fill& f, @@ -215,4 +214,18 @@ void PositionMap::_zero_out_pos_entry (bts::api::position::PositionEntry& p){ p.set_short_avg_price(0); } +// test only ////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// +PositionUnit const* PositionMap::test_only_pos (uint32_t account_id, + uint32_t sec_key){ + auto acct_it = _pos_map.find(account_id); + if (acct_it != _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 index 88721300d..a1235041a 100644 --- a/src/server/sparkd/position/PositionMap.h +++ b/src/server/sparkd/position/PositionMap.h @@ -58,6 +58,9 @@ public: void set_admin_data (const bts::api::admin::AdminData&); void get_pos (std::map&) const; bts::api::position::ManualFill const* get_manual_fill (int rec_id) const; +public: + bts::api::position::PositionUnit const* test_only_pos (uint32_t account_id, + uint32_t sec_key); }; }} diff --git a/src/server/sparkd/position/tests/PositionTests.cpp b/src/server/sparkd/position/tests/PositionTests.cpp index 51d1f5086..813305bbd 100644 --- a/src/server/sparkd/position/tests/PositionTests.cpp +++ b/src/server/sparkd/position/tests/PositionTests.cpp @@ -13,57 +13,17 @@ #include #include -namespace bts -{ -namespace position -{ +namespace bts { +namespace position { -class PositionTests -{ -public: - // static bool query_margin_webapi(MarginServiceCore& svc, const std::vector& pos, MarginResult& res) - // { - // return svc.query_margin_webapi(pos, res); - // } - // static void set_verbose_debugging(MarginServiceCore& svc, bool verb) - // { - // return svc.set_verbose_debugging(verb); - // } - -}; - -// TEST_CASE ("Margin API", "[margin]") -// { -// securities::SecurityDB _db; -// MarginServiceCore _margin(_db); - -// SECTION("test1") -// { -// bts::securities::ISecurityDB::SecurityMap secs = _db.retrieve_all(); -// Position p; -// for(const auto& i : secs) -// { -// if(i.second->has_future() && i.second->symbol() == "ES") -// p.security = i.second; -// } -// auto acct = orderman::to("TAG1"); -// p.account = acct; -// p.avg_price = Price(2500.0); -// p.position = Quantity(1); -// p.side = api::SIDE_BUY; -// std::vector pos; -// pos.push_back(p); -// p.position = Quantity(3); -// p.side = api::SIDE_SELL; -// pos.push_back(p); - -// MarginResult res; -// PositionTests::query_margin_webapi(_margin, pos, res); -// REQUIRE(res.valid); -// } - -// } - -}} - +TEST_CASE ("position map data", "[position]") { + SECTION("day") { + // add a few day trades and make sure the position + // is correct + } + SECTION("unnormalized") { + // verify standard vs legs + } +} +}} \ No newline at end of file