Reputation: 13
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")
Upvotes: 1
Views: 7807
Reputation: 63
Interesting project!
I'm not sure I've understood what you're looking for, exactly, but I think I've been able to map the previous swing high's peak (or the previous swing low's trough). I've plotted it with a step line, which you should be able to use to trigger your alerts.
I added text and symbols that trigger whenever the previous fractal's broken, just so I could see what was going on. You'll find I've changed a few variable names and things, too.
Let me know if the code's any use.
//@version=5
indicator(title='Fractals Alerts', shorttitle='Fractals Alerts', overlay=true)
n = input.int(title='Periods', defval=2, minval=2)
upFractal = high[n - 1] < high[n] and high[n + 1] < high[n]
dnFractal = low[n - 1] > low[n] and low[n + 1] > low[n]
plotshape(upFractal, "SH", shape.circle, location.abovebar, offset=-2, color=color.new(color.green, 0))
plotshape(dnFractal, "SL", shape.circle, location.belowbar, offset=-2, color=color.new(color.red, 0))
ufh = ta.valuewhen(upFractal, ta.highest(3), 0)
dfl = ta.valuewhen(dnFractal, ta.lowest(3), 0)
sh_broke = ta.crossover(high, ufh)
sl_broke = ta.crossunder(low, dfl),
plotshape(sl_broke, "Break Down!", shape.arrowdown, location.belowbar, text="Break!", offset=0, color=color.new(color.black, 0))
plotshape(sh_broke, "Break Up!", shape.arrowup, location.abovebar, text="Break!", offset=0, color=color.new(color.black, 0))
plot(upFractal ? ufh: na, "High Broken", style=plot.style_stepline, offset=-2, color=color.new(color.green, 0))
plot(dfl, "Low Broken",style=plot.style_stepline, offset=-2, color=color.new(color.red, 0))
alertcondition(sh_broke, "SH Broken", "SH Broken")
alertcondition(sl_broke, "SL Broken", "SL Broken")
Upvotes: 0