ohlc bugfixes
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user