Download MetaTrader 5

Discussion of article "Cross-Platform Expert Advisor: Custom Stops, Breakeven and Trailing" - page 2

To add comments, please log in or register
mbjen
35
mbjen  
Enrico Lambino:

Your first code activates breakeven, not trailingstop. If there would be any further modification of SL, it would be takeprofit. But if your TP is 500 points, the trailing would not activate at all at 500 points, since the trade has already left the market at that point.

Your second code uses trailingstop, but not breakeven. Because even before the breakeven can be applied, the SL has already moved above the breakeven price.


Hi Enrico,

I didn't get your point.

This is breakeven:

 //--- setting breakeven
   CTrail *trail_be=new CTrail();
   //trail_be.Set(BELevel,BEOpenPriceDist,0);   
   trail_be.Set(230,250,0);   
   trails.Add(trail_be);

This is trailing:

//--- setting trailing stop
   CTrail *trail=new CTrail();
   //trail.Set(trail_value,trail_start,trail_step);
   trail.Set(400,500,10);
   trails.Add(trail);

Breakeven activates at 250 points, trailing activates at 500 point. As you see trailing is not activated earlier than breakeven.

SL was not moved above (or below) the breakeven price. Stop loss was the same initial SL below the open price. The trailing stop moved SL first time above the open price (and breakeven price).

Trailing works fine. Breakeven don't work (trail_be object). If I don't use the trailing stop breakeven works fine.

mbjen
35
mbjen  

The problem seems to be only for sell order.

double CTrailsBase::Check(const string symbol,const ENUM_ORDER_TYPE type,const double entry_price,const double price,const ENUM_TRAIL_TARGET mode)
  {
   if(!Active())
      return 0;   
   double val=0.0,ret=0.0;
   MqlDateTime time_curr;
   TimeCurrent(time_curr);
   if(time_curr.day==3 && time_curr.hour==11 && time_curr.min>=30)
      Print("");
   for(int i=0;i<Total();i++)
     {
      CTrail *trail=At(i);
      if(!CheckPointer(trail))
         continue;
      if(!trail.Active())
         continue;
      int trail_target=trail.TrailTarget();
      if(mode!=trail_target)
         continue;
      val=trail.Check(symbol,type,entry_price,price,mode);
      if((type==ORDER_TYPE_BUY && trail_target==TRAIL_TARGET_STOPLOSS) || (type==ORDER_TYPE_SELL && trail_target==TRAIL_TARGET_TAKEPROFIT))
      {
         if(val>ret || ret==0.0)
            ret=val;
      }      
      else if((type==ORDER_TYPE_SELL && trail_target==TRAIL_TARGET_STOPLOSS) || (type==ORDER_TYPE_BUY && trail_target==TRAIL_TARGET_TAKEPROFIT))
      {
         if(val<ret || ret==0.0)
            ret=val;
      }      
     }
   return ret;
  }

Problem in this place

 else if((type==ORDER_TYPE_SELL && trail_target==TRAIL_TARGET_STOPLOSS) || (type==ORDER_TYPE_BUY && trail_target==TRAIL_TARGET_TAKEPROFIT))
      {
         if(val<ret || ret==0.0)
            ret=val;
      }      

I guess it should be changed to:

 else if((type==ORDER_TYPE_SELL && trail_target==TRAIL_TARGET_STOPLOSS) || (type==ORDER_TYPE_BUY && trail_target==TRAIL_TARGET_TAKEPROFIT))
      {
         if( (val>0 && val<ret) || ret==0.0)
            ret=val;
      }      
Enrico Lambino
6144
Enrico Lambino  

Thank you for explaining the issue further, and sorry that I misunderstood earlier. Regarding this change:

else if((type==ORDER_TYPE_SELL && trail_target==TRAIL_TARGET_STOPLOSS) || (type==ORDER_TYPE_BUY && trail_target==TRAIL_TARGET_TAKEPROFIT))
      {
         if( (val>0 && val<ret) || ret==0.0)
            ret=val;
      }   

Normally, I just  put the breakeven CTrail as the last index so it is evaluated last, but the code above is a more permanent solution for trailing the stoploss of sell trades.

12
To add comments, please log in or register