Skip to content

Commit

Permalink
fix vwap
Browse files Browse the repository at this point in the history
  • Loading branch information
lit26 committed Feb 23, 2022
1 parent 84ced26 commit 05ef00f
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions ta/volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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

Expand All @@ -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):
Expand Down

1 comment on commit 05ef00f

@thouravi
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does these changes fix VWAP or something else also needs to be done?

Please sign in to comment.