Strategy · Pine Script v5 · On the chart

MACD & VWAP Strategy with ATR SL/Targets

by adminalphabots

This strategy calculates MACD, ADX trend strength, ATR, and a rolling VWAP resetting at fixed bar intervals.

View on GitHub

Repaints

GitHub stars2on the repository
Pine versionv5runs as is
Lines of code58
Settings9inputs
LicenceMIT
Last update8 months ago28 Feb 2026

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new strategy, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart. The trades and results appear in the Strategy Tester tab; adjust the settings with the gear icon.

Source Code

58 lines of Pine Script v5 from tradingview/strategies/macd_vwap_atr_strategy.pine, published under MIT. Keep the author's credit and licence lines when you reuse it.

macd_vwap_atr_strategy.pine
//@version=5  1H, 2H
strategy("MACD & VWAP Strategy with ATR SL/Targets", overlay=true)

// === Input Parameters ===
fastMACD = input.int(10, "MACD Fast Length")
slowMACD = input.int(20, "MACD Slow Length")
signalPeriods = input.int(5, "MACD Signal Periods")
vwapBars = input.int(25, "VWAP Rolling Bars", minval=1)
minADX = input.int(20, "Minimum ADX Trend Strength")
atrLength = input.int(12, "ATR Length")
atrMultiplierSL = input.float(1.5, "ATR Multiplier for Stop Loss")
atrMultiplierTarget1 = input.float(4.0, "ATR Multiplier for Target 1")
//atrMultiplierTarget2 = input.float(2.0, "ATR Multiplier for Target 2")

// === Indicators ===
[macdLine, signalLine, _] = ta.macd(close, fastMACD, slowMACD, signalPeriods)
// Rolling VWAP calculation
varip float cumulativePV = 0.0
varip float cumulativeVol = 0.0
varip int barsCount = 0

if bar_index % vwapBars == 0
    cumulativePV := close * volume
    cumulativeVol := volume
    barsCount := 1
else
    cumulativePV += close * volume
    cumulativeVol += volume
    barsCount += 1

vwap = cumulativePV / cumulativeVol
[adx, _, _] = ta.dmi(14, 14)
atr = ta.atr(atrLength)

// === Entry Conditions ===
longCondition = ta.crossover(macdLine, signalLine) and close > vwap and adx >= minADX
shortCondition = ta.crossunder(macdLine, signalLine) and close < vwap and adx >= minADX

// === Alert Messages ===
symbolName = input.string("NSE:NIFTY25APRFUT", "Alert Symbol")

// === Strategy Execution ===
if (longCondition)
    strategy.entry("MACD-VWAP Long", strategy.long)
    strategy.exit("Long Exit", "MACD-VWAP Long",stop=close - atr * atrMultiplierSL,limit=close + atr * atrMultiplierTarget1)
    
if (shortCondition)
    strategy.entry("MACD-VWAP Short", strategy.short)
    strategy.exit("Short Exit", "MACD-VWAP Short",stop=close + atr * atrMultiplierSL,limit=close - atr * atrMultiplierTarget1)


plot(vwap, "Rolling VWAP", color=color.purple)
plot(strategy.position_size > 0 ? close - atr * atrMultiplierSL : na, "Long Stop", color=color.red, style=plot.style_linebr)
plot(strategy.position_size > 0 ? close + atr * atrMultiplierTarget1 : na, "Long Target 1", color=color.green, style=plot.style_linebr)
//plot(strategy.position_size > 0 ? close + atr * atrMultiplierTarget2 : na, "Long Target 2", color=color.lime, style=plot.style_linebr)
plot(strategy.position_size < 0 ? close + atr * atrMultiplierSL : na, "Short Stop", color=color.red, style=plot.style_linebr)
plot(strategy.position_size < 0 ? close - atr * atrMultiplierTarget1 : na, "Short Target 1", color=color.green, style=plot.style_linebr)
//plot(strategy.position_size < 0 ? close - atr * atrMultiplierTarget2 : na, "Short Target 2", color=color.lime, style=plot.style_linebr)

Overview

MACD & VWAP Strategy with ATR SL/Targets plots the VWAP line, stop loss levels, and profit targets directly on the price chart. Long positions enter when MACD crosses above its signal line while price is above VWAP and ADX exceeds the minimum threshold. Short positions enter on bearish MACD crosses below VWAP with qualifying ADX. Exits execute using ATR multipliers for stop loss and profit targets. User inputs configure indicator lengths, ADX thresholds, and ATR multiples. The calculation repaints on live bars.

Details

Published by

adminalphabots on GitHub

Type

Strategy, Pine Script v5

Draws

On the price chart

Category

Trend, Oscillators & momentum, Volatility & bands, Volume

Markets

Indices

Timeframes

M15-H1

Risk Notes

  • Signals can change on the live bar or after it closes (repainting)

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
MACD Fast Lengthint10fastMACD
MACD Slow Lengthint20slowMACD
MACD Signal Periodsint5signalPeriods
VWAP Rolling Barsint25vwapBars
Minimum ADX Trend Strengthint20minADX
ATR Lengthint12atrLength
ATR Multiplier for Stop Lossfloat1.5atrMultiplierSL
ATR Multiplier for Target 1float4.0atrMultiplierTarget1
Alert SymbolstringNSE:NIFTY25APRFUTsymbolName
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

6 stars

Capital41 Fusion Core Strategy is an overlay trading strategy that combines an exponential moving average trend filter, RSI, Williams %R, and volume/ATR expansion into a composite momentum oscillator. It executes long trades when the smoothed oscillator crosse

Strategy v6 TrendOscillators & momentum
Updated 8 weeks ago · MIT GitHub repository of Capital41 Fusion Core Strategy

This strategy executes trades on gold based on a confluence of Supertrend, RSI, MACD, Bollinger Bands, and volume spikes. It enters long or short positions when trend direction aligns with momentum and price positioning relative to volatility bands, filtered b

Strategy v6 TrendOscillators & momentum
Updated 1 year ago · None stated GitHub repository of XAUUSD High Probability Strategy

Parabolic SAR

by g-moe
3 stars

This strategy overlays a smoothed moving average on price and combines it with Aroon, Chaikin Money Flow and a Chaikin oscillator to define long and short conditions. Relative Vigor Index and Parabolic SAR values are calculated for exits. Entries can be limite

Strategy v5 TrendOscillators & momentum
Updated 5 months ago · None stated GitHub repository of Parabolic SAR

This strategy generates trade entries by combining up to five normalized 0-100 technical indicators: Relative Strength Index (RSI with divergence detection), Stochastic RSI, Money Flow Index (MFI), Relative Volatility Index (RVI), and Average Directional Index

Strategy v5 Oscillators & momentumVolatility & bands
Updated 1 year ago · None stated GitHub repository of TLC 0TO100 Five Indicators Strategy

12-BB

by sam30606
1 stars

This overlay strategy combines a Bollinger-style band with separate ATR upper and lower envelopes and optional ADX, Volume Flow Indicator, and RSI filters. It sizes positions as a percentage of equity. Settings change band length and multiplier, ATR periods an

Strategy v5 TrendOscillators & momentum
Updated 3 years ago · None stated GitHub repository of 12-BB

More strategys

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers