Changing the timeframe of the current chart causes problems

 

Hi forum,

I made a script which should change the current timeframe to M1, then make a screenshot and change the timeframe back to the original one. But I always get a message from metatrader if the script should be terminated. I read in the documents that ChartSetSymbolPeriod() is asynchronous and doesn't wait for its execution completion. I believe this is the problem here. Can anyone help me?

void OnStart()
  {
   int chartPeriod = ChartPeriod(0);
   ChartSetSymbolPeriod(0, NULL, PERIOD_M1);
   ChartScreenShot(0, Symbol()+"_"+(string)Period()+".gif", 640, 480);
   ChartSetSymbolPeriod(0, NULL, chartPeriod);
  }
 
Open a new chart (ChartOpen) with the desired timeframe, set a template, capture the chart (and close the chart, if you do not like it open).
 
Thanks, Ovo. That almost works. Now I have the problem that the M1 data is not fully loaded before the screenshot and therefore the M1 screenshot does not show the current chart. I tried RefreshRates() before the screenshot but that didn't help. Do you have an idea?
 
mar:
Thanks, Ovo. That almost works. Now I have the problem that the M1 data is not fully loaded before the screenshot and therefore the M1 screenshot does not show the current chart. I tried RefreshRates() before the screenshot but that didn't help. Do you have an idea?

You can use Sleep command in the script, to allow a few seconds for data fetching. Or you may have another indicator which just calls iBars(_Symbol, PERIOD_M1) in OnCalculate; it will keep the M1 up to date.

 
Great idea with the other indicator. Just modified a standard indicator which runs all the time. Thank you!
Reason: