The example records pivots only after their right-hand bars close. With rightBars = 5, a pivot on bar 10 becomes available at the close of bar 15. Breaks are checked against the same level already known before the current bar, before adopting a new pivot, so a level update cannot by itself create a cross. Each stored pivot can trigger once. This detects pivot breaks, not a prior trend, divergence, a complete Dow Theory reversal, or profitable entries. Plots are not shifted into the past, and the code places no orders.
Confirmed pivot-break alerts in Pine Script v6pinescript
//@version=6
indicator("Confirmed Pivot Breaks [Pineify]", overlay=true)
int leftBars = input.int(5, "Left Bars", minval=1, maxval=100)
int rightBars = input.int(5, "Right Bars", minval=1, maxval=100)
float ph = ta.pivothigh(high, leftBars, rightBars)
float pl = ta.pivotlow(low, leftBars, rightBars)
var float lastSwingHigh = na
var float lastSwingLow = na
var bool highBroken = false
var bool lowBroken = false
// Test the level known before this bar. Do not cross a changing series.
float testedHigh = lastSwingHigh
float testedLow = lastSwingLow
bool bullishBreak = barstate.isconfirmed and not highBroken and not na(testedHigh) and close > testedHigh and close[1] <= testedHigh
bool bearishBreak = barstate.isconfirmed and not lowBroken and not na(testedLow) and close < testedLow and close[1] >= testedLow
if barstate.isconfirmed
if bullishBreak
highBroken := true
if bearishBreak
lowBroken := true
// Newly confirmed pivots are available for testing from the next bar.
if not na(ph)
lastSwingHigh := ph
highBroken := false
if not na(pl)
lastSwingLow := pl
lowBroken := false
plot(testedHigh, "High tested on this bar", color=color.red, style=plot.style_linebr)
plot(testedLow, "Low tested on this bar", color=color.green, style=plot.style_linebr)
plotshape(bullishBreak, title="Bullish Pivot Break", style=shape.labelup, location=location.belowbar, color=color.green, text="BREAK UP", textcolor=color.white)
plotshape(bearishBreak, title="Bearish Pivot Break", style=shape.labeldown, location=location.abovebar, color=color.red, text="BREAK DOWN", textcolor=color.white)
if bullishBreak
alert("Confirmed close above pivot " + str.tostring(testedHigh), alert.freq_once_per_bar_close)
if bearishBreak
alert("Confirmed close below pivot " + str.tostring(testedLow), alert.freq_once_per_bar_close)