forked from twopirllc/pandas-ta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvwap.py
63 lines (48 loc) · 1.87 KB
/
vwap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# -*- coding: utf-8 -*-
from .hlc3 import hlc3
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)"""
# Validate Arguments
high = verify_series(high)
low = verify_series(low)
close = verify_series(close)
volume = verify_series(volume)
offset = get_offset(offset)
# if not is_datetime_ordered(volume):
# print(f"[!] VWAP volume 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()
# Offset
if offset != 0:
vwap = vwap.shift(offset)
# Name & Category
vwap.name = "VWAP"
vwap.category = "overlap"
return vwap
vwap.__doc__ = \
"""Volume Weighted Average Price (VWAP)
The Volume Weighted Average Price that measures the average typical price
by volume. It is typically used with intraday charts to identify general
direction.
Sources:
https://www.tradingview.com/wiki/Volume_Weighted_Average_Price_(VWAP)
https://www.tradingtechnologies.com/help/x-study/technical-indicator-definitions/volume-weighted-average-price-vwap/
https://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:vwap_intraday
Calculation:
tp = typical_price = hlc3(high, low, close)
tpv = tp * volume
VWAP = tpv.cumsum() / volume.cumsum()
Args:
high (pd.Series): Series of 'high's
low (pd.Series): Series of 'low's
close (pd.Series): Series of 'close's
volume (pd.Series): Series of 'volume's
offset (int): How many periods to offset the result. Default: 0
Kwargs:
fillna (value, optional): pd.DataFrame.fillna(value)
fill_method (value, optional): Type of fill method
Returns:
pd.Series: New feature generated.
"""