Modifying order returns error 130

13,744

Solution 1

Error #130 is ERR_INVALID_STOPS.

The most likely problem is that

a) the stoploss level you are inputting is too close to the order open price. This is dictated by

MarketInfo( Symbol(), MODE_STOPLEVEL ) // returns a min allowed distance [pts]

else

b) because you have not normalized the stoploss level with NormalizeDouble().

See below for a buy order example. In your example, i.e. for a sell order, note that you should be opening the order at the Bid price, not Ask as you have. Note also that the stoploss and takeprofit are usually calculated relative to the bid price, as the bid is what is displayed on your charts, unfortunately you just have to take the spread loss in stride.

Only other minor problem is that you input no colour for the last parameter in OrderModify(). Unlike in OrderSend(), these are not initialized by default in the function definition, so you should pass them really.

//--- get minimum stop level
   double minstoplevel = MarketInfo( Symbol(), MODE_STOPLEVEL );
   Print( "Minimum Stop Level=", minstoplevel, " points" );
   double price = Ask;
//--- calculated SL and TP prices must be normalized
   double stoploss   = NormalizeDouble( Bid - minstoplevel * Point, Digits );
   double takeprofit = NormalizeDouble( Bid + minstoplevel * Point, Digits );
//--- place market order to buy 1 lot
   int ticket = OrderSend( Symbol(), OP_BUY, 1, price, 3, stoploss, takeprofit, "My order", 16384, 0, clrGreen );

Solution 2

OrderModify() call may collide with not one, but two constraints

The first, being a trivial one -- one cannot put SL/TP closer than your Broker allows via a MODE_STOPLEVEL defined distance.

The second, being a less visible one -- one cannot change { SL | TP } in case a Broker defined freezing distance is visited by a respective XTO price ( an eXecute-Trade-Operation price, being { Ask for Short.SL & Short.TP | Bid for Long.TP & Long.SL } )

MarketInfo( Symbol(), MODE_STOPLEVEL ) // returns a min allowed distance [pts]

MarketInfo( Symbol(), MODE_FREEZELEVEL ) // returns a freezing distance [pts]

OrderSend() may be constrained on some ECN/STP account types

Another quite common condition set on STP/ECN systems ( introduced by the Broker's inhouse Risk Management policy ) is that one is not allowed to setup TP/SL right at the OrderSend(), but has to leave these blank and upon a positive confirmation of the OrderSend(), submit a separate OrderModify() instruction for the given OrderTicketNumber to add ex-post the TP and/or SL price-level(s)

A NormalizeDouble()-whenever-possible practice is not separately commented here, as MetaQuotes Inc. publishes this as a must-do.


A recommended practice

Carefully review your Broker's Terms & Conditions and consult with your Account Manager the complete mix of Broker-side policies that apply to your type of Trading Account.

Solution 3

When you execute a buy trade, your price is the Ask, your stoploss and takeprofit are reference to the opposite trade, as when closing you're subject to the Bid price.

using this simple rule, when you buy your stoploss and takeprofit will be:

   double stoploss   = NormalizeDouble( Bid - minstoplevel * Point, Digits );
   double takeprofit = NormalizeDouble( Bid + minstoplevel * Point, Digits );

   int    ticket     = OrderSend( Symbol(),
                                  OP_BUY,
                                  lots,
                                  price,
                                  slippage,
                                  stoploss,
                                  takeprofit
                                  );

the opposite, when you sell:

   double stoploss   = NormalizeDouble( Ask + minstoplevel * Point, Digits );    
   double takeprofit = NormalizeDouble( Ask - minstoplevel * Point, Digits );

   int    ticket     = OrderSend( Symbol(),
                                  OP_SELL,
                                  lots,
                                  price,
                                  slippage,
                                  stoploss,
                                  takeprofit
                                  );
Share:
13,744
Filipe Ferminiano
Author by

Filipe Ferminiano

Updated on June 04, 2022

Comments

  • Filipe Ferminiano
    Filipe Ferminiano about 2 years

    I'm trying to modify an order, but I keep Error modifying order!, error#130. I'm using an ECN broker, so I need to modify the order to set a stoploss/takeprofit. What I am doing wrong?

    int digits = MarketInfo( Symbol(), MODE_DIGITS );
    if (      digits == 2 || digits == 3 ) pipdigits = 0.01;
    else if ( digits == 4 || digits == 5 ) pipdigits = 0.0001;
    
    selltakeprofit = Ask + ( takeprofit * pipdigits );
    sellstoploss   = Ask - ( stoploss   * pipdigits );
    
    ticket = OrderSend( Symbol(), OP_SELL, lotsize, Ask, 100, 0, 0, 0, 0, 0, CLR_NONE );
    if ( ticket < 0 )
        {
           Print( "venda Order send failed with error #", GetLastError() );
           Print( "stop loss = ",                         sellstoploss );
         }
    else
        {
           Print( "Order send sucesso!!" );
           Print( "Balance = ",                           AccountBalance() );
           Print( "Equity = ",                            AccountEquity() );
    
           bool res = OrderModify( ticket, 0, sellstoploss, selltakeprofit, 0 );
    
           if ( res == false )
             {
                 Print( "Error modifying order!, error#", GetLastError() );
                 Print( "sellstoploss ",                  sellstoploss );
                 Print( "selltakeprofit ",                selltakeprofit );
                 Print( "StopLevel ",                     StopLevel );
                 Print( "Ask ",                           Ask );
              }
          else
            {
                 Print( "Order modified successfully" );
             }
         }
    
  • Filipe Ferminiano
    Filipe Ferminiano over 9 years
    got it, but I still can't modify order, even using Bid prices