find profit function help please

 

Hi guys,

I am trying to find the sum of my all open profitable trades, I wrote this function any ideas?

Thank you

int  findProfit()
{
   int total = OrdersTotal(), profit;
 for (int cnt = 0 ; cnt <total ; cnt++)
         {
            OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
            if(OrderProfit() > 0)			
            {  
            profit=profit+OrderProfit();
           }
         }      
         return (profit);   
}
 

Yes, it's right, and it can be added also

   ticketsProfit[x]=OrderTicket();
   x++;

In case you need to work with them later or to close them later...

TY, good tip of yours.

 

I think it needs a little tweak . . .

double  findProfit()            //  Profit is a double not an int
   {
   int total = OrdersTotal(); 
   double profit;                // OrderProfit() returns a double not an int
 
   for (int cnt = 0 ; cnt <total ; cnt++)
      {
      if (OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))    //  does your OrderSelect() work . . . this checks it 
         {
         if(OrderProfit() > 0)                  
            {  
            profit = profit + OrderProfit();                // you can use  profit += OrderProfit()
            }
         }
      else 
         {
          Print("OrderSelect failed !");
          return(-999);                          //  check for -999 from the code that calls the function so that you know you have an error
         }
      }      
   return (profit);   
   }
 
RaptorUK:

I think it needs a little tweak . . .

I think it needs another couple of tweaks.

1) You are using profit to sum values but you do not explicitly set it to zero before you start. (I know MQL4 will do that for you implicitly but not all languages do and it is bad practice)

2) Profit should include OrderSwap() (and also OrderCommission() );

 
dabbler:

I think it needs another couple of tweaks.

:-) there is always room for improvement.
 

Thank you guys?

But I still have a problem, something wrong with the order select, any thought?

Thanks again!

 
I solve the problem thank you all
Reason: