Phanthom Trades

 

Hello all, I an posting this in hopes someone can tell me the problem with this code. I have been writing EA's a few years now and in my efforts to find the cause of, or  fix phanthom trades I came up with this template for EA's. When I say phanthom trade I mean when code specifies > or < but trades trigger at >= or <= and such.  Although it seems to trade on time it also triggers an alert that the trade came from somewhere else. I have attached copy of source code please help.


Files:
Spirit.mq4  28 kb
 
  whasup = -1;
//--- go trading only for first tiks of new bar
   if(Volume[0]>1) whasup = 1;
For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 and MetaTrader 4 - MQL4 programming forum.) Always use time.
I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
          New candle - MQL4 and MetaTrader 4 - MQL4 programming forum
 

Thank you for responding whroeder1. If I am understanding right, you are saying that if I initialize a datetime variable, lets call it NewBar and change code to something like 

 whasup = 1;
//--- go trading only for first tiks of new bar
   if(TimeCurrent()>NewBar) {whasup = -1; NewBar=(iTime(NULL,0,0)+PeriodSeconds());}

then that might solve the issue?

 
datetime bar=iTime(Symbol(),PERIOD_D1,0);

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//--- compare
   if(bar!=iTime(Symbol(),PERIOD_D1,0))
    {
     Print(" NEW BAR!");
     bar=iTime(Symbol(),PERIOD_D1,0);
    }
  }
 
Thank Marco vd Heijden that does seem to be a simpler solution.
Reason: