oldest order's profits

 
Is this the right way to get the oldest order's profits from too many opened orders thanks for help
//+------------------------------------------------------------------+
//|                   oldest order profit                            |
//+------------------------------------------------------------------+
double OLDESTPROFIT()
  {
   double resul=0;
   for(int i=1;i<OrdersTotal();i++)
     {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
      if(OrderMagicNumber()==MagicNumber && (OrderType()==OP_SELL || OrderType()==OP_BUY))
         resul=OrderProfit()+OrderSwap()+OrderCommission();
     }
   return (resul);
  }
 
hadher ramadhan:
Is this the right way to get the oldest order's profits from too many opened orders thanks for help

By "profits from too many opened orders " you mean the sum of all opened trades profits then :

resul = resul + OrderProfit()+OrderSwap()+OrderCommission();
 
Olderst order can use 
if(OrderOpenTime() > ...
 

Marco vd Heijden:
Olderst order can use 

if(OrderOpenTime() > ...


I think you made a typo there.

Loop through the orders and check the OrderOpenTime(), look for the one with the earliest open time.

for(int i=1;i<OrdersTotal();i++)

should be

for(int i=0;i<OrdersTotal();i++)
 

it's called pseudo code and it's allowed to contain typo's.

It's used to just give the idea, in this case the use of the function OrderOpenTime().

A hint if you wish.

I am very busy and i can not post complete working concepts but i try to help with a quick response to point him in the right direction.
 
hadher ramadhan:
Is this the right way to get the oldest order's profits from too many opened orders thanks for help
double OLDESTPROFIT()
{
     double resul= 0;
     datetime timeOpenOrder = TimeCurrent();
     for ( int i=0; i<OrdersTotal(); i++)
     {
          if (! OrderSelect(i, SELECT_BY_POS, MODE_TRADES ))  continue;
          if ( OrderMagicNumber() != MagicNumber)             continue;
          if ( OrderType()!= OP_SELL && OrderType()!= OP_BUY )continue;
          if ( OrderOpenTime() > timeOpenOrder)               continue;

          timeOpenOrder = OrderOpenTime();
          resul= OrderProfit()+OrderSwap()+OrderCommission();
     }
     return(resul);
}

Or

double OLDESTPROFIT()
{
     double resul= 0;
     int ticket = 0;
     for ( int i=0; i<OrdersTotal(); i++)
     {
          if (! OrderSelect(i, SELECT_BY_POS, MODE_TRADES ))  continue;
          if ( OrderMagicNumber() != MagicNumber)             continue;
          if ( OrderType()!= OP_SELL && OrderType()!= OP_BUY )continue;
          if ( OrderTicket() > ticket && ticket > 0 )         continue;
          ticket = OrderTicket();
          resul= OrderProfit()+OrderSwap()+OrderCommission();
     }
     return(resul);
}
 
Konstantin Nikitin:

Or

thanks konstantin but i guess this code 

 if ( OrderType()!= OP_SELL && OrderType()!= OP_BUY )continue;

must be written in this way 

 if ( OrderType()!= OP_SELL || OrderType()!= OP_BUY )continue;

 thanks for your help 

 
Marco vd Heijden:

it's called pseudo code and it's allowed to contain typo's.

It's used to just give the idea, in this case the use of the function OrderOpenTime().

A hint if you wish.

I am very busy and i can not post complete working concepts but i try to help with a quick response to point him in the right direction.

thanks for the hint sir it is very useful 

 
Marco vd Heijden:
Olderst order can use 

thanks allot 

 
paulselvan:

By "profits from too many opened orders " you mean the sum of all opened trades profits then :

no i mean only the oldest opened order profits 

exam 

if i open 20 sell order one after one then i close the first then the second then the third and there is the rest 17 orders her the oldest open one is the order number 4 ,,so i need to get the profits of this order only

 
//+------------------------------------------------------------------+
double OLDESTPROFIT()
  {
   double result=0;
   datetime timeOpenOrder=TimeCurrent();

   for(int i=0; i<OrdersTotal(); i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderMagicNumber()==MagicNumber)
           {
            if(OrderType()==OP_SELL || OrderType()==OP_BUY)
              {
               if(OrderOpenTime()<timeOpenOrder)
                 {
                  timeOpenOrder=OrderOpenTime();
                  result=OrderProfit()+OrderSwap()+OrderCommission();
                 }
              }
           }
        }
     }
   return(result);
  }
//+------------------------------------------------------------------+
Reason: