Hi,
I have created an EA that I load into a chart (chart 1). The EA does the following...
- Creates a new chart, using ChartOpen (chart 2).
- I then add a new chart (chart 3) inside chart 2 using ObjectCreate - OBJ_CHART .
- I then set the chart properties for chart 3 using OBJPROP_....
- I then apply a template to chart 3 using ChartApplyTemplate.
Everything seems to be working, but the template is not being applied. If I stop the EA that is running, the template is then applied and everything looks and behaves as expected, apart from the fact that my EA is no longer running.
Is it not possible to apply a template to a chart created by ObjectCreate - OBJ_CHART ?
Show your MQL5 code (using the button )
Here is a quick sample of the lines (hopefully I haven't missed anything)...
// Create a new chart long chartID = ChartOpen(_symbol, period); // Create a sub chart ObjectCreate(chartID, chart_name, OBJ_CHART, 0, 0, 0); // Add a property ObjectSetInteger(chartID, chart_name, OBJPROP_XDISTANCE, x_pos); // Get the chart ID long id = -1; ObjectGetInteger(chartID, chart_name, OBJPROP_CHART_ID, 0, id)) // Apply the template ChartApplyTemplate(id, "\\profiles\\Templates\\test_template.tpl");
Here is a complete EA that demonstrates the issue...
#property copyright "Copyright 2021, MetaQuotes Ltd." #property link "https://www.mql5.com" #property version "1.00" int OnInit() { long chartID = ChartOpen("GBPUSD", PERIOD_M1); ObjectCreate(chartID, "Sub-Chart", OBJ_CHART, 0, 0, 0); ObjectSetInteger(chartID, "Sub-Chart", OBJPROP_XDISTANCE, 100); ObjectSetInteger(chartID, "Sub-Chart", OBJPROP_YDISTANCE, 200); long id = -1; ObjectGetInteger(chartID, "Sub-Chart", OBJPROP_CHART_ID, 0, id); ChartApplyTemplate(id, "\\profiles\\Templates\\test.tpl"); return(INIT_SUCCEEDED); } void OnDeinit(const int reason) { }
The template is not applied, but as soon as the EA is stopped, the template is applied.
The ChartRedraw command will help:
//+------------------------------------------------------------------+ //| Expert 1.mq5 | //| Copyright © 2021, Vladimir Karputov | //+------------------------------------------------------------------+ #property copyright "Copyright © 2021, Vladimir Karputov" #property version "1.00" //--- bool m_first_start=false; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- forced initialization of variables m_first_start=false; //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- if(!m_first_start) { long chartID = ChartOpen("GBPUSD", PERIOD_M1); ObjectCreate(chartID, "Sub-Chart", OBJ_CHART, 0, 0, 0); ObjectSetInteger(chartID, "Sub-Chart", OBJPROP_XDISTANCE, 100); ObjectSetInteger(chartID, "Sub-Chart", OBJPROP_YDISTANCE, 200); long id = -1; ObjectGetInteger(chartID, "Sub-Chart", OBJPROP_CHART_ID, 0, id); bool res=ChartApplyTemplate(id, "Blue_Red_bars.tpl"); ChartRedraw(id); //--- m_first_start=true; } } //+------------------------------------------------------------------+
Result:
Hi Vladimir,
Firstly, thank you for your prompt responses!
OK, I think I must been missing something here :-( I appreciate what the OnCalculate and OnTick functions are, but I am trying to configure the screen before anything is displayed. Is this not the way it should be done?
For instance, If I debug the above code, I can see the sub-chart move to the correct location when it gets to the commands...
ObjectSetInteger(chartID, "Sub-Chart", OBJPROP_XDISTANCE, 100); ObjectSetInteger(chartID, "Sub-Chart", OBJPROP_YDISTANCE, 200);
But If I then add the following code to change the candles to bars...
long id = -1; ObjectGetInteger(chartID, "Sub-Chart", OBJPROP_CHART_ID, 0, id); // Set the chart mode, bars, candle or line ChartSetInteger(id, CHART_MODE, CHART_BARS);
Again, nothing happens, until I stop the EA. Once he EA is stopped, the template is applied and the candles change to bars. I am failing to see what the difference is :-(
If this is blindingly obvious to you, would you mind directing me to some documentation or a code sample, as I would really like to understand this.
Hi Vladimir,
Excellent, thank you.
In my original code (not the sample), I was calling ChartRedraw() and not ChartRedraw(long chart_id), and that was the issue :-(

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi,
I have created an EA that I load into a chart (chart 1). The EA does the following...
Everything seems to be working, but the template is not being applied. If I stop the EA that is running, the template is then applied and everything looks and behaves as expected, apart from the fact that my EA is no longer running.
Is it not possible to apply a template to a chart created by ObjectCreate - OBJ_CHART?