From e89d3f956047f6eca5b5a8965303f22cd4bb50ce Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Tue, 1 Dec 2020 17:14:28 -0500 Subject: [PATCH] Use `numpy.divide()` to avoid divide-by-zero --- piker/fsp/_volume.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/piker/fsp/_volume.py b/piker/fsp/_volume.py index 735f3d8b..30397920 100644 --- a/piker/fsp/_volume.py +++ b/piker/fsp/_volume.py @@ -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. """