OPEN-SOURCE SCRIPT

VWAP and MA Mean Reversion Strategy with ATR Stop Loss

//version=5
strategy("VWAP and MA Mean Reversion Strategy with ATR Stop Loss", overlay=true)
// Inputs
length = input(14, "MA Length")
atrLength = input(14, "ATR Length")
atrMultiplier = input(1.5, title="ATR Multiplier for Stop Loss")
// Calculate VWAP, Moving Average, and ATR
vwap = ta.vwap(hlc3)
ma = ta.sma(close, length)
atr = ta.atr(atrLength)
// Buy condition: Price is below VWAP but above MA
longCondition = close < vwap and close > ma
if (longCondition)
strategy.entry("Buy", strategy.long)
stopPrice = close - atrMultiplier * atr
strategy.exit("Long Stop Loss", "Buy", stop=stopPrice)
// Sell condition: Price is above VWAP but below MA
shortCondition = close > vwap and close < ma
if (shortCondition)
strategy.entry("Sell", strategy.short)
stopPrice = close + atrMultiplier * atr
strategy.exit("Short Stop Loss", "Sell", stop=stopPrice)
// Plotting
plot(vwap, title="VWAP", color=color.blue)
plot(ma, title="MA", color=color.orange)
Bands and ChannelsBill Williams IndicatorsBreadth Indicators

Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in publication is governed by House rules. You can favorite it to use it on a chart.

Want to use this script on a chart?


Also on:

Disclaimer