error 130 on OrderSend

 
void SellStop(double price,int MagN,double LotSize,datetime expiration=0,double sl=0,double tp=0,string comment=NULL)
  {

   double stoplevel=MarketInfo(_Symbol,MODE_STOPLEVEL)*Point();
   price=NormalizePrice(MathMin(price,Bid-stoplevel));

//===============================|| SELL ||======================================//
//------------------- Open Sell Order ---------------------------------------------
   RefreshRates();                                                                  //
   int ticket=OrderSend(_Symbol,OP_SELLSTOP,LotSize,price,5,0,0,comment,MagN);                 //
   if(!ticket) //
      Print("Something went wrong with SellStop Order. Error= ",GetLastError());        //

   bool Select = OrderSelect(ticket,SELECT_BY_TICKET);                              //
   if(!Select)                                                                      //
      Print("Something went wrong with Order Select while Buying. Error= ",GetLastError());      //
//------------------- Set Stop Loss -----------------------------------------------
//-------------- Modify Order with Stop Loss --------------------------------------
   RefreshRates();
   if(sl<=OrderOpenPrice()+stoplevel && sl!=0)
      sl=OrderOpenPrice()+stoplevel;
   if(tp>=OrderOpenPrice()-stoplevel && tp!=0)
      tp=OrderOpenPrice()-stoplevel;
   
   bool Modify = OrderModify(OrderTicket(),OrderOpenPrice(),sl,tp,OrderExpiration());          //
   if(!Modify)                                                                      //
      Print("Something went wrong with Modify while Buying. Error= ",GetLastError());      //

  }

This is my code for placing a sell stop. I don't place the stops in the OrderSend function, however I am still receiving 130 errors returned from the OrderSelect function (which I don't understand) and the OrderModify at the end (even though Stop Level is 0)

Does anyone have an idea what could be happening there?

 

OrderSelect returns either true or false depending on the outcome of the action namely to select the order.

It will not return 130.

OrderModify can return 130 which means INVALID_STOPS

So the logical action for you would be to analyze 

bool Modify = OrderModify(OrderTicket(),OrderOpenPrice(),sl,tp,OrderExpiration());

sl and tp values.

So can you post the log of 

void SellStop(double price,int MagN,double LotSize,datetime expiration=0,double sl=0,double tp=0,string comment=NULL)
  {

   double stoplevel=MarketInfo(_Symbol,MODE_STOPLEVEL)*Point();
   price=NormalizePrice(MathMin(price,Bid-stoplevel));

//===============================|| SELL ||======================================//
//------------------- Open Sell Order ---------------------------------------------
   RefreshRates();                                                                  //
   int ticket=OrderSend(_Symbol,OP_SELLSTOP,LotSize,price,5,0,0,comment,MagN);                 //
   if(!ticket) //
      Print("Something went wrong with SellStop Order. Error= ",GetLastError());        //

   bool Select = OrderSelect(ticket,SELECT_BY_TICKET);                              //
   if(!Select)                                                                      //
      Print("Something went wrong with Order Select while Buying. Error= ",GetLastError());      //
//------------------- Set Stop Loss -----------------------------------------------
//-------------- Modify Order with Stop Loss --------------------------------------
   RefreshRates();
   if(sl<=OrderOpenPrice()+stoplevel && sl!=0)
      sl=OrderOpenPrice()+stoplevel;
   if(tp>=OrderOpenPrice()-stoplevel && tp!=0)
      tp=OrderOpenPrice()-stoplevel;
   
   Print("SL: "+DoubleToString(sl)+" TP: "+(string)tp);

   bool Modify = OrderModify(OrderTicket(),OrderOpenPrice(),sl,tp,OrderExpiration());          //
   if(!Modify)                                                                      //
      Print("Something went wrong with Modify while Buying. Error= ",GetLastError());      //

  }
 
Marco vd Heijden:

OrderSelect returns either true or false depending on the outcome of the action namely to select the order.

It will not return 130.

OrderModify can return 130 which means INVALID_STOPS

So the logical action for you would be to analyze 

sl and tp values.

So can you post the log of 

I know OrderSelect shouldn't be returning a 130 error as I doesn't make sense at all, but that's the message I was getting:

2020.04.03 13:13:34.500 TW EA 1.3 XAUUSD,M1: Something went wrong with Order Select while Buying. Error= 130


I also don't get any of those errors on the StrategyTester, but only when I run the EA on a live chart. So I will get back to you with the log from the live chart whenever the error comes up again.

 

Because you are printing GetLastError() which prints the last known error.

It's your own mistake to think that it came from OrderSelect while it actually comes form OrderModify return value.

 

The error returned on OrderModify is caused by the NormalizePrice function not Normalizing correctly.

2020.04.03 13:19:37.140 TW EA 1.3 XAUUSD,M1: SL: 1612.77000000 TP: 0

double NormalizePrice(double p,string pair="")
  {

   if(pair=="")
      pair=Symbol();
   double ts=MarketInfo(pair,MODE_TICKSIZE);
   return(MathRound(p/ts) * ts);
  }
 
Naim El Hajj:

The error returned on OrderModify is caused by the NormalizePrice function not Normalizing correctly.

2020.04.03 13:19:37.140 TW EA 1.3 XAUUSD,M1: SL: 1612.77000000 TP: 0

maybe digit need to adjust 1612.77000000

example :

double TargetPrice=NormalizeDouble(yoursourcedata,Digits);

 
   int ticket=OrderSend(_Symbol,OP_SELLSTOP,LotSize,price,5,0,0,comment,MagN);                 //
   if(!ticket) //
Perhaps you should read the manual. A ticket number is not a boolean,
 

As @William Roeder correctly advised, you should check ticket against (-1):

Returns number of the ticket assigned to the order by the trade server or -1 if it fails

The condition

if(!ticket) //

checks ticket against (0).

 

The 130 error is showing again on a different broker.

Here's the current code and the log. I can't tell what the problem is, especially that sl and tp are 0.

void SellStop(double price,int MagN,double LotSize,datetime expiration=0,double sl=0,double tp=0,string comment=NULL)
  {

   double stoplevel=MarketInfo(_Symbol,MODE_STOPLEVEL)*_Point;
   price=NormalizePrice(MathMin(price,Bid-stoplevel));
Print("price: "+DoubleToString(price)+" Bid: "+Bid);
//===============================|| SELL ||======================================//
//------------------- Open Sell Order ---------------------------------------------
   RefreshRates();                                                                  //
   int ticket=OrderSend(_Symbol,OP_SELLSTOP,LotSize,price,5,0,0,comment,MagN);                 //
   if(ticket==-1) //
      Print("Something went wrong with SellStop Order. Error= ",GetLastError());        //
   else
      Print("sellstop: "+ticket);
   bool Select = OrderSelect(ticket,SELECT_BY_TICKET);                              //
   if(!Select)                                                                      //
      Print("Something went wrong with Order Select while Selling. Error= ",GetLastError());      //
//------------------- Set Stop Loss -----------------------------------------------
//-------------- Modify Order with Stop Loss --------------------------------------
   RefreshRates();
   if(sl<=OrderOpenPrice()+stoplevel && sl!=0)
      sl=OrderOpenPrice()+stoplevel;
   if(tp>=OrderOpenPrice()-stoplevel && tp!=0)
      tp=OrderOpenPrice()-stoplevel;

   Print("ticket: "+DoubleToString(OrderTicket())+"open: "+DoubleToString(OrderOpenPrice())+"SL: "+DoubleToString(sl)+" TP: "+(string)tp);

   bool Modify = OrderModify(OrderTicket(),OrderOpenPrice(),sl,0,0);          //
   if(!Modify)                                                                      //
      Print("Something went wrong with Modify while Selling. Error= ",GetLastError());      //

  }


2020.04.09 14:44:57.364 TW EA 1.4 XAUUSD,M1: price: 1662.42000000 Bid: 1662.62

2020.04.09 14:44:57.783 TW EA 1.4 XAUUSD,M1: Something went wrong with SellStop Order. Error= 130


 
   price=NormalizePrice(MathMin(price,Bid-stoplevel));
   int ticket=OrderSend(_Symbol,OP_SELLSTOP,LotSize,price,5,0,0,comment,MagN);         
  1. You can't move stops (or pending prices) closer to the market than the minimum: MODE_STOPLEVEL * _Point or SymbolInfoInteger(SYMBOL_TRADE_STOPS_LEVEL).
              Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial

    On ECN brokers you get the value zero. The broker doesn't know. I'd use 2 pips minimum.

  2. You used NormalizeDouble, It's use is usually wrong, as it is in your case.
    1. Floating point has infinite number of decimals, it's your not understanding floating point and that some numbers can't be represented exactly. (like 1/10.)
                Double-precision floating-point format - Wikipedia, the free encyclopedia

      See also The == operand. - MQL4 programming forum

    2. Print out your values to the precision you want with DoubleToString - Conversion Functions - MQL4 Reference.

    3. SL/TP (stops) need to be normalized to tick size (not Point) — code fails on metals. (On 5Digit Broker Stops are only allowed to be placed on full pip values. How to find out in mql? - MQL4 programming forum) and abide by the limits Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial and that requires understanding floating point equality Can price != price ? - MQL4 programming forum

    4. Open price for pending orders need to be adjusted. On Currencies, Point == TickSize, so you will get the same answer, but it won't work on Metals. So do it right: Trailing Bar Entry EA - MQL4 programming forum or Bid/Ask: (No Need) to use NormalizeDouble in OrderSend - MQL4 programming forum

    5. Lot size must also be adjusted to a multiple of LotStep and check against min and max. If that is not a power of 1/10 then NormalizeDouble is wrong. Do it right.

    6. MathRound() and NormalizeDouble() are rounding in a different way. Make it explicit.
                MT4:NormalizeDouble - MQL5 programming forum
                How to Normalize - Expert Advisors and Automated Trading - MQL5 programming forum

    7. Prices you get from the terminal are already normalized.

 
William Roeder:
  1. You can't move stops (or pending prices) closer to the market than the minimum: MODE_STOPLEVEL * _Point or SymbolInfoInteger(SYMBOL_TRADE_STOPS_LEVEL).
              Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial

    On ECN brokers you get the value zero. The broker doesn't know. I'd use 2 pips minimum.

  2. You used NormalizeDouble, It's use is usually wrong, as it is in your case.
    1. Floating point has infinite number of decimals, it's your not understanding floating point and that some numbers can't be represented exactly. (like 1/10.)
                Double-precision floating-point format - Wikipedia, the free encyclopedia

      See also The == operand. - MQL4 programming forum

    2. Print out your values to the precision you want with DoubleToString - Conversion Functions - MQL4 Reference.

    3. SL/TP (stops) need to be normalized to tick size (not Point) — code fails on metals. (On 5Digit Broker Stops are only allowed to be placed on full pip values. How to find out in mql? - MQL4 programming forum) and abide by the limits Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial and that requires understanding floating point equality Can price != price ? - MQL4 programming forum

    4. Open price for pending orders need to be adjusted. On Currencies, Point == TickSize, so you will get the same answer, but it won't work on Metals. So do it right: Trailing Bar Entry EA - MQL4 programming forum or Bid/Ask: (No Need) to use NormalizeDouble in OrderSend - MQL4 programming forum

    5. Lot size must also be adjusted to a multiple of LotStep and check against min and max. If that is not a power of 1/10 then NormalizeDouble is wrong. Do it right.

    6. MathRound() and NormalizeDouble() are rounding in a different way. Make it explicit.
                MT4:NormalizeDouble - MQL5 programming forum
                How to Normalize - Expert Advisors and Automated Trading - MQL5 programming forum

    7. Prices you get from the terminal are already normalized.

Thanks for the references. I thought maybe the error was due the ECN broker stoplevel, so I adjusted it to be a minimum of 20 points.

I also made some more adjustments according to all the points you mentioned, like adding a final check before the OrderSend function:

if((Bid-price)-stoplevel>-_Point/2)

But I just got the error again! 

2020.04.09 20:26:17.384 TW EA 1.4 XAUUSD,M1: price: 1688.13000000 Bid: 1688.33000000

2020.04.09 20:26:17.868 TW EA 1.4 XAUUSD,M1: Something went wrong with SellStop Order. Error= 130


I'm sure there's something I'm missing but I really can't find it. Here's my updated code:

void SellStop(double price,int MagN,double LotSize,datetime expiration=0,double sl=0,double tp=0,string comment=NULL)
  {
   RefreshRates();                                                                  //

   double stoplevel=MathMax(MarketInfo(_Symbol,MODE_STOPLEVEL)*_Point,20*_Point);

   price=NormalizePrice(MathMin(price,Bid-stoplevel));
   Print("price: "+DoubleToString(price)+" Bid: "+DoubleToString(Bid));

//===============================|| SELL ||======================================//
//------------------- Open Sell Order ---------------------------------------------
   if((Bid-price)-stoplevel>-_Point/2)
     {
      int ticket=OrderSend(_Symbol,OP_SELLSTOP,LotSize,price,5,0,0,comment,MagN);                 //
      if(ticket==-1) //
         Print("Something went wrong with SellStop Order. Error= ",GetLastError());        //
      else
         Print("sellstop: "+ticket);
      bool Select = OrderSelect(ticket,SELECT_BY_TICKET);                              //
      if(!Select)                                                                      //
         Print("Something went wrong with Order Select while Selling. Error= ",GetLastError());      //
      //------------------- Set Stop Loss -----------------------------------------------
      //-------------- Modify Order with Stop Loss --------------------------------------

      if((OrderOpenPrice()+stoplevel)-sl>-_Point/2 && sl!=0)
         sl=OrderOpenPrice()+stoplevel;
      if(tp-(OrderOpenPrice()-stoplevel)>-_Point/2 && tp!=0)
         tp=OrderOpenPrice()-stoplevel;

      Print("ticket: "+DoubleToString(OrderTicket())+"open: "+DoubleToString(OrderOpenPrice())+"SL: "+DoubleToString(sl)+" TP: "+(string)tp);

      bool Modify = OrderModify(OrderTicket(),OrderOpenPrice(),sl,0,0);          //
      if(!Modify)                                                                      //
         Print("Something went wrong with Modify while Selling. Error= ",GetLastError());      //
     }
  }

I'm feeling it's only a matter of broker stop level, is that it?

Reason: