about cancelling previous pending orders and basket profits

 

Dear All,


I am newbie in programming. I have been learning about building an ea for two months. This forum is very helpful for my purpose. I am searching the forum and looking for the right code.

Unfortunately programming is very complex sometimes. I have met some problems about my ea.  

1. The EA reads an indicator's buy sell arrow then it gives BuyStop/Sell Stop Orders.

The problem is I want it to cancel all previous (buystop/sellstop) pending order after one of them executed/opened. I tried different methods but i failed.
I use the cancel pending orders code from "https://www.mql5.com/en/forum/159380" but when i use the functions in different ways, it cancels the new pending order if there is sell/buy order.
I understand why it is doing because if there is a sell/buy order it cancels all the pending order without looking previous or new. I want it to cancel only previous pending order if there is one them executed.


2. The EA has 3 basket profit feature. They are Total Sell Order Profits, Total Buy Order Profits and Total Order Profits.

The problem is all basket functions close order at the begining of the next bar. It doesn't close when the profits hits the profit target in the process, it waits until closing the bar.  I want it to close when orders hit profit target.

I am posting the code here. Please help me to solve.

Kind regards,



extern double LotSize = 0.03; 
extern bool BasketOn = false;
input double Basket = 0.1;
input double BuyBasket = 0.3;
input double SellBasket = 0.3;
extern bool BuySellBasketOn = false;
extern double Distance = 50;
int    Magicnumber = 10;


int longtkt, shorttkt, cnt, t;

double pips2dbl, pips2point, pipValue;

int init()
  {
  
 
  
 if (Digits == 5 || Digits == 3)    // Adjust for five (5) digit brokers.
   {            
      pips2dbl = Point*10; pips2point = 10;pipValue = (MarketInfo(Symbol(),MODE_TICKVALUE))*10;
   } 
   else 
   {    
      pips2dbl = Point;   pips2point = 1;pipValue = (MarketInfo(Symbol(),MODE_TICKVALUE))*1;
   }
        
  return(0);
  }


int deinit()
  {

   return(0);
  }


int start()

  {
  
  static datetime Time0; if (Time0 == Time[0]) return(0); Time0 = Time[0];

  
  if ((BuySellBasketOn == true )) BuySellBasket();  
  
  if ((BasketOn == true  )) BasketProfit();  
 
  
 
  for (int i = OrdersTotal(); i > 0; i--)
      {
         if ( OrderSelect(i,SELECT_BY_POS,MODE_TRADES) )
         {
            if (OrderType() == OP_BUY || OrderType() == OP_SELL)
            {
             CheckMarketDeletePending();
            }
         }
      }
   
  
 
///////////////////////////INDICATORS///////////////////////

//this is iCustom fields

///////////////////////////INDICATORS///////////////////////
     
 return(0);
 
  }
  
  //start function end
  
  
  
  
  
  
//+---------------------------LONG AND SHORT ORDER---------------------------------------+
int GoLong()
{
   longtkt = -1;
   
   cnt = 0;
   while((longtkt < 0) && (cnt < 20))
   {
      longtkt = OrderSend(Symbol(), OP_BUYSTOP, LotSize, Ask+Distance*Point, 3,0 ,0, NULL, Magicnumber, 0, Blue);
        
      cnt++;
   
      
   }
   return(0);
   
   
   
}

int GoShort()
{
   shorttkt = -1;
   cnt = 0;
   while((shorttkt < 0) && (cnt < 20))
   {
      shorttkt = OrderSend(Symbol(), OP_SELLSTOP, LotSize, Bid-Distance*Point, 3,0,0, NULL, Magicnumber, 0, Red);
      cnt++;
     
      
   }
   return(shorttkt);
}






//****************************************BUY SELL BASKET*************************************************************



void BuySellBasket() { 
for(int k = 0; k < SymbolsTotal(true); k++) {
                string symbol_marketwatch = SymbolName(k, true);
                double sum = 0;
                for(int j = OrdersTotal()-1; j >= 0; j--) {
                        if(OrderSelect(j, SELECT_BY_POS) &&  OrderType() == OP_SELL && OrderMagicNumber()== Magicnumber)  
                                sum += OrderProfit();
                                
                }
                if(sum >= SellBasket) {
                        Print("closing sell orders: ", symbol_marketwatch, " in profit of >= ", sum);
                        for(int m = OrdersTotal()-1; m >= 0; m--) {
                                if(OrderSelect(m, SELECT_BY_POS) && OrderType() == OP_SELL && OrderMagicNumber()== Magicnumber)
                                        if(!OrderClose(OrderTicket(), OrderLots(), OrderType() == OP_BUY ? MarketInfo(symbol_marketwatch, MODE_BID) : MarketInfo(symbol_marketwatch, MODE_ASK), 3))
                                                Print("close failed: ", GetLastError());
                        }
                }
        }
        
        
        
        
        for(int k2 = 0; k2 < SymbolsTotal(true); k2++) {
                string symbol_marketwatch2 = SymbolName(k2, true);
                double sum2 = 0;
                for(int j2 = OrdersTotal()-1; j2 >= 0; j2--) {
                        if(OrderSelect(j2, SELECT_BY_POS) &&  OrderType() == OP_BUY && OrderMagicNumber()== Magicnumber)  
                                sum2 += OrderProfit();
                                
                }
                if(sum2 >= BuyBasket) {
                        Print("closing buy orders: ", symbol_marketwatch2, " in profit of >= ", sum2);
                        for(int m2 = OrdersTotal()-1; m2 >= 0; m2--) {
                                if(OrderSelect(m2, SELECT_BY_POS) && OrderType() == OP_BUY && OrderMagicNumber()== Magicnumber)
                                        if(!OrderClose(OrderTicket(), OrderLots(), OrderType() == OP_BUY ? MarketInfo(symbol_marketwatch2, MODE_BID) : MarketInfo(symbol_marketwatch2, MODE_ASK), 3))
                                                Print("close failed: ", GetLastError());
                        }
                }
        }
        
        }
        
        
//****************************************BASKET*************************************************************** 
        
        
        void BasketProfit() {
        for(int k = 0; k < SymbolsTotal(true); k++) {
                string symbol_marketwatch = SymbolName(k, true);
                double sum = 0;
                for(int j = OrdersTotal()-1; j >= 0; j--) {
                        if(OrderSelect(j, SELECT_BY_POS) && symbol_marketwatch == OrderSymbol() && OrderType() < 2 && OrderMagicNumber()== Magicnumber)
                                sum += OrderProfit();
                                
                }
                if(sum >= Basket) {
                        Print("closing all: ", symbol_marketwatch, " in profit of >= ", sum);
                        for(int m = OrdersTotal()-1; m >= 0; m--) {
                                if(OrderSelect(m, SELECT_BY_POS) && symbol_marketwatch == OrderSymbol() && OrderType() < 2 && OrderMagicNumber()== Magicnumber)
                                        if(!OrderClose(OrderTicket(), OrderLots(), OrderType() == OP_BUY && OP_SELL ? MarketInfo(symbol_marketwatch, MODE_BID) : MarketInfo(symbol_marketwatch, MODE_ASK), 3))
                                                Print("close failed: ", GetLastError());
                        }
                }
        }
        
        }
        
        


//****************************************CANCEL PENDING ORDERS***************************************************************  

void CheckMarketDeletePending()
{
   for( int i = OrdersTotal() - 1; i >= 0; i-- )
   {
      if( OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) )
      {
         if( ( OrderMagicNumber() == Magicnumber ) && ( OrderSymbol() == _Symbol ) )
         {
            if( ( OrderType() == OP_BUY ) || ( OrderType() == OP_SELL ) )
            {
               DeletePendingOrders();
               break;
            }
         }
      }
   }
}

void DeletePendingOrders()
{
   for( int i = OrdersTotal() - 1; i >= 0; i-- )
   {
      if( OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) )
      {
         if( ( OrderMagicNumber() == Magicnumber ) && ( OrderSymbol() == _Symbol ) )
         {
            if( ( OrderType() != OP_BUY ) && ( OrderType() != OP_SELL ) )
            {
               if( !OrderDelete( OrderTicket() ) )
               {
                  // Delete failed (need to deal with situation)
                  // Check Error Codes
               }
            }
         }
      }
   }
}
Help coding delete pending after Buy or Sell Execute
Help coding delete pending after Buy or Sell Execute
  • 2016.06.18
  • www.mql5.com
I am a new coder in MQL4...