Changing Templates

Post Reply
Bruster400
Trader
Posts: 192
Joined: Tue Sep 24, 2013 3:19 pm

Changing Templates

Post by Bruster400 »

Hi,

I'm having a couple of issues with the ChartApplyTemplate function that I hope someone can help me with. I have a template that I like to trade on an offline range bar chart which has a bunch of indicators and a trade pannel EA. However, some of the indicators don't update properly, so occasionally I force them to refresh by re-applying the template. To make this easier I coded the following simple script that I assign to a key and just hit that to refresh.

Code: Select all

//+------------------------------------------------------------------+
//|                                               TemplateScript.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

input  string   Template = "My_Template.tpl";

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
         if(ChartApplyTemplate(0,"\\"+Template))Print("Template Refreshed");
         else 
         Print("Failed to apply template, error code ",GetLastError()); 

  }
//+------------------------------------------------------------------+
It works....... except for the first problem I have which is: where the template is saved. I couldn't work out the correct path to point to my active template folder (the one that the terminal saves to by default). The reference shows how to point to; "\MQL4\" and "terminal_data_directory\Profiles\Templates\" or even "terminal_directory\Profiles\Templates\My_templates\" but not to "terminal_directory\templates" which is where they all are saved. If anyone can tell me the correct way to represent this path that would be great!



The second problem I had was when I moved this code to an on-screen button within an indicator. Using standard button code taken from the mql4 reference files I put a button on the chart with the sole purpose of refreshing the template. So the active code itself hasn't changed, I just dropped it into an OnChartEvent (with input string Template declared globally).

Code: Select all

if(id==CHARTEVENT_OBJECT_CLICK)
     {
      if(sparam=="Button1")
        {
         
         if(ChartApplyTemplate(0,"\\"+Template))Print("Template Refreshed");
         else 
         Print("Failed to apply template, error code ",GetLastError()); 
  
         // unpress the button
         ObjectSetInteger(0,"Button1",OBJPROP_STATE,false);
         
         return;
        }

     }//if(id==CHARTEVENT_OBJECT_CLICK)

So in theory, this is the same code it's just that it is called from a button click rather than a script. However, the difference is that this way of sending the code removes the live trading permissions from an EA that I have loaded in the template - even though the original template was saved with the EA trading permissions ticked. I've read in the reference that "Live trading and DLL imports permissions cannot be extended for the Expert Advisors launched by applying the template using ChartApplyTemplate() function." The script method works fine though so how do I give the indicator "permission to trade" so that it can extend it to the EA? Or is it simply that scripts can trade and indicators can't? If that's the case, can a button click call a script perhaps? Any ideas?

Thanks

Bruster
Radar
Trader
Posts: 437
Joined: Fri Mar 23, 2012 5:39 pm
Location: Round the bend ;)

Changing Templates

Post by Radar »

Hey Bruster,

How...

Code: Select all

string terminal_path=TerminalInfoString(TERMINAL_PATH); 
ChartApplyTemplate(0,"terminal_path\\templates\\my_template.tpl"
Check whether the path returned by TerminalInfoString(TERMINAL_PATH) ends with a backslash or not, and adjust accordingly.

Where? In the EA... Indicators cannot trade, so anything launched by indicators also cannot trade. As for how to put buttons into an EA, that's beyond me :(

Have fun!
Radar =8^)
Check out my new, (well, old now), manual trade & automatic scale-in manager,
StackManV2
User avatar
renexxxx
Trader
Posts: 860
Joined: Sat Dec 31, 2011 3:48 am

Changing Templates

Post by renexxxx »

Bruster400 » Mon May 21, 2018 10:31 pm wrote: I'm having a couple of issues with the ChartApplyTemplate function that I hope someone can help me with. I have a template that I like to trade on an offline range bar chart which has a bunch of indicators and a trade pannel EA. However, some of the indicators don't update properly, so occasionally I force them to refresh by re-applying the template.
Hello Bruster,

I think you should concentrate on getting the indicator to work on the offline chart, rather than installing buttons you need to press from time to time, to force the indicator to reload.

For offline charts there are two things to remember, when it comes to EAs and indicators:
1. Offline charts do not receive ticks. So, whatever you put in the OnTick() event handler (for EAs), or OnCalculate() event handler (for Custom Indicators), is not executed.
Instead, create a timer, with EventSetTimer() or EventSetMillisecondTimer(), and put your main loop in the OnTimer() event handler.
2. You have to refresh the latest OHLCV values from the offline file into the automatic variables and arrays (i.e. Ask, Bid, Open[], Close[], High[], Low[], Time[], Volume[] etc), by calling the RefreshRates() function. Do this at the beginning of your OnTimer() function.

Provided you have done this, there is no reason why your custom indicator should not update on an offline chart.
Bruster400 » Mon May 21, 2018 10:31 pm wrote: It works....... except for the first problem I have which is: where the template is saved. I couldn't work out the correct path to point to my active template folder (the one that the terminal saves to by default). The reference shows how to point to; "\MQL4\" and "terminal_data_directory\Profiles\Templates\" or even "terminal_directory\Profiles\Templates\My_templates\" but not to "terminal_directory\templates" which is where they all are saved. If anyone can tell me the correct way to represent this path that would be great!
Templates saved by ChartSaveTemplate() or manually, are saved in the folder: <data_folder>\templates\, where <data_folder> is the folder you get from File->Open Data Folder. Templates loaded by ChartApplyTemplate() are read/loaded from the same <data_folder>\templates\ folder. No need to prefix the template name with '\\'.

Bruster400 » Mon May 21, 2018 10:31 pm wrote: .... The script method works fine though so how do I give the indicator "permission to trade" so that it can extend it to the EA? Or is it simply that scripts can trade and indicators can't? If that's the case, can a button click call a script perhaps? Any ideas?
As Radar already mentioned: An indicator can not trade -- therefore, as a security precaution, MetaQuotes has built their code such that anything that is loaded from an indicator, can not trade either. If you want to load the template by pressing a button, you should create that button and the event handler for that button inside an EA.
Bruster400
Trader
Posts: 192
Joined: Tue Sep 24, 2013 3:19 pm

Changing Templates

Post by Bruster400 »

Radar & Renexxxx,

Once again you've proved what a great source of expert help this forum can be - very good advice, thanks guys.

The advice to get my indicators working offline seems to be the best place to start as it looks like my indicator button is a no-go because of the permissions.

One question about using OnTimer rather than OnCalculate is - how best to make it so that the indicator works for both offline and standard charts.... ?

I've got two possible ideas.

1 - just copy the current indicator into an additional OnTimer loop and have both the OnCalculate and OnTimer loops check whether the chart is offline or not. Simple but lots of code (albeit copy and pasted!)

2 - or check whether the chart is offline check within an OnTimer loop and if it is, somehow trigger the OnCalculate loop to run?

I'll play around with both but if I'm missing the obvious please let me know! :-)

Thanks again

Bruster
User avatar
renexxxx
Trader
Posts: 860
Joined: Sat Dec 31, 2011 3:48 am

Changing Templates

Post by renexxxx »

I was looking at this a little more and found something interesting:

It is possible for the offline generating EA, to send ticks to the offline chart. For instance the offline charts created with MathTrader7_RenkoChartCreatorEA, do receive ticks and therefore the EAs loaded on the offline charts do execute the OnTick() event handler. I didn't code that EA and I don't have the source code for it, but I think it uses the window message "Metatrader4_Internal_Message", that is sent with the WM_COMMAND 0x822c.

I am not a big Renko and/or Offline chart user, therefore wasn't aware of this before. But I tested with the little attached EA, that indeed these offline charts do receive ticks. So, I now have had to eat my own words.

Good luck with your project.
You do not have the required permissions to view the files attached to this post.
Bruster400
Trader
Posts: 192
Joined: Tue Sep 24, 2013 3:19 pm

Changing Templates

Post by Bruster400 »

renexxxx » Wed May 23, 2018 3:24 am wrote:
But I tested with the little attached EA, that indeed these offline charts do receive ticks.
Hi,

Thanks for this, very interesting and helpful. I've run your EA on my charts and although I don't use the same generator, I too get ticks. I'm not sure how they are generated though... it doesn't look like an OnTimer call because they are irregular much like normal ticks - several in one second and sometimes a 3 or even 4 second delay between them.

However, whilst that means that I don't have to go down the full OnTimer loop route I'm still going to follow up on the other advice you've given. In fact, your path advice for finding my templates folder worked perfectly and I will certainly be putting in some RefreshRates() calls.

Thanks again for your help

Bruster
Post Reply

Return to “Coders Hangout”