Use `numpy.divide()` to avoid divide-by-zero

tina_free_vwap
Tyler Goodlet 2020-12-01 17:14:28 -05:00
parent 599429e54b
commit 36445363a0
1 changed files with 14 additions and 2 deletions

View File

@ -30,7 +30,20 @@ def wap(
"""
cum_weights = np.cumsum(weights)
cum_weighted_input = np.cumsum(signal * weights)
return cum_weighted_input / cum_weights, cum_weighted_input, cum_weights
# cum_weighted_input / cum_weights
# but, avoid divide by zero errors
avg = np.divide(
cum_weighted_input,
cum_weights,
where=cum_weights != 0
)
return (
avg,
cum_weighted_input,
cum_weights,
)
async def _tina_vwap(
@ -40,7 +53,6 @@ async def _tina_vwap(
) -> AsyncIterator[np.ndarray]: # maybe something like like FspStream?
"""Streaming volume weighted moving average.
Calling this "tina" for now since we're using HLC3 instead of tick.
"""