Learning Center

Converting alertcondition() to MQL alerts that fire once

Stop MetaTrader alerts from repeating on every tick by remembering the last bar that alerted.

The problem

TradingView decides how often an alert fires in its alert dialog. In MetaTrader, Alert() runs every time your code reaches it, which can be dozens of times per candle.

Pine Script

alertcondition(ta.crossover(ta.rsi(close,14), 30), "RSI up", "RSI crossed above 30")

MQL5

datetime lastAlertBar = 0;

void CheckAlert(bool condition){
   datetime bar = iTime(_Symbol, _Period, 1);   // last closed bar
   if(condition && bar != lastAlertBar){
      Alert(_Symbol, " ", EnumToString(_Period), ": RSI crossed above 30");
      SendNotification("RSI crossed above 30 on " + _Symbol);  // optional push to phone
      lastAlertBar = bar;
   }
}

Tip

Evaluate the condition on bar 1, not bar 0, unless you really want intrabar alerts. Push notifications need your MetaQuotes ID set in Tools → Options → Notifications.

Related code snippets