This commit is contained in:
Tim
2024-03-19 13:40:59 -04:00
parent 31bf1da1db
commit 893b09b1ed

View File

@@ -6,7 +6,7 @@ from typing import Optional, NamedTuple, Reversible, Union, TypedDict
from cachetools import LFUCache
from dexorder import dec, config, from_timestamp, timestamp, now
from dexorder import dec, config, from_timestamp, timestamp, now, minutely
from dexorder.base.chain import current_chain
from dexorder.blockstate import BlockDict, DiffItem, current_blockstate
from dexorder.blockstate.diff import DiffEntryItem
@@ -18,9 +18,10 @@ log = logging.getLogger(__name__)
OHLC_PERIODS = [
timedelta(minutes=1),
timedelta(minutes=3), timedelta(minutes=5), timedelta(minutes=10), timedelta(minutes=15), timedelta(minutes=30),
timedelta(hours=1), timedelta(hours=2), timedelta(hours=4), timedelta(hours=8), timedelta(hours=12),
timedelta(days=1), timedelta(days=2), timedelta(days=3), timedelta(days=7)
# TODO remove debug
# timedelta(minutes=3), timedelta(minutes=5), timedelta(minutes=10), timedelta(minutes=15), timedelta(minutes=30),
# timedelta(hours=1), timedelta(hours=2), timedelta(hours=4), timedelta(hours=8), timedelta(hours=12),
# timedelta(days=1), timedelta(days=2), timedelta(days=3), timedelta(days=7)
]
OHLC_DATE_ROOT = datetime(2009, 1, 4, tzinfo=timezone.utc) # Sunday before Bitcoin Genesis
@@ -93,11 +94,11 @@ class NativeOHLC:
self.high = max(self.high, price)
self.low = min(self.low, price)
if self._ohlc:
self._ohlc[1] = str(self.high)
self._ohlc[2] = str(self.low)
self._ohlc[2] = str(self.high)
self._ohlc[3] = str(self.low)
self.close = price
if self._ohlc:
self._ohlc[3] = pstr
self._ohlc[4] = pstr
self._json = None
@@ -120,6 +121,12 @@ class NativeOHLC:
self._json = json.dumps(self.ohlc)
return self._json
def __repr__(self):
return f'{{NativeOHLC {minutely(self.start)} o:{self.open} h:{self.high} l:{self.low} c:{self.close}}}'
def __str__(self):
return str(self.ohlc)
def period_name(period: timedelta) -> str:
return f'{period // timedelta(minutes=1)}m' if period < timedelta(hours=1) \
@@ -146,7 +153,7 @@ def update_ohlc(prev: NativeOHLC, period: timedelta, time: datetime, price: Opti
returns an ordered list of OHLC's that have been created/modified by the new time/price
if price is None, then bars are advanced based on the time but no new price is added to the series.
"""
# log.debug(f'\tupdating {prev} with {minutely(time)} {price}')
log.debug(f'\tupdating {prev} with {minutely(time)} {price}')
cur = prev
if time < cur.start:
# data corruption. just shut down
@@ -165,7 +172,7 @@ def update_ohlc(prev: NativeOHLC, period: timedelta, time: datetime, price: Opti
cur.update(price)
result.append(cur)
# log.debug(f'\tappended current bar: {cur.ohlc}')
# log.debug(f'\tupdate result: {result}')
log.debug(f'\tupdate result: {result}')
return result
class OHLCKey (NamedTuple):