How to properly calculate profit (in pips) of all open trades ?

 

Hello,

I am trying to write a grid trading EA which opens 4 or 5 trades at one time and I am planning to set profit target in Pips. So  I have written a snippet of code to calculate the profit of all the open trades in Pips (regardless of the lot size). Which means if one position is 10 pips in profit, I want to consider it as only 10 pips of profit  regardless whether the lot size is 0.01 or 2.5 lots. 

However my code has been reaching the wrong profit target if more than one trade is involved in calculation. More the trades open at one time the worse is error in the calculation. I am attaching my code. I would appreciate if some one can pint me in right direction as what is wrong with my code?


// To calculate PipPoint value
   int CalcDigits=MarketInfo(Symbol(),MODE_DIGITS);
   if(CalcDigits==2 || CalcDigits==3) PipPoint=0.01;
   else if(CalcDigits==4 || CalcDigits==5) PipPoint=0.0001;
   Print("The PipPoint value is :",PipPoint);



//Calculate Profit in Pips. 0.01 lot is used as base lot and subsequent lots are compared if these are greater in lot size. 
double CalculateProfit()

  {
   double CurrentProfit=0;
   for(int Counter8=0; Counter8<=OrdersTotal()-1; Counter8++)
     {
      double GuessProfit;
      double DesProfit;
      OrderSelect(Counter8,SELECT_BY_POS);
      if(OrderType()==OP_BUY && OrderMagicNumber()==BaseMagic) GuessProfit=(CurrentAsk-OrderOpenPrice())/PipPoint; // Calculate the Profit in Pips. 
      else if(OrderType()==OP_SELL && OrderMagicNumber()==BaseMagic) GuessProfit=(OrderOpenPrice()-CurrentBid)/PipPoint;  // Calculate the Profit in Pips. 
      DesProfit=GuessProfit *(OrderLots()/0.01);  // Adjust the Orderprofit in Pips multiplied by lotsize.
      CurrentProfit=CurrentProfit+DesProfit;
     }
   return(CurrentProfit);
  }



Thanks for reading.

 
raindrop: Which means if one position is 10 pips in profit, I want to consider it as only 10 pips of profit

That isn't what you are doing. You are computing the sum of pip lots. Find the order with the largest profit remember that and it's lotsize.

 

@raindrop I am asnwering you a bit late (2 years :D), I read your code, it was good but needed some editings, I think new code should calc all profit

after spread (spread is paid twice during opening and close of the trade). Hope you can use it:

double CalculateProfit()

  {
   double CurrentProfit=0;
   for(int i = OrdersTotal()-1; i >= 0; i--)
     {
      double GuessProfit;
      double DesProfit;
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if(OrderType()==OP_BUY && OrderMagicNumber()==MagicNumGen(Symbol())) GuessProfit=(Ask-OrderOpenPrice()-Spread()*2); // Calculate the Profit in Pips. 
      else if(OrderType()==OP_SELL && OrderMagicNumber()==MagicNumGen(Symbol())) GuessProfit=(OrderOpenPrice()-Bid-Spread()*2);  //spread is twice in forex 
      CurrentProfit+=GuessProfit;
     }
   return(CurrentProfit);
  }
raindrop
raindrop
  • www.mql5.com
Added topic Account statement is not accurate Hello,  I have a question about printing the correct account statement in MT4 so that I can submit it for tax purposes. I am running MT4 build 1170 on Windows 7. I want to print account statement for the year 2018 so I have selected 'custom Added topic How to properly...