EA taking wrong order for no obvious reason

 
Hi,

I created a simple robot which takes a position 1 hour and 5 minutes before the ftse open (ie 6.55am) based on if its above (buy) the 8ema or below (sell) for the gbpjpy pair. 

for some reason today the robot took a sell position when it should have been a buy. I then tested it again using the strategy tester and on there, it took the correct buy position. i then checked it again on the strategy tester using 50 as the spread and still took the correct buy position. 

has anyone experienced an EA taking the opposite position and could anyone point me in a direction to resolve this (or at least figure out why it happened). 

much appreciated in advance 
 
algotrading01: and could anyone point me in a direction to resolve this (or at least figure out why it happened).

Do you really expect an answer? There are no mind readers here and our crystal balls are cracked, so we can't see your machine.
     How To Ask Questions The Smart Way. (2004)
          Be precise and informative about your problem

We can't see your broken code.

Always post all relevant code (using Code button) or attach the source file.
      Questions Not To Ask
          My program doesn't work. I think system facility X is broken.

It is almost always your code.

 
algotrading01:
Hi,

I created a simple robot which takes a position 1 hour and 5 minutes before the ftse open (ie 6.55am) based on if its above (buy) the 8ema or below (sell) for the gbpjpy pair. 

for some reason today the robot took a sell position when it should have been a buy. I then tested it again using the strategy tester and on there, it took the correct buy position. i then checked it again on the strategy tester using 50 as the spread and still took the correct buy position. 

has anyone experienced an EA taking the opposite position and could anyone point me in a direction to resolve this (or at least figure out why it happened). 

much appreciated in advance 

apoligies.

the code as been added.


Thanks in advance


extern int minute = 55;
extern int hour = 9; //due to broker times
extern double takeProfit = 0.9;
extern double stopLoss = 0.3;
extern int MA_Period = 8;
extern int magic = 987654321;
extern bool UseTrailingSL = true;
extern double TrailValue = 0.5;




void OnTick()
  {
   static bool isFirstTick = true;
   static int ticket = 0;
   double ma = iMA (Symbol(),PERIOD_M15, MA_Period, 0,0,0,0);
   

/*stopLoss = NormalizeDouble(stopLoss, Digits);
takeProfit = NormalizeDouble(takeProfit, Digits);*/
   
   if (Hour() == hour)
   {
      if (Minute() == minute)
      {
         if (isFirstTick == true)
         {
            isFirstTick = false;
            ticket = FindTicket(magic);
            
            bool res;
            res = OrderSelect(ticket,SELECT_BY_TICKET);
            if (res == true)
            {
               if (OrderCloseTime() == 0)
               {
               bool res2;
               res2 = OrderClose(ticket,OrderLots(),OrderClosePrice(),10);
               
               }//if orderclose = 0
               
            }//if res true
            
            if(Close[1] < ma/* - 10*/)
            {
            /*Print(ND(Ask+stopLoss));
            Print(ND(Ask));*/
            ticket = OrderSend(Symbol(),OP_SELL,0.12,ND(Bid),10,ND(Ask+stopLoss),ND(Bid-takeProfit),"sells",magic);
            //OrderModify(ticket,OrderOpenPrice(),Ask+stopLoss,Ask-takeProfit,0);
            
            }//if close below ma, sells setup
            
            else if (Close[1] > ma /*+ 10*/)
            {
            ticket = OrderSend(Symbol(),OP_BUY,0.12,ND(Ask),10,ND(Bid-stopLoss),ND(Ask+takeProfit),"buys",magic);
            //OrderModify(ticket,OrderOpenPrice(),Bid-stopLoss,Bid+takeProfit,0);
            }//else if for buys
            
            
         
         }//isfirsttick true
      
      
      
      }//if minute bracket
      
   }//if hour bracket
   
   
   else
   {
      isFirstTick = true; 
       if(UseTrailingSL == true) //execute trailing stop - this has to be done on every tick
      {
         //FindTicket makes sure that the EA will pick up its orders
         //even if it is relaunched
         int ticket_trailing = FindTicket(magic);  
         
         if(ticket_trailing > 0)
         {
            TrailingStop(ticket_trailing);      
         }
      }
   }
   
   
   
  }//last bracket - onTick
  
  
  
  
  
  
int FindTicket(int M)
{
   int ret = 0;
   
   for(int i = OrdersTotal()-1; i >= 0; i--)
   {
      bool res;
      res = OrderSelect(i, SELECT_BY_POS);
      if(res == true)
      {
         if(OrderMagicNumber() == M)
         {
            ret = OrderTicket();
            break;
         }
      }
   }
   
   return(ret);
}


double ND(double val)
{
   return(NormalizeDouble(val, Digits()));
}


void TrailingStop(int ticket)                                                 
{
   bool res;                                                                  
   res = OrderSelect(ticket, SELECT_BY_TICKET);                               
   
   if(res == true)
   {
      if(OrderType() == OP_BUY)   
      {                                            
         if(Bid - OrderStopLoss() > TrailValue)    //adjust stop loss if it is too far                           
         {
            res = OrderModify(OrderTicket(), OrderOpenPrice(), ND(Bid - TrailValue), OrderTakeProfit(), 0); 
            if(!res)
            {
               Alert("TrailingStop OrderModify Error: ", GetLastError());  
            }
        }
      }
         
      if(OrderType() == OP_SELL)
      {
         if(OrderStopLoss() - Ask > TrailValue)    //adjust stop loss if it is too far  
         {
            res = OrderModify(OrderTicket(), OrderOpenPrice(), ND(Ask + TrailValue), OrderTakeProfit(), 0);        
            if(!res)
            {
               Alert("TrailingStop OrderModify Error: ", GetLastError());  
            }
         }
      }
   }
}