Setting stoploss: OrderModify error 130

 

Hi,

I know this has been covered a trillion times everywhere but I am stuck on setting a stoploss:

My code:

 

Print("Setting stop loss: " + Stopper);
            if (OrderSymbol() == Symbol() && (OrderMagicNumber() == MagicNumber||OrderMagicNumber()==0)  && Stopper != 0)
            {
               ticket=OrderModify(OrderTicket(), OrderOpenPrice(), NormalizeDouble(Stopper,Digits), NormalizeDouble(PriceTarget,Digits), 0, Yellow);
               int err = GetLastError();
               if(!ticket && err > 0){
                  Print(MarketInfo(Symbol(),MODE_STOPLEVEL));
                   Print("SetSLTP: OrderModify(#"+IntegerToString(OrderTicket())+"  @"+DoubleToStr(OrderOpenPrice(), _Digits)
                  +") - SL("+DoubleToStr(OrderStopLoss(), _Digits)+"): " + DoubleToStr(Stopper, _Digits) + " TP("+DoubleToStr(OrderTakeProfit(), _Digits)+"):" + DoubleToStr(PriceTarget, _Digits)+ " Ask:" + Ask + " Bid:" + Bid);
                  Print("Error in OrderModify. Error code=",err, " -> ");
                  Print("");
               }
             }

 Which gives the following error in the Journal: 

2016.05.01 13:24:04.480 2003.11.18 16:00  Tokyo USDJPY,M15: Error in OrderModify. Error code=130 -> 
2016.05.01 13:24:04.480 2003.11.18 16:00  Tokyo USDJPY,M15: SetSLTP: OrderModify(#172  @110.932) - SL(0.000): 108.285 TP(110.495):110.495 Ask:108.242 Bid:108.24
2016.05.01 13:24:04.480 2003.11.18 16:00  Tokyo USDJPY,M15: 0
2016.05.01 13:24:04.480 2003.11.18 16:00  Tokyo USDJPY,M15: OrderModify error 130
2016.05.01 13:24:04.480 2003.11.18 16:00  Tokyo USDJPY,M15: Setting stop loss: 108.285
2016.05.01 13:24:04.480 2003.11.18 16:00  Tokyo USDJPY,M15: price=110.395 stoploss=108.285 bid=108.24 ask=108.242
 

Can you tell me why that stoploss doesn't work?

It doesn't seem to be far away from bid/ask?

It is in the backtester so min stop level is zero as you can see. 

 
chrisdk2015:

Can you tell me why that stoploss doesn't work?

It doesn't seem to be far away from bid/ask?

It is in the backtester so min stop level is zero as you can see. 

no but

ticket=OrderModify(OrderTicket(), OrderOpenPrice(), Stopper*_Point, PriceTarget*_Point, 0, Yellow);
 
It looks like a long order? So the stoploss value should be less than the bid. In your case, 108.285 > 108.24. It's an invalid stop.
 
Jian Chen:
It looks like a long order? So the stoploss value should be less than the bid. In your case, 108.285 > 108.24. It's an invalid stop.

Yes, that's true.

I fixed it and now I am getting this.

 

2016.05.01 16:30:08.092 2003.08.29 21:32  Tokyo USDJPY,M15: Error in OrderModify. Error code=130 -> 
2016.05.01 16:30:08.092 2003.08.29 21:32  Tokyo USDJPY,M15: SetSLTP: OrderModify(#82  @117.232) - SL(0.000): 116.885 TP(117.332):117.332 Ask:116.932 Bid:116.93
2016.05.01 16:30:08.092 2003.08.29 21:32  Tokyo USDJPY,M15: 0
2016.05.01 16:30:08.092 2003.08.29 21:32  Tokyo USDJPY,M15: OrderModify error 130
2016.05.01 16:30:08.092 2003.08.29 21:32  Tokyo USDJPY,M15: Setting stop loss: 116.885
2016.05.01 16:30:08.092 2003.08.29 21:32  Tokyo USDJPY,M15: price=117.232 stoploss=116.885 bid=116.93 ask=116.932

Now the stoploss is less than the bid but it is still complaining.

I changed the ordermodify to

ticket=OrderModify(OrderTicket(), OrderOpenPrice(), Stopper*_Point, PriceTarget*_Point, 0, Yellow);
 
chrisdk2015:

Yes, that's true.

I fixed it and now I am getting this.

 

Now the stoploss is less than the bid but it is still complaining.

I changed the ordermodify to

You can't set a SL below the market for a SELL order.
 

yes you should at least

if(OrderType()==OP_BUY)
 {
  // Do This.
 }

else if(OrderType()==OP_SELL)
 {
  // Do This.
 }
otherwise you keep running into problems.
 
Alain Verleyen:
You can't set a SL below the market for a SELL order.

Hello,

It is a buy (long) order so it should be correct.

For sell you are right. 

This is for a SELL order:

 

2016.05.01 19:56:48.380 2003.08.01 05:45  Tokyo USDJPY,M15: Error in OrderModify. Error code=130 -> 
2016.05.01 19:56:48.130 2003.07.29 05:45  Tokyo USDJPY,M15: SetSLTP: OrderModify(#60  @119.010) - SL(0.000): 119.297 TP(118.910):118.910 Ask:119.252 Bid:119.25 OrderType: 1
2016.05.01 19:56:48.130 2003.07.29 05:45  Tokyo USDJPY,M15: 0
2016.05.01 19:56:48.130 2003.07.29 05:45  Tokyo USDJPY,M15: OrderModify error 130
2016.05.01 19:56:48.130 2003.07.29 05:45  Tokyo USDJPY,M15: Setting stop loss: 119.297
2016.05.01 19:56:48.130 2003.07.29 05:45  Tokyo USDJPY,M15: price=119.01 stoploss=119.297 bid=119.25 ask=119.252

Not sure what is going on...

SL is above ask so should be OK. 

EDIT: this code does the trick

https://book.mql4.com/trading/ordermodify

Thanks all! 

Modification of Orders - Programming of Trade Operations - MQL4 Tutorial
Modification of Orders - Programming of Trade Operations - MQL4 Tutorial
  • book.mql4.com
Modification of Orders - Programming of Trade Operations - MQL4 Tutorial
 
chrisdk2015:

Can you tell me why that stoploss doesn't work?

It doesn't seem to be far away from bid/ask?

It is in the backtester so min stop level is zero as you can see. 

Excuse me, but the sl can be OrderOpenprice in OrdemModify fuction? I cannot seti it because MT4 doesn't work and send me error 130... 

 

If it's too close it will reject.

So you must check.