Error 130

 

Hi everyone,

I am a newbie to MQL. I'm trying a testing EA. I am getting Error#130. Couldn't find the reason. Pls find the code below and copy of the journal below.

double Buy_Signal = 0;
double Sell_Signal = 0;
int i = 0;
int Total_Orders = 0;
int Magic_Check = 0;
double Lot_Check = 1;

         int ticket;
         int Order_Type;             // operation
         double Lot_Size;// = 0.1;   // volume
         double Trade_Price = 0;     // price
         double SL = 0;              // stop loss
         double Take_Profit = 0;     // take profit
         double Trade_Gap = 0.001;   // profit gap
         int Magic_Num = 1;              // Magic Number
         string Trade_Comment = NULL;// comment
         color Trade_Colour = NULL;  // color

      double PRI;
      double SSL;
      double TGT;
      double PRI2;
      double SSL2;
      double TGT2;
      double dLot_Size = 0.1;
      
      string sComment = "";
      string NL = "\n";
      int Highest_Open_Trades = 0;
      double Max_Lot_Size = 0;
      int Total_Open_Trades = 0;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   //---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//--------------------------------First Trade----------------------////
   if(OrdersTotal()==0)
   {
        Lot_Size = dLot_Size;// volume
        Buy_Signal = iCustom(NULL,15,"BFS_Stoc",0,1);
        Sell_Signal = iCustom(NULL,15,"BFS_Stoc",1,1);
        Magic_Num++;
                    
      if(Buy_Signal>0 && Sell_Signal<=0)// && Buy_Signal==Stoc_Buy_Signal)
      {
         Order_Type = OP_BUY;          // operation
         Lot_Size = 0.1;//dLot_Size;
         Trade_Price = Ask;            // price
         Take_Profit = Ask + Trade_Gap; // take profit
         SL = Ask - 2 * Trade_Gap;     // stop loss
         Trade_Comment = "First_Open_Order";  // comment
         Trade_Colour = clrBlue;       // color
         ticket = Send_Order();
         
         if(ticket>=0)
         {
            PRI = Trade_Price;//OrderOpenPrice();
            SSL = SL;//OrderStopLoss();
            TGT = Take_Profit;//OrderTakeProfit();
            Magic_Check = Magic_Num;//OrderMagicNumber();
         
         Order_Type = OP_SELLLIMIT;
         Lot_Size = 0.4;
         Trade_Price = PRI - Trade_Gap;
         Take_Profit = SSL;
         SL = TGT;
         Trade_Comment = "Second_Limit_Order";
         Trade_Colour = clrBlue;       
         ticket = Send_Order();//---Second Order----///
         
         }
      }
      else if(Buy_Signal<=0 && Sell_Signal>0)
      {
         Order_Type = OP_SELL;          // operation
         Lot_Size = 0.1;// dLot_Size;
         Trade_Price = Bid;            // price
         Take_Profit = Bid - Trade_Gap; // Take Profit
         SL = Bid + 2 * Trade_Gap;     // stop loss
         Trade_Comment = "First_Open_Order";  // comment
         Trade_Colour = clrBlue;       // color
         ticket = Send_Order();
         if(ticket>=0)
         {
         //////////Backup//////////////////////////////
            PRI = Trade_Price;//OrderOpenPrice();
            SSL = SL;//OrderStopLoss();
            TGT = Take_Profit;//OrderTakeProfit();
            Magic_Check = Magic_Num;//OrderMagicNumber();
         /////////////////////////////////////////////
         Order_Type = OP_BUYSTOP;    
         Lot_Size = 0.4;
         Trade_Price = PRI + Trade_Gap;
         Take_Profit = SSL;
         SL = TGT;
         Trade_Comment = "Second_Limit_Order";
         Trade_Colour = clrBlue;  
         ticket = Send_Order(); //---Second Order----///
         }
      }
   }


//-------------End of OnTick----------------------------////      
  }
//+------------------------------------------------------------------+
int Send_Order()
{
ticket = OrderSend(
         Symbol(),               // symbol
         Order_Type,             // operation
         Lot_Size,               // volume
         Trade_Price,            // price
         3,                      // slippage
         SL,                     // stop loss
         Take_Profit,            // take profit
         Trade_Comment,          // comment
         Magic_Num,                      // magic number
         0,                      // pending order expiration
         Trade_Colour            // color
   );
   
   if(ticket<0)
      {
      Print("Order Failed with ",GetLastError());
      Print("OType:",Order_Type,"; LotSize:",Lot_Size,"; LotCheck:",Lot_Check,"; Price:",Trade_Price,
      "; SL:",SL,"; TP:",Take_Profit,"; Magic:",Magic_Num,"; CMP:",Ask);
      }
   else
      {
      Print(Trade_Comment," Order Placed Successfully");
      }
      
return ticket;
}


2016.09.07 15:01:01.234 2010.01.03 23:00  TestOrders EURUSD,H1: OType:3; LotSize:0.4; LotCheck:1.0; Price:1.43183; SL:1.43383; TP:1.43083; Magic:3; CMP:1.43283
2016.09.07 15:01:01.234 2010.01.03 23:00  TestOrders EURUSD,H1: Order Failed with 130
2016.09.07 15:01:01.234 2010.01.03 23:00  TestOrders EURUSD,H1: OrderSend error 130
2016.09.07 15:01:01.234 2010.01.03 23:00  TestOrders EURUSD,H1: First_Open_Order Order Placed Successfully
2016.09.07 15:01:01.234 2010.01.03 23:00  TestOrders EURUSD,H1: open #1 buy 0.10 EURUSD at 1.43283 sl: 1.43083 tp: 1.43383 ok
2016.09.07 15:01:01.215 2010.01.03 23:00  Custom indicator BFS_Stoc EURUSD,M15: loaded successfully
2016.09.07 15:01:01.084 2010.01.01 00:00  TestOrders test started


Please help me find the error.

 
krishna_gopal_2:

Hi everyone,

I am a newbie to MQL. I'm trying a testing EA. I am getting Error#130. Couldn't find the reason. Pls find the code below and copy of the journal below.

Please help me find the error.

Error 130, ERR_INVALID_STOPS - Please learn to read the documentation - Runtime Error Codes & Requirements and Limitations in Making Trades

Also read, the following related post: https://www.mql5.com/en/forum/159983

 
FMIC:

Error 130, ERR_INVALID_STOPS - Please learn to read the documentation - Runtime Error Codes & Requirements and Limitations in Making Trades

Also read, the following related post: https://www.mql5.com/en/forum/159983

It seems the Second SellLimit Order meets most of the requirements. Still it says "Invalid Stops". That is confusing. I'll try to make Market Orders then, but I would like to know exact fix to the error.
 
krishna_gopal_2:
It seems the Second SellLimit Order meets most of the requirements. Still it says "Invalid Stops". That is confusing. I'll try to make Market Orders then, but I would like to know exact fix to the error.

It is not a question of if it looks OK or not! It is a fact that your code does NOT check for the various conditions, such as the Stop Level for example. So how to you KNOW if it is OK or not?

StopLevelPoints = MarketInfo( _Symbol, MODE_STOPLEVEL );

Also, for example, for the Buy Order, you are calculating your StopLoss and TakeProfit based on the Ask Price, but a Buy Order will close at the Bid price. So you are not considering the effect the spread will have on the minimum stop size (STOPLEVEL). The reverse is valid for the Sell Order.

EDIT: Also, just noticed that you are placing your Sell Limit Order below the Bid Price (I assume, since you only printed the Ask price), when it must be above.
You MUST check these conditions before placing the order -
Requirements and Limitations in Making Trades

 

Avoid using intermediate variables that have no use elsewhere, it can be confusing in larger blocks of code

         Trade_Price = Ask;
         PRI = Trade_Price;//OrderOpenPrice();
         Trade_Price = PRI - Trade_Gap;         

take out PRI

         Trade_Price = Ask;
         Trade_Price = Trade_Price - Trade_Gap;  //Or Trade_Price -= Trade_Gap;


krishna_gopal_2:
It seems the Second SellLimit Order meets most of the requirements. Still it says "Invalid Stops". That is confusing. I'll try to make Market Orders then, but I would like to know exact fix to the error.
Make sure that SellLimit entry price is above the current price
 
A SellLimit must be above the current price, a SellStop must be below.
 
Thank you all.