Guides
Alerts
alert(condition, "Message", every: duration)alert records an alert on each bar where condition is true.
- At most one alert per call place and per bar. On a live bar that is recalculated many times, the alert is still counted once for that bar.
every:sets a minimum delay between two alerts from the same call, measured in bar time. Without it, every bar where the condition is true raises an alert.- The alert is drawn as a small orange mark at the top of the chart, on its bar.
- It can be drawn on the price chart from a pane script with
overlay: true.
What happens when an alert fires#
The app only signals new alerts on recent bars — never the alerts of the history it loads, and never right after the context changes (instrument, unit, session, or the script's code). Each alert is signaled once. A script hidden from the chart signals nothing.
The … menu of the StrateScript panel holds the alert options, shared by all scripts:
- Alert sound: the sound played, or none;
- Alert notifications: a system notification;
- Alerts in Replay too: also signal alerts while a Replay plays;
- Alert log…: the list of alerts raised.
Alerts also appear in the Console, next to print lines.
Avoiding false alerts#
A condition computed on the bar that is still forming can be true for a moment and false at the close. Add bar.confirmed to alert only on closed bars:
indicator "Cross alert" { overlay: true }
len = input.int("Length", 20, min: 1)
avg = sma(close, len)
plot(avg, #e8c15a, title: "SMA")
cross = crossover(close, avg)
alert(cross and bar.confirmed, "Close crossed above the SMA", every: 30m)
marker(cross and bar.confirmed, shape: "triangle_up", location: "below")Alerts from events#
alert belongs to the bar it is called on. To alert on an order-flow event, record the event in a state variable inside the handler, then raise the alert from the body:
indicator "Large print alert" { overlay: true }
minSize = input.int("Min size", 100, min: 1)
state biggest = 0
on trade(t) {
if t.size > biggest { biggest = t.size }
}
alert(biggest >= minSize, "Print of " + str(biggest) + " contracts")
biggest = 0Related functions#
marker(condition, …)draws a shape without alerting: see Plotting.signal(…)draws a small violet mark at the top of the chart whenever it is called, whatever its first argument (one mark per call place and per bar): put it inside anif. It is not an alert.- Patterns emit named signals that other code can listen to: see Patterns.