Download MetaTrader 5

Scripts and chart change - page 2

To add comments, please log in or register
sd59
499
sd59 2015.07.21 09:25  
Ovo:

Not sure (I never tried) if the static int i=0; keeps its value after the reinit. Can you confirm?

Yes it does - so the EventSetTimer works like a continuous loop with 3 second (as above) delay.
Ex Ovo Omnia
3192
Ex Ovo Omnia 2015.07.21 09:28  
sd59:
Yes it does - so the EventSetTimer works like a continuous loop with 3 second (as above) delay.
And what is your expectation (how it should behave)? I cannot see you killed or rescheduled the timer elsewhere except the init and deinit.
sd59
499
sd59 2015.07.21 12:37  
Ovo:
And what is your expectation (how it should behave)? I cannot see you killed or rescheduled the timer elsewhere except the init and deinit.

Yes as that is what is recommended in the documentation. So descriptively I want it to change charts every 3 seconds (say) for a specified number of charts i.e from 5M to H4 (5 charts) - at each change I have other code to calculate stuff and print results/parameters.

After it has finished scrolling through all charts I want it to stop until another specified time - let's say 5 minutes later. So 5 minutes later it will perform the same tasks.

At the moment the way the code is written it continuously scrolls through charts every 3 seconds.

Ex Ovo Omnia
3192
Ex Ovo Omnia 2015.07.21 13:25  
sd59:

Yes as that is what is recommended in the documentation. So descriptively I want it to change charts every 3 seconds (say) for a specified number of charts i.e from 5M to H4 (5 charts) - at each change I have other code to calculate stuff and print results/parameters.

After it has finished scrolling through all charts I want it to stop until another specified time - let's say 5 minutes later. So 5 minutes later it will perform the same tasks.

At the moment the way the code is written it continuously scrolls through charts every 3 seconds.

OK, it is clear now. You may change the timer period any time. In this case right after the timeframe change, something like:

   ChartSetSymbolPeriod(0,NULL,tf[i]);   
   if(i==ArraySize(tf)-1) {
      EventSetTimer(5*60);
   }

 In case you expect the 3-second interval to be reached likely during init (all indicators on the chart would init), use rather a static variable in init() for the new schedule decision.

sd59
499
sd59 2015.07.21 14:45  
Ovo:

OK, it is clear now. You may change the timer period any time. In this case right after the timeframe change, something like:

 In case you expect the 3-second interval to be reached likely during init (all indicators on the chart would init), use rather a static variable in init() for the new schedule decision.

 

It seems the program ignores the second EventSetTimer() command. The chart change is still 3 seconds from the original in the init() function.

This is what should happen:

change to M1 chart: do calcs.....after 3 seconds

change to M15 chart: do calcs...after 3seconds

change to H4 chart: do calcs......stop chart change process for 5 minutes

resume the 3 second chart change process...then stop again for 5 minutes etc;

whroeder1
14741
whroeder1 2015.07.21 15:14  
sd59: It seems the program ignores the second EventSetTimer() command.
So turn it off, then set it. If it works, tell the service desk.
Ex Ovo Omnia
3192
Ex Ovo Omnia 2015.07.21 17:08  
sd59:

It seems the program ignores the second EventSetTimer() command. The chart change is still 3 seconds from the original in the init() function.

This is what should happen:

change to M1 chart: do calcs.....after 3 seconds

change to M15 chart: do calcs...after 3seconds

change to H4 chart: do calcs......stop chart change process for 5 minutes

resume the 3 second chart change process...then stop again for 5 minutes etc;

Well, it is the way I do it, setting the next timer delay in the OnTimer() multiple times. There must be some other reason.
sd59
499
sd59 2015.07.21 17:39  
WHRoeder:
So turn it off, then set it. If it works, tell the service desk.

Couldn't get the Timer() to work.

This did though.

int tf[3] = {PERIOD_M1,PERIOD_M15,PERIOD_H4};
static int j=0;

int OnInit()
{
 if(GlobalVariableGet("cnt")< ArraySize(tf))
 {
  Sleep(3000);
 }else {GlobalVariableSet("cnt",0);Sleep(20*1000);j=0;}
 Print(GlobalVariableGet("cnt"));

  
return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
 int cnt = GlobalVariableGet("cnt");
 ChartSetSymbolPeriod(0,NULL,tf[cnt]);
 
 .
 .
 SOME CALCS
 .
 .    
 GlobalVariableSet("cnt",j++);
  
}
  
Ex Ovo Omnia
3192
Ex Ovo Omnia 2015.07.21 17:54  
sd59:

Couldn't get the Timer() to work.

This did though.

I tested your code, and I realized they changed (or fixed) behaviour of the ChartSetSymbolPeriod(0,NULL,...).

Last time I checked it interrupted current method to initialize the entire chart, and now it seems to finish the OnTimer() first before it runs OnDeinit & OnInit() couple. In that case, setting the long timer delay was overwritten back in the OnInit() rather than the other way round.
sd59
499
sd59 2015.07.22 10:34  
Ovo:

I tested your code, and I realized they changed (or fixed) behaviour of the ChartSetSymbolPeriod(0,NULL,...).

Last time I checked it interrupted current method to initialize the entire chart, and now it seems to finish the OnTimer() first before it runs OnDeinit & OnInit() couple. In that case, setting the long timer delay was overwritten back in the OnInit() rather than the other way round.

 

If I run the code below it shows that the ChartSetSymbolPeriod() function is out of sync with the displayed chart Period().

int tf[3] = {PERIOD_M1,PERIOD_M15,PERIOD_H4};

int OnInit()
  {
//--- create timer
   EventSetTimer(3);
      
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- destroy timer
   EventKillTimer();
      
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
   static int i=0;
   ChartSetSymbolPeriod(0,NULL,tf[i]);   
   Print("Chart Period = ","  ",Period(),"  ","Display Chart = ",tf[i]); 
   i++;
   if(i>=3) { i=0; };
  }
//+------------------------------------------------------------------+


If I change the chart symbol I would have expected it to show under Period()!!

Am I doing something wrong here?

12
To add comments, please log in or register