In MQL4, you can set up alerts and notifications to keep you updated about market conditions or the status of your trading strategies. These notifications can be visual, auditory, or even sent to external devices such as your mobile phone or email.
All of the following would be preceded by a condition that would trigger the alert. They can also be included with another function. Follow along to see a full example at the end.
Alerts are immediate notifications that appear as popup messages in your MetaTrader terminal, useful for urgent information that requires your attention.
Alert("Price has reached a new high!");
Example using variable:
double price = High; // Previous bar's high
Alert("Previous high was: ", price);
Receive real-time notifications on your mobile device using the MT4 mobile application. A great way to stay informed when away from your desktop.
SendNotification("Take profit reached on EURUSD!");
Email notifications allow you to receive updates straight to your inbox, which is perfect for detailed reports or when you're unable to access the MT4 app.
SendMail("Trade Alert", "A trade has been executed on your account.");
The PlaySound function provides an auditory alert, which can be a custom sound file, signaling specific events like a filled order or a price alert.
Print is used for logging and debugging purposes, outputting text to the 'Experts' tab. It's not an alert but is useful for developers to track the EA's operation.
Print("Order executed at: ", OrderClosePrice());
Let's see what this looks like in a real scenario. Something simple like an RSI indicators value being in the overbought zone. Let pretend we want to recieve an email notification and also a print out within the EA journal tab, when this situation occurs.
The following code would all be within your OnTick() function.
///FIRST WE WOULD HAVE TO HAVE DECLARED OUR INDICATORS
///WE USE 2 RSI INDICATORS THAT ARE THE SAME EXCEPT ONE IS 2 BARS BACK AND THE OTHER 1 BAR BACK
//THIS IS SO THAT WE DON'T RECIEVE A NOTIFICATION ON EVERY SINGLE TICK AS LONG AS THE RSI IS ABOVE 70.0
//THIS WILL ONLY SEND THE NOTIFICATION WHEN THE LAST BAR WAS BELOW 70.0 AND THE CURRENT IS ABOVE 70.0
double MYRSIPAST=iRSI(NULL,0,14,0,2); //RSI INDICATOR FOR 2 BARS BACK
double MYRSICURRENT=iRSI(NULL,0,14,0,1); //RSI INDICATOR FOR 1 BAR BACK
///NOW WE CREATE OUR CONDITION:
if (MYRSICURRENT > 70.0 && MYRSIPAST < 70.0) //IF THE RSI IS NOW ABOVE 70.0 AND WAS PREVIOUSLY BELOW 70.0
SendNotification("RSI indicator has crossed above 70.0"); ///SEND THIS TEXT NOTIFICATION
Print("RSI indicator is in overbought zone"); ///PRINT THIS IN THE EA'S JOURNAL TAB
As you can see, you can have multiple notifications or alerts within the same condition. These alerts could also accompany a trade modification or trade execution and much more.
If you have not read our page on debugging, a reminder is that the Print function is extremely useful in discovering why certain things may not be working within your EA.
Effectively using alerts and notifications can significantly enhance your trading experience. They ensure that you’re promptly informed of important events, enabling quick decisions and management of your trades. Customize your MQL4 scripts to utilize these features to suit your strategy and trading style.
Click here to return to MQL4 Basics Directory