Check 2 or more order closed at the same time.

 

Hi Guys, I'm new in mt4 code, i have the issue on the loop. I'm using MODE_HISTORY on the OrderSelect() to find the close order. My EA will run 2 order at 1 time. each time the order is closed. It will check the condition of the closed order and open the new order. Sometime, the EA will have 2 closed order at 1 time.

When 2 order close at 1 time, the code will check the last closed order only, how to make it check 2 or more order closed at the same time. Thanks.

void CheckForTrade()
{
  
//------------------ Check OrderType in History
   for(int i=0;i<OrdersHistoryTotal();i++)
   {
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) break;
      if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
      OProfit = OrderProfit();
      OType   = OrderType();
      OLot    = OrderLots();
   }
//------------------ Hedge Lot Calculation
   Hedge_Lot = NormalizeDouble(OLot*2,1);

//------------------ Initial trades
   if(OrdersHistoryTotal()==0  && Volume[0]==1)
   {
      Lot = Lot_Size;
      Selling();
      Buying();
      return;
   }    
//------------------ Hedge Condition
   if(OrdersTotal()<2 && OType==OP_SELL && OProfit>0)
   {
      Lot = Lot_Size;
      Buying();
      return;
   }
   else if(OrdersTotal()<2 && OType==OP_SELL && OProfit<0)
   {
      Lot = Hedge_Lot;
      Buying();
      return;
   }
   if(OrdersTotal()<2 && OType==OP_BUY && OProfit>0)
   {
      Lot = Lot_Size;
      Selling();
      return;
   }  
   else if(OrdersTotal()<2 && OType==OP_BUY && OProfit<0)
   {
      Lot = Hedge_Lot;
      Selling();
      return;
   }  
}
 
zaharulrizal:

My EA will run 2 order at 1 time

Declare different Magic Number on each order.

for(int order = 0; order <= OrdersHistoryTotal() - 1; order++)
   {
      bool select = OrderSelect(order,SELECT_BY_POS,MODE_HISTORY);
      
      if(OrderMagicNumber() == MagicNumber_1 && select == true)
 
  1. You assume history is ordered by date, it's not. Could EA Really Live By Order_History Alone? (ubzen) - MQL4 forum
  2. Also see Sort multiple arrays - MQL4 and MetaTrader 4 - MQL4 programming forum
  3. No need for multiple MNs, just get the orders by date and compare the last two OrderCloseTime's
 
Hairi Baba:

Declare different Magic Number on each order.

for(int order = 0; order <= OrdersHistoryTotal() - 1; order++)
   {
      bool select = OrderSelect(order,SELECT_BY_POS,MODE_HISTORY);
      
      if(OrderMagicNumber() == MagicNumber_1 && select == true)
its work. Thanks
 
zaharulrizal:
its work. Thanks
Hi hairi,

as you can see on my code, i'm using OrdersTotal()<2 method to inform that 1 of my order is close, this limit me to have 2 order at one time, because if i'm open the 3rd order, it will not perform any condition when it close.

is there any other method can you suggest me to used, to inform the EA that 1 of my order is close and need to be checked.