alert reference
Script-declared alerts with typed payloads, triggered from handler code.
Script-declared alerts with typed payloads, triggered from handler code.
alert
alert(title: string, description: string = "", onClose: bool = false, payload: Type = null) -> AlertDeclares an alert that the script can trigger.
Use this directly in an alert declaration. Keep the declaration's name
stable; it identifies the alert.
titleis the required display name.descriptionadds optional help text. Both must be string literals.onClosedefaults to false, allowing the alert to fire on the first trigger during a bar. Set it to true to fire only when a bar is confirmed.payloadoptionally names a declared record type. Its fields can be string, int, float, bool, or nullable versions of those types. Omit it or use null for an alert with no payload.
Alerts fire only on live data, at most once per bar for each declaration.
Example
script "Alert Declaration"
type CrossPayload {
side: string
price: float
}
alert crossed = alert(
payload: CrossPayload,
title: "EMA cross",
description: "Fast EMA crossed slow EMA",
)
data chart = subscribe(data.ohlcv)
on chart.update {
let fast = ta.ema(chart.close, 12)
let slow = ta.ema(chart.close, 26)
if ta.crossover(fast, slow) {
crossed.trigger(CrossPayload { side: "up", price: chart.close ?? 0.0 })
}
}alert.trigger
alert.trigger(payload: P = null) -> voidTriggers an alert on live data.
Call this on an alert you declared, or an alias of that alert, from a handler or a function called by a handler.
For an alert with a payload, pass one record of its declared type. For an alert without a payload, pass no argument. The call returns no value.
Each alert can fire at most once per bar. Historical replay evaluates the
arguments but does not send an alert. With onClose: true, triggers during a
forming bar do not send an alert either.
Example
script "Alert Trigger"
alert oversold = alert(title: "RSI oversold")
data chart = subscribe(data.ohlcv)
on chart.update {
let rsi = ta.rsi(chart.close, 14)
if (rsi ?? 50.0) < 30.0 {
oversold.trigger()
}
}