Skip to content

Instantly share code, notes, and snippets.

@jxm262
Last active June 25, 2023 07:44
Show Gist options
  • Save jxm262/b0859b1b9d72f3f0f74de927a2cab1a6 to your computer and use it in GitHub Desktop.
Save jxm262/b0859b1b9d72f3f0f74de927a2cab1a6 to your computer and use it in GitHub Desktop.
TradingView VWAP-Stdev-Bands-v2-Mod-UPDATE in Python
We can make this file beautiful and searchable if this error is corrected: No commas found in this CSV file in line 0.
Date Open High Low Close Volume
2020-05-06 18:15:00-04:00 2832 2838.5 2832 2838 3750
2020-05-06 18:30:00-04:00 2838 2838.5 2826 2827.75 6864
2020-05-06 18:45:00-04:00 2827.75 2830.5 2827 2829.5 2937
2020-05-06 19:00:00-04:00 2829.5 2830.5 2823 2826.5 4619
2020-05-06 19:15:00-04:00 2826.75 2829.5 2825.25 2827.75 3610
2020-05-06 19:30:00-04:00 2827.75 2829.5 2825.5 2826.75 2460
2020-05-06 19:45:00-04:00 2826.75 2830.75 2826.25 2830.5 2531
2020-05-06 20:00:00-04:00 2830.5 2833.5 2830.25 2833.5 2361
import pandas as pd
import mplfinance as mpf
import math
df = pd.read_csv('./data.csv', sep=',', quotechar='"')
df.set_index(['Date'], inplace=True)
df.index = pd.to_datetime(df.index)
df.index.name = 'Date'
# from here = https://www.tradingview.com/script/fD2ppI7I-VWAP-Stdev-Bands-v2-Mod-UPDATE/
# from tradingview = hl2 = midpoint of bar
df['HL2'] = (df.High + df.Low) / 2
df['VOLUME_SUM'] = df.Volume.cumsum()
df['VWAP'] = (df.Volume * df.HL2).cumsum() / df.VOLUME_SUM
# y'all doing some weird ass variance calculation here
df['V2SUM'] = (df.Volume * df.HL2 * df.HL2).cumsum()
def sigma_tradingview(x):
val = x.V2SUM / x.VOLUME_SUM - x.VWAP * x.VWAP
# kind of a hack to mimic that tv max() function
if val <= 0:
val = 0
return math.sqrt(val)
df['STDEV_TV'] = df.apply(sigma_tradingview, axis=1)
stdev_multiple_1 = 1.28
stdev_multiple_2 = 2.01
stdev_multiple_3 = 2.51
df['STDEV_1'] = df.VWAP + stdev_multiple_1 * df['STDEV_TV']
df['STDEV_N1'] = df.VWAP - stdev_multiple_1 * df['STDEV_TV']
addplot = [
mpf.make_addplot(df['VWAP']),
mpf.make_addplot(df['STDEV_1']),
mpf.make_addplot(df['STDEV_N1']),
]
mpf.plot(df, type='candle', addplot=addplot)
@mohaayman
Copy link

How do I write vwap z score code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment