Pending Order in MQL5 gives "error buy stop (4756)"

 

I'm trying to write MQL5 code that submits a buy limit order but it always fails with "error buy stop (4756)".  The code is:


           MqlTradeRequest request;
           MqlTradeResult result;
           MqlTradeCheckResult chkresult;
           ZeroMemory(result);
   
           request.action = TRADE_ACTION_PENDING;
           request.symbol = Symbol();
           request.volume = tradeSize;
           request.price = buyOrderPrice;
           request.stoplimit = buyOrderPrice;
           request.tp = buyOrderPrice +tp1;
           request.sl = buyOrderPrice - sl1;
           request.type = ORDER_TYPE_BUY_STOP;
           request.type_filling = ORDER_FILLING_FOK;
           request.type_time = ORDER_TIME_SPECIFIED;
           request.expiration = TimeCurrent()+48*60*60;
           ok = OrderCheck(request,chkresult);
           ok = OrderSend(request,result);
           if(!ok)
           {
               err = GetLastError();
               Print("error buy stop (",err,")");
           }
           ZeroMemory(result);


The journal shows the values being sent:

failed buy stop 0.5 US30_SB at 35286.0 (35286.0) sl: 35186.0 tp: 35366.0 [Invalid request]

...and the current price is 34960


Anyone have any idea why this is failing?

 
kingbiga:

I'm trying to write MQL5 code that submits a buy limit order but it always fails with "error buy stop (4756)".  The code is:



The journal shows the values being sent:

failed buy stop 0.5 US30_SB at 35286.0 (35286.0) sl: 35186.0 tp: 35366.0 [Invalid request]

...and the current price is 34960


Anyone have any idea why this is failing?

The current price doesn't mean anything tell us instead: what are Ask and Bid and the time of the order (is the market open).

 
Carl Schreiber #:

The current price doesn't mean anything tell us instead: what are Ask and Bid and the time of the order (is the market open).


I included the current price because I read that has to be lower than the order price for buy stop orders.

The market is open and bid = 34,978.8, ask = 34983.6

 
kingbiga :

I'm trying to write MQL5 code that submits a buy limit order but it always fails with "error buy stop (4756)".  The code is:



The journal shows the values being sent:

failed buy stop 0.5 US30_SB at 35286.0 (35286.0) sl: 35186.0 tp: 35366.0 [ Invalid request ]

...and the current price is 34960


Anyone have any idea why this is failing?

Take an example from the ( Trade Operation Types ) help and compare with your example.

#property description "Example of placing pending orders"
#property script_show_inputs
#define EXPERT_MAGIC 123456                             // MagicNumber of the expert
input ENUM_ORDER_TYPE orderType=ORDER_TYPE_BUY_LIMIT;   // order type
//+------------------------------------------------------------------+
//| Placing pending orders                                           |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- declare and initialize the trade request and result of trade request
   MqlTradeRequest request={};
   MqlTradeResult  result={};
//--- parameters to place a pending order
   request.action   =TRADE_ACTION_PENDING;                             // type of trade operation
   request.symbol   =Symbol();                                         // symbol
   request.volume   =0.1;                                              // volume of 0.1 lot
   request.deviation=2;                                                // allowed deviation from the price
   request.magic    =EXPERT_MAGIC;                                     // MagicNumber of the order
   int offset = 50;                                                    // offset from the current price to place the order, in points
   double price;                                                       // order triggering price
   double point=SymbolInfoDouble(_Symbol,SYMBOL_POINT);                // value of point
   int digits=SymbolInfoInteger(_Symbol,SYMBOL_DIGITS);                // number of decimal places (precision)
   //--- checking the type of operation
   if(orderType==ORDER_TYPE_BUY_LIMIT)
     {
      request.type     =ORDER_TYPE_BUY_LIMIT;                          // order type
      price=SymbolInfoDouble(Symbol(),SYMBOL_ASK)-offset*point;        // price for opening 
      request.price    =NormalizeDouble(price,digits);                 // normalized opening price 
     }
   else if(orderType==ORDER_TYPE_SELL_LIMIT)
     {
      request.type     =ORDER_TYPE_SELL_LIMIT;                          // order type
      price=SymbolInfoDouble(Symbol(),SYMBOL_ASK)+offset*point;         // price for opening 
      request.price    =NormalizeDouble(price,digits);                  // normalized opening price 
     }
   else if(orderType==ORDER_TYPE_BUY_STOP)
     {
      request.type =ORDER_TYPE_BUY_STOP;                                // order type
      price        =SymbolInfoDouble(Symbol(),SYMBOL_ASK)+offset*point; // price for opening 
      request.price=NormalizeDouble(price,digits);                      // normalized opening price 
     }
   else if(orderType==ORDER_TYPE_SELL_STOP)
     {
      request.type     =ORDER_TYPE_SELL_STOP;                           // order type
      price=SymbolInfoDouble(Symbol(),SYMBOL_ASK)-offset*point;         // price for opening 
      request.price    =NormalizeDouble(price,digits);                  // normalized opening price 
     }
   else Alert("This example is only for placing pending orders");   // if not pending order is selected
//--- send the request
   if(!OrderSend(request,result))
      PrintFormat("OrderSend error %d",GetLastError());                 // if unable to send the request, output the error code
//--- information about the operation
   PrintFormat("retcode=%u  deal=%I64u  order=%I64u",result.retcode,result.deal,result.order);
  }
//+------------------------------------------------------------------+
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Operation Types
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Operation Types
  • www.mql5.com
Trade Operation Types - Trade Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Vladimir Karputov #:

Take an example from the ( Trade Operation Types ) help and compare with your example.

Thanks Vladimir!

That helped.  It's working now.  It seemed to be the declaration and use of zero memory that I had wrong.  I'm using the declaration as above now:

   MqlTradeRequest request={};
   MqlTradeResult  result={};
Reason: