diff --git a/src/lib/orderman/actors/SODPosition.cpp b/src/lib/orderman/actors/SODPosition.cpp index 4d3cc885e..9f99cf894 100644 --- a/src/lib/orderman/actors/SODPosition.cpp +++ b/src/lib/orderman/actors/SODPosition.cpp @@ -24,30 +24,6 @@ _sod_time(sod_time){ LOG(persist, Info, "SODPosition::cons sod_time:{}", sod_time.tv_sec); } -// returns the timeval (secs/usecs from epoch) for today's hh,mm,ss -// (in local machine time zone) -timeval SODPosition::to_timeval(uint16_t hh, uint16_t mm, uint16_t ss) { - // function does the conversion the following way: - // 1. gets the secs from epoch (time_t) - // 2. converts it into local time - // 3. overrides the hh, mm, ss for local time with the func args - // 4. converts it back into timeval - bts_assert(hh < 24); - bts_assert(mm < 60); - bts_assert(ss < 60); - time_t secs; - time(&secs); // seconds since epoch (utc) (independent of machine timezone) - struct tm *ptm_local = localtime(&secs); // in local timezone - ptm_local->tm_hour = hh; - ptm_local->tm_min = mm; - ptm_local->tm_sec = ss; - secs = mktime(ptm_local); // converts local time to UTC epoch time - timeval rv; - rv.tv_sec = secs; - rv.tv_usec = 0; - return rv; -} - timeval SODPosition::subtract_days(const timeval &tv, uint32_t n_days) { uint32_t secs_in_day = 86400 * n_days; timeval rv = tv; @@ -57,28 +33,27 @@ timeval SODPosition::subtract_days(const timeval &tv, uint32_t n_days) { timeval SODPosition::sod_time() { const auto &sod_time = config::get(); - timeval sod = to_timeval(sod_time.sod_hh(), sod_time.sod_mm(), 0); - timeval now; - gettimeofday(&now, NULL); + // 1. gets the local time (calls localtime with epoch) + // 2. overrides the hh, mm for local time with sod hh and mm + // 3. converts it back into timeval + // 4. if this time happens to me > now, it goes back one day + bts_assert(sod_time.sod_hh() < 24); + bts_assert(sod_time.sod_mm() < 60); + time_t now_epoch; + time(&now_epoch); // seconds since epoch (utc) (independent of machine timezone) + struct tm *ptm_local = localtime(&now_epoch); // in local timezone + ptm_local->tm_hour = sod_time.sod_hh(); + ptm_local->tm_min = sod_time.sod_mm(); + ptm_local->tm_sec = 0; + ptm_local->tm_isdst = -1; + time_t sod_epoch = mktime(ptm_local); // converts local time to UTC epoch time + timeval rv; + rv.tv_sec = sod_epoch; + rv.tv_usec = 0; // 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); - return sod; -} - -timeval SODPosition::get_orders_start_time() { - const auto &sod_time = config::get(); - timeval sod = to_timeval(sod_time.sod_hh(), sod_time.sod_mm(), 0); - const auto &orders_cleanup = config::get(); - 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()); - return sod; + if (rv.tv_sec >= now_epoch) + rv = subtract_days (rv,1); + return rv; } timeval SODPosition::get_order_cutoff_time( diff --git a/src/lib/orderman/actors/SODPosition.h b/src/lib/orderman/actors/SODPosition.h index 523ab11aa..9f9c20b55 100644 --- a/src/lib/orderman/actors/SODPosition.h +++ b/src/lib/orderman/actors/SODPosition.h @@ -25,14 +25,13 @@ 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& ordermap); + static timeval subtract_days(const timeval& tv, uint32_t n_days=1); + // returns latest order's time - orders_cleanup days + static timeval get_order_cutoff_time( + const std::map& ordermap); static timeval now(); static google::protobuf::Timestamp to_timestamp (timeval); bool is_sod (const google::protobuf::Timestamp& fill_time) const; }; -}} +}} \ No newline at end of file diff --git a/testing/manual/positions_and_fills/SOD_timezone.txt b/testing/manual/positions_and_fills/SOD_timezone.txt new file mode 100644 index 000000000..78b53ffef --- /dev/null +++ b/testing/manual/positions_and_fills/SOD_timezone.txt @@ -0,0 +1,62 @@ +0. SOD config time is expressed in local timezone (local to the server). This test +makes sure it works for Central (Chicago), Eastern (New York) and UTC timezones. + +ExchangeSim { + value { + merge_with_md: true + forward_implied: false + } +} + + +2. Clean up your fills database. Cross the market to buy a 10 lot future (any future). Lets say the fill time is 6:03:XX +EST. Wait until 6:05 and do a 20 lot sell. + +3. Now we will make 6:05 as the SOD Time. Enter this in your spark.conf + +SODTime{ + value { + sod_hh: 18 + sod_mm: 05 + } +} + +Now start your server with the environment variable TZ set to East coast +TZ="America/New_York" ./sparkd run + +4. In the fills window verify that the first fill(fills if there were partials) is SOD, and the second is DAY. +Make sure the PNL correctly divided it into DAY and SOD. + +5. Now start the server in Chicago TZ, and all the fills should be STANDARD (this is an excercise for the user, to +figure out why its not all SOD) + +TZ="America/Chicago" ./sparkd run + +6. Change the SOD Time to + +SODTime{ + value { + sod_hh: 17 + sod_mm: 05 + } +} + +and start the server in Chicago timezone again +TZ="America/Chicago" ./sparkd run +This should be the functional equivelant to 3 (and produce the same result, verify that) + +7. Change the SOD Time to +SODTime{ + value { + sod_hh: 22 + sod_mm: 05 + } +} + +and start the server in UTC +TZ="Etc/UTC" ./sparkd run + +This should be the functional equivelant to 3 (and produce the same result, verify that) + + + \ No newline at end of file