Close last order

 

 Hi all,

 I pretend to close the last order maintaining the first order in place. 

 The ea is working fine to the moment that an opposite order is in place, but when I try to close the last order regarding it bounces back and passes the line of the indicator that do not happen.

 Is that possible for you to support me on this issue?

 Best regards

 Luis Neves 

 void OpenOppositeOrder()
    {//0                     
     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();
             TotalOrders = OrdersTotal();                                                                             
             if(Op == OP_BUY && OrderOpenPrice() - Bid >= CloseLevel * pips2dbl && SellAllowed)                                   
               {//4                                                                                                      
                RefreshRates();                                                                                              
                SellTicket = OrderSend(Symbol(), OP_SELL,MathPow(Multiplier,TotalOrders) * LotSize, Bid, Slippage*pips2points, 0,0, "Sell Opposite Order", MagicNumber, 0, Red);                                                 
               if(SellTicket > -1)                                    
                   {//5 
                    BuyAllowed = true;SellAllowed = false;CloseOpenSell();                                     
                    PlaySound("tick.wav");                                                                                            
                    Print(" Opposite Sell Order Placed: ","Order Ticket: ",SellTicket," Spread: ",DoubleToStr(Spread,Digits)," Open Price: ",DoubleToStr(OrderOpenPrice(),Digits));                 
                  return;
                   }//5                 
               else
                   {//6                 
                    GetLastError();                                      
                   }//6
               }//4                                   
                  if(Op == OP_SELL && Ask - OrderOpenPrice() >= CloseLevel * pips2dbl && BuyAllowed)                                          
                   {//7                                                                                                          
                         RefreshRates();                                                                
                    BuyTicket = OrderSend(Symbol(), OP_BUY,MathPow(Multiplier,TotalOrders) * LotSize, Ask, Slippage*pips2points, 0,0, "Buy Opposite Order", MagicNumber, 0, Green);                  
                    if(BuyTicket > -1)
                   {//8   
                    BuyAllowed=false;SellAllowed=true;CloseOpenBuy();                                    
                    PlaySound("tick.wav");                                                                                      
                    Print(" Opposite Buy Order Placed: ",BuyTicket," Spread: ",DoubleToStr(Spread,Digits)," Open Price: ",DoubleToStr(OrderOpenPrice(),Digits));                                   
                return;
                  }//8
               else
                  {//9                                       
                   GetLastError();                   
                  }//9   
               }//7
            }//3
         }//2   
      }//1          
   }//0
   
//------------------------------------------------------------------------------------------------------------------------------------+
     
  void MinimumProfitIni()
//---- 
       {//0       
        TotalProfit = 0.0;             
        for(int Orders = OrdersTotal()-1; Orders >= 0; Orders--)       
          {//1
          if(!OrderSelect(Orders,SELECT_BY_POS,MODE_TRADES))continue;
            {//2
            if(OrderSymbol() == Symbol()&& OrderMagicNumber() == MagicNumber)            
              {//3                                          
               TotalProfit += OrderProfit() + OrderCommission() + OrderSwap();                                                        
              }//3          
            }//2
          }//1                            
         if (TotalProfit > Profit)
         {
          Profit = TotalProfit;         
          if(Profit > MinProfit)
           {
            GetOutLevel = Profit - ProfitToProtect;
            ProfitLevelSet = true;
           }
         }
         if(ProfitLevelSet && TotalProfit < GetOutLevel)          
          {
           CloseOpen();PlaySound("stops.wav");
          }                                                                                                                                                                               
//----                                
       }//0  
         
//------------------------------------------------------------------------------------------------------------------------------------+
  
 void CloseOpen()
 //----
       {//0
         int OrdType;               
         for(int OrderPos = OrdersTotal()-1; OrderPos >= 0; OrderPos--)       
           if(OrderSelect(OrderPos, SELECT_BY_POS, MODE_TRADES)
              && OrderMagicNumber()== MagicNumber 
              && OrderSymbol()== Symbol())                                       
             {//1
              OrdType = OrderType();
           if(OrdType == OP_BUY || OrdType==OP_SELL)
             {//2
               RefreshRates();           
                if(!OrderClose(OrderTicket(), OrderLots(),NormalizeDouble (OrderClosePrice(),Digits),Slippage *  pips2points, Yellow))               
                 {//3
                  GetLastError();
                 }//3
             }//2               
          }//1                           
          Profit = 0.0;
          GetOutLevel = 0.0;
          TotalProfit = 0.0;
          ProfitLevelSet = false;           
           
//----           
       }//0 
  
//-----------------------------------------------------------------------------+ 

void CloseOpenBuy()
 //----
       {//0
       
       double blue_current=iCustom(Symbol(),TimeFrame, "MBFX System", NumberBars, Order, 1.61803399, 0, 0);
         int OrdType;               
         for(int OrderPos = OrdersTotal()-1; OrderPos >= 0; OrderPos--)       
           if(OrderSelect(OrderPos, SELECT_BY_POS, MODE_TRADES)
              && OrderMagicNumber()== MagicNumber 
              && OrderSymbol()== Symbol())                                       
             {//1
              OrdType = OrderType();
           if(OrdType == OP_BUY && Bid <= blue_current)
             {//2
               RefreshRates();           
                if(!OrderClose(OrderTicket(), OrderLots(),NormalizeDouble (OrderClosePrice(),Digits),Slippage *  pips2points, Yellow))               
                 {//3
                  GetLastError();
                 }//3
             }//2               
          }//1                                                         
//----           
       }//0 
  
//-----------------------------------------------------------------------------+ 
void CloseOpenSell()
 //----
       {//0
       
       double blue_current=iCustom(Symbol(),TimeFrame, "MBFX System", NumberBars, Order, 1.61803399, 0, 0);
         int OrdType;               
         for(int OrderPos = OrdersTotal()-1; OrderPos >= 0; OrderPos--)       
           if(OrderSelect(OrderPos, SELECT_BY_POS, MODE_TRADES)
              && OrderMagicNumber()== MagicNumber 
              && OrderSymbol()== Symbol())                                       
             {//1
              OrdType = OrderType();
           if(OrdType == OP_SELL && Bid >= blue_current) 
             {//2
               RefreshRates();           
                if(!OrderClose(OrderTicket(), OrderLots(),NormalizeDouble (OrderClosePrice(),Digits),Slippage *  pips2points, Yellow))               
                 {//3
                  GetLastError();
                 }//3
             }//2               
          }//1                                                         
//----           
       }//0 
  
//-----------------------------------------------------------------------------+ 
 
  1. luisneves I pretend to close the last order maintaining the first order in place.
    In the USA you must close the oldest order first do to FIFO rules and hedging is not allowed.
  2.                   GetLastError();
    
    This does nothing. Check your return codes and find out why. What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles