different open distance

 

Hi all,

I have one issue with an ea regarding open orders. I'm looking to open opposite orders apart of an opposite distance from an existing one.

Some times it opens at right distance, others at a distance above and others even a distance below.

While I can understand that sometimes it could open at a distance above I can't understand why can it opens below.

I thought that could happen because of spread, but I've done a test with a spread control and the problem maintains.  

Could I ask you to take a look to the code and show me where I'm wrong ? 

Thank you in advance for time spent

Best Regards

Luis 

extern int    OppositeDistance  =  30;

 

void OpenOppositeOrder()
    {//0     
     int Op; 
    
     for(int Counter = OrdersTotal()-1; Counter >= 0; Counter--)     
      {//1
      if(!OrderSelect(Counter,SELECT_BY_POS,MODE_TRADES))continue;          
         {//2
         if(OrderSymbol() == Symbol()&& OrderMagicNumber() == MagicNumber)           
            {//3
             Op = OrderType();            
             if (Op == OP_BUY && (OrderOpenPrice() - OrderClosePrice())/Point >= OppositeDistance && SellAllowed )
               {//4                             
                  RefreshRates();                             
                  SellTicket = OrderSend(Symbol(), OP_SELL, MLots, Bid, Slippage*pips2points, 0, 0, "Sell Opposite Order", MagicNumber, 0, Red);
                  if(SellTicket > -1)                   
                   {//5
                     AddLimitsSell();
                     BuyAllowed=true;SellAllowed=false;                                                                                
                     Print(" Opposite Sell Order Placed: ","Order Ticket: ",SellTicket," Spread: ",DoubleToStr(Spread,Digits)," Open Price: ",DoubleToStr(OrderOpenPrice(),Digits));                 
                  return;
                   }//5                 
               else
                   {//6                  
                    ErrorCode = GetLastError();
                    string ErrDesc =ErrorDescription(ErrorCode);
                   
                    string ErrAlert = StringConcatenate(" Open Opposite Order - Opposite Sell Failed: ",ErrorCode, ": ",ErrDesc);
                    Alert(ErrAlert);
         
                    string ErrLog = StringConcatenate(" Open Opposite Order - Bid: ",DoubleToStr(Bid,Digits)," Open Price: ",DoubleToStr(OrderOpenPrice(),Digits)," Ticket: ",SellTicket," Spread: ",DoubleToStr(Spread,Digits)," Lots: ",MLots," Stop Level: ",StopLevel," Freeze Level: " ,FreezeLevel);
                    Print(ErrLog);                    
                   }//6
               }//4                      
             if(Op == OP_SELL && (OrderClosePrice() - OrderOpenPrice())/Point >= OppositeDistance && BuyAllowed)              
                {//7                                                 
                       RefreshRates();                          
                  BuyTicket = OrderSend(Symbol(), OP_BUY, MLots, Ask, Slippage*pips2points, 0, 0, "Buy Opposite Order", MagicNumber, 0, Green); 
                  if(BuyTicket > -1)
                   {//8
                     AddLimitsBuy();
                     BuyAllowed=false;SellAllowed=true;                                                                            
                     Print(" Opposite Buy Order Placed: ",BuyTicket," Spread: ",DoubleToStr(Spread,Digits)," Open Price: ",DoubleToStr(OrderOpenPrice(),Digits));                                   
                return;
                  }//8
               else
                  {//9                     
                    ErrorCode = GetLastError();
                    ErrDesc =ErrorDescription(ErrorCode);
                  
                    ErrAlert = StringConcatenate(" Open Opposite Order - Opposite Buy Failed: ",ErrorCode, ": ",ErrDesc);
                    Alert(ErrAlert);
         
                    ErrLog = StringConcatenate(" Open Opposite Order -  Ask: ",DoubleToStr(Ask,Digits)," Open Price: ",DoubleToStr(OrderOpenPrice(),Digits)," Ticket: ",BuyTicket," Spread: ",DoubleToStr(Spread,Digits)," Lots: ",MLots," Stop Level: ",StopLevel," Freeze Level: ",FreezeLevel);
                    Print(ErrLog);
                  }//9   
               }//7
            }//3
         }//2   
      }//1          
   }//0
 
luisneves:

Hi all,

I have one issue with an ea regarding open orders. I'm looking to open opposite orders apart of an opposite distance from an existing one.

Some times it opens at right distance, others at a distance above and others even a distance below.

While I can understand that sometimes it could open at a distance above I can't understand why can it opens below.

I thought that could happen because of spread, but I've done a test with a spread control and the problem maintains.  

I think the best way to diagnose this is to re-run your test with modified code that prints all the relevant values to the log so you can see what is going on.  So when the original trade is opened print, Bid, Ask, OrderOpenPrice(), etc.   when you place the opposite order print  Bid, Ask, OrderOpenPrice(), OrderClosePrice() for the current order and then OrderOpenPrice() for the opposite order as well as OppositeDistance  all to a precision of Digits for doubles.  Then when it goes wrong you can diagnose why . . .  anything else is just guessing,  even if I guess correctly you still need to know for sure what is wrong.