🚀 AI One-Click Generation of mql4 Strategy Code

How to Use ChartSaveTemplate() and ChartApplyTemplate() in Mql4

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

Problem Description

How can I write a script to automatically apply my favorite chart template to a new chart?

Solution

To create a script that automatically opens a new chart and applies a specific template, you need to use the ChartOpen() function to create the window and the ChartApplyTemplate() function to load your settings.

MQL4 Script Implementation

Save the following code as a script (e.g., OpenChartWithTemplate.mq4) in your MQL4/Scripts folder.

//+------------------------------------------------------------------+
//|                                      OpenChartWithTemplate.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 script_show_inputs

//--- Input parameters
input string          InpSymbol      = "";             // Symbol (leave empty for current)
input ENUM_TIMEFRAMES InpTimeframe   = PERIOD_CURRENT; // Timeframe
input string          InpTemplate    = "ADX";          // Template Name (without .tpl)

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
   // 1. Determine the symbol to use
   string symbol = InpSymbol;
   
   // If the user left the input empty, use the symbol of the chart where the script is dropped
   if(symbol == "") 
      symbol = _Symbol;

   // 2. Open the new chart
   // ChartOpen returns the ID of the new chart, or 0 if it failed
   long newChartID = ChartOpen(symbol, InpTimeframe);

   if(newChartID == 0)
   {
      Print("Error: Failed to open new chart for ", symbol, ". Error code: ", GetLastError());
      return;
   }

   Print("Chart opened successfully. ID: ", newChartID);

   // 3. Prepare the template name
   string templateName = InpTemplate;
   
   // Automatically add .tpl extension if the user forgot it
   if(StringFind(templateName, ".tpl") == -1)
      templateName = templateName + ".tpl";

   // 4. Apply the template to the new chart
   // Note: The template file must exist in the /templates folder or MQL4/Files
   if(ChartApplyTemplate(newChartID, templateName))
   {
      Print("Successfully applied template '", templateName, "' to chart ", newChartID);
      
      // Optional: Force a redraw to ensure immediate visual update
      ChartRedraw(newChartID);
   }
   else
   {
      int error = GetLastError();
      Print("Error: Failed to apply template '", templateName, "'. Error code: ", error);
      
      if(error == 4024) Print("Hint: Check if the template file exists in the 'templates' directory.");
   }
}
//+------------------------------------------------------------------+

How It Works

  1. Inputs: The script provides inputs for the Symbol, Timeframe, and Template Name.
    • If InpSymbol is left empty, it defaults to the symbol of the chart where you ran the script.
    • InpTemplate is the name of the file you saved (e.g., "MySystem").
  2. ChartOpen: This function opens a new window in the terminal. It returns a long value representing the Chart ID.
  3. ChartApplyTemplate: This function takes the ID of the newly created chart and the filename of the template. It queues the command to apply the visual settings, indicators, and EAs saved in that template.

Prerequisites

For this script to work, the template file must exist.

  1. Configure a chart manually with your desired colors, indicators, and settings.
  2. Right-click the chart -> Template -> Save Template.
  3. Name it (e.g., ADX.tpl).
  4. Enter ADX into the InpTemplate input when running the script.

Q&A

Q: Where does the script look for the template file?
A: The ChartApplyTemplate function searches in the following order:

  1. The MQL4/Files directory (if the path starts with a backslash).
  2. The directory of the executable script.
  3. The standard terminal_directory\Profiles\Templates\ folder (this is where templates are saved by default when you use the Right-Click menu).

Q: Can I apply a template to the current chart instead of opening a new one?
A: Yes. You would remove the ChartOpen line and pass 0 (zero) as the first argument to ChartApplyTemplate. 0 always represents the current chart.

Q: Why does ChartApplyTemplate return true, but the chart doesn't change immediately?
A: The function is asynchronous. It adds the command to the chart's message queue. The changes will be applied as soon as the terminal processes the queue, which is usually almost instantaneous but technically happens after the script finishes its current instruction block.

Q: Will the script copy Expert Advisors saved in the template?
A: Yes, if the template was saved with an EA attached, applying that template to a new chart will attempt to attach that EA. However, you must ensure that "Allow Live Trading" and "Allow DLL imports" are enabled in the terminal settings, or the EA might be attached but disabled.