Need advice - How to manage multiple orders opened in the same time?

 

Hi all,

i have one question about manage multiple orders.

Example:

My EA (MQL4) is open one order BUY and five odrers BUYLIMIT below the first one at invervals 50 point one by one. 

When the price exceeds the TP of the first order, we close all pending orders but if the price does down we modyfing TP of the first order to BE and the rest of oders to open price of the first one.

I tried to group orders with different magic numbers for each group (1 BUY and 5 BUYLIMIT) but this does not work at all. 

Have you any idea how to manage with that ?


Thanks a lot!

 
bartfx: but this does not work at all.
  1. You don't "close all pending orders" you delete them.

  2. "Doesn't work" is meaningless - just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires - meaningless. There are no mind readers here and our crystal balls are cracked. We can't help you unless you show us your attempt and state the nature of your problem. 

  3. No need for multiple magic numbers (unless using multiple strategies/TFs in one EA.) MN just allows the EA to identify it's orders from other EAs and manual trading.
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 and MetaTrader 4 - MQL4 programming forum

 
whroeder1:
  1. You don't "close all pending orders" you delete them.

  2. "Doesn't work" is meaningless - just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires - meaningless. There are no mind readers here and our crystal balls are cracked. We can't help you unless you show us your attempt and state the nature of your problem. 

  3. No need for multiple magic numbers (unless using multiple strategies/TFs in one EA.) MN just allows the EA to identify it's orders from other EAs and manual trading.
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 and MetaTrader 4 - MQL4 programming forum


You are right, I delete pending orders.

Ok, I try to explain where is the problem:

First of all i open 6 orders. 1 buy and 5 buylimit.

               int ticket = OrderSend(Symbol(),OP_BUY,GetLots(),Ask,3,sl,takeprofit,"My order",MagicNumberBuy,0,Green);
               if(ticket2<0)
                 {
                  Print("OrderSend  BUY failed with error #",GetLastError());
                 }
               else
                 {
                  Print("OrderSend placed successfully");
                 }
	       OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES);
               
               double takeprofit2=OrderOpenPrice();

               for(int i=1;i<=pendingPositionCount;i++)
                 {
                  int ticket = OrderSend(Symbol(),OP_BUYLIMIT,GetLots(),NormalizeDouble(Ask-pendingRange*i*Point,Digits),3,sl,takeprofit2,"My order BuyLimit",MagicNumberBuyLimit,0,Green);

                  if(ticket<0)
                    {
                     Print("OrderSend  BUYLIMIT failed with error #",GetLastError());

                    }
                  else
                    {
                     Print("Order BUYLIMIT placed successfully");
                    }

That is simple. The same thing for sell and selllimit orders if conditions are met.


Then i am checking where is the price.

   total=OrdersTotal();
   ModifyBeOnOpenedOrders(total);

   total=OrdersTotal();
   ClosePendingOrders(total);




//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//
void ModifyBeOnOpenedOrders(int total2)
  {
   for(cnt=0;cnt<total2-1;cnt++)
     {
      if(!OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
        {
         continue;
        }
      if(OrderSymbol()==Symbol())
        {
         if(OrderMagicNumber()==MagicNumberBuy && OrderType()==OP_BUY)
           {
            if(Ask < NormalizeDouble(OrderOpenPrice()-pendingRange*Point,Digits))
              {
               if(OrderTakeProfit()!=NormalizeDouble(OrderOpenPrice()+BeTakeProfit*Point,Digits))
                 {

                  if(!OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),NormalizeDouble(OrderOpenPrice()+BeTakeProfit*Point,Digits),0,0))
                    {
                     Print("Order modify failed Order ticket: ",OrderTicket()+" TP: "+NormalizeDouble(OrderOpenPrice()+BeTakeProfit*Point,Digits)+" SL: "+OrderStopLoss());
                    }
                 }
              }
           }

         else if(OrderType()==OP_SELL && OrderMagicNumber()==MagicNumberSell)
           {
            if(Bid>NormalizeDouble(OrderOpenPrice()+pendingRange*Point,Digits))
              {
               if(OrderTakeProfit()!=NormalizeDouble(OrderOpenPrice()-BeTakeProfit*Point,Digits))
                 {
                  if(!OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),NormalizeDouble(OrderOpenPrice()-BeTakeProfit*Point,Digits),0,0))
                    {
                     Print("Order modify failed Order ticket: ",OrderTicket()+" TP: "+NormalizeDouble(OrderOpenPrice()-BeTakeProfit*Point,Digits)+" SL: "+OrderStopLoss());
                    }
                 }
              }
           }
        }
     }

  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//
void ClosePendingOrders(int total2)
  {
   for(int cnt=0;cnt<total2;cnt++)
     {
      if(!OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
        {
         continue;
        }
      if(OrderSymbol()==Symbol()) // check for symbol and magic number
        {
         if(OrderType()==OP_BUYLIMIT)
           {
            for(int i=1;i<=pendingPositionCount;i++)
              {
               if(Ask >= NormalizeDouble(OrderTakeProfit()+(pendingRange*i*Point),Digits))
                 {
                  if(!OrderDelete(OrderTicket()))
                    {
                     Print("DELETE order failed with error #",GetLastError());
                    }
                 }
              }

           }

         else if(OrderType()==OP_SELLLIMIT)
           {
            for(int i=1;i<=pendingPositionCount;i++)
              {
               if(Bid<=NormalizeDouble(OrderTakeProfit()-(pendingRange*i*Point),Digits))
                 {
                  if(!OrderDelete(OrderTicket()))
                    {
                     Print("DELETE order failed with error #",GetLastError());
                    }
                 }
              }
           }
        }
     }
  }


I attached file You have example.

Files:
forum.PNG  7 kb
 
void ModifyBeOnOpenedOrders(int total2){
   for(cnt=0;cnt<total2-1;cnt++) if(
      OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)
   || OrderSymbol()!=Symbol()
   ){
 if(OrderType()==OP_BUYLIMIT) ...
  1. In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading,) while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing:
    1. For non-FIFO (US brokers,) (or the EA only opens one order per symbol,) you can simply count down in a position loop, and you won't miss orders. Get in the habit of always counting down.
                Loops and Closing or Deleting Orders - MQL4 and MetaTrader 4 - MQL4 programming forum
    2. For FIFO (US brokers,) and you (potentially) process multiple orders per symbol, you must count up and on a successful operation, reprocess all positions (set index to -1 before continuing.)
    3. and check OrderSelect in case earlier positions were deleted.
                What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
                Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    4. and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use the Predefined Variables (Bid/Ask) or OrderClosePrice() instead, on the next order/server call.

  2. Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 and MetaTrader 4 - MQL4 programming forum
  3. bartfx: When the price exceeds the TP of the first order, we close all pending orders but if the price does down we modyfing TP of the first order to BE and the rest of oders to open price of the first one.
    Where do you do those tests and modifications?
 
start(){



{ // Checking all conditions and place orders.

...

...

...

}


// Here i delete and modify orders. Look at the code in previous comment.
{ 

//First delete pendings (when price is above the TakeProfit )



//Next check if price of the order with type "OP_BUY" is below at interval "pendingRange", then modify takeprofit to BE.

}



} // end of start function

Maybe the problem is that i place pendings orders already with TP of 'OpenPrice' of the first order BUY.

Here:

               int ticket = OrderSend(Symbol(),OP_BUY,GetLots(),Ask,3,sl,takeprofit,"My order",MagicNumberBuy,0,Green);
               if(ticket2<0)
                 {
                  Print("OrderSend  BUY failed with error #",GetLastError());
                 }
               else
                 {
                  Print("OrderSend placed successfully");
                 }
               OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES);
               
               double takeprofit2=OrderOpenPrice();

               for(int i=1;i<=pendingPositionCount;i++)
                 {
                  int ticket = OrderSend(Symbol(),OP_BUYLIMIT,GetLots(),NormalizeDouble(Ask-pendingRange*i*Point,Digits),3,sl,takeprofit2,"My order BuyLimit",MagicNumberBuyLimit,0,Green);


It should work like in attached file. :) 

Files:
forum2.PNG  59 kb
 
bartfx: It should work like in attached file. :) 

Not going to happen until you fix it.

whroeder1:


  1. Where do you do those tests and modifications?
 
whroeder1:

Not going to happen until you fix it.


I know that.


Where do you do those tests and modifications?

Do you mean place in all code ? 

I close pending on this function 

void ClosePendingOrders(int total2)

and modify TP of first BUY in this:

void ModifyBeOnOpenedOrders(int total2)


All code is above.

Reason: