How to Create an MT5 Expert Advisor That Sends Alerts Without Placing Trades
Last checked: 12 October 2026. This guide draws on the MQL5 reference for OnTick, CopyRates, SendNotification and the Strategy Tester documentation. Code is a teaching example; test on a demo account.
Most people think an Expert Advisor must place trades. It does not. An EA is simply a program that runs on every tick and is allowed to use trading functions if you let it. An alert-only EA watches the market and tells you when your conditions are met, and then leaves the decision to you. For many traders this is a safer starting point than full automation.
Why build an alert-only EA
- It cannot lose money by itself, because it contains no order functions at all.
- It lets you observe a rule in real market conditions before you consider automating it.
- It can watch several symbols at once, which a chart indicator cannot do easily.
- Unlike an indicator, an EA runs in its own thread, so it can call functions that indicators are not allowed to use.
Prove it cannot trade
Do not include the Trade.mqh library, do not call OrderSend, and do not use CTrade. Anyone reading the source can then see at a glance that no trading code exists. You can also leave the platform’s Algo Trading button switched off for chart-only monitoring, though alerts and notifications may still work depending on the function. Check how your terminal build behaves rather than assuming.

New-bar detection
An EA’s OnTick runs on every tick. You want to evaluate your rule once per closed candle, so you need a new-bar test. The standard method is to read the time of the current bar and compare it with the last one you stored.
The EA
input int InpFast = 20;
input int InpSlow = 50;
input bool InpPopup = true;
input bool InpPush = true;
int hFast = INVALID_HANDLE, hSlow = INVALID_HANDLE;
datetime g_lastBar = 0;
int g_lastState = 0; // 1 up, -1 down, 0 unknown
int OnInit()
{
hFast = iMA(_Symbol, _Period, InpFast, 0, MODE_EMA, PRICE_CLOSE);
hSlow = iMA(_Symbol, _Period, InpSlow, 0, MODE_EMA, PRICE_CLOSE);
if(hFast == INVALID_HANDLE || hSlow == INVALID_HANDLE) return(INIT_FAILED);
return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason)
{
IndicatorRelease(hFast);
IndicatorRelease(hSlow);
}
bool NewBar()
{
datetime t = iTime(_Symbol, _Period, 0);
if(t == 0 || t == g_lastBar) return(false);
g_lastBar = t;
return(true);
}
void Notify(const string text)
{
string msg = _Symbol + " " + EnumToString(_Period) + ": " + text;
if(InpPopup) Alert(msg);
if(InpPush && !SendNotification(msg))
PrintFormat("Push failed, error %d", GetLastError());
}
void OnTick()
{
if(!NewBar()) return; // evaluate once per candle
double f[2], s[2];
// shift 1 = last closed bar, shift 2 = the one before it
if(CopyBuffer(hFast, 0, 1, 2, f) != 2) return;
if(CopyBuffer(hSlow, 0, 1, 2, s) != 2) return;
// arrays are oldest-first: [0] is shift 2, [1] is shift 1
bool crossUp = (f[0] <= s[0] && f[1] > s[1]);
bool crossDown = (f[0] >= s[0] && f[1] < s[1]);
if(crossUp && g_lastState != 1) { g_lastState = 1; Notify("fast EMA crossed above slow EMA"); }
if(crossDown && g_lastState != -1) { g_lastState = -1; Notify("fast EMA crossed below slow EMA"); }
}
How it behaves
When a new candle opens, NewBar returns true once. The EA then compares the two closed candles before it. A cross is detected only if the fast average was below or equal to the slow one on the older candle and above it on the newer candle. The state variable stops the same cross from being announced twice if the EA restarts or data reloads.
Calling SendNotification from an EA is permitted, but the MQL5 reference says it does not run in the Strategy Tester and has rate limits of two calls per second and ten per minute. A rule that fires only once per candle is far inside those limits on a typical chart. If you scan many symbols from one EA, keep a counter so you do not exceed ten pushes in a minute.
Testing alert logic without waiting
- Run the EA in the Strategy Tester in visual mode and watch the Journal. Alert() messages should appear in the log. Do not expect push messages there.
- Then run it on a live demo chart to confirm popups and pushes.
- Scroll the chart back and mark where crosses occurred. Compare with your alerts.
Extending it safely
- Add inputs for symbols and read prices with CopyRates and iMA handles for each symbol.
- Log every alert to a file with FileOpen so you keep a record of what the EA told you and when.
- Add a trend filter from a higher timeframe so you receive fewer, better-qualified alerts (covered in the next article).
Step by step: installing and running the EA
- In MetaEditor choose File, New, Expert Advisor (template) and name it CrossAlert. Replace the generated code with the version above.
- Compile with F7. The code includes no trading library, so it compiles quickly.
- Open a chart on the symbol and timeframe you want to monitor, then drag CrossAlert from the Navigator onto it.
- In the Common tab you can leave algorithmic trading unticked, because the EA contains no trade calls. Confirm that popup and push alerts still reach you before relying on them.
Why one EA per chart matters
An Expert Advisor is attached to a single chart, and only one can run on each chart. If you want to watch five symbols with this design, open five charts and attach the EA to each. If that feels heavy, the better route is a single EA that loops over a list of symbols on a timer, using handles for each symbol. That version is more code, but it uses one chart and one set of inputs.
Keeping a log of alerts
When you rely on alerts, a log is valuable. Add a small function that appends the time, symbol and message to a text file in the MQL5/Files folder using FileOpen with the FILE_WRITE, FILE_READ, FILE_TXT and FILE_ANSI flags, moving to the end of the file with FileSeek before writing. After a week you can compare the log with your charts to see how many alerts were useful, which is a better test than a feeling.
What we could not confirm
- Whether your broker or VPS blocks notifications. Use the Test button in the Notifications tab.
- That a moving-average crossover is a good signal. It is only a demonstration rule.
Frequently asked questions
Do I need the Algo Trading button on? Not for the reading and alert functions, but a running EA shows a smiley or frown face based on that setting. Follow your build’s behaviour.
Can I run it on a VPS? Yes. A VPS keeps the terminal running so alerts continue when your computer is off.
Key takeaways
An EA does not have to trade. Leave out all order code, evaluate once per closed candle, guard against duplicate alerts and respect the notification limits.
Sources checked
- MQL5 Reference and MQL5 Book, “Sending push notifications”, mql5.com
- MetaTrader 5 Help, “How the Tester Downloads Historical Data”, metatrader5.com
Educational content only — not financial advice. Income and savings results vary and are never guaranteed. How we write
Enjoyed this story?
Get the best stories delivered weekly.


