From 64384c3d3a9dafc00dee8048696b7075dc7cd24f Mon Sep 17 00:00:00 2001 From: Tim Date: Thu, 25 Jan 2024 19:39:58 -0400 Subject: [PATCH] ohlc bugfixes --- src/dexorder/__init__.py | 2 +- src/dexorder/ohlc.py | 33 +++++++++++++++++---------------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/dexorder/__init__.py b/src/dexorder/__init__.py index 41730fa..98945c2 100644 --- a/src/dexorder/__init__.py +++ b/src/dexorder/__init__.py @@ -11,7 +11,7 @@ def now(): return datetime.now(timezone.utc) def timestamp(): - return datetime.now().timestamp() + return int(datetime.now().timestamp()) def from_timestamp(ts): return datetime.fromtimestamp(ts, timezone.utc) diff --git a/src/dexorder/ohlc.py b/src/dexorder/ohlc.py index 776eeaa..456befe 100644 --- a/src/dexorder/ohlc.py +++ b/src/dexorder/ohlc.py @@ -97,7 +97,7 @@ def update_ohlc(prev: OHLC, period: timedelta, time: datetime, price: Optional[d break result.append(cur.ohlc) cur = NativeOHLC(end, None, None, None, cur.close) - log.debug(f'\tresult after finalization: {result}') + log.debug(f'\ttime advancements: {result}') # if we are setting a price, update the current bar if price is not None: if cur.open is None: @@ -149,29 +149,30 @@ class OHLCRepository: log.debug(f'Updating OHLC {logname} {minutely(time)} {price}') key = (symbol, period) # bars is a list of "recent" OHLC's stored as blockdata. we try to keep the recent array long enough to extend before the root block time - bars: Optional[list[OHLC]] = recent_ohlcs.get(key) - if not bars: + historical: Optional[list[OHLC]] = recent_ohlcs.get(key) + if not historical: if create is False or price is None: return # do not track symbols which have not been explicity set up p = str(price) + historical = [] updated = [OHLC((minutely(ohlc_start_time(time, period)), p, p, p, p))] log.debug(f'\tcreated new bars {updated}') else: - updated = update_ohlc(bars[-1], period, time, price) - # we need to retain enough recent history to at least cover the root block time, plus one previous finalized block - # first we construct the longest possible sequence - if not bars or not updated: - updated = (bars or []) + (updated or []) + updated = update_ohlc(historical[-1], period, time, price) + # drop any historical bars that are older than we need + oldest_needed = from_timestamp(current_block.get().timestamp) - period # cover the root block time plus one period prior + trim = (oldest_needed - from_isotime(historical[0][0])) // period + if trim > 0: + historical = historical[trim:] + + # now overlap the updated data on top of the historical data + if not historical or not updated: + updated = historical + updated else: - last_bar = from_isotime(bars[-1][0]) + last_bar = from_isotime(historical[-1][0]) first_updated = from_isotime(updated[0][0]) - overlap = (first_updated - last_bar) // period - updated = bars[:-overlap] + updated if overlap > 0 else bars + updated - # now we drop history that is older than we need - oldest_needed = from_timestamp(current_block.get().timestamp) - period # cover the root block time plus one period prior - trim = (oldest_needed - from_isotime(updated[0][0])) // period - if trim > 0: - updated = updated[trim:] + overlap = (first_updated - last_bar) // period + 1 + updated = historical[:-overlap] + updated if overlap > 0 else historical + updated log.debug(f'\tnew recents: {updated}') recent_ohlcs.setitem(key, updated) return updated