include order commission on close of last order

 

Hi all,

So, I've a logic that will open consecutive market orders on an ECN Broker.

Each order have it's own commission and when the last order closes  I need to incorporate  all commissions  to get  AccountProfit() - commission > 0.

How could I get a way to get the sum of all commissions to subtract from the AccountProfit() and when the last order closes it will close in profit ?

Thanks in advance for any help here

Luis 

 
Use the OrderCommission(). And AccountProfit(). You can loop through all historical orders and sum-up the OrderCommissions.
 
ubzen:
Use the OrderCommission(). And AccountProfit(). You can loop through all historical orders and sum-up the OrderCommissions.


Hi ubzen,

Thank you for your attention to my issue.

So, What I'm think to do is to declare a variable called TotalOrderCommission and then get all all individual OrderCommission() using a counter and then sum up. 

Using the above in the following code  following code is the right way ?

Thank you

Luis 

MLots = 0;
   for(int cnt = OrdersTotal()-1; cnt >= 0; cnt--)
      {//11
      if(!OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))continue;
         {//12
         if(OrderSymbol() == Symbol()&& OrderMagicNumber() == MagicNumber)
            {//13
            TotalOrderCommission + = OrderCommission();//<--------------------------------------------Is this ok ?
            LastLot = OrderLots();           
            if(MLots <= OrderLots())MLots = NormalizeDouble(OrderLots() * Multiplier,2);               
            }//13
         }//12      
      }//11 
   return(0);
   }//0 
 

Something like this is what I had in mind. [Not_Tested].

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void start(){
    double Active_Com=Commission_All_Active();
    double Histor_Com=Commission_All_Histor();
    double Total_Comm=Active_Com+Histor_Com;
    double Prof_MiCom=AccountProfit()-Total_Comm;
    Print("Account Profit Minus Commissions = "+Prof_MiCom);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
double Commission_All_Active(){
    double Commission;
    for(int i=OrdersTotal()-1; i>=0; i--){
        if(!OrderSelect(i,SELECT_BY_POS)){continue;}
        //if(OrderMagicNumber()!=Magic_Nm){continue;}
        //if(OrderSymbol()!=Symb){continue;}
        Commission+=OrderCommission();
    }return(Commission);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
double Commission_All_Histor(){
    double Commission;
    for(int i=OrdersHistoryTotal()-1; i>=0; i--){
        if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)){continue;}
        //if(OrderMagicNumber()!=Magic_Nm){continue;}
        //if(OrderSymbol()!=Symb){continue;}
        Commission+=OrderCommission();
    }return(Commission);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
ubzen:

Something like this is what I had in mind. [Not_Tested].

 


Hi ubzen;

 Thank you for your help. Nevertheless your code that you so promptly sent will this two pieces of code get the result that I'm looking for ?

Just for consistency what am looking for is to get the start of trailing stop after all open orders are in profit.

 

TotalOrderProfit = 0;
   MLots = 0;
   for(int cnt = OrdersTotal()-1; cnt >= 0; cnt--)
      {//11
      if(!OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))continue;
         {//12
         if(OrderSymbol() == Symbol()&& OrderMagicNumber() == MagicNumber)
            {//13
            TotalOrderProfit = OrderProfit() +OrderCommission() +OrderSwap();
            OrderLots();           
            if(MLots <= OrderLots())MLots = NormalizeDouble(OrderLots() * Multiplier,2);               
            }//13
         }//12      
      }//11 
   return(0);
   }//0 


 

void Trail()
    {        
      
     double TrailingStop = 0.75;
             
      for(int OrderCounter = OrdersTotal()-1; OrderCounter >= 0; OrderCounter--)                   
         {//23
        if(!OrderSelect(OrderCounter,SELECT_BY_POS))continue;
         if(OrderType()== OP_BUY && OrderSymbol()== Symbol() 
            && OrderMagicNumber()== MagicNumber)                                                                        
            {//24
               RefreshRates();
            if(OrderStopLoss() < Bid - (TrailingStop * pips2dbl) && Bid - OrderOpenPrice() > (MinimumProfit*pips2dbl) && TotalOrderProfit > 0) 
               {//25
                  RefreshRates();
               if(!OrderModify(OrderTicket(),OrderOpenPrice(),Bid - (TrailingStop * pips2dbl), OrderTakeProfit(),0,Yellow))
                Print("Buy TrailingStop Failed, order number:",OrderTicket(),"Error:",GetLastError());
               }//25
             }//24
           }//23          
           if(OrderType()==OP_SELL && OrderSymbol()== Symbol() 
              && OrderMagicNumber()== MagicNumber)
              {//26
                 RefreshRates();
              if(OrderStopLoss() > Ask + (TrailingStop * pips2dbl) && OrderOpenPrice() - Ask > (MinimumProfit*pips2dbl) && TotalOrderProfit > 0) 
                 {//2
                    RefreshRates();
                 if(!OrderModify(OrderTicket(),OrderOpenPrice(),Ask + (TrailingStop * pips2dbl) ,OrderTakeProfit(),0,Yellow))              
                 Print("Sell TrailingStop Failed, order number:",OrderTicket(),"Error:",GetLastError());
               }//27                                         
            return(0);     
          }//26   
    }

Thanks in advance for any critic around my idea.

 

Luis