From 05ef00f111b044e07ac5fc110f2d03d656908e08 Mon Sep 17 00:00:00 2001 From: Tianning Li Date: Wed, 23 Feb 2022 15:16:27 -0800 Subject: [PATCH] fix vwap --- ta/volume.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/ta/volume.py b/ta/volume.py index 7655ed79..dfc48e2b 100644 --- a/ta/volume.py +++ b/ta/volume.py @@ -430,18 +430,18 @@ class VolumeWeightedAveragePrice(IndicatorMixin): def __init__( self, + date: pd.Series, high: pd.Series, low: pd.Series, close: pd.Series, volume: pd.Series, - window: int = 14, fillna: bool = False, ): + self._date = date self._high = high self._low = low self._close = close self._volume = volume - self._window = window self._fillna = fillna self._run() @@ -453,13 +453,13 @@ def _run(self): typical_price_volume = typical_price * self._volume # 3 total price * volume - min_periods = 0 if self._fillna else self._window - total_pv = typical_price_volume.rolling( - self._window, min_periods=min_periods - ).sum() + df_pv = pd.concat([self._date.dt.date, typical_price_volume, self._volume], + keys=['Date', 'Price Volume', 'Volume'], axis=1) + + total_pv = (df_pv.groupby(['Date'])['Price Volume'].cumsum(axis=0).reset_index()['Price Volume']) # 4 total volume - total_volume = self._volume.rolling(self._window, min_periods=min_periods).sum() + total_volume = (df_pv.groupby(['Date'])['Volume'].cumsum(axis=0).reset_index()['Volume']) self.vwap = total_pv / total_volume @@ -470,7 +470,7 @@ def volume_weighted_average_price(self) -> pd.Series: pandas.Series: New feature generated. """ vwap = self._check_fillna(self.vwap) - return pd.Series(vwap, name=f"vwap_{self._window}") + return pd.Series(vwap, name=f"vwap") def acc_dist_index(high, low, close, volume, fillna=False):