Download MetaTrader 5

want to select Second LAST Order

To add comments, please log in or register
Do you want to write a trading robot? Find solution in MQL5 Reference!
Muhammad Mudasir
7042
Muhammad Mudasir 2016.07.03 18:00 

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

Marco vd Heijden
Moderator
4701
Marco vd Heijden 2016.07.03 18:44  

Can filter

OrderOpenTime()

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

Muhammad Mudasir
7042
Muhammad Mudasir 2016.07.03 18:55  
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
Marco vd Heijden
Moderator
4701
Marco vd Heijden 2016.07.03 20:26  
muhammadmudasir:
do you have code for it . i will be very thankful to you
I have no idea what you are trying to do.
Muhammad Mudasir
7042
Muhammad Mudasir 2016.07.03 20:32  
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

Marco vd Heijden
Moderator
4701
Marco vd Heijden 2016.07.03 20:57  

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.

Muhammad Mudasir
7042
Muhammad Mudasir 2016.07.03 21:02  
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

Marco vd Heijden
Moderator
4701
Marco vd Heijden 2016.07.03 21:16  
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.
Muhammad Mudasir
7042
Muhammad Mudasir 2016.07.04 15:17  
Marco vd Heijden:
Here is an example.

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

To add comments, please log in or register