Calculating drawdown created by running trades for a magic number

 

Hi,

I can't seem to get this code to work. I want to know drawdown (ideally including swap+commission but I can live without those if needed) created by all running trades for a magic number.

Here is the code I'm using (the EA is bigger but I think that these are the relevant lines):

#include <Trade\DealInfo.mqh>
CDealInfo deal;
input int      MagicNum=0;

void OnTick()
  {
        Print("ProfitAllPositionsForMagic is " + ProfitAllPositionsForMagic());
  }

double ProfitAllPositionsForMagic(void)
  {
   double profit=0.0;

   for(int i=PositionsTotal()-1; i>=0; i--)
      if(deal.SelectByIndex(i)) // selects the position by index for further access to its properties
         if(deal.Symbol()==Symbol() && deal.Magic()==MagicNum)
            profit+=deal.Commission()+deal.Swap()+deal.Profit();
//---
   return(profit);
  }

I have a manually placed trade (buy EURUSD) that is in $1.76 running profit (still open) and yet the Print statement prints 0.

What am I doing wrong?  Thanks

 
credetarud:

Hi,

I can't seem to get this code to work. I want to know drawdown (ideally including swap+commission but I can live without those if needed) created by all running trades for a magic number.

Here is the code I'm using (the EA is bigger but I think that these are the relevant lines):

I have a manually placed trade (buy EURUSD) that is in $1.76 running profit (still open) and yet the Print statement prints 0.

What am I doing wrong?  Thanks

I think that deal is only used when looping through history,

instead, replace DealInfo for PositionInfo.mqh

And since that you are looping through the positions anyway, i do not think your code would work anyway.

example: note not tested, just written.

#include <Trade\PositionInfo.mqh>
CPositionInfo position;
input int      MagicNum=0;

void OnTick()
  {
        Print("ProfitAllPositionsForMagic is " + DoubleToString(ProfitAllPositionsForMagic(),2));
  }

double ProfitAllPositionsForMagic(void)
  {
   double profit=0.0;

   for(int i=PositionsTotal()-1; i>=0; i--)
      if(position.SelectByIndex(i)) // selects the position by index for further access to its properties
         if(position.Symbol()==Symbol() && position.Magic()==MagicNum)
            profit+=position.Commission()+position.Swap()+position.Profit();
//---
   return(profit);
  }