Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix vwap #285

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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