Need Help with Error #130 invalid stoploss - page 3

 
same Problem. The order opens fine with stoploss = 0 and takeprofit = 0.
But then OrderModify fails.

The code below is executed after the code above from the last post.

   bool modify = false;
   entry_price = OrderOpenPrice();
   if(long) {  
      SL = MarketInfo(Symbol(), MODE_BID) - stoploss * MarketInfo(Symbol(), MODE_POINT);
      TP = MarketInfo(Symbol(), MODE_BID) + takeprofit * MarketInfo(Symbol(), MODE_POINT);
      modify = OrderModify(result_ticket, entry_price, SL, TP, Red);  
   } else {    
      SL = MarketInfo(Symbol(), MODE_ASK) + stoploss * MarketInfo(Symbol(), MODE_POINT);
      TP = MarketInfo(Symbol(), MODE_ASK) - takeprofit * MarketInfo(Symbol(), MODE_POINT);
      modify = OrderModify(result_ticket, entry_price, SL, TP, Green); 
   }
   Log("[5] SL: " + SL);
   Log("[5] TP: " + TP);
   
   if(!modify) {
      Log("error="+GetLastError());
      return(-1);
   }

The Log ist:

#ESZ1,M5: Opening Position
#ESZ1,M5: tickvalue: 12.50000000
#ESZ1,M5: pos size: 1.00000000
#ESZ1,M5: Ask/Bid 1242.00000000/1241.75000000
#ESZ1,M5: Spread 0.25000000
#ESZ1,M5: open #13679252 buy 1.00 #ESZ1 at 1242.00 ok
#ESZ1,M5: Order 13679252 Successfully Opened
#ESZ1,M5: SL: 1241.25000000
#ESZ1,M5: TP: 1242.25000000
#ESZ1,M5: error=130

So even first opening and then setting stoploss doesn't seem to work :-/
 

Spread is 0.25

#ESZ1,M5: Spread 0.25000000

OK, you open a BUY at . . 1242.00 (Ask)

#ESZ1,M5: open #13679252 buy 1.00 #ESZ1 at 1242.00 ok

#ESZ1,M5: Order 13679252 Successfully Opened

Then you try to modify with a TP at the Bid price + 2 * Spread and SL at the Bid price - 2 * Spread

#ESZ1,M5: SL: 1241.25000000

#ESZ1,M5: TP: 1242.25000000

If your FreezeLevel is 0.50 then that is your problem . . what is your FreezeLevel ?

 
shinobi:
same Problem. The order opens fine with stoploss = 0 and takeprofit = 0.
But then OrderModify fails.

Not adjusting for 4/5 digit brokers. Not opening first and then setting stops.

As previously answered

 

yes you are probably trading with an ECN broker you have to open orders with zero stops then modify the order after it is opened so there is a problem with 50 pips SL and TP, so do like Raptor said check the freezelevel and stoplevel too just in case

Also check your OrderModify() code .... maybe it works that way, but I feel you should select the order using OrderSelect() before you can do OrderOpenPrice()

 
Thanks again for your help!
Alas #130 is still haunting me at night ;)

Here is my next try:

   double stoploss = 50;
   double SL = 0.0;
   
   //open position
   if(long)  //take long position
      result_ticket = OrderSend(Symbol(), 0, 1, MarketInfo(Symbol(), MODE_ASK), 2, 0, 0, "", 12345);  
   else     //take short position
      result_ticket = OrderSend(Symbol(), 1, 1, MarketInfo(Symbol(), MODE_BID), 2, 0, 0, "", 12345); 
   //check for errors
   if(result_ticket == -1) {
      Log("error="+GetLastError());
      return(-1);
   }
   Log("Order "+result_ticket+" Successfully Opened");

   //select order
   if (!OrderSelect(result_ticket, SELECT_BY_TICKET)) {
      Log("error="+GetLastError());
      return(-1);
   }    
   entry_price = OrderOpenPrice();
   
   //check stoplevel
   double stoplevel = MarketInfo(Symbol(), MODE_STOPLEVEL);
   Log("Stoplevel: " + stoplevel);
   Log("stoploss: " + stoploss);
   if(stoploss < stoplevel)
      stoploss = stoplevel + 1;

   //checl freezelevel
   double freezelevel = MarketInfo(Symbol(), MODE_FREEZELEVEL);
   Log("Freezelevel: " + freezelevel);
   Log("stoploss: " + stoploss);
   if(stoploss < freezelevel)
      stoploss = freezelevel + 1;
   
   if(long)
      SL = MarketInfo(Symbol(), MODE_BID) - stoploss * MarketInfo(Symbol(), MODE_POINT);
   else
      SL = MarketInfo(Symbol(), MODE_ASK) + stoploss * MarketInfo(Symbol(), MODE_POINT);
   Log("SL: " + SL*pips2dbl);
   if(!OrderModify(result_ticket, entry_price, SL* pips2dbl, 0, Red)) {
      Log("error="+GetLastError());
      return(-1);
   }
   Log("Stoploss successfully set");

As you can see, I simplified the example by not using takeprofit. The error definitely occurs due to stoploss, so we don't need takeprofit.
I first open the order with 0 stoploss (and takeprofit) and later try to set stoploss using OrderModify.
Before that I check Mode_Stoplevel and Mode_Freezelevel and adjust stoploss, if needed. (An adjustment occurs below in the log, because stoploss is initially 50, but Mode_Stoplevel is 75, I then set stoploss to Mode_Stoplevel + 1, so 76)

RaptorUK and SDC:
MarketInfo(MODE_FREEZELEVEL) returns 0. I don't think Freezelevel is the problem.
I included the output into the log file.

SDC:
I also inserted an OrderSelect, before calling OrderOpenPrice just to get sure. In the log you can see that OrderSelect worked, otherwise the code would have not proceeded as far (exit -1).

WHRoeder:
I followed SDC's simple example. But I now extended it by using the pips 4/5 Digit adjustment when setting stoploss, as you can see above. You can also see the the order is first opened with stoploss and takeprofit set to zero and then stoploss is set with OrderModify. As you can see in the log below with S/L and T/P set to zero, the order opens up successfully, but I still am unable to set the S/L in the next step with OrderModify.

Here is the complete log:

#ESZ1,M5: loaded successfully 
#ESZ1,M5: Date: 2011/11/15 9:24
#ESZ1,M5: Symbol: #ESZ1
#ESZ1,M5: Depot: 56489.95000000
#ESZ1,M5: Stop Level [Points]: 75.00000000
#ESZ1,M5: Freeze Level [Points]: 0.00000000
#ESZ1,M5: Spread [Points]: 25.00000000
#ESZ1,M5: Min/Max Lot: 0.01000000/1000.00000000
#ESZ1,M5: Point: 0.01000000
#ESZ1,M5: Tick Size: 0.25000000
#ESZ1,M5: Tick Value: 12.50000000
#ESZ1,M5: Digits: 2.00000000
#ESZ1,M5: Contract: 2011.09.14 00:00-2011.12.16 23:59
#ESZ1,M5: Init successfully completed.
#ESZ1,M5: initialized

#ESZ1,M5: Opening Position
#ESZ1,M5: tickvalue: 12.50000000
#ESZ1,M5: Ask/Bid 1250.50000000/1250.25000000
#ESZ1,M5: Spread 0.25000000
#ESZ1,M5: pos size: 1
#ESZ1,M5: open #13697436 sell 1.00 #ESZ1 at 1250.25 ok 
#ESZ1,M5: Order 13697436 Successfully Opened
#ESZ1,M5: Stoplevel: 75.00000000
#ESZ1,M5: stoploss: 50.00000000
#ESZ1,M5: Freezelevel: 0.00000000
#ESZ1,M5: stoploss: 76.00000000
#ESZ1,M5: SL: 12.51260000
#ESZ1,M5: error=130

I am really grateful for your ongoing help and I think we are narrowing down the problem :)
So what else could be the problem? Anything else I could try?

shinobi
 
RaptorUK:

Spread is 0.25

#ESZ1,M5: Spread 0.25000000

OK, you open a BUY at . . 1242.00 (Ask)

#ESZ1,M5: open #13679252 buy 1.00 #ESZ1 at 1242.00 ok

#ESZ1,M5: Order 13679252 Successfully Opened

Then you try to modify with a TP at the Bid price + 2 * Spread and SL at the Bid price - 2 * Spread

Isn't your STOP_LEVEL 3 * Spread ? your Stop is too close . . .

#ESZ1,M5: Stoplevel: 75.00000000
#ESZ1,M5: stoploss: 50.00000000
 
Hey Raptor,

Yeah that's why I check the stoplevel here (from the code above):

   double stoplevel = MarketInfo(Symbol(), MODE_STOPLEVEL);
   Log("Stoplevel: " + stoplevel);
   Log("stoploss: " + stoploss);
   if(stoploss < stoplevel)
      stoploss = stoplevel + 1;

So in the last example stoploss was set to 76, which is 3*Spread +1.
That should be far away enough, right?
 
shinobi:
Hey Raptor,

Yeah that's why I check the stoplevel here (from the code above):


So in the last example stoploss was set to 76, which is 3*Spread +1.
That should be far away enough, right?
Yes it should . . . but I'm wondering if you have a rounding issue here and if you need to round to the nearest 0.25 (S&P500 moves in 0.25 steps) so that your SL is clear of STOP_LEVEL by 0.25 or more . . . if you get what I mean ?
 
Good Idea, but alas that didn't help either.
Here's what I did.

I first wrote a custom function to round doubles to the nearest double:
double DoubleRound(double number, double step)
{
    double mod = MathMod(number, step);
    if(mod < step/2.0)
      step = 0;
    double rounded = number - mod + step;
    return (rounded);
}

It does what you expect. If you call it with DoubleRound(1237.687360000), it returns 1237.50.

Then I used it on my S/L just before calling OrderModify. And I used MODE_TICKVALUE, which is 0.25 for #ESZ1.
So:

   //round to nearest Tickvalue   
   SL = DoubleRound(SL, MarketInfo(Symbol(), MODE_TICKVALUE));

 
  Log("SL: " + SL);
   if(!OrderModify(result_ticket, entry_price, SL* pips2dbl, 0, Red)) {
      Log("error="+GetLastError());
      return(-1);
   }

In the log you can see, that the rounding worked fine. However the stoploss was still rejected :/

#ESZ1,M5: loaded successfully 
#ESZ1,M5: Date: 2011/11/16 7:44
#ESZ1,M5: Symbol: #ESZ1
#ESZ1,M5: Depot: 56364.95000000
#ESZ1,M5: Stop Level [Points]: 75.00000000
#ESZ1,M5: Freeze Level [Points]: 0.00000000
#ESZ1,M5: Spread [Points]: 25.00000000
#ESZ1,M5: Min/Max Lot: 0.01000000/1000.00000000
#ESZ1,M5: Point: 0.01000000
#ESZ1,M5: Tick Size: 0.25000000
#ESZ1,M5: Tick Value: 12.50000000
#ESZ1,M5: Digits: 2.00000000
#ESZ1,M5: Contract: 2011.09.14 00:00-2011.12.16 23:59
#ESZ1,M5: Opening Position
#ESZ1,M5: Ask/Bid 1240.00000000/1239.75000000
#ESZ1,M5: Spread 0.25000000
#ESZ1,M5: open #13708089 sell 1.00 #ESZ1 at 1239.75 ok 
#ESZ1,M5: Order 13708089 Successfully Opened
#ESZ1,M5: Stoplevel: 75.00000000
#ESZ1,M5: Freezelevel: 0.00000000
#ESZ1,M5: stoploss: 76.00000000
#ESZ1,M5: SL: 1237.50000000
#ESZ1,M5: error=130

I can't imagine what else the problem could be! This #130 is really persistent!!!
 

You opened a Sell

#ESZ1,M5: open #13708089 sell 1.00 #ESZ1 at 1239.75 ok

and tried to set a SL below it . . .

#ESZ1,M5: SL: 1237.50000000

#ESZ1,M5: error=130

Reason: