Alert on a condition
An alert: a loud marker and a zone the moment a breakout triggers, and a level left behind so you can see where it fired. Turn the alert itself on in the indicator settings, Alert tab, and it fires on the marker and the zone with a sound and a popup titled Breakout Alert.
//@xfscript 1
indicator("Breakout Alert")
let lookback: int = input.int(20, "lookback", "Lookback", 2, 500, 1)
let require_body: bool = input.bool(true, "requireBody", "Body must close beyond")
let alert_color: color = input.color("#F59E0B", "alertColor", "Alert")
let highest_high: float = ta.highest(high, lookback)
let lowest_low: float = ta.lowest(low, lookback)
let prior_high: float = highest_high[1]
let prior_low: float = lowest_low[1]
let up_edge: bool = close > prior_high
let down_edge: bool = close < prior_low
let strong_up: bool = !require_body || close > open
let strong_down: bool = !require_body || close < open
let broke_up: bool = up_edge && strong_up
let broke_down: bool = down_edge && strong_down
var last_alert: float = nan
last_alert := nz(last_alert[1], nan)
if broke_up {
last_alert := close
}
if broke_down {
last_alert := close
}
plot.marker(broke_up, high, alert_color, "triangle", "above", 14, 4)
plot.marker(broke_down, low, alert_color, "triangle", "below", 14, 4)
plot.zone(broke_up, high, prior_high, alert_color, 0.2, 0, 10, "bullish")
plot.zone(broke_down, prior_low, low, alert_color, 0.2, 0, 10, "bearish")
plot.text(broke_up, high, "BREAKOUT UP", alert_color, "above", 10, 18, 60)
plot.text(broke_down, low, "BREAKOUT DOWN", alert_color, "below", 10, 18, 60)
plot.hline(last_alert, alert_color, 1, "dashed", "Last alert")
plot.label(str.concat("LAST ALERT ", format.fixed(last_alert, 2)), alert_color)