diff --git a/src/server/sparkd/position/PositionMap.cpp b/src/server/sparkd/position/PositionMap.cpp index 252777781..2f4c6b809 100644 --- a/src/server/sparkd/position/PositionMap.cpp +++ b/src/server/sparkd/position/PositionMap.cpp @@ -129,6 +129,10 @@ ManualFill const* PositionMap::get_manual_fill (int rec_id) const{ else return &(it->second); } + +bool PositionMap::is_sod (google::protobuf::Timestamp fill_time) const{ + return _sod_helper.is_sod(fill_time); +} /////////////////////////////////////////////////////////////////////////////// // helpers //////////////////////////////////////////////////////////////////// PositionUnit& PositionMap::_get_or_add(uint32_t account_id, uint32_t seckey){ @@ -146,7 +150,7 @@ PositionUnit& PositionMap::_get_or_add(uint32_t account_id, uint32_t seckey){ } PositionMap::FillType PositionMap::_fill_type( - google::protobuf::Timestamp fill_time) const{ + google::protobuf::Timestamp fill_time) const{ if(fill_time.seconds() <= _base_pos_ts.seconds()) return FillType::BASE; else diff --git a/src/server/sparkd/position/PositionMap.h b/src/server/sparkd/position/PositionMap.h index 6efa096c3..f98392888 100644 --- a/src/server/sparkd/position/PositionMap.h +++ b/src/server/sparkd/position/PositionMap.h @@ -64,6 +64,7 @@ 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; + bool is_sod (google::protobuf::Timestamp fill_time) const; public: bts::api::position::PositionUnit const* test_only_pos (uint32_t account_id, uint32_t sec_key) const; diff --git a/src/server/sparkd/position/PositionServiceServer.cpp b/src/server/sparkd/position/PositionServiceServer.cpp index b42642180..f622e2e5a 100644 --- a/src/server/sparkd/position/PositionServiceServer.cpp +++ b/src/server/sparkd/position/PositionServiceServer.cpp @@ -83,7 +83,8 @@ PositionServiceServer::PositionServiceServer(bts::api::ServiceContext &context, make_pnl_data_stateful_publisher(), make_pnl_account_data_stateful_publisher(), make_manual_fills_stateful_publisher(), md_client)) { - // incremental fill update + _delete_presod_local_on_start(); + // incremental fill update me->order_service.subscribe_fills() | rxcpp::operators::subscribe( [this](auto const &update) { @@ -127,6 +128,30 @@ PositionServiceServer::PositionServiceServer(bts::api::ServiceContext &context, me->context.post([this]() { _periodic_poll(); }, me->poll_period_ms); } +void PositionServiceServer::_delete_presod_local_on_start() const{ + std::set rm_ids; + auto all = me->manual_fills_store->retrieve_all()[manual_fills_ns]; + ManualFill rec; + for (auto const &kvp : all){ + if (utils::pb::from_string(kvp.second, &rec)) { + if(rec.fill_type() == ManualFill::LOCAL && + me->state.is_sod(rec.timestamp())){ + LOG(OrderMan, Info, + "PositionServiceServer::_delete_presod_local_on_start " + "fill: {}", kvp.second); + auto id = std::to_string(rec.record_id()); + rm_ids.insert(id); + } + } + else + LOG(OrderMan, Error, + "Could not recover manual fill {} from storage. " + "This should be investigated", + kvp.first); + } + me->manual_fills_store->remove(manual_fills_ns, rm_ids); +} + const PositionUnit &PositionServiceServer::_on_fill(const Fill &f, bool is_add) { return me->state.on_fill(f,is_add); @@ -134,7 +159,7 @@ const PositionUnit &PositionServiceServer::_on_fill(const Fill &f, const PositionUnit &PositionServiceServer::_on_manual_fill(const ManualFill &mf, bool is_add) { - return me->state.on_manual_fill(mf,is_add); + return me->state.on_manual_fill(mf,is_add); } void PositionServiceServer::_set_admin_data( diff --git a/src/server/sparkd/position/PositionServiceServer.h b/src/server/sparkd/position/PositionServiceServer.h index 329858600..769c66e6d 100644 --- a/src/server/sparkd/position/PositionServiceServer.h +++ b/src/server/sparkd/position/PositionServiceServer.h @@ -35,11 +35,14 @@ class PositionServiceServer : public bts::api::position::PositionServiceBase { private: struct PImpl; std::unique_ptr me; + // this function deleted all local fills with + // a timestamp less than SOD + void _delete_presod_local_on_start() const; // 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); + _on_fill(const bts::api::orderman::Fill &fill, bool is_add); // analogous to the above, for manual fills (this inclused // both client(local) and server(admin) fills) const bts::api::position::PositionUnit & diff --git a/testing/manual/positions_and_fills/SOD.txt b/testing/manual/positions_and_fills/SOD.txt index dab64d72d..cd5c62572 100644 --- a/testing/manual/positions_and_fills/SOD.txt +++ b/testing/manual/positions_and_fills/SOD.txt @@ -16,6 +16,9 @@ Open up the PNL and make sure the Day Long and Day Short positions match what yo The Start Net Pos should be 0. (The Total Net Pos in the PNL should match the pos button in the ladder) +2. Enter admin fills (from risk admin) and local fills. Make sure they reflect correctly in the PNL +(local fills apply to the PNL, but are not used to enforce limits) + 2. Now comes the SOD part. The SOD is what demarcates a day. Find the most recent time on your fills, select 1 minute past that override the SOD time in your spark.conf Here's an example of what the key looks like, assuming your last fill was say at 10:29 and its now 10:35 @@ -27,10 +30,16 @@ SODTime{ } } -3. Now restart the server. Verify that all rows in the fills have a Fill Kind 'Sod' +(Enter this in the timezone on your server, which you can check with "date") -4. Verify that the ladder is displaying the correct position (it should be the same as before the server start) +3. Now restart the server. Verify that all fill rows in the fills have a Fill Kind 'Sod' -5. Verify that the PNL is displaying the correct Start Net Pos and the Day part is 0. +4. The pre SOD local fills should be all gone (they are deleted by the system at startup) + +5 Verify that the ladder is displaying the correct position (it should be the same as before the server start) + +6. Verify that the PNL is displaying the correct Start Net Pos and the Day part is 0. + +7. Now force a few more fills (trade fills as well as admin/local). Verify that the ladder position box and the PNL is +correctly counting them. -6. Now force a few more fills. Verify that the ladder position box and the PNL is correctly counting them.