want to select Second LAST Order

 

i am working on ea , but i dont know how to select second last order. actually my ea opened serval orders and i want to average down like if most recent last 2 Orders average profit == Min_Profit then Ordershould close.


i am trying to code this from a week but no sucess .i will be thankful to you anyone if try to give suggestion or idea how to do it or any code for it mql4




Regards



Muhammad Mudasir

 

Can filter

OrderOpenTime()

The one older then the last one but younger then all the others, will be the previous last one.

 
Marco vd Heijden:

Can filter

The one older then the last one but younger then all the others, will be the previous last one.

do you have code for it . i will be very thankful to you
 
muhammadmudasir:
do you have code for it . i will be very thankful to you
I have no idea what you are trying to do.
 
Marco vd Heijden:
I have no idea what you are trying to do.




marco

   i want to do  for example


   1  Buy  -5

   2 buy  -4

   3  Buy  -3

   4 buy  -2

   5 Buy  -1

   6 buy  +2

i want to close only 5 order and 6 order because average profit is 1 within 2 orders then both orders close

 

i have no idea what you mean please make a flow chart.

Like i said you can find the second last order by comparing OrderOpenTime().

Without code i am unable to help you.

 
Marco vd Heijden:

i have no idea what you mean please make a flow chart.

Like i said you can find the second last order by comparing OrderOpenTime().

Without code i am unable to help you.

ok


can you only tell me how to select second last order from Loop ?  i try with OrderOpenTime but unable to do that

 
muhammadmudasir:

ok


can you only tell me how to select second last order from Loop ?  i try with OrderOpenTime but unable to do that

   datetime orderopentime[];                 //--- declare array                     
   int orderstotal=OrdersHistoryTotal();     //--- declare orderstotal
   ArrayResize(orderopentime,orderstotal,0); //--- resize array
   for(int i=0;i<orderstotal;i++)            //--- loop
     {
      //---- check selection result 
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false)     //--- check result
        {
         Print("Access to history failed with error (",GetLastError(),")");
         break;
        }
      else if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==true) //--- check result 
        {
         //Print("Symbol: ",OrderSymbol(),
         //      " Ticket: ",OrderTicket(),
         //      " Open Time: ",OrderOpenTime(),
         //      " Open Price: ",OrderOpenPrice(),
         //      " Close Time: ",OrderCloseTime(),
         //      " Close Price: ",OrderClosePrice());
            
         //--- fill array      
          orderopentime[i]=OrderOpenTime(); //--- fill array     
        } 
     }
  int index;                                                        //--- declare index
  index=ArrayMaximum(orderopentime,WHOLE_ARRAY,0);                  //--- find oldest order
  Print(" Last order: ",TimeToString(orderopentime[index]));        //--- print result
  orderopentime[index]=0;                                           //--- reset value so it will not be found again
  index=ArrayMaximum(orderopentime,WHOLE_ARRAY,0);                  //--- find second oldest order
  Print(" Second Last order: ",TimeToString(orderopentime[index])); //--- print result 
  orderopentime[index]=0;                                           //--- reset value so it will not be found again
  index=ArrayMaximum(orderopentime,WHOLE_ARRAY,0);                  //--- find third oldest order
  Print(" Third Last order: ",TimeToString(orderopentime[index]));  //--- print result
Here is an example.
 
Marco vd Heijden:
Here is an example.

thanks man your code worked. now going to implement in my ea , thank you very much.

Reason: