Download MetaTrader 5

Error modifying trailing stop and stop orders

To add comments, please log in or register
Share your knowledge. Become a well-known developer!
Marc Boggs
578
Marc Boggs 2016.06.13 18:20 

This has been bugging (no pun intended) me for some time.  I created an EA that performs quite well on price spikes, but it sometimes cannot modify the order to set a trailing stop or move the stop order which is set to 30 pips in this example.  I've tried other values and still run into the error occasionally.

If anyone could help, it would be greatly appreciated.  (EA Attached)

Log Messages:

MCB ScalpWiz 3.2 GBPUSDpro,M1: Modify Trade params: Symbol: GBPUSDpro OrderType: SELL 0 Open: 1.46122 Ask: 1.42213 Bid: 1.42192 SL: 1.42243 TP: 1.41192 ChaseAsk: 1.42253 ExpDate : 0
 MCB ScalpWiz 3.2 GBPUSDpro,M1: Failed to modify, error # invalid stops.
MCB ScalpWiz 3.2 GBPUSDpro,M1: OrderModify error 130


Edit and FYI:

Just made $32 on 0.01 lost size today on GBPUSD and EURUSD




Files:
Osama Shaban
1446
Osama Shaban 2016.06.13 23:53  

As I see from the EA external settings ...

extern int    TrailingStop        = 15;

 and you said that you use 30 (3 Pips) ...

The question ...

Are you sure that your broker allow you put a stoploss level at 3 pips (or 1.5 pip) away from current price ?

There could be a slippage in the price during the order modifying !!! ...

 I'm not much good at tracing others code that much but I do my own code :(

...

If things didn't work with you well ... you could stop the trailing stop and use an EA (I already have) to watch all

your orders and modify the stop according to your requirement. If you agree, please PM me because I really forgot

the name of that EA. 

Marc Boggs
578
Marc Boggs 2016.06.14 03:25  

The value 30 is 30 pips.  So, if the price spikes beyond 300 pips (up), a sell_stop is placed.  The sell_stop trails 30 pips behind the bid, in this case.  As the spike reverses, the sell_stop becomes a sell order and the trailing stop chases the ask as the price falls back down.  It will eventually hit the trailing stop loss.

I've actually figured out the issue - It was the calculated stop loss adjustment for the OrderModify() call.  I had to draw it out on paper to get my thoughts straight, but it is working, at long last. 

Attached is the EA with the correction.  Feel free to play around with it.

Files:
Mohammad Soubra
24923
Mohammad Soubra 2016.06.14 16:00  

Also you can use my code of the trailing stop/step

input int      MagicNumber    = 198202;   //Magic Number
input bool     enableTStop    = true;     //Enable Trailing Stop
input double   TrailingStart  = 30;       //Trailing Start
input double   TrailingStop   = 25;       //Trailing Stop
input double   TrailingStep   = 5;        //Trailing Step
//---
double MyPoint=Point;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   if(Digits==3 || Digits==5) MyPoint=Point*10;

//---
   return(INIT_SUCCEEDED);
  }
//-------------------------------------------------------------------+
// Trailing stop Function                                            |
//-------------------------------------------------------------------+
void TrailStop()
{
   if(enableTStop)
   {
      RefreshRates();
      
      int    Res     = 0;
      double SLPrice = 0;
      double NewSL   = 0;
      double Range   = 0;
      int    digit   = 0;
   
      for(int cnt=0; cnt<OrdersTotal(); cnt++)
         if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
           {
            while(IsTradeContextBusy()) Sleep(100);
            if(OrderType()==OP_BUY && 
               OrderMagicNumber()==MagicNumber)
              {
               Range = MarketInfo(OrderSymbol(),MODE_BID) - OrderOpenPrice();
               NewSL = MarketInfo(OrderSymbol(),MODE_BID) - TrailingStop*MyPoint;
               NewSL = NormalizeDouble(NewSL,Digits);
   
               if(Range>=TrailingStart*MyPoint)
                  if(NewSL-OrderStopLoss()>=TrailingStep*MyPoint)
                    {
                     Res=OrderModify(OrderTicket(),OrderOpenPrice(),NewSL,OrderTakeProfit(),0,clrNONE);
                     
                     //---
                     if(Res)
                     Print("Order Stop Loss Has Been Modified Successfully.");
                     else
                     Print("Error With Trailing Stop: ", GetLastError()/*,ErrorDescription(GetLastError())*/);
                    }
              }
   
            if(OrderType()==OP_SELL && 
               OrderMagicNumber()==MagicNumber)
              {
               Range = OrderOpenPrice() - MarketInfo(OrderSymbol(),MODE_ASK);
               NewSL = MarketInfo(OrderSymbol(),MODE_ASK) + TrailingStop*MyPoint;
               NewSL = NormalizeDouble(NewSL,Digits );
   
               if(Range>=TrailingStart*MyPoint)
                  if(OrderStopLoss()-NewSL>=TrailingStep*MyPoint || OrderStopLoss()==0)
                    {
                     Res=OrderModify(OrderTicket(),OrderOpenPrice(),NewSL,OrderTakeProfit(),0,clrNONE);
                     
                     //---
                     if(Res)
                     Print("Order Stop Loss Has Been Modified Successfully.");
                     else
                     Print("Error With Trailing Stop: ", GetLastError()/*,ErrorDescription(GetLastError())*/);
                    }
              }
           }
   }
}
  
To add comments, please log in or register