Error 130 on pending orders

 

Hi,

Could you please help with the issue I am facing:

Basically when I open a buy stop order without stop loss and take profit I got the error number 130 (ERR_INVALID_STOPS) .

On my code I have checked the limitations as defined on this page:https://book.mql4.com/appendix/limits

So on the price order I have check the stop level and freeze level limitation. You will find below my code openind the order. I got the error 130 when I call the function as following:

ticketBuy=OrderOpenBuyStopOrder(1,Ask+(200*Point),MagicNumber);


int OrderOpenBuyStopOrder(
               double     OO_volume,
               double     OO_price,
               int        OO_magic)
  {
   int      result      = -1;    //result of opening an order
   int      Error       = 0;     //error when opening an order
   int      attempt     = 0;     //amount of performed attempts
   int      attemptMax  = 3;     //maximum amount of attempts
   bool     exit_loop   = false; //exit the loop

   double stoplevel       = MarketInfo(Symbol(),MODE_STOPLEVEL)*Point;  //minimum stop loss/ take profit level, in points or pending order
   double freezelevel     = MarketInfo(Symbol(),MODE_FREEZELEVEL)*Point;
                                                                                                                      //the module provides safe order opening. 
//--- check stop orders for buy stop

   double min_price=Ask-stoplevel; //limitation with stop level
   
   double checked_price=OO_price;
   checked_price=MathMax(min_price,checked_price);
   min_price=Ask-freezelevel; //limitation with freeze level
   checked_price=NormalizeDouble(MathMax(min_price,checked_price),Digits);

//--- while loop
   while(!exit_loop)
     {
      result=OrderSend(Symbol(),OP_BUYSTOP,OO_volume,checked_price,3,0,0,"test",OO_magic,0,clrWhite); //attempt to open an order using the specified parameters
      //--- if there is an error when opening an order
      if(result<0)
        {
         Error = GetLastError();                                     //assign a code to an error
         switch(Error)                                               //error enumeration
           {                                                         //order closing error enumeration and an attempt to fix them
            case  2:
               if(attempt<attemptMax)
                 {
                  attempt=attempt+1;                                 //define one more attempt
                  Sleep(3000);                                       //3 seconds of delay
                  RefreshRates();
                  break;                                             //exit switch
                 }
               if(attempt==attemptMax)
                 {
                  attempt=0;                                         //reset the amount of attempts to zero 
                  exit_loop = true;                                  //exit while
                  break;                                             //exit switch
                 }
            case  3:
               RefreshRates();
               exit_loop = true;                                     //exit while
               break;                                                //exit switch   
            case  4:
               if(attempt<attemptMax)
                 {
                  attempt=attempt+1;                                 //define one more attempt
                  Sleep(3000);                                       //3 seconds of delay
                  RefreshRates();
                  break;                                             //exit switch
                 }
               if(attempt==attemptMax)
                 {
                  attempt = 0;                                       //reset the amount of attempts to zero 
                  exit_loop = true;                                  //exit while
                  break;                                             //exit switch
                 }
            case 130:
               exit_loop=true;                                       //exit while
               break;
           }
        }
      //--- if no errors detected
      else
        {
         Print("The order is successfully opened.", result);
         Error = 0;                                //reset the error code to zero
         break;                                    //exit while
         //errorCount =0;                          //reset the amount of attempts to zero
        }
     }
   return(result);
  }
//+------------------------------------------------------------------+


Thanks for your help.


Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial
Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial
  • book.mql4.com
Tables below show calculation values that limit the conduction of trades when opening, closing, placing, deleting or modifying orders. To get the minimum distance to StopLevel and freezing distance FreezeLevel the MarketInfo() function should be called. Requirements. Correct prices used when performing trade operations. Order Type Open Price...