Generate code in 3 minutes
Enter your idea and AI generates runnable code.
To change the current chart's symbol and timeframe in MQL4, you use the ChartSetSymbolPeriod function. This function is asynchronous, meaning it sends a command to the terminal queue to update the chart properties.
Below is a complete, ready-to-use script.
//+------------------------------------------------------------------+
//| ChangeChartSymbolPeriod.mq4 |
//| Copyright 2023, Quantitative AI |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, Quantitative AI"
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
#property script_show_inputs // Forces the input window to appear on launch
//--- Input parameters
input string InpSymbol = "GBPUSD"; // Target Symbol (e.g., EURUSD)
input ENUM_TIMEFRAMES InpPeriod = PERIOD_H1; // Target Timeframe
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
// 0 denotes the current chart the script is attached to
long chart_id = 0;
// Reset the last error code before execution
ResetLastError();
// Check if the symbol is available in Market Watch
// If not, we attempt to select it to ensure data is available
if(!SymbolSelect(InpSymbol, true))
{
Print("Warning: Symbol ", InpSymbol, " might not be available in Market Watch.");
}
// Execute the change
// Note: ChartSetSymbolPeriod is asynchronous. It queues the command.
bool res = ChartSetSymbolPeriod(chart_id, InpSymbol, InpPeriod);
if(res)
{
Print("Successfully sent command to change chart to ", InpSymbol, " on ", EnumToString(InpPeriod));
}
else
{
// Common errors: 4106 (Unknown symbol), 4051 (Invalid function parameter)
Print("Failed to change chart configuration. Error Code: ", GetLastError());
}
}
//+------------------------------------------------------------------+
ChartSetSymbolPeriod:
bool ChartSetSymbolPeriod(long chart_id, string symbol, ENUM_TIMEFRAMES period)true if the command was successfully added to the chart's message queue, not necessarily when the chart visually updates.SymbolSelect:
#property script_show_inputs:
InpSymbol and InpPeriod before the code executes.Q: Can I use this function on a specific chart ID instead of the current one?
A: Yes. Instead of passing 0 as the chart_id, you can pass a specific long value obtained via ChartFirst(), ChartNext(), or ChartID().
Q: What happens to the Expert Advisor (EA) attached to the chart if I run this script?
A: Changing the symbol or timeframe of a chart causes the terminal to re-initialize any Expert Advisor attached to it. The EA will go through OnDeinit and then OnInit again with the new symbol/period parameters.
Q: Why does the script finish before the chart actually changes?
A: ChartSetSymbolPeriod is asynchronous. The script sends the instruction to the terminal kernel and terminates immediately. The terminal processes the visual update in its own time (usually milliseconds later).