Random Issue with an EA

 

Hello EA Guru,


I have coded an EA and it is under test on demo account. I have noticed that sometimes EA dont open the valid trade. Further investigation reveals that EA is taking wrong candle value (open & close) for processing the entry rules. To elaborate further, as per rule EA executes all code at each hour (when an hour completes i.e. new hour just starts & new candle just opens up, the EA executes) and supposed to read open & close value of previous candle. But whenever issue happens I have noticed that EA reads open & close value of a candle which is printed before the previous candle (e.g. end of 11th hour which is nothing but beginning of 12th hour, 12th candle opens, EA supposed to read 11th candles open & close price however sometimes it read open & close value of 10th candle).

Any idea why I am facing this issue? 

 
Post some of the code here. How does it detect that a new bar has started?
 

Here is the code to detect new bar. Please note that I am working on 1hour TF so Hour() function works for me to detect new bar.


void OnTick()

{

  if (time != Hour() )

   {

    order opening & management code


     time = Hour(); (this is the last command of my code)

   }

}

 

This looks like sometimes you detect a new hour, but the bars did not shift so far. Could maybe happen on the very first few ms of the new hour. Try this fix:

int BarCount;

void OnInit()
{
   BarCount=-1; // reset
   ...
}

void OnTick()
{
   if(Bars(_Symbol,PERIOD_H1))!=BarCount && BarCount!=-1)
    {
      ... // new bar
    }
   BarCount=Bars(_Symbol,PERIOD_H1);
}
 

That is not a good example.

Bar number can vary due to download of history.

Use Time instead.

//--- system variable
datetime bartime=0;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- set bartime
   bartime!=iTime(Symbol(),PERIOD_H1,0);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//--- check time
   if(bartime!=iTime(Symbol(),PERIOD_H1,0))
    {
     Alert("New Bar!");
     bartime!=iTime(Symbol(),PERIOD_H1,0); // overwrite old value
    }
  }
//+------------------------------------------------------------------+
 
lippmaje:

This looks like sometimes you detect a new hour, but the bars did not shift so far. Could maybe happen on the very first few ms of the new hour. Try this fix:

Thanks for your reply, I will try this one. 

 
Marco vd Heijden:

That is not a good example.

Bar number can vary due to download of history.

Use Time instead.

Thanks for your reply, I will try this one as well...