Problem Description
Solution
To enable push notifications in your MQL4 trading robot, you need to use the SendNotification() function. This function sends a text message to the mobile terminals (iOS or Android) associated with the MetaQuotes ID specified in your desktop terminal settings.
Here is the step-by-step implementation guide and the complete code structure.
1. MQL4 Implementation
You should place the SendNotification call immediately after a successful OrderSend execution.
Here is a robust function wrapper that opens a trade and sends a notification containing the Symbol, Order Type, and Open Price.
//+------------------------------------------------------------------+
//| TradeWithNotification.mq4 |
//| Copyright 2023, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property strict
//--- Input Parameters
input double Lots = 0.1;
input int StopLoss = 50;
input int TakeProfit = 100;
input int MagicNumber = 123456;
input bool EnablePush = true; // Toggle to turn notifications on/off
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// --- EXAMPLE TRADING LOGIC ---
// This is a placeholder. Replace this with your actual strategy conditions.
// Example: If no orders are open, open a random Buy
if(OrdersTotal() == 0)
{
OpenOrder(OP_BUY);
}
}
//+------------------------------------------------------------------+
//| Custom Function to Open Order and Send Notification |
//+------------------------------------------------------------------+
void OpenOrder(int type)
{
double price, sl, tp;
color arrowColor;
string typeStr;
// 1. Set price and color based on order type
if(type == OP_BUY)
{
price = Ask;
arrowColor = clrBlue;
typeStr = "BUY";
// Calculate SL/TP for Buy
sl = (StopLoss > 0) ? price - StopLoss * Point : 0;
tp = (TakeProfit > 0) ? price + TakeProfit * Point : 0;
}
else if(type == OP_SELL)
{
price = Bid;
arrowColor = clrRed;
typeStr = "SELL";
// Calculate SL/TP for Sell
sl = (StopLoss > 0) ? price + StopLoss * Point : 0;
tp = (TakeProfit > 0) ? price - TakeProfit * Point : 0;
}
else return; // Exit if not Buy or Sell
// 2. Send the Order
int ticket = OrderSend(Symbol(), type, Lots, price, 3, sl, tp, "My EA Trade", MagicNumber, 0, arrowColor);
// 3. Check if Order was successful
if(ticket > 0)
{
// Order opened successfully
Print("Order opened successfully. Ticket: ", ticket);
// 4. Send Push Notification
if(EnablePush)
{
// Select the order to get exact execution price and time
if(OrderSelect(ticket, SELECT_BY_TICKET))
{
string message = StringFormat("New Trade: %s %s \nPrice: %s \nTime: %s",
Symbol(),
typeStr,
DoubleToString(OrderOpenPrice(), Digits),
TimeToStr(TimeCurrent(), TIME_SECONDS));
// Send the actual notification
if(!SendNotification(message))
{
int err = GetLastError();
Print("Failed to send notification. Error code: ", err);
}
}
}
}
else
{
Print("Error opening order: ", GetLastError());
}
}
2. Key Code Explanation
input bool EnablePush = true;: It is best practice to create an input parameter. This allows you to turn off notifications from the EA settings window without modifying the code if they become annoying.OrderSend(...): This attempts to open the trade. It returns aticketnumber.if(ticket > 0): We only attempt to send a notification if the trade was actually successfully opened.OrderSelect(...): We select the order using the returned ticket. This ensures we are reporting the actual execution price (which might differ slightly from the requested price due to slippage).SendNotification(message): This function sends the string to your phone.- Note: The message length is limited to 255 characters.
- Limitations: You cannot send more than 2 notifications per second or 10 per minute.
3. Required Terminal Configuration
For the code above to work, you must link your desktop terminal to your mobile app:
-
On your Phone (Android/iOS):
- Open the MetaTrader 4 app.
- Go to Settings -> Chat and Messages.
- At the bottom, find your MetaQuotes ID (a mix of letters and numbers, e.g.,
8B4E6A12).
-
On your Desktop Terminal:
- Go to Tools -> Options (or press
Ctrl+O). - Click the Notifications tab.
- Check "Enable Push Notifications".
- Enter your ID in the MetaQuotes ID field.
- Click Test. You should immediately receive a test message on your phone.
- Click OK.
- Go to Tools -> Options (or press
Q&A: Troubleshooting Notifications
Q: Why am I getting Error 4250, 4251, or 4252?
A: These are specific notification errors:
- 4250 (ERR_NOTIFICATION_SEND_FAILED): General failure. Check your internet connection.
- 4251 (ERR_NOTIFICATION_WRONG_PARAMETER): The text message might be empty or too long (max 255 chars).
- 4252 (ERR_NOTIFICATION_WRONG_SETTINGS): You have not enabled notifications in the terminal options or the MetaQuotes ID is invalid.
Q: Can I send charts or images via SendNotification?
A: No, the SendNotification function in MQL4 only supports text strings. To send images, you would need to use SendMail (Email) or SendFTP.
Q: Does this work in the Strategy Tester?
A: No. SendNotification is disabled in the Strategy Tester to prevent spamming your phone during backtesting. It only works on Demo or Live charts.
Q: Can I send notifications to multiple phones?
A: Yes. In the Desktop Terminal options (MetaQuotes ID field), you can enter multiple IDs separated by a comma (e.g., ID1,ID2,ID3). The EA will broadcast the message to all listed IDs.