How to set: trailing stop to minimum, if trail

 

Title says it all.

This is what I am using, but I seem to be running into issues with brokers who minimum trail is 0.00003 rather than 0.00030.

if (TrailingStop < (MarketInfo(Symbol(),MODE_STOPLEVEL)/ broker_digits + MarketInfo(Symbol(), MODE_SPREAD)/ broker_digits +1)){ //checks if trail is < minimum+spread
TrailingStop = (MarketInfo(Symbol(),MODE_STOPLEVEL)/ broker_digits + MarketInfo(Symbol(), MODE_SPREAD)/ broker_digits +1); // increase trail to min+spread
 

What is broker_digits and why do you divide the spread by broker_digits +1 ?

 

broker_digits determines if the broker is 4/5 digits.

I divide the spread so that it displays as 3, rather than 30.

I then add +1 to make sure the trailing stop is > minimum stop + spread + 1 ( almost a buffer you could say)

E.g. The EA has a trailing stop of 3, but the broker minimum trail(2) + spread(2) is 4, so the trailing stop is not triggered. In this case, the EA will change the trailing stop to 2+2+1=5.

The trailing stop will therefore be 5 and work without an order modify error.

I think this code is okay, but I am looking for an alternative solution.

 
Sorry, my error, I was reading your code wrong. :-)
 
RaptorUK:
Sorry, my error, I was reading your code wrong. :-)

Do you think it is a good solution? Would you do something similar to adjust the trailing stop to the minimum required?
 
  1. MarketInfo(Symbol(), MODE_STOPLEVEL)*Point
    is the closest a stop can be, (3 pips or 30 points on IBFX.) 30/5 is meaning less.

  2. MarketInfo(Symbol(), MODE_SPREAD)
    is 20 on EURUSD (IBFX) but is zero on on some other pairs. Instead just add Ask-Bid
  3. if (TrailingStop < (MarketInfo(Symbol(),MODE_STOPLEVEL)/ broker_digits + MarketInfo(Symbol(), MODE_SPREAD)/ broker_digits +1)){ //checks if trail is < minimum+spread
    TrailingStop = (MarketInfo(Symbol(),MODE_STOPLEVEL)/ broker_digits + MarketInfo(Symbol(), MODE_SPREAD)/ broker_digits +1); // increase trail to min+spread
    Don't write impenetrable code, document it
    double  minGapPts   = MarketInfo(Symbol(),MODE_STOPLEVEL),  // unit is points
            minGapPtsAdj= minGapPts/ broker_digits;             // 1/5 minimum points
            
            spreadPts   = MarketInfo(Symbol(), MODE_SPREAD),    // unit is points
            spreadPtsAdj= spreadPts / broker_digits,            // 1/5 spread points
            
            whatever    = minGapPtsAdj + spreadPtsAdj + 1;      // unit is still points
            
    if (TrailingStop < whatever){ //checks if trail is < minimum+spread
        TrailingStop = whatever; // increase trail to min+spread
        // compareing a double to points is MEANINGLESS
    
    correct it
    double  minGapPts   = MarketInfo(Symbol(),MODE_STOPLEVEL),  // unit is points
            spreadDbl   = Ask - Bid,
    
            trailByDbl  = minGapPts * Point + spreadDbl         // delta price
            newSlPrc    = Bid - trailBy;
    if (TrailingStop < newSlPrc){ //checks if trail is < minimum+spread
        TrailingStop = newSlPrc; // increase trail to min+spread
    
Reason: