alert() - Pine Script Function
alert()
Section titled “alert()”Overview
Section titled “Overview”Use alert() inside an indicator or strategy to fire a TradingView alert the moment a realtime condition occurs. The function runs only on the last, realtime bar; historical bars do not trigger the alert, so it won’t spam your log when the script loads.
Syntax
Section titled “Syntax”alert(message, freq) → voidParameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
message | series string | Text payload for the alert notification. |
freq | const string | Alert frequency constant (see alert.freq_*). |
Remarks
Section titled “Remarks”- Works only on the realtime bar; historical bars are ignored.
- The script must have an active alert created via Create Alert → Any alert() function call.
- Alert frequency constants include
alert.freq_once_per_bar,alert.freq_once_per_bar_close, andalert.freq_all.
Examples
Section titled “Examples”Example 1
Section titled “Example 1”//@version=5indicator("alert() example", overlay=true)
maLen = input.int(14, "MA length")ma = ta.sma(close, maLen)crossUp = ta.crossover(close, ma)
if barstate.isrealtime and crossUp alert( "Price (" + str.tostring(close, format.mintick) + ") crossed above MA (" + str.tostring(ma, format.mintick) + ").", alert.freq_once_per_bar)
plot(ma, "MA")plotshape(crossUp, title="Cross up", style=plot.shape.triangleup, location=location.belowbar)