Path + ChartApplyTemplate

 

Hi,


I'm trying to make an algorithm which will open all charts in selected template based on user inputs. I have 2 problems:

1) I cannot specify correct path to find proper template

2) Even I'll use workaround to 1) ChartApplyTemplate doesn't work for me.


Description for 1)

input string templateName = "Testing.tpl";
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
     string templatePath = "templates\\"+templateName;
     long chartID;
     //bool setTemplate;
     
     ChartOpen("AUDNZD", 1440);
     chartID = ChartID();
     
     //check if template exists or kill the script
     if(FileIsExist(templateName)) Print ("Template found");
     else {Print("File with template not found. Be sure you have correct path \"MQL4\Files\templates\". Current Template name: "+templateName); return;}
     
     setTemplate(chartID, templateName);
     /*
     Print(chartID);
     setTemplate = ChartApplyTemplate(chartID, templateName);
     if(!setTemplate) Print("Unable to set template - Last Error "+GetLastError());
     else Print("Template was correctly set");
   */
  }

This piece of code works only if I put template into "c:\Program Files (x86)\MetaTrader 4 IC Markets\MQL4\Files\" I was trying to use TERMINAL_PATH or TERMINAL_DATA_PATH + my path and it didn't work. If I will move template to original template folder which is c:\Program Files (x86)\MetaTrader 4 IC Markets\templates\ it won't work... I was using probably everything. Last way I tried was

Print(TerminalInfoString(TERMINAL_DATA_PATH)+"\\templates\\Testing.tpl");

This is the result of the Print execution 2017.07.18 20:01:06.427 OpenAllCharts AUDUSD,H1: C:\Program Files (x86)\MetaTrader 4 IC Markets\templates\Testing.tpl. and this is the result of whole program compilation:

2017.07.18 20:04:07.659 OpenAllCharts AUDUSD,H1: File with template not found. Be sure you have correct path "MQL4\Files\templates". Current Template name: Testing.tpl Error 5002. It says "wrong file name" but If I copy completely same file into "c:\Program Files (x86)\MetaTrader 4 IC Markets\MQL4\Files\" and I will use FileIsExists("Testing.tpl") then it's work.

Problem 2 - the worst one

I put the template of correct name into Files\ and program found it. Here is the setTemplate function. It works, or it says it works... 

void setTemplate(long ID, string path)
{
     bool templateStatus = ChartApplyTemplate(ID, templateName);
     if(!templateStatus) Print("Unable to set template - Last Error "+GetLastError());
     else Print("Template was correcctly set");
     ChartRedraw();
}

I got 2017.07.18 20:09:01.135 OpenAllCharts AUDUSD,H1: Template was correcctly set. But nothing happened. And here I have the biggest problem because I could live with 1) even I don't like it... But this should be essential part of my algorithm and I cannot finish it without it.

I really appreciate your help cause all of my ideas has gone and googling didn't find the answer :(

Many thanks

majusenko


 

Please read the documentation carefully.

The template file is searched according to the following rules:

  • if the backslash "\" separator (written as "\\") is placed at  the beginning of the path, the template is searched for relative to the path _terminal_data_directory\MQL5,
  • if there is no backslash, the template is searched for relative to the executable EX5 file, in which ChartApplyTemplate() is called;
  • if a template is not found in the first two variants, the search is performed in the folder terminal_directory\Profiles\Templates\.

About your "problem 2", it is not possible to help without more details. "...nothing happened", what is in your template ?

Please provide a full example to reproduce your issue.

 

Dear Alain,

thanks for your reply.

Problem 2:

Here is a full code

//+------------------------------------------------------------------+
//|                                                OpenAllCharts.mq4 |
//|                                                       M. Vaczula |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "M. Vaczula"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
input string chartSymbols = "AUDNZD;AUDUSD";
input string templateName = "Testing.tpl";
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
     string templatePath = "templates\\"+templateName;
     long chartID;
     //string myPath = TerminalInfoString(TERMINAL_DATA_PATH)+"\\templates\\Testing.tpl";
     //bool setTemplate;
     
     ChartOpen("AUDNZD", 1440);
     chartID = ChartID();
     
     //check if template exists or kill the script
     if(FileIsExist(templateName)) Print ("Template found");
     else {Print("File with template not found. Be sure you have correct path \"MQL4\Files\\templates\". Current Template name: "+templateName + " Error "+GetLastError()); return;}
     
     setTemplate(chartID, templateName);
     /*
     Print(chartID);
     setTemplate = ChartApplyTemplate(chartID, templateName);
     if(!setTemplate) Print("Unable to set template - Last Error "+GetLastError());
     else Print("Template was correctly set");
   */
  }
  
void setTemplate(long ID, string path)
{
     bool templateStatus = ChartApplyTemplate(ID, templateName);c
     if(!templateStatus) Print("Unable to set template - Last Error "+GetLastError());
     else Print("Template was correctly set");
     ChartRedraw();
}
//+------------------------------------------------------------------+

After execution I will get terminal message: 2017.07.19 10:35:50.297 OpenAllCharts AUDUSD,H1: Template was correctly set. "Nothing happens" means that I see new Chart AUDNZD on D1 timeframe (what is correct). But there is default MT4 look and template is not applied.

In my template I have different colors of bars so I can see on the first look if it was set or not. Furthermore I have 2 proprietary indicators in template. I thought it's not set because of indicators but then I created new testing template which has only different color scheme and same issue.

 
majusenko:

Dear Alain,

thanks for your reply.

Problem 2:

Here is a full code

After execution I will get terminal message: 2017.07.19 10:35:50.297 OpenAllCharts AUDUSD,H1: Template was correctly set. "Nothing happens" means that I see new Chart AUDNZD on D1 timeframe (what is correct). But there is default MT4 look and template is not applied.

In my template I have different colors of bars so I can see on the first look if it was set or not. Furthermore I have 2 proprietary indicators in template. I thought it's not set because of indicators but then I created new testing template which has only different color scheme and same issue.

Of course your template is not applied to your new chart, your code apply it to current chart :

     ChartOpen("AUDNZD", 1440);
     chartID = ChartID();   // ChartID() is CURRENT chart !
     
     ...
     
     setTemplate(chartID, templateName);

What is returning ChartOpen() function ?


 
Alain Verleyen:

Of course your template is not applied to your new chart, your code apply it to current chart :

What is returning ChartOpen() function ?



Dear Alain,

thank you very much for your help, it works now.

I have to double check again my issue 1 with the path. Currently it works when my template is in "Files". I'll provide further details but for now algorithm works at least with workaround.

 
majusenko #:

Dear Alain,

thank you very much for your help, it works now.

I have to double check again my issue 1 with the path. Currently it works when my template is in "Files". I'll provide further details but for now algorithm works at least with workaround.

Please share your code, I have the same problem and it was not solved
 
majusenko:

Hi,


I'm trying to make an algorithm which will open all charts in selected template based on user inputs. I have 2 problems:

1) I cannot specify correct path to find proper template

2) Even I'll use workaround to 1) ChartApplyTemplate doesn't work for me.


Description for 1)

This piece of code works only if I put template into "c:\Program Files (x86)\MetaTrader 4 IC Markets\MQL4\Files\" I was trying to use TERMINAL_PATH or TERMINAL_DATA_PATH + my path and it didn't work. If I will move template to original template folder which is c:\Program Files (x86)\MetaTrader 4 IC Markets\templates\ it won't work... I was using probably everything. Last way I tried was

This is the result of the Print execution 2017.07.18 20:01:06.427 OpenAllCharts AUDUSD,H1: C:\Program Files (x86)\MetaTrader 4 IC Markets\templates\Testing.tpl. and this is the result of whole program compilation:

2017.07.18 20:04:07.659 OpenAllCharts AUDUSD,H1: File with template not found. Be sure you have correct path "MQL4\Files\templates". Current Template name: Testing.tpl Error 5002. It says "wrong file name" but If I copy completely same file into "c:\Program Files (x86)\MetaTrader 4 IC Markets\MQL4\Files\" and I will use FileIsExists("Testing.tpl") then it's work.

Problem 2 - the worst one

I put the template of correct name into Files\ and program found it. Here is the setTemplate function. It works, or it says it works... 

I got 2017.07.18 20:09:01.135 OpenAllCharts AUDUSD,H1: Template was correcctly set. But nothing happened. And here I have the biggest problem because I could live with 1) even I don't like it... But this should be essential part of my algorithm and I cannot finish it without it.

I really appreciate your help cause all of my ideas has gone and googling didn't find the answer :(

Many thanks

majusenko


//+------------------------------------------------------------------+
//|                                                      ChartID.mq4 |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
    long ase= ChartOpen("XAUUSD", 5);
    OnIDChange(ase);
    Alert("New Chart ID: "+string(ase));
               ChartApplyTemplate(ase,"P1P2P3Haskaya.tpl");
               
  }
//+------------------------------------------------------------------+
void OnID(long asex)
  {
//---
   
   
     {
      long id=asex;
      string Islem="Chart ID : "+string(id)+" ";
      ObjectCreate("ChartID", OBJ_LABEL, 0, 0, 0);
      ObjectSetText("ChartID",Islem, 13,"Arial", Yellow);  //
      ObjectSet("ChartID", OBJPROP_CORNER, 3);
      ObjectSet("ChartID", OBJPROP_XDISTANCE, 1);
      ObjectSet("ChartID", OBJPROP_YDISTANCE, 15);
     }
   
 }



//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnIDChange(long asex)
  {
//---

   long id=asex;
   //ChartID();


   if(!ChartSetInteger(id,CHART_BRING_TO_TOP,0,true))
     {

      Print(__FUNCTION__+", Error Code = ",GetLastError());
      return(false);
     }
//---
   return(INIT_SUCCEEDED);
  }
Files:
1.JPG  256 kb
2.JPG  200 kb
Reason: