ChartSaveTemplate Error 4112

 

Hello,


I currently have an issue with the ChartSaveTemplate function. The code

ChartSaveTemplate(0,"test");

returns the error code 4112 when called inside my indicator. Does anyone have an idea why the error occurs?

When I just create an empty indicator with the function it return true.

ERR_CHART_TEMPLATE_FAILED

4112

Error applying template



Thanks!

Cheers, Nico

 
Just add '.tpl' extension to the file name
 
Vladimir Karputov #:
Just add '.tpl' extension to the file name


Thank you for your reply Vladimir!

That was actually the version how I hat it previously. I changed it as I saw in the documentation that it is not required.

Unfortunately i still get the 4112 error returned with both versions. Also tried to save it in the Files folder with the same result...

 
Nico Deuscher #:


Thank you for your reply Vladimir!

That was actually the version how I hat it previously. I changed it as I saw in the documentation that it is not required.

Unfortunately i still get the 4112 error returned with both versions. Also tried to save it in the Files folder with the same result...

Show your code (in full).
 
Vladimir Karputov #:
Show your code (in full).


//+------------------------------------------------------------------+
//|                                             SaveTemplateTest.mq5 |
//|                                  Copyright 2022, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
  
  
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
  
  
void OnDeinit(const int reason)
{   
   Print("OnDeInit");
   int currentTimeframe = PeriodSeconds(Period());
   Print("Try to save template, Symbol: " + Symbol());
   Print(ChartSaveTemplate(0,"test.tpl"));
   Print("Last Error: " + GetLastError());
   return;
}


I just created this simple example that also generates the error. It works if it is on a chart and a compile it (true and GetLastError() = 0). But when I change the timeframe / symbol it returns false/4112.

 
Nico Deuscher #:



I just created this simple example that also generates the error. It works if it is on a chart and a compile it (true and GetLastError() = 0). But when I change the timeframe / symbol it returns false/4112.

Please read carefully the documentation on calling the “GetLastError” function.
 
Vladimir Karputov #:
Please read carefully the documentation on calling the “GetLastError” function.


I read the documentation again, but actually don't know to what you are referring to.

My main problem is still, that ChartSaveTemplate returns false and the template does not get saved.

 
Nico Deuscher #: I read the documentation again, but actually don't know to what you are referring to.

Check your return codes, and report your errors (including market prices and your variables). Don't look at GLE/LE unless you have an error. Don't just silence the compiler (MT5/MT4+strict), it is trying to help you.
          What are Function return values ? How do I use them ? - MQL4 programming forum (2012)
          Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles (2014)

 
Nico Deuscher #I read the documentation again, but actually don't know to what you are referring to. My main problem is still, that ChartSaveTemplate returns false and the template does not get saved.
  1. Before checking for errors you should first reset and pervious error states, by using the ResetLastError() function.
  2. To check for an error, first you have to make sure that an error did in fact occur, so first check the return value of the ChartSaveTemplate function to see if it failed or not.
  3. Saving a template in the OnDeinit event handler seems to be a bad idea to me. Firstly, the current running code is in a transitional state, and also the OnDeinit handler has a time-limit for execution before it is forcefully terminated. I would suggest you rethink your code.
 
William Roeder #:

Check your return codes, and report your errors (including market prices and your variables). Don't look at GLE/LE unless you have an error. Don't just silence the compiler (MT5/MT4+strict), it is trying to help you.
          What are Function return values ? How do I use them ? - MQL4 programming forum (2012)
          Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles (2014)

Thank you for your answer. In the simple example above there were only two warnings with the property strict. One that there is no indicator plot defined and the other one was the implicit conversion from GetLastError() into a string.


Fernando Carreiro #:
  1. Before checking for errors you should first reset and pervious error states, by using the ResetLastError() function.
  2. To check for an error, first you have to make sure that an error did in fact occur, so first check the return value of the ChartSaveTemplate function to see if it failed or not.
  3. Saving a template in the OnDeinit event handler seems to be a bad idea to me. Firstly, the current running code is in a transitional state, and also the OnDeinit handler has a time-limit for execution before it is forcefully terminated. I would suggest you rethink your code.

Also thank you for your reply.

1. Added

2. As shown above I Print the ChartSaveTemplate result, therfore can check in the log that it failed.

3. Thank you for the information about the time limit! Unfortunately this is exactly what I want to do (and what worked perfectly in earlier MT builds).

Reason: