Update vwap.py

An Attempt to fix the VWAP problem presented in this issue:
https://github.com/twopirllc/pandas-ta/issues/38

Is it fixing the problem?
This commit is contained in:
YuvalWein
2021-01-07 18:39:10 +02:00
committed by GitHub
parent 4cf1d86996
commit 5c4a72ee99
+8 -4
View File
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
from .hlc3 import hlc3
from ..utils import get_offset, is_datetime_ordered, verify_series
from pandas_ta.utils import get_offset, is_datetime_ordered, verify_series
def vwap(high, low, close, volume, offset=None, **kwargs):
"""Indicator: Volume Weighted Average Price (VWAP)"""
@@ -11,12 +11,17 @@ def vwap(high, low, close, volume, offset=None, **kwargs):
volume = verify_series(volume)
offset = get_offset(offset)
typical_price = hlc3(high=high, low=low, close=close)
if not is_datetime_ordered(volume):
print(f"[!] VWAP volume series is not datetime ordered. Results may not be as expected.")
if not is_datetime_ordered(typical_price):
print(f"[!] VWAP price series is not datetime ordered. Results may not be as expected.")
# Calculate Result
tp = hlc3(high=high, low=low, close=close)
vwap = (tp * volume).cumsum() / volume.cumsum()
weighted_price = typical_price * volume
vwap = weighted_price.groupby(weighted_price.index.to_period('d')).cumsum() / \
volume.groupby(volume.index.to_period('d')).cumsum()
# Offset
if offset != 0:
@@ -29,7 +34,6 @@ def vwap(high, low, close, volume, offset=None, **kwargs):
return vwap
vwap.__doc__ = \
"""Volume Weighted Average Price (VWAP)