Order Modify Error 130 in trailing stop

 

I am testing it with an ECN broker where minimum stop level is 0 & freeze level is 0 too.  

Here is the code:

 int init(){
   
  double CalcDigits = MarketInfo(Symbol(),MODE_DIGITS);
      {
      if(CalcDigits == 2 || CalcDigits == 3) CalcPoint = 0.01;
      else if (CalcDigits == 4 || CalcDigits == 5) CalcPoint = 0.0001;
      }
      
if(CalcDigits == 2 || CalcDigits == 4) CalcSlippage = SlippagePips;
else if(CalcDigits == 3 || CalcDigits == 5) CalcSlippage = SlippagePips * 10;   
UseSlippage = (int) CalcSlippage;  

   return(0);
}
//------Trailing BUY-----------
    for (int b = OrdersTotal()-1;b>=0;b--)
    { 
     if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES) == true &&  OrderMagicNumber() == MagicNumber &&  OrderSymbol() == Symbol())
     {

      if (OrderType() == OP_BUY && (MathAbs(OrderStopLoss()-MA60)/CalcPoint)>= 1 )
     {
         if((MA60 > OrderOpenPrice()) && (MathAbs(MA60-OrderOpenPrice())/CalcPoint) >= stoploss)

         
        bool T1 = OrderModify(BuyTicket,OrderOpenPrice(),MA60,OrderTakeProfit(),0,Blue);
      
        }
       }
   }
       //------Trailing SELL-----------
   for (int b = OrdersTotal()-1;b>=0;b--)
    { 
     if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES) == true &&  OrderMagicNumber() == MagicNumber &&  OrderSymbol() == Symbol())
     {
       
      if (OrderType() == OP_SELL && (MathAbs(OrderStopLoss()-MA60)/CalcPoint)>= 1)
       {
          if((MA60 < OrderOpenPrice()) && (MathAbs(LR60-OrderOpenPrice())/CalcPoint) >= stoploss)

       bool T2 = OrderModify(SellTicket,OrderOpenPrice(),MA60,OrderTakeProfit(),0,Blue);
       }
      }
   }

I know error 130 means invalid stops but why it's invalid here. In terminal its modifying at some cases. 

 
What about printing Bid, Ask, old  stop, new stop, differences, .... if this error occurs?
 
cashcube:

I am testing it with an ECN broker where minimum stop level is 0 & freeze level is 0 too.  

Here is the code:

I know error 130 means invalid stops but why it's invalid here. In terminal its modifying at some cases. 

Gooly is absolutely correct, and you should always do Prints of relevant data so as to debug what is happening.

However, since it seems that you are using a Moving Average as your Stop-Loss, remember that the value of that MA has more decimal digits and is not in anyway aligned with the symbol's Tick Size. To correct it, always align it (example code, untested):

double dblTickSize = MarketInfo( _Symbol, MODE_TICKSIZE );
double dblStopLoss = round( MA60 / dblTickSize ) * dblTickSize;

Also, there seems to be a possible bug in your code as you use MA60 for the Buy order and LR60 in the sell order:

//------Trailing BUY-----------
   ...
      if((MA60 > OrderOpenPrice()) && (MathAbs(MA60-OrderOpenPrice())/CalcPoint) >= stoploss)

//------Trailing SELL-----------
   ...
      if((MA60 < OrderOpenPrice()) && (MathAbs(LR60-OrderOpenPrice())/CalcPoint) >= stoploss)
 

Sorry about the typo mistake with LR on place of MA, but in original program there is no such mistake. Yes its true I am using MA for trailing stop.

Now about the decimal digit alignment, I added the code, also printed the following data. 

2016.07.02 20:36:56.831 2016.05.27 06:08  EURUSD,M1: OrderModify error 130

2016.07.02 20:36:56.831 2016.05.27 06:08  EURUSD,M1: Bid = 1.11859 Ask = 1.11861 Old Stop = 1.11885 New Stop = 1.11838 SELL  OrderOpenPrice 1.11854

2016.07.02 20:36:56.831 2016.05.27 06:08  EURUSD,M1: OrderModify error 130

2016.07.02 20:36:56.831 2016.05.27 06:08  EURUSD,M1: Bid = 1.11858 Ask = 1.1186 Old Stop = 1.11885 New Stop = 1.11838 SELL  OrderOpenPrice 1.11854

In this data for sell trade old stop & new stop looks ok, but not the Bid Ask price.  I got it where is the problem. For Sell Ask & Bid both are above the new stop.

Then I need to add Bid/Ask checker before the order modify.

 

Checking it with Bid > MA60 & Ask < MA60 solved the problem.

Thank you

Reason: