Question about "Bars"

 

I have a silly question about this code...

init(){
   BarCount = Bars;
//...
}

start(){
   if (Bars != BarCount) {...}
}

Clearly the test will not pass on the first bar.

I assume "Bars" increments with each new bar, otherwise the test will never pass?

Thank you for a great forum, Helmut

 

This is true but be aware the value of Bars is limited to that set by the user in the Mt4 client under Options (Ctrl+O) and select the Charts tab. Last option at bottom of tab is "Max bars in chart".

In live/forward testing once the chart reaches this bar count then the parameter Bars no longer changes (but trading can continue).

I use the timestamp of the bar for incrementing by bars:

if(iTime(Symbol(),0,0)==CurrentBarOpenTime) return;
else CurrentBarOpenTime=iTime(Symbol(),0,0);
 
  1. int start() {       static datetime Time0;  bool newBar = Time0 < Time[0];
        if (newBar) {                                         Time0 = Time[0];
            ...

  2. Also, beware of looking at bar data (time/open/close..) in init(). Prices may not yet be available, such as terminal startup, the EA gets attached but current prices haven't yet been downloaded. Look at bar data only in start(). This also means Bar may not be valid in init() also. Another works in tester but not live problem.
  3. Why to people use iTime(Symbol(),0,X), iHigh, etc. when Time[X], High[X] makes it much more readable. Those functions should only be used when looking at a different pair or a different timeframe than the current chart.
 
WHRoeder:

  1. Also, beware of looking at bar data (time/open/close..) in init(). Prices may not yet be available, such as terminal startup, the EA gets attached but current prices haven't yet been downloaded. Look at bar data only in start(). This also means Bar may not be valid in init() also. Another works in tester but not live problem.
  2. Why to people use iTime(Symbol(),0,X), iHigh, etc. when Time[X], High[X] makes it much more readable. Those functions should only be used when looking at a different pair or a different timeframe than the current chart.

Thank you both. I got this from an auto-generated code (simple EA on this forum) and shall change it.

That the assignment in init() is unreliable is born out by the fact that the line in start() passes on the first bar.

Thanks again, Helmut

 
WHRoeder:
  1. Why to people use iTime(Symbol(),0,X), iHigh, etc. when Time[X], High[X] makes it much more readable. Those functions should only be used when looking at a different pair or a different timeframe than the current chart.

In my case I happen to look at the time for bars on different timeframes quite a bit. It is plenty "readable" to me.
Reason: