Easy fix for OrderModify error 1 problem (trailing stops)

 

See 'Error code 1 but not all the time ?' for details.

In basic trailing stops code this error occurrs when the new stoploss price is the same as the old one.

Trying to filter this out by compairing the two prices does not always work (see the above forum link). This seems to be a bug in mql4.

Hopefully they will fix this in the next version so we won't have to use these workarounds.


Here is the basic trailing stop code as found in the MACD sample EA: This code will often cause the OrderModify error 1.

           if(OrderType()==OP_BUY)  
           {           
              if(TrailingStop>0)  
              {                 
                 if(Bid-OrderOpenPrice()>Point*TrailingStop)
                 {
                    if(OrderStopLoss()<Bid-Point*TrailingStop)
                    {
                       OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
                       return(0);
                    }
                 }
              }
           }


Here is an easy fix simply by checking that the new stoploss price is at least 1 point or greater then the old stoploss. Just do the reverse for a Sell order.

      if (OrderType() == OP_BUY)
      {      
         if(Bid-OrderOpenPrice() > (Point*TrailingStop))
         {       
            double oldStopLoss = OrderStopLoss();
            double newStopLoss = Bid-Point*TrailingStop;
            double nextStopLoss = oldStopLoss + Point; // Adding 1 point to oldStopLoss, i.e. 76.78 to 76.79            
                       
            if(newStopLoss >= nextStopLoss) // checking that the new stoploss price is at least 1 point or greater then the old stoploss
            {                              
               OrderModify(OrderTicket(),OrderOpenPrice(),newStopLoss,0,0,Green);
               return;
            }
         }
      }


Hope this helps...

 
I think you should care about the STOPLEVEL and may be incorpore it in your StopLoss chek before modifying. (Your easy fix won't work for every case)
 

I've found that a simple comparison doesn't always work because of precision differences from one variable to the next. So I've gotten in the habit of normalizing everything in these calculations. For instance:

CurrentSL = NormalizeDouble( OrderStopLoss(), DIGITS )

NewSL = NormalizeDouble( NewStopLossValue, DIGITS )

Now a compare statement will always work properly. I had a lot of problems in this area before I started doing this.

- Tovan

 
Jacques366 wrote >>
I think you should care about the STOPLEVEL and may be incorpore it in your StopLoss chek before modifying. (Your easy fix won't work for every case)

MODE_STOPLEVEL is used to set initial stop loss and has no use in Trailing Stop code. The trailing stop will start when the order is in profit by the pips defined in the TrailingStop var.

Previous code checks that TrailingStop is >= MODE_STOPLEVEL.

I think you misundersand what this fix is about, over the standard trailing stop code. This code fixes the problem compairing doubles. MQL4 has a known problem with compairing doubles, even when

normalized. You can often get 2 (seemingly identical values) and compair them, buy they will not be equal due to small differences when converted to binary.

Like var1=145.85, var2=145.85; if (var1 == var2) return(true); //will return false sometimes!

This is a known issue, look at the forum post https://www.mql5.com/en/forum/110521 about Double Precision Math.

 
tovan wrote >>

I've found that a simple comparison doesn't always work because of precision differences from one variable to the next. So I've gotten in the habit of normalizing everything in these calculations. For instance:

CurrentSL = NormalizeDouble( OrderStopLoss(), DIGITS )

NewSL = NormalizeDouble( NewStopLossValue, DIGITS )

Now a compare statement will always work properly. I had a lot of problems in this area before I started doing this.

- Tovan

Your right about precision differences, but NormalizeDouble() does not fix the compare double problem. The problem is that the values can become very slightly different when converted to binary.

The problem shows up when two doubles are the same, like 124.23 & 124.23 and code like this is used (from trailing stop mql4 example in the MACD sample EA:

if(Bid-OrderOpenPrice()>Point*TrailingStop)

If the 2 above doubles are the same, but different in the binary, then the result would actually return TRUE if the binary was greater in the second value.

I hope Metaquotes can get this fixed in the next version...

Reason: