Alert and SendNotification in Pine Script: alert()
Short answer
All four map to Pine's alert(msg, freq), or to alertcondition() for a condition the user picks. Pine has one alert mechanism: the user chooses pop-up, sound, app push, email or webhook in TradingView's Create alert dialog, so the channel choice moves out of the code.
MQL5 has one function per delivery channel: Alert() opens the terminal's pop-up with a sound, SendNotification() pushes to the MetaTrader mobile app, SendMail() sends an email through the configured SMTP account, and PlaySound() plays a .wav file. Pine Script has one mechanism: the script raises an alert event with alert(message, freq) or declares a condition with alertcondition(condition, title, message), and the user decides in TradingView's "Create alert" dialog whether it becomes a pop-up, sound, app push, email or webhook. All four MQL5 calls map to alert(); the channel choice moves out of the code.
What Alert and SendNotification compute
Alert(arg1, arg2, …) concatenates up to 64 arguments into one string and shows it in the Alerts window with the symbol and timeframe; it fires every time the line executes. SendNotification(text) sends at most 255 characters to the MetaQuotes ID set in the terminal options, is rate-limited (no more than two calls per second and ten per minute), and does nothing in the Strategy Tester. PlaySound plays a file from the terminal's Sounds folder. None of them has a built-in "once per bar": an indicator that calls Alert inside OnCalculate fires on every tick while the condition holds, so authors add a guard such as storing the bar time of the last alert, and they usually test the last closed bar (rates_total - 2) to avoid alerting on a signal that disappears before the bar closes.
Pine's alert() is called inside the script like Alert, but it only has an effect when the user has created an alert on the script with the "Any alert() function call" option. Its freq argument does the guarding: alert.freq_once_per_bar (default; the first call in a bar), alert.freq_once_per_bar_close (only when the realtime bar closes, so only confirmed signals) or alert.freq_all. The message can be built at runtime. alertcondition() instead registers a named condition that appears in the dialog's condition list; its message must be a constant string, with placeholders like {{ticker}}, {{close}} and {{plot_0}} filled in when it fires.
The Pine Script equivalent
An RSI indicator that alerts once when the RSI of the last closed bar crosses above 30, through pop-up, push and sound.
//@version=6
indicator("RSI cross alert", overlay = false)
len = input.int(14, "RSI length")
rsi = ta.rsi(close, len)
upX = ta.crossover(rsi, 30) or (rsi[1] < 30 and rsi == 30) // MQL5: prev < 30 and now >= 30
plot(rsi, "RSI", color = color.rgb(186, 85, 211))
hline(30)
hline(70)
// Script-defined alert: fires when the condition is true on a closed bar, at most once per bar.
// Pop-up, sound, push, email and webhook are all chosen in the alert dialog, not in code.
if upX
alert(syminfo.ticker + " RSI crossed above 30 at " + str.tostring(close, format.mintick), alert.freq_once_per_bar_close)
// Alternative: a named condition the user picks in "Create alert". The message must be constant
// (placeholders like {{ticker}} and {{close}} are filled in by TradingView).
alertcondition(upX, "RSI up through 30", "{{ticker}} RSI crossed above 30 at {{close}}")#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots 1
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrMediumOrchid
#property indicator_level1 30
#property indicator_level2 70
input int InpLen = 14; // RSI length
input bool InpPush = true; // Send push notification
input bool InpSound = true; // Play sound
double RsiBuffer[];
int rsiH;
datetime lastAlert = 0;
int OnInit()
{
SetIndexBuffer(0, RsiBuffer, INDICATOR_DATA);
rsiH = iRSI(_Symbol, _Period, InpLen, PRICE_CLOSE);
return(rsiH == INVALID_HANDLE ? INIT_FAILED : INIT_SUCCEEDED);
}
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
int toCopy = (prev_calculated == 0) ? rates_total : rates_total - prev_calculated + 1;
if(CopyBuffer(rsiH, 0, 0, toCopy, RsiBuffer) <= 0)
return(0);
if(rates_total < 3 || prev_calculated == 0)
return(rates_total); // no alerts while loading history
// Check the last CLOSED bar (rates_total - 2), once per bar.
int c = rates_total - 2;
bool upX = RsiBuffer[c - 1] < 30.0 && RsiBuffer[c] >= 30.0;
if(upX && time[c] != lastAlert)
{
lastAlert = time[c];
string msg = _Symbol + " RSI crossed above 30 at " + DoubleToString(close[c], _Digits);
Alert(msg); // terminal pop-up
if(InpPush) SendNotification(msg); // MetaQuotes ID push
if(InpSound) PlaySound("alert2.wav");
}
return(rates_total);
}| MQL5 | Pine |
|---|---|
Alert(a, b, c) | alert(str.tostring(a) + b + …); pop-up chosen in the dialog |
SendNotification(msg) | Same alert(); "Notify in app" in the dialog |
SendMail(subject, body) | Same alert(); "Send email" in the dialog (subject is fixed by TradingView) |
PlaySound(file) | "Play sound" in the dialog; no per-call sound choice |
| Last closed bar + bar-time guard | alert.freq_once_per_bar_close |
| Guard on bar time only | alert.freq_once_per_bar |
| No guard | alert.freq_all |
| WebRequest to a URL | Webhook URL in the dialog; the alert message is the request body |
Where the two differ
Where the alert runs. MQL5 alerts run in the terminal, so the computer must be on. TradingView alerts run on TradingView's servers once created, with the script and inputs frozen at creation time; changing inputs later requires recreating the alert.
Who chooses the channel. In MQL5 the code (often driven by input booleans like InpPush) decides. In Pine those inputs are unnecessary; one alert() call is delivered wherever the user's alert is configured.
History. MQL5 code must explicitly avoid alerting while history loads (prev_calculated == 0). Pine's alert() only fires on realtime bars, so no guard is needed.
Repainting. With the default freq_once_per_bar, a condition evaluated on the forming bar can fire and then be false at the close. Testing the closed bar, as the MQL5 code does with rates_total - 2, corresponds to freq_once_per_bar_close on the current bar, not to reading [1].
Strategies. In a Pine strategy, order fills can also trigger alerts with a per-order alert_message on strategy.entry/exit; that is the usual route for automated execution through a webhook.
What the Stratlate converter emits
The MQL5 to Pine engine maps Alert to alert(…), joining multiple arguments into one string with str.tostring on numeric values; SendNotification(msg) to the same alert(str.tostring(msg)); SendMail(subject, body) to one alert with subject + ": " + body; PlaySound(file) to alert("sound: " + file); and MessageBox to a helper that alerts and returns 1 (PineCatalog, messaging section). No freq argument is emitted, so the default alert.freq_once_per_bar applies, and no alertcondition is generated. Expect duplicates where the original sent the same message to several channels: each call becomes its own alert(). In the program above the calls converted, but the surrounding check is written against rates_total - 2 outside a per-bar loop, and the condition variable was declared but never assigned (it stays false), so the converted script would never alert; restate it as a per-bar condition like the hand-written version. Create a test alert on a fast timeframe to confirm it fires before relying on it.
Pitfalls
- Creating the alert with a condition from the dropdown list instead of "Any alert() function call".
alert()calls only fire with the latter;alertcondition()entries only with the former. - Building
alertconditionmessages from variables. Pine rejects a non-constant message; use placeholders such as{{close}}or switch toalert(). - Translating the closed-bar test as
upX[1]with the default frequency. It fires correctly but one bar late compared with MetaTrader's first-tick alert;freq_once_per_bar_closeon the current condition fires at the same moment. - Calling
alert()inside a loop or several times per bar withfreq_all. TradingView throttles alerts that fire too often and may stop the alert. - Keeping three separate calls for pop-up, push and sound. They become three alert events; one call is enough.
FAQ
Does Pine have a SendNotification for the TradingView app?
Not as a function. Any alert() or alertcondition() reaches the app when "Notify in app" is ticked in the alert's settings. The script cannot send a push on its own without an alert set up on it.
How do I alert only once per bar like the bar-time guard in MQL5?
Pass alert.freq_once_per_bar (the default) to fire on the first occurrence in a bar, or alert.freq_once_per_bar_close to fire only if the condition is still true when the bar closes.
Can Pine play a specific sound file like PlaySound?
No. The sound is a setting of the alert in TradingView, chosen from its own list; the script cannot select or play a file.
Convert the whole program
Paste an MQL4 or MQL5 indicator or Expert Advisor and get Pine Script v6 with this and every other call mapped, checked against the original on the same bars, and every construct that could not be carried over reported.
Open the converter