Why Alerts Matter
An indicator that requires you to watch the chart constantly defeats its purpose. Alerts let your indicator notify you when conditions are met — so you can step away from the screen and still catch every signal.
MQL5 provides four alert delivery methods, from simple on-screen pop-ups to mobile push notifications.
Alert Types in MQL5
1. Pop-up Alert — A dialog box with a sound. The simplest and most common.
Alert("EURUSD: RSI crossed above 70 - Overbought!");
You can pass multiple arguments and they will be concatenated:
Alert(_Symbol, " | ", EnumToString(_Period), " | RSI: ",
DoubleToString(rsiValue, 1), " - OVERBOUGHT");
2. Sound Alert — Play a .wav file without a dialog box. Less intrusive.
PlaySound("alert.wav"); // plays from MQL5/Sounds/ folder
3. Push Notification — Sends to your phone via the MetaTrader mobile app. You must configure your MetaQuotes ID in MT5 (Tools > Options > Notifications).
SendNotification("EURUSD Buy Signal - RSI: " + DoubleToString(rsiValue, 1));
4. Email Alert — Sends an email. Requires SMTP configuration in MT5 (Tools > Options > Email).
SendMail("Trading Alert: EURUSD",
"Buy signal detected.
RSI: " + DoubleToString(rsiValue, 1) +
"
Time: " + TimeToString(TimeCurrent()));
Preventing Duplicate Alerts
The biggest mistake in alert coding: firing the same alert hundreds of times because the condition remains true across multiple ticks. You must ensure each alert fires once per signal.
// Method 1: Track the last alert bar
datetime lastAlertBar = 0;
void CheckAlert(double rsiValue, datetime barTime)
{
if(barTime == lastAlertBar) return; // already alerted this bar
if(rsiValue > 70)
{
Alert(_Symbol, " RSI Overbought: ", DoubleToString(rsiValue, 1));
lastAlertBar = barTime;
}
else if(rsiValue < 30)
{
Alert(_Symbol, " RSI Oversold: ", DoubleToString(rsiValue, 1));
lastAlertBar = barTime;
}
}
// Method 2: Track the signal state (better for crossover signals)
bool wasAbove70 = false;
void CheckCrossAlert(double rsiValue)
{
bool isAbove70 = (rsiValue > 70);
// Alert only on the TRANSITION from below to above
if(isAbove70 && !wasAbove70)
{
Alert(_Symbol, " RSI crossed above 70");
}
// Alert on transition from above to below
if(!isAbove70 && wasAbove70)
{
Alert(_Symbol, " RSI crossed below 70");
}
wasAbove70 = isAbove70;
}
Making Alerts Configurable
Professional indicators let users choose which alert types they want:
input bool EnablePopupAlert = true; // Show Pop-up Alerts
input bool EnableSoundAlert = true; // Play Sound
input bool EnablePushAlert = false; // Send Push Notification
input bool EnableEmailAlert = false; // Send Email
input string AlertSound = "alert.wav"; // Alert Sound File
void FireAlert(string message)
{
if(EnablePopupAlert)
Alert(message);
if(EnableSoundAlert)
PlaySound(AlertSound);
if(EnablePushAlert)
SendNotification(message);
if(EnableEmailAlert)
SendMail("MT5 Alert: " + _Symbol, message);
}
Alert with Price Level
A practical pattern — alert when price crosses a moving average:
input bool AlertOnCross = true; // Alert on MA Cross
datetime lastCrossAlert = 0;
bool wasPriceAboveMA = false;
void CheckMACrossAlert(double price, double maValue, datetime barTime)
{
if(!AlertOnCross) return;
if(barTime == lastCrossAlert) return;
bool isPriceAboveMA = (price > maValue);
if(isPriceAboveMA && !wasPriceAboveMA)
{
FireAlert(_Symbol + " Price crossed ABOVE MA " +
DoubleToString(maValue, _Digits));
lastCrossAlert = barTime;
}
else if(!isPriceAboveMA && wasPriceAboveMA)
{
FireAlert(_Symbol + " Price crossed BELOW MA " +
DoubleToString(maValue, _Digits));
lastCrossAlert = barTime;
}
wasPriceAboveMA = isPriceAboveMA;
}
Best Practices
- Always prevent duplicates — One alert per signal, not per tick
- Include context — Symbol, timeframe, indicator value, and timestamp in every alert message
- Make alerts optional — Use input parameters so users can enable/disable each alert type
- Use meaningful sounds — Different sounds for buy vs sell signals help when you are away from the screen
- Test on live charts — Alert behavior can differ between backtesting and live trading
To receive push notifications, install the MetaTrader 5 mobile app, find your MetaQuotes ID in the app settings, and enter it in MT5 Desktop under Tools > Options > Notifications. Test with SendNotification("Test") to confirm it works.