Pivot High/Low Comparison
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © rmunoz

//@version=5
indicator("Pivot High/Low Comparison", overlay = true)

leftBars = input(3)
rightBars=input(3)
pivotHigh = ta.pivothigh(high, leftBars, rightBars)
mostRecentPH = ta.valuewhen(pivotHigh > 0, pivotHigh, 0)
secondMostRecentPH = ta.valuewhen(pivotHigh > 0, pivotHigh, 1)
phColor = color.black

pivotLow = ta.pivotlow(low, leftBars, rightBars)
mostRecentPL = ta.valuewhen(pivotLow > 0, pivotLow, 0)
secondMostRecentPL = ta.valuewhen(pivotLow > 0, pivotLow, 1)
plColor = color.black

if mostRecentPH > secondMostRecentPH
    phColor := color.green
else
    phColor := color.red

if mostRecentPL > secondMostRecentPL
    plColor := color.green
else
    plColor := color.red

plot(pivotHigh, style=plot.style_line, linewidth = 1, color = phColor, offset = -rightBars)
plot(pivotLow, style=plot.style_line, linewidth = 1, color = plColor, offset = -rightBars)
plot(pivotHigh, style=plot.style_circles, linewidth = 3, color = phColor, offset = -rightBars)
plot(pivotLow, style=plot.style_circles, linewidth = 3, color = plColor, offset = -rightBars)

