OrderSend failed with error #130 - page 2

 
Ticket1 = OrderSend(Symbol(), OP_SELLSTOP, 1,1.13561, 3, 1.13731, 1.13541, "Pending Sell Order 1", 16384, 0, clrGreen);
You are setting the TP at 20 points, that is ridiculously close. If the spread is 20 Points or more the trade would be closed immediately if it was opened.
 
Vitalii Ananev:

If the error persists, check that the SL and TP are not closer than SYMBOL_TRADE_STOPS_LEVEL. 

Also, the order opening price should not be closer than  MarketInfo (Symbol(), MODE_STOPLEVEL) to сurrent price. 

If MarketInfo (Symbol (), MODE_STOPLEVEL) is equal to zero, then this value is floating and it is necessary to use an offset from the current price of at least two spreads.

StopLossSell = NormalizeDouble(1.13761 - ((3 * 10) * Point), 5);

Price = NormalizeDouble(1.1367100000000001 - ((3 * 10) * Point), 5);

TpOrderOne = NormalizeDouble(1.13561 - ((10 * 10) * Point), 5);

double STOPLEVEL = MarketInfo (Symbol(), MODE_STOPLEVEL); // Getting 8.0

long ex = SymbolInfoInteger(Symbol(), SYMBOL_TRADE_STOPS_LEVEL); // Getting 8

Ticket1 = OrderSend(Symbol(), OP_SELLSTOP, 1,1.13561, 3, 1.13731, 1.13541, "Pending Sell Order 1", 16384, 0, clrGreen);
STOPLEVEL = Getting 8.0

Still not understanding the issue

 
Keith Watford:
You are setting the TP at 20 points, that is ridiculously close. If the spread is 20 Points or more the trade would be closed immediately if it was opened.

Is this is the main reason or getting 130 error?

 
Keith Watford:
You are setting the TP at 20 points, that is ridiculously close. If the spread is 20 Points or more the trade would be closed immediately if it was opened.

Could you write an example code with dummy values ?

I want for pending sell order SL to be 3 pips below of 2nd last candle i.e ( SL = Low[2] - 3 pips)

TP = TradeOpenPrice - 10 pips

TradeOpenPrice =  Low[1] - 3 pips


how could I code this? Please write an example code I am stuck on this unable to understand.

 
 
// This worked
Ticket1 = OrderSend(Symbol(), OP_SELLSTOP, 0.01, Bid - 20 * Point, 3, Ask+3 *Point, Ask-100*Point, "Pending Sell Order 1", 16384, 0, Red);

// This didn't worked what is the difference???
Ticket1 = OrderSend(Symbol(),OP_SELLSTOP,0.01,Price,3,StopLossSell,TpFirstOrder,"Pending Sell Order 1",16384,0,clrGreen);

I want my EA to work in this manner:

  1. When Close[1] > EMA (EXPONENTIAL MOVING AVERAGE) open a trade immediately on the start of current candle (example: Close[1]  = 1.16147  > EMA = 1.16076)
  2. Price for opening pending trade will be High[1] + 3 pips (example: High[1] = 1.16186 + 3 pips, SO TradeOpenPrice = 1.16216)
  3. StopLoss for pending trade will be High[2] - 3 pips (example: High[2] = 1.16180 - 3 pips, SO SL = 1.16150)
  4. TakeProfit for this pending order should be 10 pips above of the trade open price (exampleTradeOpenPrice = 1.16216 + 10 pips, SO TP = 1.16316)
 
void OnTick()
  {
   
   H1EMAprev = NormalizeDouble(iMA(NULL,PERIOD_H1,14,0,MODE_EMA,PRICE_CLOSE,1), 5);
   H4EMAprev = NormalizeDouble(iMA(NULL,PERIOD_H4,14,0,MODE_EMA,PRICE_CLOSE,1), 5);
   StopLossBuy = NormalizeDouble(High[2] + ((3 * 10) * Point),Digits());
   StopLossSell = NormalizeDouble(Low[2] - ((3 * 10) * Point),Digits());
   double Spreed = Ask-Bid;
   if(
      Close[1]>H4EMAprev && 
      Close[1]>H1EMAprev
      )
     { 
      Price = NormalizeDouble(High[1] + ((3 * 10) * Point),Digits());
      TpOrderOne = NormalizeDouble(Price + ((10 * 10) * Point),Digits());
      if (StopLossBuy>Price-2*Spreed) Print("Not correct SL BUY");
      //Pending order BUY
      if (Price>Ask+2*Spreed)
      {
         Ticket1 = OrderSend(Symbol(),OP_BUYSTOP,1,Price,3,StopLossBuy,TpFirstOrder,"Pending Buy Order 1",16384,0,clrGreen);
         if(Ticket1<=0)
         {
            Print("OrderSend failed with error #",GetLastError());
         }
         else
         {
            Print("OrderSend placed successfully");
          }
     
      }else Print("The opening price is too close to the current price.");

   if(
      Close[1]<H4EMAprev && 
      Close[1]<H1EMAprev
      )
     {
      
      Price = NormalizeDouble(Low[1] - ((3 * 10) * Point),Digits());
      TpOrderOne = NormalizeDouble(Price - ((10 * 10) * Point),Digits());
       if (StopLossSell<Price+2*Spreed) Print("Not correct SL SELL");
      //Pending order BUY
      if (Price<Bid-2*Spreed)
      {
  
           Ticket1 = OrderSend(Symbol(),OP_SELLSTOP,1,Price,3,StopLossSell,TpFirstOrder,"Pending Buy Order 1",16384,0,clrGreen);
      
         if(Ticket1<0)
          {
            Print("OrderSend failed with error #",GetLastError());
          }
         else
         {
        Print("OrderSend placed successfully");
        }
      }else Print("The opening price is too close to the current price.")
  }

Try this code. I didn't check it myself. The point is: the open Price of BUY STOP should be higher than the current price plus two spreads (I use two spreads instead of MarketInfo (Symbol (), MODE_STOPLEVEL)), SL should be lower than the open price. The open price of the SELL STOP should be below the current price and minus two spreads, SL should be above the open price plus two spreads.


     ............  
     TpOrderOne = NormalizeDouble(Price + ((10 * 10) * Point),Digits());
    .....................
      if (Price>Ask+2*Spreed)
      {
         Ticket1 = OrderSend(Symbol(),OP_BUYSTOP,1,Price,3,StopLossBuy,TpFirstOrder,"Pending Buy Order 1",16384,0,clrGreen);

Note that it is not clear from your code what the value of the variable is TpFirstOrder. In the code, it is not implanted anywhere of any value.

 
Vitalii Ananev:

Try this code. I didn't check it myself. The point is: the open Price of BUY STOP should be higher than the current price plus two spreads (I use two spreads instead of MarketInfo (Symbol (), MODE_STOPLEVEL)), SL should be lower than the open price. The open price of the SELL STOP should be below the current price and minus two spreads, SL should be above the open price plus two spreads.

What is spreads? why u have used it?

 
kumaillakhani:

What is spreads? why u have used it?

This is the difference between Ask and Bid. I used two spreads, because it happens that the value of MarketInfo (Symbol (), MODE_STOPLEVEL) is zero. If it is equal to zero, it means that it is "floating" and each time can be different. Therefore, I usually use the value of two spread sizes ((Ask-Bid)*2 ).

 
Vitalii Ananev:

Try this code. I didn't check it myself. The point is: the open Price of BUY STOP should be higher than the current price plus two spreads (I use two spreads instead of MarketInfo (Symbol (), MODE_STOPLEVEL)), SL should be lower than the open price. The open price of the SELL STOP should be below the current price and minus two spreads, SL should be above the open price plus two spreads.


Note that it is not clear from your code what the value of the variable is TpFirstOrder. In the code, it is not implanted anywhere of any value.

//+------------------------------------------------------------------+
//|                                                    MyPending.mq4 |
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict



double 
   H1EMA, H1EMAprev, H4EMA, H4EMAprev, StopLossBuy, StopLossSell, Price, TpOrderOne;
int 
   MagicNo = 4352, Ticket1, Ticket2, Ticket3;
   
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   H1EMAprev = NormalizeDouble(iMA(NULL,PERIOD_H1,14,0,MODE_EMA,PRICE_CLOSE,1), 5);
   H4EMAprev = NormalizeDouble(iMA(NULL,PERIOD_H4,14,0,MODE_EMA,PRICE_CLOSE,1), 5);
   StopLossBuy = NormalizeDouble(High[2] + ((3 * 10) * Point),Digits());
   StopLossSell = NormalizeDouble(Low[2] - ((3 * 10) * Point),Digits());
   double Spreed = Ask-Bid;
   if(
      Close[1]>H4EMAprev && 
      Close[1]>H1EMAprev
      )
     { 
      Price = NormalizeDouble(High[1] + ((3 * 10) * Point),Digits());
      TpOrderOne = NormalizeDouble(Price + ((10 * 10) * Point),Digits());
      if (StopLossBuy>Price-2*Spreed) Print("Not correct SL BUY");
      //Pending order BUY
      if (Price>Ask+2*Spreed)
      {
         Ticket1 = OrderSend(Symbol(),OP_BUYSTOP,1,Price,3,StopLossBuy,TpOrderOne,"Pending Buy Order 1",16384,0,clrGreen);
         if(Ticket1<=0)
         {
            Print("OrderSend failed with error #",GetLastError());
         }
         else
         {
            Print("OrderSend placed successfully");
          }
     
      }else Print("The opening price is too close to the current price.");
     }

   if(
      Close[1]<H4EMAprev && 
      Close[1]<H1EMAprev
      )
     {
      
      Price = NormalizeDouble(Low[1] - ((3 * 10) * Point),Digits());
      TpOrderOne = NormalizeDouble(Price - ((10 * 10) * Point),Digits());
       if (StopLossSell<Price+2*Spreed) Print("Not correct SL SELL");
      //Pending order BUY
      if (Price<Bid-2*Spreed)
      {
  
           Ticket1 = OrderSend(Symbol(),OP_SELLSTOP,1,Price,3,StopLossSell,TpOrderOne,"Pending Buy Order 1",16384,0,clrGreen);
      
         if(Ticket1<0)
          {
            Print("OrderSend failed with error #",GetLastError());
          }
         else
         {
        Print("OrderSend placed successfully");
        }
      }else Print("The opening price is too close to the current price.");
     }
  }
//+------------------------------------------------------------------+

Here it is TpFirstOrder = 10  which you have already set in 

TpOrderOne = NormalizeDouble(Price + ((10 * 10) * Point),Digits());

 but your code is still not working still getting error.

 
 //+------------------------------------------------------------------+
//|                                                    MyPending.mq4 |
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property  copyright "Copyright 2018, MetaQuotes Software Corp."
#property  link        "https://www.mql5.com"
#property  version    "1.00"
#property  strict



double 
   H1EMA, H1EMAprev, H4EMA, H4EMAprev, StopLossBuy, StopLossSell, Price, TpOrderOne;
int 
   MagicNo = 4352 , Ticket1, Ticket2, Ticket3;
   
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit ()
  {
//---
   
//---
   return ( INIT_SUCCEEDED );
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit ( const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick ()
  {
//---
   H1EMAprev = NormalizeDouble ( iMA ( NULL , PERIOD_H1 , 14 , 0 , MODE_EMA , PRICE_CLOSE , 1 ), Digits());
   H4EMAprev = NormalizeDouble ( iMA ( NULL , PERIOD_H4 , 14 , 0 , MODE_EMA , PRICE_CLOSE , 1 ), Digits() );
   int dig = 1;
   if (Digits() == 3 || Digits() >= 5) dig = 10;
   
   StopLossBuy = NormalizeDouble ( High [ 2 ] + (( 3 * dig ) * Point ), Digits ());
   StopLossSell = NormalizeDouble ( Low [ 2 ] - (( 3 * dig ) * Point ), Digits ());
   double Spreed = Ask - Bid ;
   if (
       Close [ 1 ]>H4EMAprev && 
       Close [ 1 ]>H1EMAprev
      )
     { 
      Price = NormalizeDouble ( High [ 1 ] + (( 3 * dig ) * Point ), Digits ());
      TpOrderOne = NormalizeDouble (Price + (( 10 * dig ) * Point ), Digits ());
       if (StopLossBuy>Price- 2 *Spreed) 
       {
         Print ( "Not correct SL BUY " );
         return;
       }
       //Pending order BUY
       if (Price> Ask + 2 *Spreed)
      {
         Ticket1 = OrderSend ( Symbol (), OP_BUYSTOP , 1 ,Price, 3 ,StopLossBuy,TpOrderOne, "Pending Buy Order 1" , 16384 , 0 , clrGreen );
         if (Ticket1<= 0 )
         {
             Print ( "OrderSend failed with error #" , GetLastError ());
         }
         else
         {
             Print ( "OrderSend placed successfully" );
          }
     
      } else Print ("BUY The opening price is too close to the current price. ",Price," ",Ask );
     }

   if (
       Close [ 1 ]<H4EMAprev && 
       Close [ 1 ]<H1EMAprev
      )
     {
      
      Price = NormalizeDouble ( Low [ 1 ] - (( 3 * dig ) * Point ), Digits ());
      TpOrderOne = NormalizeDouble (Price - (( 10 * dig ) * Point ), Digits ());
       if (StopLossSell<Price+ 2 *Spreed) 
       {
         Print ( "Not correct SL SELL" );
         return;
       }
       //Pending order BUY
       if (Price< Bid - 2 *Spreed)
      {
  
           Ticket1 = OrderSend ( Symbol (), OP_SELLSTOP , 1 ,Price, 3 ,StopLossSell,TpOrderOne, "Pending Buy Order 1" , 16384 , 0 , clrGreen );
      
         if (Ticket1< 0 )
          {
             Print ( "OrderSend failed with error #" , GetLastError ());
          }
         else
         {
         Print ( "OrderSend placed successfully" );
        }
      } else Print ("SELL The opening price is too close to the current price. ",Price," ",Bid );
     }
  }

See the log messages, most of the errors are not correct SL