3

Not sure why I cannot get my DataFrame VWAP calculations to TradingView version at this link: https://www.tradingview.com/support/solutions/43000502018-volume-weighted-average-price-vwap/

They provide a simple calculation method which I can duplicate in a DataFrame but my calculations do not match. I believe it has something to do with the TradingView “Anchor Session” setting. Not sure how to adjust or add to my DataFrame calculations to match TradingView. I have also tried the Python Technical Analysis Library which does not match TradingView.

Simple Calculation

There are five steps in calculating VWAP:

Calculate the Typical Price for the period.

[(High + Low + Close)/3)]

Multiply the Typical Price by the period Volume.

(Typical Price x Volume)

Create a Cumulative Total of Typical Price.

Cumulative(Typical Price x Volume)

Create a Cumulative Total of Volume.

Cumulative(Volume)

Divide the Cumulative Totals.

VWAP = Cumulative(Typical Price x Volume) / Cumulative(Volume)

Anchor Period

Indicator calculation period. This setting specifies the Anchor, i.e. how frequently the VWAP calculation will be reset. For VWAP to work properly, each VWAP period should include several bars inside of it, so e.g. setting Anchor to 'Session' and timeframe to '1D' is not useful because the indicator will be reset on every bar. Possible values: Session, Week, Month, Quarter, Year, Decade, Century, Earnings (reset on earnings), Dividends (reset on dividends), Splits (reset on splits).

# My VWAP - DataFrame Code:

df['Typ_Price'] = (df['high'] + df['low'] + df['close'] ) /3

df['Typ_PriceVol'] = df['Typ_Price'] * df['volume']

df['Cum_Vol_Price'] = df['Typ_PriceVol'].cumsum()

df['Cum_Vol'] = df['volume'].cumsum()

df['VWAP'] = df['Cum_Vol_Price'] / df['Cum_Vol']

print(df)
Abhishek Rai
  • 2,159
  • 3
  • 18
  • 38
Hawks
  • 71
  • 1
  • 2
  • VWAP is an intraday indicator, you have to limit the `cumsum( )` in order to start at the 1st trading hour and end at market close for the day, for each day. – P. Pinho Mar 08 '22 at 16:12
  • I am also looking for an answer to this question. My VWAP function is here - `def calculate_vwap(data): data['VWAP'] = data.groupby('date').apply(lambda x: ((x['high'] + x['low'] + x['close']) / 3 * x['volume']).cumsum() / x['volume'].cumsum()).reset_index(level=0, drop=True) return data['VWAP']` – Shri Jun 18 '23 at 16:35

0 Answers0