Trying to calculate total profit for trades

 

having issue with a function that will calculate total profit. this is what i have so far. thanks for any help

int BuyProfit ()
{
   static int TotalBuyProfit = 0;
   for(int iPos= OrdersTotal()-1; iPos >= 0; --iPos)
   if(OrderSelect(iPos, SELECT_BY_POS)
   && OrderMagicNumber() == MagicNumber
   && OrderSymbol()      == Symbol()
   && OrderType()        == OP_BUY )continue;
   TotalBuyProfit = OrderProfit()+OrderSwap()+OrderCommission();
   return (TotalBuyProfit);
}
 
int BuyProfit ()
{
   static int TotalBuyProfit = 0;
   for(int iPos= OrdersTotal()-1; iPos >= 0; --iPos)
   if(OrderSelect(iPos, SELECT_BY_POS)
   && OrderMagicNumber() == MagicNumber
   && OrderSymbol()      == Symbol()
   && OrderType()        == OP_BUY )continue;
   TotalBuyProfit = OrderProfit()+OrderSwap()+OrderCommission();
   return (TotalBuyProfit);
}

No need for the static variable as you are not storing the value.

Use iPos--

Why the continue?  

You may also consider whether you want a double or not instead of an integer 

int BuyProfit ()
{
   int TotalBuyProfit = 0;
   for(int iPos= OrdersTotal()-1; iPos >= 0; iPos--)
   if(OrderSelect(iPos, SELECT_BY_POS)
   && OrderMagicNumber() == MagicNumber
   && OrderSymbol()      == Symbol()
   && OrderType()        == OP_BUY )
   TotalBuyProfit += OrderProfit()+OrderSwap()+OrderCommission();
   return (TotalBuyProfit);
}
 
I tried the updated function. It isn't pulling any info. I have no coding errors in meta editor. I even tried this code that I got from this forum
double ClosedSellProfit()
{
int icnt, itotal, retval;

retval=0;
itotal=(OrderProfit()+OrderSwap()+OrderCommission());

   for(icnt=0;icnt<itotal;icnt++) // for loop
     {
      OrderSelect(icnt, SELECT_BY_POS, MODE_HISTORY);
       // check for opened position, symbol & MagicNumber
       if (OrderSymbol()== Symbol())
        {
          if (OrderMagicNumber()==MagicNumber) 
            {
              if(OrderType()==OP_SELL) retval++;
            }       
        } // sOrderSymbol
     } // for loop

return(retval);
}
 
jtubbs13791:
I tried the updated function. It isn't pulling any info. I have no coding errors in meta editor. I even tried this code that I got from this forum

How are you testing it?

 The function that you show in your last post is absolute nonsense, OrdersTotal() should be used for the loop and it will return the number of trades, not profit

double ClosedSellProfit()
{
int icnt, itotal, retval;

retval=0;
itotal=(OrderProfit()+OrderSwap()+OrderCommission());

   for(icnt=0;icnt<itotal;icnt++) // for loop
     {
      OrderSelect(icnt, SELECT_BY_POS, MODE_HISTORY);
       // check for opened position, symbol & MagicNumber
       if (OrderSymbol()== Symbol())
        {
          if (OrderMagicNumber()==MagicNumber) 
            {
              if(OrderType()==OP_SELL) retval++;
            }       
        } // sOrderSymbol
     } // for loop

return(retval);
}
 
Remember if you have attached an EA to a chart, there are no incoming ticks at the weekend
 
I am back testing using Metatrader. I am trying to post info on the chart that the EA is trading on to make it easier to document performance
Comment("Closed Buy Orders = "+ClosedBuyOrders(),"\nClosed Sell Orders = "+ClosedSellOrders(),
   "\nTotal Closed Orders = "+(ClosedBuyOrders()+ClosedSellOrders()),
   "\nBuy Profit = ","\nSell Profit = ","\nTotal Profit = ",
   "\nOpen Buy Orders = "+OpenBuyOrders(),"\nOpen Sell Orders = "+OpenSellOrders(),
   "\nTotal Open Orders = "+(OpenBuyOrders()+OpenSellOrders()),
   "\nOpen Buy P/L = ","\nOpen Sell P/L = ","\nTotal Open P/L = ");
 
jtubbs13791:

having issue with a function that will calculate total profit. this is what i have so far. thanks for any help

this is what i use

string symbol=Symbol();
int newMagic=1234;
int SellsOpen, BuysOpen;
double SellsOpenProfit, BuysOpenProfit;
void OrdersTotalInfo()
   {
   SellsOpen=0; BuysOpen=0; BuysOpenProfit=0; SellsOpenProfit=0;
   for(int OrdersOpenTotal=OrdersTotal()-1; OrdersOpenTotal>=0; OrdersOpenTotal--)
      {
      if(OrderSelect(OrdersOpenTotal,SELECT_BY_POS,MODE_TRADES)==true)
         {
         if(OrderSymbol()==symbol && OrderMagicNumber()==newMagic &&
            //OrderComment()==_comment &&
            OrderType()==OP_SELL)
            {
            SellsOpen++;
            if(OrderProfit()+OrderCommission()+OrderSwap()>0)
               {
               SellsOpenProfit+=OrderProfit()+OrderCommission()+OrderSwap();
               }
            }
         else
            {
            if(OrderSymbol()==symbol && OrderMagicNumber()==newMagic &&
               //OrderComment()==_comment &&
               OrderType()==OP_BUY)
               {
               BuysOpen++;
               if(OrderProfit()+OrderCommission()+OrderSwap()>0)
                  {
                  BuysOpenProfit+=OrderProfit()+OrderCommission()+OrderSwap();
                  }
               }
            }
         }
      }
   }

to figure out open order quantity and floating profit/loss

 
Subgenius:

this is what i use

to figure out open order quantity and floating profit/loss

and how are you checking/reporting the value of the floating profit?

Incidentally, your code above does not calculate floating loss .

 
try
string symbol=Symbol();
int MagicNumber=1234;
double LastBuyProfit()
   {
   for(int iPos=OrdersTotal()-1; iPos>=0; iPos--)
      {//loop end to beginning
      if(OrderSelect(iPos,SELECT_BY_POS,MODE_TRADES)==TRUE)
         {//we found a ticket number
         if(OrderSymbol()==symbol &&
            OrderMagicNumber()==MagicNumber &&
            OrderType()==OP_BUY
            )
            {//we found a buy order
            return(OrderProfit()+OrderSwap()+OrderCommission());
            }
         }
      }
   return(0.00);
   }

 
GumRai:

and how are you checking/reporting the value of the floating profit?

Incidentally, your code above does not calculate floating loss .

in fact it does have to be inclusive to time so that your able to query more than is visible in your terminal display

if(TimeDayOfYear(OrderOpenTime())==TimeDayOfYear(TimeCurrent()))
   {
   TotalOrders+=1;
   }

but i use another function similar to that..

i use global variables in displaying profit.

okay, floating loss.. i mean you can find closed loss amounts by

looping though history, but open trade floating loss calculation is just a matter of 

finding out the difference between the open price +/- the bid/ask and the spread

 
Subgenius:

Thanks for the the help. it works, but it only pulls the info from the last open trade. Is there away to get all the open trades? Plus I am confused on how to get the closed profit amount. I know you change MODE_TRADES to MODE_HISTORY, but I am not getting any info. As you can tell I am fairly new to the coding scene.