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

vwap_backup
Tyler Goodlet 2020-12-01 17:14:28 -05:00
parent 2a933c3808
commit 1d7bd3f748
1 changed files with 14 additions and 2 deletions

View File

@ -30,7 +30,20 @@ def wap(
""" """
cum_weights = np.cumsum(weights) cum_weights = np.cumsum(weights)
cum_weighted_input = np.cumsum(signal * 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( async def _tina_vwap(
@ -40,7 +53,6 @@ async def _tina_vwap(
) -> AsyncIterator[np.ndarray]: # maybe something like like FspStream? ) -> AsyncIterator[np.ndarray]: # maybe something like like FspStream?
"""Streaming volume weighted moving average. """Streaming volume weighted moving average.
Calling this "tina" for now since we're using HLC3 instead of tick. Calling this "tina" for now since we're using HLC3 instead of tick.
""" """