How to know the current bar number while testing? (Array Out Of Range On First Chart Bar)

 

I am looking for a function that allows me to know the current bar number while I am testing in the strategy tester.

I have an error:
2021.07.10 17: 47: 02.201 Core 1 1993.09.30 00:00:00 array out of range in 'ADXWilder.mqh' (83,35)

And I think it is because there are no bars prior to 1993.09.30 in the history.

I tried to do this to avoid the error but it doesn't work..


      if(CopyBuffer(indicatorHandler, 0, start, size, dataArray)==-1)
      {
         Print(__FUNCTION__, " Error coping buffer!! Code = ", GetLastError() );
         return -1;
      }   


      if(ArraySize(dataArray)<=0)  //--> does not avoid the error
         return -1;
      

      return dataArray[1]; //-->Error: (Array Out Of Range)

Is there any way to know which bar the test is on? Or some other way to avoid this error?

Thank you so much!!

 
karp wak:

I am looking for a function that allows me to know the current bar number while I am testing in the strategy tester.

I have an error:
2021.07.10 17: 47: 02.201 Core 1 1993.09.30 00:00:00 array out of range in 'ADXWilder.mqh' (83,35)

And I think it is because there are no bars prior to 1993.09.30 in the history.

I tried to do this to avoid the error but it doesn't work..


Is there any way to know which bar the test is on? Or some other way to avoid this error?

Thank you so much!!

double MyFunction(){

        double dataArray[]; // this must NOT have a defined size, since it is a dynamic array! (cannot be dataArray[100] for example, it must not have a defined value)
        int copied = 0;
        int size = 2500;
        int start = 0;


        copied = CopyBuffer(indicatorHandler, 0, start, size, dataArray);

        if (copied < size) {
                // copied receives the value of how much data was copied into the array
                // it must be equal as the desired size which is 2500 on this example.
                Print(__FUNCTION__, " Error coping buffer!! Code = ", GetLastError() );
                return -1;
        }   

        ArraySetAsSeries(dataArray,true); // the most recent value will be on dataArray[0], the next older one on dataArray[1], and so on.. 

return dataArray[0];
}
 
rrocchi:

I always work with the previous candle because the current candle is very unstable. That's why I have this problem with the first candle in the history ...

But I think your example can help me.


if (copied <size) is a very good idea. I'm going to test it.

Thank you so much rrocchi !! :-)

 
karp wak:

I always work with the previous candle because the current candle is very unstable. That's why I have this problem with the first candle in the history ...

But I think your example can help me.


if (copied <size) is a very good idea. I'm going to test it.

Thank you so much rrocchi !! :-)


You dont have a problem with the current/first candle, its normal the current candle to be oscilating. 


Because the current candle is always "live", since it has not yet Closed.. So it is changing its values on every tick.. that is why its harder to work with the current candle. 


But depending on the values range of what is being measured.. Ex:, if it is an oscillator ranging from 0 to 100 its easier to work with the current candle.

But on other situations, where value is not normalized under some kind of range, any minor value oscillation may lead to wrong decisions (by comparing its values)..


The disadvantage is what you mentioned: Harder to work with the current candle (candle zero), and you will always be "late" 1 candle.. Considering it runs on M15, the code will take decisions always 15 minutes late.. (if working on the candle 1)

The advantage: On candle Zero, Your code will take an instant decision on any market movement, if you manage to work with the current candle zero. 


That is the reason of many "smooth"  algorithms we implement on some indicator values... to make it easier to not have false positives due to high frequency oscilation on candle Zero.. while still not being so late to take decisions by working on candle 1.

It is a real challenge sometimes, but it is worth the effort to make it work on candle zero.

Reason: