Sends an alert message when the line runs on a bar, if an alert on the script is active.
alert(message, freq) fires an alert for the script that calls it. It runs inside your logic, so you decide when: alert("Crossed up") inside an if block fires only on bars where the condition holds. The message can be built at run time, for example with str.tostring(close).
freq controls how often one bar can fire: alert.freq_once_per_bar (the default) allows one alert per bar even when the condition flips on ticks, alert.freq_once_per_bar_close waits for the bar to close, and alert.freq_all fires on every call.
Syntax
alert(message: series string, freq: input string (optional)) -> voidArguments
| Name | Type | Default | Description |
|---|---|---|---|
| message | series string | required | The text of the alert. It can be built from variables at run time. |
| freq | input string | alert. | How often an alert may fire within a bar: alert.freq_once_per_bar, alert.freq_once_per_bar_close or alert.freq_all. |
Returns
Nothing.
Remarks
- One alert on the script, set to fire on any alert() call, covers every alert() in it.
- Unlike alertcondition(), alert() works in indicators and strategies and can sit in any scope, including loops and functions.
- Alerts fire in real time only. Calls on historical bars do nothing.
- To receive these messages, create an alert on the script and choose any alert() function call as its condition. The text you pass becomes the alert message.
Example
//@version=6
indicator("Cross alert", overlay = true)
fast = ta.sma(close, 10)
slow = ta.sma(close, 30)
plot(fast, "Fast", color.green)
plot(slow, "Slow", color.red)
if ta.crossover(fast, slow)
alert("Fast crossed above slow at " + str.tostring(close), alert.freq_once_per_bar_close)See also
alertconditionalert.freq_once_per_baralert.freq_once_per_bar_closealert.freq_allbarstate.isconfirmed
Runs on Sidechair