tests for sd

This commit is contained in:
Taha Afzal committed 2019-08-16 13:39:58 -04:00
1 parent 20f65a9196
commit 2c1cf5f2e0
5 files changed
+43 -15

No files matched your search

+10 -10
View File
@@ -15,7 +15,7 @@ namespace orderman {
SODPosition::SODPosition()
:
SODPosition(_get_fills_sod_time()) {
SODPosition(sod_time()) {
}
SODPosition::SODPosition(timeval sod_time)
@@ -26,7 +26,7 @@ _sod_time(sod_time){
// returns the timeval for today's hh,mm,ss
// (timeval is the microseconds from epoch)
timeval SODPosition::_to_timeval(uint16_t hh, uint16_t mm, uint16_t ss) {
timeval SODPosition::to_timeval(uint16_t hh, uint16_t mm, uint16_t ss) {
bts_assert(hh < 24);
bts_assert(mm < 60);
bts_assert(ss < 60);
@@ -44,36 +44,36 @@ timeval SODPosition::_to_timeval(uint16_t hh, uint16_t mm, uint16_t ss) {
return rv;
}
timeval SODPosition::_subtract_days(const timeval &tv, uint32_t n_days) {
timeval SODPosition::subtract_days(const timeval &tv, uint32_t n_days) {
uint32_t secs_in_day = 86400 * n_days;
timeval rv = tv;
rv.tv_sec = (rv.tv_sec > secs_in_day) ? rv.tv_sec - secs_in_day : 0;
return rv;
}
timeval SODPosition::_get_fills_sod_time() {
timeval SODPosition::sod_time() {
const auto &sod_time = config::get<config::SODTime>();
timeval sod = _to_timeval(sod_time.sod_hh(), sod_time.sod_mm(), 0);
timeval sod = to_timeval(sod_time.sod_hh(), sod_time.sod_mm(), 0);
timeval now;
gettimeofday(&now, NULL);
// if the server started before the sod for today
// we go back to yesterday
if (sod.tv_sec >= now.tv_sec)
sod = _subtract_days(sod);
sod = subtract_days(sod);
return sod;
}
timeval SODPosition::get_orders_start_time() {
const auto &sod_time = config::get<config::SODTime>();
timeval sod = _to_timeval(sod_time.sod_hh(), sod_time.sod_mm(), 0);
timeval sod = to_timeval(sod_time.sod_hh(), sod_time.sod_mm(), 0);
const auto &orders_cleanup = config::get<config::OrdersSoftCleanupCutoff>();
timeval now;
gettimeofday(&now, NULL);
// if the server started before the sod for today
// we go back to yesterday
if (sod.tv_sec >= now.tv_sec)
sod = _subtract_days(sod);
sod = _subtract_days(sod, orders_cleanup.extra_days());
sod = subtract_days(sod);
sod = subtract_days(sod, orders_cleanup.extra_days());
return sod;
}
@@ -110,7 +110,7 @@ timeval SODPosition::get_order_cutoff_time(
rv.tv_sec = max_ts.seconds();
rv.tv_usec = static_cast<double>(max_ts.nanos()) / 1000;
const auto &orders_cleanup = config::get<config::OrdersSoftCleanupCutoff>();
return _subtract_days(rv, orders_cleanup.extra_days());
return subtract_days(rv, orders_cleanup.extra_days());
}
timeval SODPosition::now(){
+4 -4
View File
@@ -20,13 +20,13 @@ class SODPosition{
private:
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);
static timeval _subtract_days(const timeval& tv, uint32_t n_days=1);
static timeval _get_fills_sod_time();
// converts today:hh:mm:ss to timeval
public:
SODPosition();
SODPosition(timeval);
static timeval sod_time();
static timeval to_timeval(uint16_t hh, uint16_t mm, uint16_t ss);
static timeval subtract_days(const timeval& tv, uint32_t n_days=1);
// all inactive orders before this time are not published
static timeval get_orders_start_time();
static timeval get_order_cutoff_time(const std::map<std::string,std::string>& ordermap);
+6 -1
View File
@@ -20,9 +20,14 @@ using namespace bts::api::orderman;
using namespace bts::api::position;
PositionMap::PositionMap()
:
PositionMap (SODPosition::sod_time()){
}
PositionMap::PositionMap(timeval sod_time)
:
_pos_id(0),
_sod_helper(),
_sod_helper(sod_time),
_account_id_map(),
_fill_map(),
_manual_fill_map(),
+1
View File
@@ -48,6 +48,7 @@ private:
void _zero_out_pos_entry (bts::api::position::PositionEntry&);
public:
PositionMap();
PositionMap(timeval sod_time);
const bts::api::position::PositionUnit& on_fill (
const bts::api::orderman::Fill& f,
bool is_add);
@@ -194,5 +194,27 @@ TEST_CASE("basic_badge", "[position_internal]") {
}
}
TEST_CASE("sod", "[position_internal]") {
// makes sure fills on either side of the SOD
// go the right place
timeval sod = SODPosition::now();
PositionMap pmap(sod);
timeval pre_sod = sod, post_sod = sod;
pre_sod.tv_sec = pre_sod.tv_sec - 60*60;
post_sod.tv_sec = post_sod.tv_sec + 60*60;
// presod fill
Fill presod_f = create_fill(1, 1, 10, 50, api::Side::SIDE_BUY, Fill::FILL_KIND_STANDARD);
presod_f.mutable_timestamp()->CopyFrom(SODPosition::to_timestamp(pre_sod));
pmap.on_fill(presod_f,true);
// postsod fill
Fill postsod_f = create_fill(1, 1, 20, 100, api::Side::SIDE_BUY, Fill::FILL_KIND_STANDARD);
postsod_f.mutable_timestamp()->CopyFrom(SODPosition::to_timestamp(post_sod));
pmap.on_fill(postsod_f,true);
PositionUnit const* pu = pmap.test_only_pos(1,1);
REQUIRE(pu!=nullptr);
compare_pos(pu->sod(),50,10,0,0);
compare_pos(pu->day(),100,20,0,0);
}
} // namespace test
} // namespace bts