Code Library / VWAP

Pine Script VWAP Mean Reversion

Fade price extending beyond 2 SD from VWAP.

Language: Pine Script · Category: VWAP · Tags: VWAP, mean reversion, deviation, Pine Script, fade

Source code

//@version=5
strategy("VWAP MR",overlay=true)
v=ta.vwap(hlc3)
svol=ta.cum(hlc3*volume); sv=ta.cum(volume)
s2v=ta.cum(hlc3*hlc3*volume)
sd=math.sqrt(math.max(s2v/sv-math.pow(svol/sv,2),0))
if close<v-sd*2
    strategy.entry("Long",strategy.long)
if close>v+sd*2
    strategy.entry("Short",strategy.short)
if strategy.position_size>0 and close>v
    strategy.close("Long")
plot(v,"VWAP",color.blue,2)

How to use this snippet

This is a building block, not a finished trading system. Paste it into your own Pine Script project, compile it, and test it on a demo account with your broker's real spreads before relying on it. You can convert it to MQL4 or MQL5 with the Pine Script converter.

Related snippets