Average True Range

Below is a Pine Script (v6) that leverages the Average True Range (ATR) to generate dynamic signals. The script includes customizable parameters, optional enhancements, and plots background colors when alert conditions are met.

//@version=6
indicator("ATR Signal with Background Alerts", overlay=true)

// === Inputs ===
atrLength     = input.int(14, "ATR Length", minval=1)
atrMultiplier = input.float(1.5, "ATR Multiplier", minval=0.1)
signalType    = input.string("Breakout", title="Signal Type", options=["Breakout", "Reversal"])
showATR       = input.bool(false, "Show ATR Plot?")

// === ATR Calculation ===
atr = ta.atr(atrLength)

// === Signal Logic ===
breakoutUp   = close > close[1] + atr * atrMultiplier
breakoutDown = close < close[1] - atr * atrMultiplier
reversalUp   = close > high[1] + atr * atrMultiplier
reversalDown = close < low[1] - atr * atrMultiplier

longSignal  = signalType == "Breakout" ? breakoutUp : reversalUp
shortSignal = signalType == "Breakout" ? breakoutDown : reversalDown

// === Plotting ===
plot(showATR ? atr : na, title="ATR", color=color.orange, linewidth=2)
plotshape(longSignal,  title="Long Signal",  location=location.belowbar, color=color.green, style=shape.triangleup, size=size.small)
plotshape(shortSignal, title="Short Signal", location=location.abovebar, color=color.red,   style=shape.triangledown, size=size.small)

// === Background Coloring on Alerts ===
bgcolor(longSignal ? color.new(color.green, 85) : na, title="Long Signal BG")
bgcolor(shortSignal ? color.new(color.red, 85) : na, title="Short Signal BG")

// === Alerts ===
alertcondition(longSignal,  title="ATR Long Signal",  message="ATR-based LONG signal triggered!")
alertcondition(shortSignal, title="ATR Short Signal", message="ATR-based SHORT signal triggered!")

Key Parameters

1

ATR Length (atrLength)

  • Number of bars used to calculate ATR - default is 14

2

ATR Multiplier (atrMultiplier)

  • Mulitiplies the ATR value to set the threshold for signals. Default is 1.5

3

Signal Type (signalType)

  • Choose between "breakout" (Price moves more than ATR above/below previous close or "reversal" where price reverses by more than the ATR)

4

Show ATR Plot (showATR)

  • Optionally show the ATR line on the plot.

How It Works

1

ATR Calculation:

Uses ta.atr() to measure volatility over the slected period.

2

Signal Generation:

  • Breakout: Signals when price moves more than ATR * multiplier above or below the previous close.

  • Reversal: Signals when price reverses by more than ATR * multiplier from the previous high/low.

3

Visuals:

  • Plots up/down triangles for signals

  • Colors the chart background green/red when a signal is triggered.

  • Optionally plots the ATR line for reference.

Optional Enhancements

1

Add Trailing Stop Logic

Use ATR to plot dynamic stop-loss levels.

2

Multi-Timeframe ATR

Calculate ATR from a higher timeframe for more robust signals.

3

Combine with other indicators

Add RSI or moving average filters for confirmation.

4

Customizable Colors

Allow users to select their own background and signal colors.

Last updated

Was this helpful?