Download MetaTrader 5

Unexplainable Error 130 on Modify Order · · · – – – · · ·

To add comments, please log in or register
Having troubles understanding the code? Articles can help you!
Duarte Silva
127
Duarte Silva 2016.08.23 08:40 

Hi,

In my EA, when I try to change pending orders it generates Error 130 a lot of times... I know that its because the SL levels but I don't understand how before it worked for the same order and now doesn't work!

I have seen https://book.mql4.com/appendix/limits, changed to code and the result is the same! Testing the 2006 in EURUSD, for an OP_BUYSTOP modify order with Price: 1.2125 SL: 1.1795 TP: 1.278499999999999 it was generated "modify #2 buy stop 0.10 EURUSD at 1.21250 sl: 1.17950 tp: 1.27850 ok" and now it gives "OrderModify error 130"

My code to modify function is:

void ModifyOrders(string sTypeOrder)
  {
   int CountOrders = 0, IndexSave = 0, OrderTypeNum = 0;
 
   if(sTypeOrder == "OP_BUY") OrderTypeNum = 0;
   else if(sTypeOrder == "OP_SELL") OrderTypeNum = 1;
   else if(sTypeOrder == "OP_BUYLIMIT") OrderTypeNum = 2;
   else if(sTypeOrder == "OP_SELLLIMIT")  OrderTypeNum = 3;  
   else if(sTypeOrder == "OP_BUYSTOP") OrderTypeNum = 4;
   else if(sTypeOrder == "OP_SELLSTOP")  OrderTypeNum = 5;  
   else OrderTypeNum = -1;
   
   for(PositionIndex = OrdersTotal()-1; PositionIndex >= 0; PositionIndex --) //  <-- for loop to loop through all Orders . .   COUNT DOWN TO ZERO !
     {
      if(!OrderSelect(PositionIndex,SELECT_BY_POS,MODE_TRADES)) continue;   // <-- if the OrderSelect fails advance the loop to the next PositionIndex
         {
         if(OrderMagicNumber() == iMagicNum && OrderSymbol() == Symbol() && OrderType() == OrderTypeNum)
            {
            if (sTypeOrder == "OP_BUY" || sTypeOrder == "OP_SELL")   // No caso das ordens abertas apenas alteramos o stop loss vindo do main
               {
               PriceValue = 0;
               TakeProfitValue = OrderTakeProfit();
               }
            if (sTypeOrder == "OP_BUYSTOP")        
               {
               if ((PriceValue - Ask >= MarketInfo(Symbol(),MODE_STOPLEVEL)*Point())
                  && (PriceValue - StopLossValue >= MarketInfo(Symbol(),MODE_STOPLEVEL)*Point())
                  && (TakeProfitValue - PriceValue >= MarketInfo(Symbol(),MODE_STOPLEVEL)*Point())) Print("Valores dentro dos limites do Stop Level.");
               else Print ("Valores da ordem necessitam de ser melhorados para estarem dentro do Stop Level: ", MarketInfo(Symbol(),MODE_STOPLEVEL)*Point());
               }
            
            Print("Dados para modificação de ordens: Price: ", PriceValue, " SL: ", StopLossValue, " TP: ", TakeProfitValue);
            if(OrderModify(ticket,PriceValue,StopLossValue,TakeProfitValue,0,Green))
              {
               Print("Ordem de modificação bem sucedida.");
              }
            else Print("Erro: Ordem de modificação mal executada!!",GetLastError());
            CountOrders = CountOrders + 1;
             }
         }
     }
  }

I need help to understand this... I'm stuck with this for a few days!!

Thanks,

Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial
Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial
  • book.mql4.com
Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial
Marco vd Heijden
Moderator
4685
Marco vd Heijden 2016.08.23 09:38  
Duarte Silva:

Hi,

In my EA, when I try to change pending orders it generates Error 130 a lot of times... I know that its because the SL levels but I don't understand how before it worked for the same order and now doesn't work!

I have seen https://book.mql4.com/appendix/limits, changed to code and the result is the same! Testing the 2006 in EURUSD, for an OP_BUYSTOP modify order with Price: 1.2125 SL: 1.1795 TP: 1.278499999999999 it was generated "modify #2 buy stop 0.10 EURUSD at 1.21250 sl: 1.17950 tp: 1.27850 ok" and now it gives "OrderModify error 130"

My code to modify function is:

I need help to understand this... I'm stuck with this for a few days!!

Thanks,

void ModifyOrders(string sTypeOrder)
  {
   int CountOrders = 0, IndexSave = 0, OrderTypeNum = 0;
 
   if(sTypeOrder == "OP_BUY") OrderTypeNum = 0;
   else if(sTypeOrder == "OP_SELL") OrderTypeNum = 1;
   else if(sTypeOrder == "OP_BUYLIMIT") OrderTypeNum = 2;
   else if(sTypeOrder == "OP_SELLLIMIT")  OrderTypeNum = 3;  
   else if(sTypeOrder == "OP_BUYSTOP") OrderTypeNum = 4;
   else if(sTypeOrder == "OP_SELLSTOP")  OrderTypeNum = 5;  
   else OrderTypeNum = -1;
   
   for(PositionIndex = OrdersTotal()-1; PositionIndex >= 0; PositionIndex --) //  <-- for loop to loop through all Orders . .   COUNT DOWN TO ZERO !
     {
      if(!OrderSelect(PositionIndex,SELECT_BY_POS,MODE_TRADES)) continue;   // <-- if the OrderSelect fails advance the loop to the next PositionIndex
         {
         if(OrderMagicNumber() == iMagicNum && OrderSymbol() == Symbol() && OrderType() == OrderTypeNum)
            {
            if (sTypeOrder == "OP_BUY" || sTypeOrder == "OP_SELL")   // No caso das ordens abertas apenas alteramos o stop loss vindo do main
               {
               PriceValue = 0;
               TakeProfitValue = OrderTakeProfit();
               }
            if (sTypeOrder == "OP_BUYSTOP")        
               {
               if ((PriceValue - Ask >= MarketInfo(Symbol(),MODE_STOPLEVEL)*Point())
                  && (PriceValue - StopLossValue >= MarketInfo(Symbol(),MODE_STOPLEVEL)*Point())
                  && (TakeProfitValue - PriceValue >= MarketInfo(Symbol(),MODE_STOPLEVEL)*Point())) Print("Valores dentro dos limites do Stop Level.");
               else Print ("Valores da ordem necessitam de ser melhorados para estarem dentro do Stop Level: ", MarketInfo(Symbol(),MODE_STOPLEVEL)*Point());
               }
            
            Print("Dados para modificação de ordens: Price: ", PriceValue, " SL: ", StopLossValue, " TP: ", TakeProfitValue);
            if(OrderModify(ticket,PriceValue,StopLossValue,TakeProfitValue,0,Green))
              {
               Print("Ordem de modificação bem sucedida.");
              }
            else Print("Erro: Ordem de modificação mal executada!!",GetLastError());
            CountOrders = CountOrders + 1;
             }
         }
     }
  }
You call Ordermodify() function for every order regardless of it's type.
Duarte Silva
127
Duarte Silva 2016.08.24 08:31  
Marco vd Heijden:
You call Ordermodify() function for every order regardless of it's type.

Hi Marco,

I have tried or suggestion just to see if it worked but it stil gives Error 130... The code that is scratched its for modify only the SL for open order and to check if stoplevels are being correctly done. And they are!

 

I don't understand how it gives Error 130 for the same order and values when it worked before! Any help? Suggestion? 

Duarte Silva
127
Duarte Silva 2016.08.24 09:18  

I have seen this links and i'm tring to outwit the problem:

https://www.forexboat.com/ordersend-error-130/

https://book.mql4.com/appendix/limits

That why the code has those lines... 

OrderSend Error 130 in MetaTrader 4 - Causes & Solutions
OrderSend Error 130 in MetaTrader 4 - Causes & Solutions
  • 2015.01.19
  • Kirill Eremenko
  • www.forexboat.com
Where does this error come from? What does it mean for your Expert Advisor? How can you find the part of your code that is causing the error? We tackle all this and more… That’s right! That is all you get from MetaQuotes. And the rest… Go figure! is briefly mentioned in other sections of the documentation. However, there is no thorough guide...
Ernst Van Der Merwe
4727
Ernst Van Der Merwe 2016.08.24 09:29  

It's probably a brace problem.

if (sTypeOrder == "OP_BUYSTOP")        
   {
   if ((PriceValue - Ask >= MarketInfo(Symbol(),MODE_STOPLEVEL)*Point())
      && (PriceValue - StopLossValue >= MarketInfo(Symbol(),MODE_STOPLEVEL)*Point())
      && (TakeProfitValue - PriceValue >= MarketInfo(Symbol(),MODE_STOPLEVEL)*Point())) Print("Valores dentro dos limites do Stop Level.");
   else Print ("Valores da ordem necessitam de ser melhorados para estarem dentro do Stop Level: ", MarketInfo(Symbol(),MODE_STOPLEVEL)*Point());
   //} remove brace
   Print("Dados para modificação de ordens: Price: ", PriceValue, " SL: ", StopLossValue, " TP: ", TakeProfitValue);
   if(OrderModify(ticket,PriceValue,StopLossValue,TakeProfitValue,0,Green))
     {
      Print("Ordem de modificação bem sucedida.");
     }
   else Print("Erro: Ordem de modificação mal executada!!",GetLastError());
   CountOrders = CountOrders + 1;
   }
Duarte Silva
127
Duarte Silva 2016.08.25 07:48  
Ernst Van Der Merwe:

It's probably a brace problem.

Hi Ernst,

 I don't think so... If it was a brace problem the compiler would give error.

The stop levels are in the boundaries and I have used the NormalizedDouble function to oblied value to have the same digits of the forex pair. And the result is the same!!!

 

Any suggestion? 

Marco vd Heijden
Moderator
4685
Marco vd Heijden 2016.08.25 08:22  
Duarte Silva:

Hi Ernst,

 I don't think so... If it was a brace problem the compiler would give error.

The stop levels are in the boundaries and I have used the NormalizedDouble function to oblied value to have the same digits of the forex pair. And the result is the same!!!

 

Any suggestion? 


Duarte Silva:


 but i woul d like to use the string to be more explicit in the code when calling for modifyOrders Function.


Rewrite the whole thing the way it should be.

Ernst Van Der Merwe
4727
Ernst Van Der Merwe 2016.08.25 09:51  
Duarte Silva:

Hi Ernst,

 I don't think so... If it was a brace problem the compiler would give error.

The stop levels are in the boundaries and I have used the NormalizedDouble function to oblied value to have the same digits of the forex pair. And the result is the same!!!

 

Any suggestion? 

I meant that the orders should be dealt with individually. Buy orders would have different stoploss from sell orders, buystop can't be too close to price etc. We don't know what those values are, because they're not included. It may be more useful if you calculate them and then pass with order type.

void ModifyOrders(string sTypeOrder,double price,double stoploss,double takeprofit)
  {
   int CountOrders = 0, IndexSave = 0, OrderTypeNum = 0;
 
   if(sTypeOrder == "OP_BUY") OrderTypeNum = 0;
   else if(sTypeOrder == "OP_SELL") OrderTypeNum = 1;
   else if(sTypeOrder == "OP_BUYLIMIT") OrderTypeNum = 2;
   else if(sTypeOrder == "OP_SELLLIMIT")  OrderTypeNum = 3;  
   else if(sTypeOrder == "OP_BUYSTOP") OrderTypeNum = 4;
   else if(sTypeOrder == "OP_SELLSTOP")  OrderTypeNum = 5;  
   else OrderTypeNum = -1;
   
   int total=OrdersTotal()-1;
   for(PositionIndex = total;  PositionIndex >= 0; PositionIndex --) //  <-- for loop to loop through all Orders . .   COUNT DOWN TO ZERO !
     {
      if(!OrderSelect(PositionIndex,SELECT_BY_POS,MODE_TRADES)) continue;   // <-- if the OrderSelect fails advance the loop to the next PositionIndex
         {
         if(OrderMagicNumber() == iMagicNum && OrderSymbol() == Symbol() && OrderType() == OrderTypeNum)
            {
            int ticket=OrderTicket();
            Print("Dados para modificação de ordens: Price: ", price, " SL: ", stoploss, " TP: ", takeprofit);
            if(OrderModify(ticket,price,stoploss,takeprofit,0,Green))
              {
               Print("Ordem de modificação bem sucedida.");
              }
            else Print("Erro: Ordem de modificação mal executada!!",GetLastError());
            CountOrders = CountOrders + 1;
             }
         }
     }
  }
Duarte Silva
127
Duarte Silva 2016.08.25 09:59  
Marco vd Heijden:


Rewrite the whole thing the way it should be.

Hi Marco,

Let me try to explain better: When I call my ModifyOrders functions, I have to specify the Order Type. Example:

if (Fractal_Method == 1)
  {
  PriceValue = LastMinLowFractal - PriceMargin;
  StopLossValue = LastHighUpFractal + SLMargin;
  TakeProfitValue = PriceValue - 2 * (StopLossValue - PriceValue);                           
  ModifyOrders("OP_SELLSTOP");
  }

I use string to specify the Ordertype, because I think its more intuitive,  but I could use integers like your suggestion in other forum topic. This isn't the problem...

Once the function ModifyOrders is called, it makes the search of the desire OrderType to change it. The search and order slection it works fine but the ModifyOrder instrution doesn't execute and gives error 130. On previous tests that I made the order was accepted with the same value: Price Value, SL and TP.

Since this error I have been trying to understand what is the error:

- Checking the stoplevels of pending orders:

 MarketStopLevel = MarketInfo(Symbol(),MODE_STOPLEVEL)*Point();

if (OrderType() == OP_BUYSTOP)
  {
  if ((PriceValue - Ask >= MarketStopLevel) && (PriceValue - StopLossValue >= MarketStopLevel) && (TakeProfitValue - PriceValue >= MarketStopLevel)) Print("Values  OK");
  else Print ("Values NOT OK");
  }

- Normalized values:

if(OrderModify(ticket,NormalizeDouble(PriceValue, Digits),NormalizeDouble(StopLossValue, Digits),NormalizeDouble(TakeProfitValue, Digits),0,Green))
  {
  Print("Order modification well done.");
  }
else Print("Erro: Order modification!!",GetLastError());

I have read in the link above that error 130 can have several causes and I trying to find out... 

 

 After some code changes the problems remains the same... I send in attachment the mql4 file.

 

I hope that i have explained better. Feel free to ask... 

Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial
Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial
  • book.mql4.com
Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial
Marco vd Heijden
Moderator
4685
Marco vd Heijden 2016.08.25 10:02  
Duarte Silva:

Hi Marco,

Let me try to explain better: When I call my ModifyOrders functions, I have to specify the Order Type. Example:

I use string to specify the Ordertype, because I think its more intuitive,  but I could use integers like your suggestion in other forum topic. This isn't the problem...

Once the function ModifyOrders is called, it makes the search of the desire OrderType to change it. The search and order slection it works fine but the ModifyOrder instrution doesn't execute and gives error 130. On previous tests that I made the order was accepted with the same value: Price Value, SL and TP.

Since this error I have been trying to understand what is the error:

- Checking the stoplevels of pending orders:

 MarketStopLevel = MarketInfo(Symbol(),MODE_STOPLEVEL)*Point();

- Normalized values:

I have read in the link above that error 130 can have several causes and I trying to find out... 

 

 After some code changes the problems remains the same... I send in attachment the mql4 file.

 

I hope that i have explained better. Feel free to ask... 

You don't have to explain me anything.

That error is well known, your TP/SL values are rejected by the server.

130 ERR_INVALID_STOPS

It means you are trying to modify to wrong values or on the wrong order.

You code is wrong on so many levels.

         if(OrderMagicNumber() == iMagicNum && OrderSymbol() == Symbol() && OrderType() == OrderTypeNum)
            {
            int ticket=OrderTicket();
            Print("Dados para modificação de ordens: Price: ", price, " SL: ", stoploss, " TP: ", takeprofit);
            if(OrderModify(ticket,price,stoploss,takeprofit,0,Green))
              {
               Print("Ordem de modificação bem sucedida.");
              }


Here you assign the same TP/SL values to ANY (ALL) OP_BUY orders in the loop (assuming OrderTypeNum==OP_BUY)

It's just wrong.

And Ernst is right, your not showing how these values are calculated that makes it wrong, and incomplete.

Duarte Silva
127
Duarte Silva 2016.08.25 21:32  
Marco vd Heijden:

You don't have to explain me anything.

That error is well known, your TP/SL values are rejected by the server.

130 ERR_INVALID_STOPS

It means you are trying to modify to wrong values or on the wrong order.

You code is wrong on so many levels.


Here you assign the same TP/SL values to ANY (ALL) OP_BUY orders in the loop (assuming OrderTypeNum==OP_BUY)

It's just wrong.

And Ernst is right, your not showing how these values are calculated that makes it wrong, and incomplete.

Hi Marco,

I didn't saw the 2nd comment of Ernst and I miss understood!Sorry... Let's see:

Before making any modify order, I check if SL and TP are outside stoplevels: https://book.mql4.com/appendix/limits. This should be enough, yes?

The strange this is that the same order, in previous tests, it worked and now it doesn't work! See this logs:

 Previous Log

 0 08:14:17 2006.03.09 00:00  teste3_Fractal_v19-08-2016 EURUSD,Daily: *** Função Modificação de ordens ***: Order Number = 4 TypeOrder: OP_BUYSTOP

0 08:14:17 2006.03.09 00:00  teste3_Fractal_v19-08-2016 EURUSD,Daily: Data for order modify: Price: 1.2125 SL: 1.1795 TP: 1.278499999999999

2 08:14:17 2006.03.09 00:00  teste3_Fractal_v19-08-2016 EURUSD,Daily: modify #2 buy stop 0.10 EURUSD at 1.21250 sl: 1.17950 tp: 1.27850 ok


Last log

0 07:22:19 2006.03.09 00:00  teste3_Fractal_v20-08-2016 EURUSD,Daily: *** Função Modificação de ordens ***: Order Number = 4 TypeOrder: OP_BUYSTOP

0 07:22:19 2006.03.09 00:00  teste3_Fractal_v20-08-2016 EURUSD,Daily: Data for order modify: Price: 1.2125 SL: 1.1795 TP: 1.278499999999999

3 07:22:19 2006.03.09 00:00  teste3_Fractal_v20-08-2016 EURUSD,Daily: OrderModify error 130

This is the reason that I why I made this topic. I understand that error is well known and don't like to make unnecessary topics without search for the solution!

 

About Ernst question: I make the definition in "OnTick" function after confirmation of upper or lower fractal. In the EA, PriceValue, SL and TP are global variables that are defined each time a fractal appears.

The diference is that for open orders I keep the profit value and change the SL... Lets see:

void OnTick()

....

   if((FractalLowerFound) && (Bars>=LowerBarIndex + 2))
   {....
      if((CountOrders("OP_SELLSTOP") == 1 && Close[Control_Bars] < LongEMA) || (CountOrders("OP_SELL") == 1))
         {
         PriceValue = LastMinLowFractal - PriceMargin;
         StopLossValue = LastHighUpFractal + SLMargin;
         TakeProfitValue = PriceValue - 2 * (StopLossValue - PriceValue);                  
         if (CountOrders("OP_SELLSTOP") == 1) ModifyOrders("OP_SELLSTOP");
         if (CountOrders("OP_SELL") == 1) ModifyOrders("OP_SELL");
         }
                
         if (CountOrders("OP_BUY") ==1)
           {
           StopLossValue = LastMinLowFractal;
           ModifyOrders("OP_BUY");
           }
         .....

                  

 

 Marco, I am developping this EA and I trying to set a trading system. You are right: I assign the same SL to all OP_BUY orders because I't understanding the best way. But I will apply one condition to make only this when it has a trend. Thanks.

 If you can tell what points can I improve on the code, I appreciate: syntax, structure, anything. I'm serious... I want to learn.

 Thanks for you attention! 

Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial
Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial
  • book.mql4.com
Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial
123
To add comments, please log in or register