Decorate momo routines

dark_vlm
Tyler Goodlet 2022-01-28 07:43:49 -05:00
parent be93ded0e5
commit bd2460846e
1 changed files with 12 additions and 12 deletions

View File

@ -23,6 +23,7 @@ from typing import AsyncIterator, Optional
import numpy as np import numpy as np
from numba import jit, float64, optional, int64 from numba import jit, float64, optional, int64
from ._api import fsp
from ..data._normalize import iterticks from ..data._normalize import iterticks
from ..data._sharedmem import ShmArray from ..data._sharedmem import ShmArray
@ -106,7 +107,7 @@ def ema(
# nopython=True, # nopython=True,
# nogil=True # nogil=True
# ) # )
def rsi( def _rsi(
# TODO: use https://github.com/ramonhagenaars/nptyping # TODO: use https://github.com/ramonhagenaars/nptyping
signal: 'np.ndarray[float64]', signal: 'np.ndarray[float64]',
@ -146,7 +147,7 @@ def rsi(
return rsi, up_ema[-1], down_ema[-1] return rsi, up_ema[-1], down_ema[-1]
def wma( def _wma(
signal: np.ndarray, signal: np.ndarray,
length: int, length: int,
@ -169,10 +170,8 @@ def wma(
return np.convolve(signal, weights, 'valid') return np.convolve(signal, weights, 'valid')
# @piker.fsp.emit( @fsp
# timeframes=['1s', '5s', '15s', '1m', '5m', '1H'], async def rsi(
# )
async def _rsi(
source: 'QuoteStream[Dict[str, Any]]', # noqa source: 'QuoteStream[Dict[str, Any]]', # noqa
ohlcv: ShmArray, ohlcv: ShmArray,
@ -188,11 +187,11 @@ async def _rsi(
sig = ohlcv.array['close'] sig = ohlcv.array['close']
# wilder says to seed the RSI EMAs with the SMA for the "period" # wilder says to seed the RSI EMAs with the SMA for the "period"
seed = wma(ohlcv.last(period)['close'], period)[0] seed = _wma(ohlcv.last(period)['close'], period)[0]
# TODO: the emas here should be seeded with a period SMA as per # TODO: the emas here should be seeded with a period SMA as per
# wilder's original formula.. # wilder's original formula..
rsi_h, last_up_ema_close, last_down_ema_close = rsi( rsi_h, last_up_ema_close, last_down_ema_close = _rsi(
sig, period, seed, seed) sig, period, seed, seed)
up_ema_last = last_up_ema_close up_ema_last = last_up_ema_close
down_ema_last = last_down_ema_close down_ema_last = last_down_ema_close
@ -218,7 +217,7 @@ async def _rsi(
last_down_ema_close = down_ema_last last_down_ema_close = down_ema_last
index = ohlcv.index index = ohlcv.index
rsi_out, up_ema_last, down_ema_last = rsi( rsi_out, up_ema_last, down_ema_last = _rsi(
sig, sig,
period=period, period=period,
up_ema_last=last_up_ema_close, up_ema_last=last_up_ema_close,
@ -227,7 +226,8 @@ async def _rsi(
yield rsi_out[-1:] yield rsi_out[-1:]
async def _wma( @fsp
async def wma(
source, #: AsyncStream[np.ndarray], source, #: AsyncStream[np.ndarray],
length: int, length: int,
@ -243,10 +243,10 @@ async def _wma(
''' '''
# deliver historical output as "first yield" # deliver historical output as "first yield"
yield wma(ohlcv.array['close'], length) yield _wma(ohlcv.array['close'], length)
# begin real-time section # begin real-time section
async for quote in source: async for quote in source:
for tick in iterticks(quote, type='trade'): for tick in iterticks(quote, type='trade'):
yield wma(ohlcv.last(length)) yield _wma(ohlcv.last(length))