# Thread: Profit of the oldest open long- and short-position

1. ## Profit of the oldest open long- and short-position

Hello
I would like to calculate the Profit of the oldest Long-Position and the Profit of the oldest short-Position (yes i do not know how).

Code:
```//+------------------------------------------------------------------+
double countprofitoldestshort(int mode,int order_type)
{
double profit=0;
if(OrdersTotal()>0)
{
for(i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSymbol()==Symbol() &&  && OrderType()==OP_SELL && OrderMagicNumber()==magic)
{
if(mode==0)profit=profit+(OrderClosePrice()-OrderOpenPrice())/pt;
if(mode==1)profit=profit+OrderProfit()+OrderCommission()+OrderSwap();
}
}
}
return(profit);
}

//+------------------------------------------------------------------+
double countprofitoldestlong(int mode,int order_type)
{
double profit=0;
if(OrdersTotal()>0)
{
for(i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSymbol()==Symbol() &&  && OrderType()==OP_BUY && OrderMagicNumber()==magic)
{
if(mode==0)profit=profit+(OrderClosePrice()-OrderOpenPrice())/pt;
if(mode==1)profit=profit+OrderProfit()+OrderCommission()+OrderSwap();
}
}
}
return(profit);
}```

2. Try this code.

PHP Code:
``` double ProfitBuy; double ProfitSell; void countprofitoldestshort()   {    ProfitBuy=0;    ProfitSell=0;    if(OrdersTotal()>0)      {       for(i=OrdersTotal()-1;i>=0;i--)         {          OrderSelect(i,SELECT_BY_POS,MODE_TRADES);          if(OrderSymbol()==Symbol() &&  (OrderType()==OP_SELL || OrderType()==OP_BUY ) && OrderMagicNumber()==magic)            {             if(OrderType()==OP_BUY) ProfitBuy=OrderProfit()+OrderCommission()+OrderSwap();             if(OrderType()==OP_SELL) ProfitSell=OrderProfit()+OrderCommission()+OrderSwap();            }         }      }      }  ```

3. Thank you.
after Long time trying i got it:

PHP Code:
``` //_Type OP_BUY = BUY, _Type OP_SELL = SELL double GetOldestPositionPL(int _Mode, int _Type) {    double TotalProfit=0;    datetime OpenTime=0;    for(int i=0;i<OrdersTotal();i++)    {       if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))       {          if(OrderMagicNumber()!=magic || OrderSymbol()!=Symbol())        continue;                    if(OrderOpenTime()<OpenTime || OpenTime==0)          {             if(_Type==OP_BUY && OrderType()==OP_BUY)             {                if(_Mode==0)                {                   TotalProfit = (OrderClosePrice()-OrderOpenPrice())*MathPow(10,_Digits);                }else if(_Mode==1)                {                   TotalProfit = OrderProfit()+OrderCommission()+OrderSwap();                }                OpenTime = OrderOpenTime();             }else if(_Type==OP_SELL && OrderType()==OP_SELL)             {                if(_Mode==0)                {                   TotalProfit = (OrderOpenPrice()-OrderClosePrice())*MathPow(10,_Digits);                }else if(_Mode==1)                {                   TotalProfit = OrderProfit()+OrderCommission()+OrderSwap();                }                OpenTime = OrderOpenTime();             }          }                 }    }        return NormalizeDouble(TotalProfit,2); }  ```

4. Next would be to Close the Oldest-Position
like Close-Oldest-Open-Buy-Position and Close-Oldest-Open-Sell-Position....but no idea now how to do it...

5. To get orders' results

PHP Code:
``` double ProfitBuy; double ProfitSell; int OlderTicketBuy; int OlderTicketSell; void countprofitoldestshort()    {     ProfitBuy=0;     ProfitSell=0;    OlderTicketBuy=-1;    OlderTicketBuy=-1;    if(OrdersTotal()>0)       {        for(i=OrdersTotal()-1;i>=0;i--)          {           OrderSelect(i,SELECT_BY_POS,MODE_TRADES);           if(OrderSymbol()==Symbol() &&  (OrderType()==OP_SELL || OrderType()==OP_BUY ) && OrderMagicNumber()==magic)             {              if(OrderType()==OP_BUY) {ProfitBuy=OrderProfit()+OrderCommission()+OrderSwap(); OlderTicketBuy=OrderTicket();}             if(OrderType()==OP_SELL) {ProfitSell=OrderProfit()+OrderCommission()+OrderSwap();  OlderTicketSell=OrderTicket();}            }          }       }      } //code to close orders void CloseOrders(int CloseType)   {   bool CloseTicket;   if(OrdersTotal()>0)       {        for(i=OrdersTotal()-1;i>=0;i--)          {          CloseTicket=false;          OrderSelect(i,SELECT_BY_POS,MODE_TRADES);           if(OrderSymbol()==Symbol() &&  (OrderType()==OP_SELL || OrderType()==OP_BUY ) && OrderMagicNumber()==magic)             {              if((CloseType==OP_BUY)&&(OrderType()==OP_BUY)&&(OrderTicket()==OlderTicketBuy)) CloseTicket=OrderClose(OrderTicket(),OrderLots(),Bid,3,clrMediumAquamarine);             if((CloseType==OP_SELL)&&(OrderType()==OP_SELL )&&(OrderTicket()==OlderTicketSell)) CloseTicket=OrderClose(OrderTicket(),OrderLots(),Ask,3,clrMediumAquamarine);            }          }       }    }  ```