ohlc bugfixes
This commit is contained in:
@@ -11,7 +11,7 @@ def now():
|
|||||||
return datetime.now(timezone.utc)
|
return datetime.now(timezone.utc)
|
||||||
|
|
||||||
def timestamp():
|
def timestamp():
|
||||||
return datetime.now().timestamp()
|
return int(datetime.now().timestamp())
|
||||||
|
|
||||||
def from_timestamp(ts):
|
def from_timestamp(ts):
|
||||||
return datetime.fromtimestamp(ts, timezone.utc)
|
return datetime.fromtimestamp(ts, timezone.utc)
|
||||||
|
|||||||
@@ -97,7 +97,7 @@ def update_ohlc(prev: OHLC, period: timedelta, time: datetime, price: Optional[d
|
|||||||
break
|
break
|
||||||
result.append(cur.ohlc)
|
result.append(cur.ohlc)
|
||||||
cur = NativeOHLC(end, None, None, None, cur.close)
|
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 we are setting a price, update the current bar
|
||||||
if price is not None:
|
if price is not None:
|
||||||
if cur.open is None:
|
if cur.open is None:
|
||||||
@@ -149,29 +149,30 @@ class OHLCRepository:
|
|||||||
log.debug(f'Updating OHLC {logname} {minutely(time)} {price}')
|
log.debug(f'Updating OHLC {logname} {minutely(time)} {price}')
|
||||||
key = (symbol, period)
|
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 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)
|
historical: Optional[list[OHLC]] = recent_ohlcs.get(key)
|
||||||
if not bars:
|
if not historical:
|
||||||
if create is False or price is None:
|
if create is False or price is None:
|
||||||
return # do not track symbols which have not been explicity set up
|
return # do not track symbols which have not been explicity set up
|
||||||
p = str(price)
|
p = str(price)
|
||||||
|
historical = []
|
||||||
updated = [OHLC((minutely(ohlc_start_time(time, period)), p, p, p, p))]
|
updated = [OHLC((minutely(ohlc_start_time(time, period)), p, p, p, p))]
|
||||||
log.debug(f'\tcreated new bars {updated}')
|
log.debug(f'\tcreated new bars {updated}')
|
||||||
else:
|
else:
|
||||||
updated = update_ohlc(bars[-1], period, time, price)
|
updated = update_ohlc(historical[-1], period, time, price)
|
||||||
# we need to retain enough recent history to at least cover the root block time, plus one previous finalized block
|
# drop any historical bars that are older than we need
|
||||||
# first we construct the longest possible sequence
|
oldest_needed = from_timestamp(current_block.get().timestamp) - period # cover the root block time plus one period prior
|
||||||
if not bars or not updated:
|
trim = (oldest_needed - from_isotime(historical[0][0])) // period
|
||||||
updated = (bars or []) + (updated or [])
|
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:
|
else:
|
||||||
last_bar = from_isotime(bars[-1][0])
|
last_bar = from_isotime(historical[-1][0])
|
||||||
first_updated = from_isotime(updated[0][0])
|
first_updated = from_isotime(updated[0][0])
|
||||||
overlap = (first_updated - last_bar) // period
|
overlap = (first_updated - last_bar) // period + 1
|
||||||
updated = bars[:-overlap] + updated if overlap > 0 else bars + updated
|
updated = historical[:-overlap] + updated if overlap > 0 else historical + 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:]
|
|
||||||
log.debug(f'\tnew recents: {updated}')
|
log.debug(f'\tnew recents: {updated}')
|
||||||
recent_ohlcs.setitem(key, updated)
|
recent_ohlcs.setitem(key, updated)
|
||||||
return updated
|
return updated
|
||||||
|
|||||||
Reference in New Issue
Block a user