Download MetaTrader 5

Trying to calculate total profit for trades

To add comments, please log in or register
Do you need a programmer? Make an order to let the developer find you!
jtubbs13791
71
jtubbs13791 2014.11.29 19:23 

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);
}
Keith Watford
Moderator
9627
Keith Watford 2014.11.29 20:08  
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);
}
jtubbs13791
71
jtubbs13791 2014.11.29 20:33  
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);
}
Keith Watford
Moderator
9627
Keith Watford 2014.11.29 21:56  
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);
}
Keith Watford
Moderator
9627
Keith Watford 2014.11.29 22:03  
Remember if you have attached an EA to a chart, there are no incoming ticks at the weekend
jtubbs13791
71
jtubbs13791 2014.11.29 22:37  
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 = ");
Subgenius
582
Subgenius 2014.11.30 01:47  
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

Keith Watford
Moderator
9627
Keith Watford 2014.11.30 02:09  
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 .

Subgenius
582
Subgenius 2014.11.30 02:10  
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);
   }

Subgenius
582
Subgenius 2014.11.30 02:16  
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

jtubbs13791
71
jtubbs13791 2014.11.30 22:38  
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.
12
To add comments, please log in or register