I am making an indicator showing swing lows and highs (3 bar pattern where a swing high has lower candle from each side and swing low has higher candle from each side)
I made it but I want to make an alert on when it's breached. As you can see from code the actual high or low is made after swing is formed. I display correctly swing by adding (offset-2). But alert triggers at actual swing high/low.
Q: How to get second previous candles high/low?
//@version=5
indicator(title='Fractals Alerts', shorttitle='Fractals Alerts', overlay=true)
n = input.int(title='Periods', defval=2, minval=2)
//High(n-1) < High(n)
//High(n + 1) < High(n)
upFractal = high[n - 1] < high[n] and high[n + 1] < high[n]
//Low(n-1) > Low(n)
//Low(n + 1) > Low(n)
dnFractal = low[n - 1] > low[n] and low[n + 1] > low[n]
//make a shape
plotshape(upFractal, "High Broken", shape.xcross, location.abovebar, offset=-2, color=color.new(color.red, 0))
plotshape(dnFractal, "Low Broken", shape.xcross, location.belowbar, offset=-2, color=color.new(color.red, 0))
//get value high/low from fractals
upfractalhigh = ta.valuewhen(upFractal, high, 0)
upfractalbroke = (close > upfractalhigh)
//ignore//plotshape(frhitr, "Fractal High", shape.cross, location.abovebar, color=color.new(color.green, 0))
//set condition
dnfractallow = ta.valuewhen(dnFractal, low, 0)
dnfractalbroke = (close < dnfractallow)
//Alerts
alertcondition(upfractalbroke, "high brake", "High Broken")
alertcondition(dnfractalbroke, "low brake", "Low Broken")