Rename default OHLCV `np.dtype` descriptions

Use `def_iohlcv_fields` for a name and instead of copying and inserting
the index field pop it for the non-index version. Drop creating
`np.dtype()` instances since `numpy`'s apis accept both input forms so
this is simpler on our end.
basic_buy_bot
Tyler Goodlet 2023-05-31 17:53:15 -04:00
parent 848577488e
commit bf21d2e329
3 changed files with 31 additions and 19 deletions

View File

@ -22,9 +22,6 @@ and storing data from your brokers as well as
sharing live streams over a network. sharing live streams over a network.
""" """
from ._util import (
get_console_log,
)
from ._normalize import iterticks from ._normalize import iterticks
from ._sharedmem import ( from ._sharedmem import (
maybe_open_shm_array, maybe_open_shm_array,
@ -34,8 +31,8 @@ from ._sharedmem import (
ShmArray, ShmArray,
) )
from ._source import ( from ._source import (
base_iohlc_dtype, def_iohlcv_fields,
base_ohlc_dtype, def_ohlcv_fields,
) )
from .feed import ( from .feed import (
open_feed, open_feed,
@ -50,6 +47,6 @@ __all__ = [
'attach_shm_array', 'attach_shm_array',
'open_shm_array', 'open_shm_array',
'get_shm_token', 'get_shm_token',
'base_iohlc_dtype', 'def_iohlcv_fields',
'base_ohlc_dtype', 'def_ohlcv_fields',
] ]

View File

@ -33,7 +33,7 @@ from numpy.lib import recfunctions as rfn
import tractor import tractor
from ._util import log from ._util import log
from ._source import base_iohlc_dtype from ._source import def_iohlcv_fields
from .types import Struct from .types import Struct
@ -168,7 +168,7 @@ def _make_token(
to access a shared array. to access a shared array.
''' '''
dtype = base_iohlc_dtype if dtype is None else dtype dtype = def_iohlcv_fields if dtype is None else dtype
return _Token( return _Token(
shm_name=key, shm_name=key,
shm_first_index_name=key + "_first", shm_first_index_name=key + "_first",
@ -258,7 +258,6 @@ class ShmArray:
# to load an empty array.. # to load an empty array..
if len(a) == 0 and self._post_init: if len(a) == 0 and self._post_init:
raise RuntimeError('Empty array race condition hit!?') raise RuntimeError('Empty array race condition hit!?')
# breakpoint()
return a return a

View File

@ -23,26 +23,42 @@ from bidict import bidict
import numpy as np import numpy as np
ohlc_fields = [ def_iohlcv_fields: list[tuple[str, type]] = [
('time', float),
# YES WE KNOW, this isn't needed in polars but we use it for doing
# ring-buffer like pre/append ops our our `ShmArray` real-time
# numpy-array buffering system such that there is a master index
# that can be used for index-arithmetic when write data to the
# "middle" of the array. See the ``tractor.ipc.shm`` pkg for more
# details.
('index', int),
# presume int for epoch stamps since it's most common
# and makes the most sense to avoid float rounding issues.
# TODO: if we want higher reso we should use the new
# ``time.time_ns()`` in python 3.10+
('time', int),
('open', float), ('open', float),
('high', float), ('high', float),
('low', float), ('low', float),
('close', float), ('close', float),
('volume', float), ('volume', float),
('bar_wap', float),
# TODO: can we elim this from default field set to save on mem?
# i think only kraken really uses this in terms of what we get from
# their ohlc history API?
('bar_wap', float), # shouldn't be default right?
] ]
ohlc_with_index = ohlc_fields.copy() # remove index field
ohlc_with_index.insert(0, ('index', int)) def_ohlcv_fields: list[tuple[str, type]] = def_iohlcv_fields.copy()
def_ohlcv_fields.pop(0)
# our minimum structured array layout for ohlc data assert (len(def_iohlcv_fields) - len(def_ohlcv_fields)) == 1
base_iohlc_dtype = np.dtype(ohlc_with_index)
base_ohlc_dtype = np.dtype(ohlc_fields)
# TODO: for now need to construct this manually for readonly arrays, see # TODO: for now need to construct this manually for readonly arrays, see
# https://github.com/numba/numba/issues/4511 # https://github.com/numba/numba/issues/4511
# from numba import from_dtype # from numba import from_dtype
# base_ohlc_dtype = np.dtype(def_ohlc_fields)
# numba_ohlc_dtype = from_dtype(base_ohlc_dtype) # numba_ohlc_dtype = from_dtype(base_ohlc_dtype)
# map time frame "keys" to seconds values # map time frame "keys" to seconds values