From fdaa6acf0f7186aca566636f5a967217a66b8aa1 Mon Sep 17 00:00:00 2001 From: Tzion Date: Wed, 5 Jan 2022 15:01:46 +0200 Subject: [PATCH] decrease the precision of sessionend according the rounding error of date2num and num2date; The precision of 'roundtrip' conversion of datetime [i.e: num2date(date2num(datetime)) ] is 10 microseconds. therefore to prevent bug when performing double conversion of dates, the resolution needs to be in order of magnitude lower - i.e. 100 ms. Example: num2date(date2num(datetime(2021,3,12, 4,59,59,999990))) will be rounded and return 2021-03-12_05:00:00.000000. --- backtrader/feed.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backtrader/feed.py b/backtrader/feed.py index 1145c997b..39a1f0d1a 100644 --- a/backtrader/feed.py +++ b/backtrader/feed.py @@ -86,8 +86,8 @@ def dopostinit(cls, _obj, *args, **kwargs): _obj.p.sessionend = _obj.p.sessionend.time() elif _obj.p.sessionend is None: - # remove 9 to avoid precision rounding errors - _obj.p.sessionend = datetime.time(23, 59, 59, 999990) + # remove 99 to avoid precision rounding errors + _obj.p.sessionend = datetime.time(23, 59, 59, 999900) if isinstance(_obj.p.fromdate, datetime.date): # push it to the end of the day, or else intraday