🚀 AI One-Click Generation of mql4 Strategy Code

Mql4 Tutorial: Mastering Chart Control with ChartSetSymbolPeriod()

mql4 | Published on: 2025/12/2
The following content is generated by EasyQuant .

Problem Description

How do I write a script to automatically change the current chart's symbol and timeframe?

Solution

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.

MQL4 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());
     }
  }
//+------------------------------------------------------------------+

Key Functions Used

  1. ChartSetSymbolPeriod:

    • Syntax: bool ChartSetSymbolPeriod(long chart_id, string symbol, ENUM_TIMEFRAMES period)
    • Purpose: Changes the symbol and timeframe of the specified chart.
    • Behavior: It is asynchronous. The function returns true if the command was successfully added to the chart's message queue, not necessarily when the chart visually updates.
  2. SymbolSelect:

    • Used here to ensure the requested symbol is selected in the "Market Watch" window. If a symbol is not in Market Watch, the terminal may not have the necessary tick data loaded to display the chart immediately.
  3. #property script_show_inputs:

    • This directive ensures that when you drag the script onto a chart, a dialog box appears allowing you to select the InpSymbol and InpPeriod before the code executes.

Q&A

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).