OPEN-SOURCE SCRIPT

3-Candle Swing Highs & Lows

81
//version=5
indicator("3-Candle Swing Highs & Lows", overlay=true, max_lines_count=1000)

// Inputs
highColor = input.color(color.red, "Swing High (Unbroken)")
highBreachCol = input.color(color.green, "Swing High (Breached)")
lowColor = input.color(color.blue, "Swing Low (Unbroken)")
lowBreachCol = input.color(color.orange, "Swing Low (Breached)")

// Arrays for storing lines and prices
var line[] highLines = array.new_line()
var float[] highPrices = array.new_float()
var line[] lowLines = array.new_line()
var float[] lowPrices = array.new_float()

// --- Swing High condition ---
// We check candle[1] (the middle one) against candle[2] and candle[0]
isSwingHigh = high[1] > high[2] and high[1] > high[0]

// --- Swing Low condition ---
isSwingLow = low[1] < low[2] and low[1] < low[0]

// If swing high found (confirmed after bar closes)
if isSwingHigh
newHigh = line.new(bar_index - 1, high[1], bar_index, high[1], extend=extend.right, color=highColor, width=2)
array.push(highLines, newHigh)
array.push(highPrices, high[1])

// If swing low found (confirmed after bar closes)
if isSwingLow
newLow = line.new(bar_index - 1, low[1], bar_index, low[1], extend=extend.right, color=lowColor, width=2)
array.push(lowLines, newLow)
array.push(lowPrices, low[1])

// Update line colours for swing highs
for i = 0 to array.size(highLines) - 1
ln = array.get(highLines, i)
lvl = array.get(highPrices, i)
if close > lvl
line.set_color(ln, highBreachCol)
else
line.set_color(ln, highColor)

// Update line colours for swing lows
for i = 0 to array.size(lowLines) - 1
ln = array.get(lowLines, i)
lvl = array.get(lowPrices, i)
if close < lvl
line.set_color(ln, lowBreachCol)
else
line.set_color(ln, lowColor)

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.