Simplify `Flow.maxmin()` block logics

mxmn_from_m4
Tyler Goodlet 2022-06-28 09:41:49 -04:00
parent ed2c962bb9
commit 1c561207f5
1 changed files with 32 additions and 32 deletions

View File

@ -387,10 +387,11 @@ class Flow(msgspec.Struct): # , frozen=True):
lbar: int, lbar: int,
rbar: int, rbar: int,
) -> tuple[float, float]: ) -> Optional[tuple[float, float]]:
''' '''
Compute the cached max and min y-range values for a given Compute the cached max and min y-range values for a given
x-range determined by ``lbar`` and ``rbar``. x-range determined by ``lbar`` and ``rbar`` or ``None``
if no range can be determined (yet).
''' '''
rkey = (lbar, rbar) rkey = (lbar, rbar)
@ -400,45 +401,44 @@ class Flow(msgspec.Struct): # , frozen=True):
shm = self.shm shm = self.shm
if shm is None: if shm is None:
mxmn = None return None
else: # new block for profiling?.. arr = shm.array
arr = shm.array
# build relative indexes into shm array # build relative indexes into shm array
# TODO: should we just add/use a method # TODO: should we just add/use a method
# on the shm to do this? # on the shm to do this?
ifirst = arr[0]['index'] ifirst = arr[0]['index']
slice_view = arr[ slice_view = arr[
lbar - ifirst: lbar - ifirst:
(rbar - ifirst) + 1 (rbar - ifirst) + 1
] ]
if not slice_view.size: if not slice_view.size:
mxmn = None return None
elif self.yrange: elif self.yrange:
mxmn = self.yrange mxmn = self.yrange
# print(f'{self.name} M4 maxmin: {mxmn}') # print(f'{self.name} M4 maxmin: {mxmn}')
else:
if self.is_ohlc:
ylow = np.min(slice_view['low'])
yhigh = np.max(slice_view['high'])
else: else:
if self.is_ohlc: view = slice_view[self.name]
ylow = np.min(slice_view['low']) ylow = np.min(view)
yhigh = np.max(slice_view['high']) yhigh = np.max(view)
else: mxmn = ylow, yhigh
view = slice_view[self.name] # print(f'{self.name} MANUAL maxmin: {mxmin}')
ylow = np.min(view)
yhigh = np.max(view)
mxmn = ylow, yhigh # cache result for input range
# print(f'{self.name} MANUAL maxmin: {mxmin}') assert mxmn
self._mxmns[rkey] = mxmn
if mxmn is not None: return mxmn
# cache new mxmn result
self._mxmns[rkey] = mxmn
return mxmn
def view_range(self) -> tuple[int, int]: def view_range(self) -> tuple[int, int]:
''' '''