Problem Description
Solution
To pass a value from a Script to an Expert Advisor (EA) running on a different chart in MQL4, the most reliable and standard method is using Global Variables of the Client Terminal.
These variables are stored in the terminal's memory (not the specific chart's memory), making them accessible to all scripts, indicators, and EAs simultaneously.
Method 1: Using Global Variables (Recommended)
This method is best for passing numerical data (double) that needs to persist even if the script finishes execution.
1. The Sender (Script)
This script sets a Global Variable named "MySharedValue".
//+------------------------------------------------------------------+
//| SenderScript.mq4 |
//| Copyright 2023, MetaQuotes |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes"
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
#property show_inputs
input double InpValueToSend = 123.45; // Value to send to the EA
void OnStart()
{
string varName = "MySharedValue";
// Set the Global Variable
// If it doesn't exist, it creates it. If it exists, it updates it.
if(GlobalVariableSet(varName, InpValueToSend) > 0)
{
Print("Script: Successfully sent value ", InpValueToSend, " to Global Variable '", varName, "'");
}
else
{
Print("Script: Failed to set Global Variable. Error: ", GetLastError());
}
}
2. The Receiver (Expert Advisor)
This EA checks for the existence of the variable and reads it.
//+------------------------------------------------------------------+
//| ReceiverEA.mq4 |
//| Copyright 2023, MetaQuotes |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes"
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
string varName = "MySharedValue";
void OnTick()
{
// Check if the variable exists
if(GlobalVariableCheck(varName))
{
// Get the value
double receivedValue = GlobalVariableGet(varName);
Print("EA: Received value from Global Variable: ", receivedValue);
// Optional: Delete the variable after reading if it's a one-time signal
// GlobalVariableDel(varName);
}
}
Method 2: Using Custom Chart Events (Advanced)
If you need to pass Strings or require an immediate trigger without waiting for a tick, use EventChartCustom. This pushes an event directly into the EA's event queue.
1. The Sender (Script)
This script broadcasts an event to all open charts.
//+------------------------------------------------------------------+
//| EventSenderScript.mq4 |
//+------------------------------------------------------------------+
#property strict
void OnStart()
{
long currChart = ChartFirst();
int customEventID = 1001; // Custom ID (must be distinct)
double valueDouble = 1.55;
string valueString = "TradeSignal";
// Loop through all open charts to find the EA
while(currChart >= 0)
{
// Send event to the specific chart
// param 1: Chart ID
// param 2: Event ID (0-65535)
// param 3: long value
// param 4: double value
// param 5: string value
EventChartCustom(currChart, customEventID, 0, valueDouble, valueString);
currChart = ChartNext(currChart); // Move to next chart
}
Print("Event broadcasted.");
}
2. The Receiver (Expert Advisor)
The EA must use the OnChartEvent handler to catch the data.
//+------------------------------------------------------------------+
//| EventReceiverEA.mq4|
//+------------------------------------------------------------------+
#property strict
void OnTick()
{
// Normal logic
}
// Event Handler
void OnChartEvent(const int id,
const long &lparam,
const double &dparam,
const string &sparam)
{
// Check if the ID matches our custom event ID
// CHARTEVENT_CUSTOM is the base offset
if(id == CHARTEVENT_CUSTOM + 1001)
{
Print("EA: Event Received!");
Print("Double Value: ", dparam);
Print("String Value: ", sparam);
// Execute logic based on received values
}
}
Summary of Differences
| Feature | Global Variables (Method 1) | Custom Events (Method 2) |
|---|---|---|
| Data Type | double only |
long, double, and string |
| Persistence | Stored for 4 weeks or until deleted | Transient (lost if EA is not running) |
| Trigger | EA reads on next OnTick or Timer |
Immediate execution via OnChartEvent |
| Complexity | Low | Medium |
Q&A
Q: Can I pass a string using Global Variables?
A: No, GlobalVariableSet only accepts double types. To pass a string, you must use Method 2 (Chart Events) or write the string to a text file using FileOpen and FileWriteString.
Q: What happens to Global Variables if I restart MT4?
A: Global Variables of the Client Terminal persist after a restart. They are saved to the hard drive and remain available for 4 weeks after the last access.
Q: How do I ensure the EA only acts on the value once?
A: When using Global Variables, you can delete the variable immediately after reading it using GlobalVariableDel(name). Alternatively, you can store the "last read value" in a variable within the EA and only act if the Global Variable value changes.