How to get profit value of last 2 position in MT5???

 

Hi,

Can somebody help me with a MT5 Code that can get Profit/Loss value of Last 2 position.


For Example:  if I have 5 open buy positions but I want to know total profit or loss of only last 2 buy positions (the 4th and 5th buy position only)


Thanking you!!

 

Forum on trading, automated trading systems and testing trading strategies

How to delete the Last Position in MT5??

nicholi shen, 2018.10.30 03:58

#include <Trade\Trade.mqh>
void OnStart()
{
   CPositionInfo p;
   CTrade t;
   ulong ticket=0;
   datetime time=0;
   for(int i=PositionsTotal()-1; i>=0; --i){
      if(p.SelectByIndex(i) 
         && p.Symbol() == _Symbol
         && p.Time() > time
      ){
         time = p.Time();
         ticket = p.Ticket();
      }
   }
   if(t.PositionClose(ticket))
      Print("position closed ");   
}

 

Forum on trading, automated trading systems and testing trading strategies

How to get current profit value of a symbol in MT5

Ziheng Zhuang, 2018.10.26 05:21

//+------------------------------------------------------------------+
//|                                                                  |
double TotalProfit(int magic)
  {
   double pft=0;
   for(int i=PositionsTotal()-1;i>=0;i--)
     {
      ulong ticket=PositionGetTicket(i);
      if(ticket>0)
        {
         if(PositionGetInteger(POSITION_MAGIC)==magic && PositionGetString(POSITION_SYMBOL)==Symbol())
           {
            pft+=PositionGetDouble(POSITION_PROFIT);
           }
        }
     }
   return(pft);
  }
//+------------------------------------------------------------------+

 
Sergey Golubev:

I am having issue with getting Profit/Loss value of last two open positions!

 
Dilchan:

I am having issue with getting Profit/Loss value of last two open positions!

Check the Opentime.

If it is the newest order, you have the last open position.

If it is the previous newest order you have the previous last open position.

Take the Profit/Loss from those two positions.

 
Marco vd Heijden:

Check the Opentime.

If it is the newest order, you have the last open position.

If it is the previous newest order you have the previous last open position.

Take the Profit/Loss from those two positions.

Thank you! but how to get previous newest order? could you pls provide the code?

 
Dilchan:

Thank you! but how to get previous newest order? could you pls provide the code?

You copy the open times into array than filter the newest one then the one older then that one but newer then the the others will be the previous last one.

What code had you tried so far ?

Reason: