Do I need to have some kind of available bar check in my EA if using icustom to call my indicators.

 

This kinda follows on from my previous question - https://www.mql5.com/en/forum/146554

I contacted Birt about the issues with the dates...

His suggestion was it could be related to the way my EA is coded (no surprise really, as this is all still fairly new to me). His thoughts, that it could be related to "checking the number of available bars when starting up".

So i did a bit of research into this, but wasn't sure if I had to consider this or not?

(I read this article - https://www.mql5.com/en/articles/1456 and alos at this thread - https://www.mql5.com/en/forum/145036

Firstly - I presume the checking of available bars for the indicators that are run in the EA would only affect the backtesting and optimization results, but not impact a live EA so much?

Secondly - if the indicators I am using are called by icustom - do i still need to do some extra check in the EA outside of the indicators themselves?

So some example of code in the Start function

//START RUNNING THE SWING FUNCTION, SET FIB, SL & TP LINES
  
   Swings();              //this runs the function that defines the swings
   
    
    
    //START LOOKING FOR ENTRY CRITERIA
  
    if (Trend == UpTrend)
    {
    if(MarketInfo(Symbol(),MODE_SPREAD) < MaximumSpread)
    if (OrderOpened <1)
    if (BarCheck() == true)
      if (LowestPrice() > SL())
         if (DTOSCCheck ()== true)
         if ((EL()+pips2dbl) - buyprice() < Point/2)
         
               OpenBuyOrder(LotSize(),SL(),TP());

The Swings() function uses icustom with the ZigZag indicator

void Swings()
{

      int Found = 2;
      int k = 0;
      while(Found >= 0)
      {
         if(iCustom(NULL, ExtZZSwingTF, "ZigZag", ExtZZDepth, ExtZZDeviation, ExtZZBackStep, 0, k) != 0)
         {
            SwingValue[Found] = iCustom(NULL , ExtZZSwingTF, "ZigZag", ExtZZDepth, ExtZZDeviation, ExtZZBackStep, 0, k); //report back the 2 swingvalues as set by found
            Found--;
            SwingDate[Found]=iTime(Symbol(),ExtZZSwingTF,k); // report back the 2 swing dates in the array as set by found
         }
         k++;
      }
     
 
      totalBars = Bars;    ///not sure that this is needed???
      
      if (prevZigZag != SwingValue[1]) 
      {
         newZigZag = true;
         prevZigZag = SwingValue[1];
         OrderOpened = false;    ///reset orderopened upon new swing - so only one trade per swing
         OrderCount=0;           /// reset ordercount function upon new swing - so only one trade per swing
         BuyTicket = 0;
         SellTicket = 0; 
         BarCheck=false;   /// reset barcheck function upon new swing - so only one trade per swing
      }
 if (newZigZag) 
      {
         
           // define the uptrend and downtrend        
            
         if(SwingValue[0] < SwingValue[1]) 
         {
            Trend = UpTrend;
         }
         else Trend = DownTrend;

the other indicator is the DTOSC which is in the DTOSCCheck function, which again uses icustom to call the DTOSC indicator

bool DTOSCCheck ()
{
double SK0 = iCustom(NULL, ExtDTOSCTF, DTOSCIndicator, 0,0);
double SD0 = iCustom(NULL, ExtDTOSCTF, DTOSCIndicator, 1,0);
double SK1 = iCustom(NULL, ExtDTOSCTF, DTOSCIndicator, 0,1);
double SD1 = iCustom(NULL, ExtDTOSCTF, DTOSCIndicator, 1,1);


   if (Trend == UpTrend)
   {
      if (SD0 < OS_Line && SK0 < OS_Line && SD1 < OS_Line && SK1 < OS_Line)
      if (SK1 < SD1 && SK0 > SD0)
      return (1);
      else return (0);
   }
   if (Trend == DownTrend)
   {
      if (SD0 > OB_Line && SK0 > OB_Line && SD1 > OB_Line && SK1 > OB_Line)
      if (SK1 > SD1 && SK0 < SD0)
      return (1);
      else return (0);
   }
   
}

So if i am calling these two indicators in the EA, do I need to check the number of bars available on startup, or will this be taken care of in the indicator code?

If I do need to have some kind of check, do I include it in the function for Swings and DTOSCCheck?

PS

I do get error messages for the DTOSC indicator is this related? - DTOSC 5 XAUUSD,M15: incorrect start position 5308 for ArrayMinimum function and also the same for ArrayMaximum

thanks

 

If you want to check the number of bars then use:

int iBars( string symbol, int timeframe)
Returns the number of bars on the specified chart.
For the current chart, the information about the amount of bars is in the predefined variable named Bars.

Parameters:
symbol - Symbol the data of which should be used to calculate indicator. NULL means the current symbol.
timeframe - Timeframe. It can be any of Timeframe enumeration values. 0 means the current chart timeframe.

Sample:
Print("Bar count on the 'EUROUSD' symbol with PERIOD_H1 is",iBars("EUROUSD",PERIOD_H1));

You may also use the variable "bars".

The reason you'll want to check iBars is for example: You have a moving_average indicator which provides the average of the last 100_bars. If you don't have 100_bars then whats produced by the indicator would be wrong. In this case, most people do the following.

void start(){
    if(bars < 100) return;
    rest_of_your_code_below;
}

This is usually an issue when running the program live. Even then, its rare because most people usually open the chart first before attaching the ea upon that chart. When this person opens the chart, meta_trader will download some data for the pre_calculations.

When you run programs in the back_tester, it usually loads a sizable amount of bars for your pre_calculations.

There are times when the issue of bars_being_downloaded when the start is executing; this is where you'll need checking. Such times would be during Dis_Connection; Multi_Currency on Symbol_Never_Opened... etc.

If you haven't read the mql_book yet, I'll recommend it.

 

The reason you'll want to check iBars is for example: You have a moving_average indicator which provides the average of the last 100_bars. If you don't have 100_bars then whats produced by the indicator would be wrong. In this case, most people do the following.



Thanks Ubzen.

I understand the concept of doing the bar check, to make sure calculations are correct. What I was struggling with is whether i need to have a secondary check in the EA, as I think each indicator i am using already does this check.

eg from the dtosc indicator

int start()
{
   int i,limit;
   int counted_bars = IndicatorCounted();

   if(counted_bars < 0) return(-1);
   if(counted_bars > 0) counted_bars--;
             limit=Bars-counted_bars;

so if the indicator does this check and i call the indicator using icustom in the ea, do i need to have a subsequent check for bars counted? I would have thought not, but then I don't understand why sometimes i run my backtests on data, and they don't want to start on the date i provide (despite there being data)? However is i shift the start date forward a bit it starts to run. The max period my indicators look back would be 21.

I guess from a live point of view, I just need to make sure there are enough bars in the window before adding an EA.