Problem Description
Solution
To display a pop-up alert box in MQL4, you can use two primary functions depending on your specific needs:
Alert(): The standard MetaTrader 4 alert. It opens a separate "Alerts" window in the terminal, plays a sound, and logs the time. It does not stop the script's execution.MessageBox(): A standard Windows modal dialog box. It appears in the center of the screen and pauses the script execution until the user clicks a button (e.g., "OK").
Below are the implementations for both methods.
Option 1: Using Alert() (Standard Notification)
This is best for general notifications where you want to log the event but don't need to interrupt the program flow.
//+------------------------------------------------------------------+
//| SimpleAlert.mq4 |
//| Copyright 2023, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
#property show_inputs // Allows input parameters if needed
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
// 1. Simple text alert
Alert("This is a custom pop-up message.");
// 2. Alert with dynamic data (Symbol and Price)
// You can separate different values with commas
double currentAsk = Ask;
string symbol = Symbol();
Alert("Trading Alert: ", symbol, " is currently at ", currentAsk);
// Note: The Alert function converts all parameters to string automatically.
}
//+------------------------------------------------------------------+
Option 2: Using MessageBox() (Modal Dialog)
This is best if you need the user to acknowledge the message before the script continues or finishes. It allows for titles, icons, and specific buttons.
//+------------------------------------------------------------------+
//| MessageBoxAlert.mq4 |
//| Copyright 2023, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
// Define the message text and the window title
string text = "The script has finished calculating risk parameters.";
string caption = "Script Notification";
// Define flags for buttons and icons
// MB_OK: Shows an OK button
// MB_ICONINFORMATION: Shows an 'i' info icon
int flags = MB_OK | MB_ICONINFORMATION;
// Trigger the pop-up
MessageBox(text, caption, flags);
// Example of a Yes/No question
int result = MessageBox("Do you want to place a trade?", "Confirmation", MB_YESNO | MB_ICONQUESTION);
if(result == IDYES)
{
Print("User clicked Yes");
// Add order placement logic here
}
else
{
Print("User clicked No");
}
}
//+------------------------------------------------------------------+
Key Differences
| Feature | Alert() |
MessageBox() |
|---|---|---|
| Visual | Opens in the "Alerts" tab at the bottom of the terminal. | Opens a Windows dialog box in the center of the screen. |
| Execution | Non-blocking: The code continues running immediately. | Blocking: The code pauses until the user clicks a button. |
| Sound | Plays the default system alert sound. | Plays the standard Windows notification sound. |
| Customization | Text only. | Customizable Title, Icon, and Buttons (Yes/No/Cancel). |
| Usage | Best for logging events, price hits, or indicator signals. | Best for critical errors, confirmations, or end-of-script notices. |
Q&A
Q: Can I change the sound of the Alert() function?
A: You cannot change the sound via the Alert() function arguments directly. However, you can change the sound globally in MT4 by going to Tools -> Options -> Events and changing the sound for the "Alert" event. Alternatively, you can use the PlaySound("filename.wav") function immediately after calling Alert().
Q: Why isn't MessageBox working in the Strategy Tester?
A: MessageBox is disabled in the Strategy Tester because it pauses execution, which would freeze the backtest. Alert works in the Strategy Tester, but the messages are printed to the Journal tab rather than popping up.
Q: How do I format numbers in the Alert box?
A: Alert prints numbers with many decimal places by default. To format them neatly, use DoubleToString() or StringFormat().
Example: Alert("Price: ", DoubleToString(Ask, Digits));