Trailing with commission/spread

 

The picture below I want to avoid commissions being greater than profits (Loss). 

I'd like it to be the other way around when trailing, problem is I don't know if this is the correct way to do so with the trailing function below.

2. Could be that I'm not calculating spread with commissions or spread? 

for(int e=OrdersTotal()-1;e>=0;e--)
  {
   if(OrderSelect(e,SELECT_BY_POS,MODE_TRADES))
   if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
   if(OrderType()==OP_BUY)
   if(Bid-OrderOpenPrice()>WhenToTrailSL*Pips()&&AccountProfit()-OrderCommission()>0)
   if(OrderStopLoss()<Bid-Pips()*TrailAmountSL)
   bool TrailModBuy=OrderModify(OrderTicket(),OrderOpenPrice(),Bid-(Pips()*TrailAmountSL),OrderTakeProfit(),0,clrNONE); 
  }
  
  for(int f=OrdersTotal()-1;f>=0;f--)
  {
   if(OrderSelect(f,SELECT_BY_POS,MODE_TRADES))
   if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
   if(OrderType()==OP_SELL)
   if(OrderOpenPrice()-Ask>WhenToTrailSL*Pips()&&AccountProfit()-OrderCommission()>0)
   if(OrderStopLoss()>Ask+TrailAmountSL*Pips()||OrderStopLoss()==0)
   bool TrailModSell=OrderModify(OrderTicket(),OrderOpenPrice(),Ask+(TrailAmountSL*Pips()),OrderTakeProfit(),0,clrNONE);
  }
 
Scalper8:

The picture below I want to avoid commissions being greater than profits (Loss). 

I'd like it to be the other way around when trailing, problem is I don't know if this is the correct way to do so with the trailing function below.

2. Could be that I'm not calculating spread with commissions or spread? 

Try order profit instead of account profit 

also i think its +OrderCommission() +OrderSwap 
 
Lorentzos Roussos #: Try order profit instead of account profit also i think its +OrderCommission() +OrderSwap 

Total Profit is OrderProfit() + OrderSwap() + OrderCommission(). Some brokers don't use the Commission/Swap fields. Instead, they add balance entries. (Maybe related to Government required accounting/tax laws.)
          "balance" orders in account history - Day Trading Techniques - MQL4 programming forum (2017)

Broker History
FXCM Commission - «TICKET»
Rollover - «TICKET»
? >R/O - 1,000 EUR/USD @0.52
? #«ticket»  N/A
OANDA Balance update
Financing (Swap: One entry for all open orders.)
 
William Roeder #:

Total Profit is OrderProfit() + OrderSwap() + OrderCommission(). Some brokers don't use the Commission/Swap fields. Instead, they add balance entries. (Maybe related to Government required accounting/tax laws.)
          "balance" orders in account history - Day Trading Techniques - MQL4 programming forum (2017)

Broker History
FXCM Commission - «TICKET»
Rollover - «TICKET»
? >R/O - 1,000 EUR/USD @0.52
? #«ticket»  N/A
OANDA Balance update
Financing (Swap: One entry for all open orders.)

Yeah but he wants to trail above the commision and swaps live .

I haven't encountered this so i'm not familiar with it .

You can share the code you use to identify swaps and commission entries this way .It'd be useful . Was it in the OrderType() if i recall ? (the balance event)

Also the editor butchered your link .  

Edit : 

Found it https://www.mql5.com/en/forum/124726#comment_3255282

Forum on trading, automated trading systems and testing trading strategies

Check Deposit / Withdrawal information

gordon, 2010.03.21 13:45

  1. 'Balance' statement: OrderType()==6
  2. 'Credit' statement: OrderType()==7
The order must be selected from the history pool first... OrderProfit() will return the amount, OrderOpenTime() will return the datetime, etc.

p.s. This is undocumented for some reason, but it does appear in some official MQ code in the articles...


So i guess the ticket is going in the OrderComment() in such an event 

 
William Roeder #:

Total Profit is OrderProfit() + OrderSwap() + OrderCommission(). Some brokers don't use the Commission/Swap fields. Instead, they add balance entries. (Maybe related to Government required accounting/tax laws.)
          "balance" orders in account history - Day Trading Techniques - MQL4 programming forum (2017)

Broker History
FXCM Commission - «TICKET»
Rollover - «TICKET»
? >R/O - 1,000 EUR/USD @0.52
? #«ticket»  N/A
OANDA Balance update
Financing (Swap: One entry for all open orders.)

I'll give it a try : 

if(Bid-OrderOpenPrice()>WhenToTrailSL*Pips()&&OrderProfit()+OrderSwap()+OrderCommission()>0)
 
William Roeder #:

Total Profit is OrderProfit() + OrderSwap() + OrderCommission(). Some brokers don't use the Commission/Swap fields. Instead, they add balance entries. (Maybe related to Government required accounting/tax laws.)
          "balance" orders in account history - Day Trading Techniques - MQL4 programming forum (2017)

Broker History
FXCM Commission - «TICKET»
Rollover - «TICKET»
? >R/O - 1,000 EUR/USD @0.52
? #«ticket»  N/A
OANDA Balance update
Financing (Swap: One entry for all open orders.)

This would cut it for the commission i suppose , although he'd have to capture it and store it for each order .

double find_my_commission(int for_ticket){
string t_s=IntegerToString(for_ticket);
for(int i=0;i<OrdersHistoryTotal();i++){
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)){
if(OrderType()==6){
if(StringFind(OrderComment(),t_s,0)!=-1){
return(OrderProfit());
}}}}
return(0.0);
}
 
Scalper8 #:

I'll give it a try : 

Commissions are still greater than profits when using the following: 

if(Bid-OrderOpenPrice()>WhenToTrailSL*Pips()&&OrderProfit()+OrderSwap()+OrderCommission()>0)

Tried changing the 0 to 3.5, hence my broker charges 3.5 per lot size still no luck as theirs no movement when trailing . Should I be adding lot size with commission? 

 
Scalper8 #:

Commissions are still greater than profits when using the following: 

Tried changing the 0 to 3.5, hence my broker charges 3.5 per lot size still no luck as theirs no movement when trailing . Should I be adding lot size with commission? 

Can you see the OrderCommission() programmatically ? (98% you can) , lets start there

 
Lorentzos Roussos #:

Can you see the OrderCommission() programmatically ? (98% you can) , lets start there

yes I can

 
Scalper8 #:

yes I can

Okay so , if the profit of the order + commission + swaps is above zero you can trail at the current price . But , trailing distance brings the SL to a different level not the current price so there is a way to calculate (or more appropriately estimate) which price and above you can trail for each order . I'll code it now .

 

Here is the function along with a test order , it returns the trail price for the min profit specified .

The trail distance must be in points (like 10 pips should be sent like 100.0 pts)

#property strict

double check_trails(int _ticket,//the order ticket
                  double min_profit,//the min profit required for the FIRST trail that is applied
                  double trail_distance_in_points,//trail distance in points , the trail above is calculated automatically
                  bool calc_swap_ahead){//calculate swap of the current trading day (that has not been applied yet)
//select the order 
  if(OrderSelect(_ticket,SELECT_BY_TICKET))
  {
  double op=OrderOpenPrice(),sl=OrderStopLoss(),tp=OrderTakeProfit();
  double profit=OrderProfit()+OrderSwap()+OrderCommission();
  double cns=MathAbs(OrderSwap()+OrderCommission());//commissions and swaps , this is the cost to cover 
  bool is_trailed=(OrderType()==OP_BUY&&sl>=op)||(OrderType()==OP_SELL&&sl<=op&&sl!=0.0);
  //tick value for one lot currently  
    double tvol=(double)SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_VALUE);
  //tick value for these lots 
    tvol*=OrderLots();
    //if calc swap ahead
      if(calc_swap_ahead){
    //swap per lot - 
    bool swap_is_points=(SymbolInfoInteger(_Symbol,SYMBOL_SWAP_MODE)==0);
    double swap_per_lot=(double)SymbolInfoDouble(_Symbol,SYMBOL_SWAP_LONG);
    if(OrderType()==OP_SELL){swap_per_lot=(double)SymbolInfoDouble(_Symbol,SYMBOL_SWAP_SHORT);}
    if(swap_is_points){swap_per_lot*=tvol;}//per tick value since points
    //now find the day 
      ENUM_DAY_OF_WEEK dow=(ENUM_DAY_OF_WEEK)TimeDayOfWeek(TimeCurrent());
      double rollovers[]={0.0,1.0,1.0,3.0,1.0,0.0,0.0};
      swap_per_lot*=rollovers[(int)dow];
    //if its positive remove from cns if its negative add to cns so :
      cns-=swap_per_lot;
      //because the cns measures the absolute cost of the trade
    }
    //if calc swap ahead ends here
    //the min point of trail is cost+min_profit
      cns+=min_profit;
    //and turn to points 
      double min_trail_points=(cns/tvol)*_Point;
    //find trail above 
      double trail_above=op+min_trail_points;
      if(OrderType()==OP_SELL){trail_above=op-min_trail_points;}
    //project trail
      RefreshRates();
      //buy
        if(OrderType()==OP_BUY){
        double new_stop_loss=Bid-trail_distance_in_points*_Point;
        if(new_stop_loss>trail_above&&(new_stop_loss>sl||sl==0.0)){
          bool modi=OrderModify(ticket,0.0,new_stop_loss,tp,NULL,clrWhite);
        }
        else if(is_trailed&&sl<trail_above){
        //cancel stop loss if trail above rises and its trailed 
          bool modi=OrderModify(ticket,0.0,0.0,tp,NULL,clrWhite);
        }}
      //sell
        else if(OrderType()==OP_SELL){
        double new_stop_loss=Ask+trail_distance_in_points*_Point;
        if(new_stop_loss<trail_above&&(new_stop_loss<sl||sl==0.0)){
          bool modi=OrderModify(ticket,0.0,new_stop_loss,tp,NULL,clrWhite);
        }
        else if(is_trailed&&sl>trail_above){
        //cancel stop loss if trail above rises and its trailed 
          bool modi=OrderModify(ticket,0.0,0.0,tp,NULL,clrWhite);
        }}
  return(trail_above);
  }
return(0.0);
}
int ticket=-1;
int OnInit()
  {
//--- create timer
   //EventSetTimer(60);
   ticket=OrderSend(_Symbol,OP_BUY,0.08,Ask,1000,0.0,0.0,NULL,0,0,clrBlack);
   return(INIT_SUCCEEDED);
  }

void OnTick()
  {
//---
  double profit=check_trails(ticket,24.0,100.0,true);
  Comment("Profit : "+profit);
  }