last order profit

 

Hi all,

 

Say that I have more than one order open and I just want to get the profit, commission or any other information regarding the state of the last  order. Is the following code the right way to get it ?

       void OrderGetProfit()
          { 
          Profit = 0;        
          for(int OrderProfit = OrdersTotal()-1; OrderProfit >= 0; OrderProfit--)
               
         if(OrderSelect(OrderProfit, SELECT_BY_POS, MODE_TRADES)
            && OrderMagicNumber()== MagicNumber 
            && OrderSymbol()== Symbol())                                                    
            {
             Profit = OrderProfit();
             {               
              break;
             }                                      
            }               
         if(Profit > 17)Do something......         
          }
      return(0);

 Thank you in advance for clarification provided

Luis 

 

 NO

//---- Number ONE to DO  ==>  CHECK OPEN TRADES
   if(TotalOpenEA > 0)
     {
      TotalOpenEA = 0;
      TotalBuy = 0;
      TotalSell =0;
      TotalBuyStop =0;
      TotalSellStop=0;
      TotalBuyLimit =0;
      TotalSellLimit =0;
      double BuyLots,SellLots,HighestLot;
      for(i = OrdersTotal()-1; i >= 0 ; i--)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)        break;
         if(OrderMagicNumber()!=MagicNumber || OrderSymbol()!=Symbol()) continue;
         TotalOpenEA++;
         if(HighestLot < OrderLots())HighestLot=OrderLots();
         //---- check order type
         if(OrderType()==OP_BUY)
           {
            TotalBuy++;
            if(OrderLots()> BuyLots)BuyLots=OrderLots();
           } 
         if(OrderType()==OP_SELL)         
           {//4
            TotalSell++;
            if(OrderLots()> SellLots)SellLots=OrderLots(); 
           }//4
         if(OrderType()==OP_BUYSTOP)
           {//5
            TotalBuyStop++;
           }//5
         if(OrderType()==OP_SELLSTOP)
           {//6
            TotalSellStop++;        
           }//6
         if(OrderType()==OP_BUYLIMIT)
           {//7
            TotalBuyLimit++;        
           }//7
         if(OrderType()==OP_SELLLIMIT)
           {//8
            TotalSellLimit++;          
           }//8           
        }//end loop
//   if(HighestLot==BuyLots)....
//   if(HighestLot==SellLots)....           
     
    }  

 Every trade profit = OrderProfit()+OrderSwap()+OrderCommission()

like counting trades you can do this the same all in the loop while you count the trades of your EA

for profit

orderprofit

orderswap

ordercommission

check it with print command  or comment to see what happens  ... 

 and if you only want to know the profit for the last opened order you have to check for highest OrderOpenTime() every trade no break

 
luisneves:

Hi all,

 Say that I have more than one order open and I just want to get the profit, commission or any other information regarding the state of the last  order. Is the following code the right way to get it ?

You didn't say if it was the last order closed or last placed open order.  I assume it's the last placed open order as yo aare using OrdersTotal() and not OrdersHistoryTotal().  You should check that the order you are using OrderProfit() from is actually the Order you want,  you can do this by checking that the orderOpenTime() is the greatest of all the valid orders.

Your  return(0);  looks like it is outside the function,  it needs to be inside the function.   Why not return the Profit figure and then make the decision on what to do with it in the function that called  OrderGetProfit() ? 

 
RaptorUK:

You didn't say if it was the last order closed or last placed open order.  I assume it's the last placed open order as yo aare using OrdersTotal() and not OrdersHistoryTotal().  You should check that the order you are using OrderProfit() from is actually the Order you want,  you can do this by checking that the orderOpenTime() is the greatest of all the valid orders.

Your  return(0);  looks like it is outside the function,  it needs to be inside the function.   Why not return the Profit figure and then make the decision on what to do with it in the function that called  OrderGetProfit() ? 


Hi RaptorUK,

So, here is what I've done.

What am looking for is to identify the last order and get from it the OrderProfit() and OrderCommission().

If my MaxOrders is say, 5 and as soon the profit is 0 then all open orders should close.

Is the code below correct to get the above ?

Thank you in advance for any help provided.

void GetOrderProfit()
       {//0
        
        int LastOrderTime = 0;
        for(int Orders = OrdersTotal()-1; Orders >= 0; Orders--)
           {//1
          if(!OrderSelect(Orders,SELECT_BY_POS,MODE_TRADES))continue;
            {//2
          if(OrderSymbol() == Symbol()&& OrderMagicNumber() == MagicNumber)
             {//3
            if(OrderOpenTime() > LastOrderTime)
              {//4
             if(OTCurrentTick == MaxOrders)
               {//5
             if(OrderProfit() + OrderCommission() >= 0)CloseAll();                    
               }//5
              }//4      
             }//3
          LastOrderTime = OrderOpenTime();
            }//2
           }//1 
         }//0           
 
luisneves:


Hi RaptorUK,

So, here is what I've done.

What am looking for is to identify the last order and get from it the OrderProfit() and OrderCommission().

If my MaxOrders is say, 5 and as soon the profit is 0 then all open orders should close.

Is the code below correct to get the above ?

Your code . . .

void GetOrderProfit()
   {//0
   int LastOrderTime = 0;

   for(int Orders = OrdersTotal()-1; Orders >= 0; Orders--)
      {//1
      if(!OrderSelect(Orders,SELECT_BY_POS,MODE_TRADES))continue;
         {//2
         if(OrderSymbol() == Symbol()&& OrderMagicNumber() == MagicNumber)
            {//3
            if(OrderOpenTime() > LastOrderTime)
               {//4
               if(OTCurrentTick == MaxOrders)
                  {//5
                  if(OrderProfit() + OrderCommission() >= 0)CloseAll();                    
                  }//5
               }//4      
            }//3
         LastOrderTime = OrderOpenTime();
         }//2
      }//1 
   }//0  

 You keep track of the newest order by storing it's OrederOpenTime() in the variable LastOrderTime,  so when you find an order that has a OrderOpenTime() newer,  i.e. greater than LastOrderTime you need to update this variable.  Yoo are updating it regardless  . . .  for every order,  so all you achieve is getting the Order that is first in the order pool, it doesn't even matter if it matches your symbol and magic number  do you see what I mean ?

 
RaptorUK:

Your code . . .

 You keep track of the newest order by storing it's OrederOpenTime() in the variable LastOrderTime,  so when you find an order that has a OrderOpenTime() newer,  i.e. greater than LastOrderTime you need to update this variable.  Yoo are updating it regardless  . . .  for every order,  so all you achieve is getting the Order that is first in the order pool, it doesn't even matter if it matches your symbol and magic number  do you see what I mean ?

 


Hi RaptorUK,

Thank you for your prompt response to my issue and for keep to correct me get the right indenting (which I insanely keep do wrong....).

To be honest I still have some difficulties in get the right way ito write functions (among a lot of others things, but I'm trying learning...). So if I have understand your clue I need to get rid of  " LastOrderTime = OrderOpenTime();" at the bottom of the function.. Is that correct?

Best regards

Luis 

 
luisneves:


Hi RaptorUK,

Thank you for your prompt response to my issue and for keep to correct me get the right indenting (which I insanely keep do wrong....).

To be honest I still have some difficulties in get the right way ito write functions (among a lot of others things, but I'm trying learning...). So if I have understand your clue I need to get rid of  " LastOrderTime = OrderOpenTime();" at the bottom of the function.. Is that correct?

No,  you need to put it inthe correct position.

Get some paper,  a pen/pencil,  write out 5 orders that match your symbol and magic number,  work through your code with these 5 orders and see what your code does with them. 

 
RaptorUK:

No,  you need to put it inthe correct position.

Get some paper,  a pen/pencil,  write out 5 orders that match your symbol and magic number,  work through your code with these 5 orders and see what your code does with them. 


Hi RaptorUK,

 So, I need to count from the oldest to the newest order and compare the last with the former. In that case need to count up instead.

Luis

 
luisneves:


Hi RaptorUK,

 So, I need to count from the oldest to the newest order and compare the last with the former. In that case need to count up instead.

You can count up, down, left or right . . . it doesn't matter,  what matters is that you remember which order is the newest when you find one that is newer than the one you thought was the newest before.  Work through it on paper.
 
luisneves:


 So, I need to count from the oldest to the newest order and compare the last with the former. In that case need to count up instead.

Luis

 


will not make a difference 

you need to do something else

if(OrderOpenTime() > LastOrderTime)
  {
   //profit.lastopendtrade =.........
   LastOrderTime = OrderOpenTime();
  }
   

 after the loop is finished you can say if lastopendtrade is in profit

So why is CloseAll() inside the loop ??

 
deVries:


will not make a difference 

you need to do something else

 after the loop is finished you can say if lastopendtrade is in profit

So why is CloseAll() inside the loop ??


Hi deVries,

Thank you for keep attention to my issue as well.

Right, taking the clues from RaptorUK and yours I believe that am in the right way . Below is what I understood so far (if not, guys as me should be banned from the forum...),

void GetOrderProfit()
       {//0
       int LastOrderTime = 0;
       double LastOrderProfit = 0;
       
       for(int Orders = 0; Orders <= OrdersTotal()-1; Orders++)
        //for(int Orders = OrdersTotal()-1; Orders >= 0; Orders--)
          {//1
          if(!OrderSelect(Orders,SELECT_BY_POS,MODE_TRADES))continue;
            {//2
            if(OrderSymbol() == Symbol()&& OrderMagicNumber() == MagicNumber)
              {//3
              if(OrderOpenTime() > LastOrderTime)
                {//4
                 LastOrderProfit = OrderProfit() + OrderCommission(); 
                 LastOrderTime = OrderOpenTime();        
                }//4      
             }//3          
          }//2
       }//1
     if(LastOrderProfit >= 0 && OTCurrentTick == MaxOrders)CloseAll();            
    }//0 

 Luis

Reason: