How to place a buy stop order?

 

Hi all,

I don't succeed in placing a buy stop order...

When using a MqlTradeRequest and OrderSend(), what information do I need to enter??

What are stoplimit and price representing in the case of a buy stop order?

Thx,

struct MqlTradeRequest
  {
   ENUM_TRADE_REQUEST_ACTIONS    action;           // Trade operation type
   ulong                         magic;            // Expert Advisor ID (magic number)
   ulong                         order;            // Order ticket
   string                        symbol;           // Trade symbol
   double                        volume;           // Requested volume for a deal in lots
   double                        price;            // Price
   double                        stoplimit;        // StopLimit level of the order
   double                        sl;               // Stop Loss level of the order
   double                        tp;               // Take Profit level of the order
   ulong                         deviation;        // Maximal possible deviation from the requested price
   ENUM_ORDER_TYPE               type;             // Order type
   ENUM_ORDER_TYPE_FILLING       type_filling;     // Order execution type
   ENUM_ORDER_TYPE_TIME          type_time;        // Order execution time
   datetime                      expiration;       // Order expiration time (for the orders of ORDER_TIME_SPECIFIED type)
   string                        comment;          // Order comment
  };
Documentation on MQL5: Standard Constants, Enumerations and Structures / Data Structures / Trade Request Structure
  • www.mql5.com
Standard Constants, Enumerations and Structures / Data Structures / Trade Request Structure - Documentation on MQL5
 

Ok I succeed placing a buy stop order. I needed expiration time...

Here is the code I used and it's working...

 

MqlTradeRequest request;
   MqlTradeResult result;
   if(m_achat && m_achat2)
   {
     request.action = TRADE_ACTION_PENDING;
     request.type = ORDER_TYPE_BUY_STOP;
     request.symbol = Symbol();
     request.magic = m_magic;
     request.price = m_highest;
     request.volume = m_lots;
     request.tp = m_highest + m_limit*m_symbol.Point();
     request.sl = m_highest - m_stop*m_symbol.Point();
     request.deviation = 50;
     request.type_filling = ORDER_FILLING_AON;
     request.expiration = TimeCurrent()+24*60*60;
     request.comment = "Expert test buy stop";
    
     ok = OrderSend(request,result);
     if(!ok)
     {
         err = GetLastError();
         Print("error buy stop (",err,")");
     }
   }

But, I now try to place a sell stop, and this code is not working... I get the 4756 error...

 

if(m_vente && m_vente2)
   {
      MqlTradeRequest request2;
      MqlTradeResult result2;





      request2.action = TRADE_ACTION_PENDING;
      request2.type = ORDER_TYPE_SELL_STOP;
      request2.symbol = Symbol();
      request2.magic = m_magic;
      request2.price = m_lowest;
      request2.volume = m_lots;
      request2.tp = m_lowest - m_limit*m_symbol.Point();
      request2.sl = m_lowest + m_stop*m_symbol.Point();
      request2.deviation = 50;
      request2.type_filling = ORDER_FILLING_AON;
      request2.expiration = TimeCurrent()+24*60*60;
      request2.comment = "Expert test sell stop";
      
      ok = OrderSend(request2,result2);
      if(!ok)
      {
         err = GetLastError();
         Print("error sell stop(",err,")");
      }
   }

Any idea?

 Cam' 

 
cameleon33:

Ok I succeed placing a buy stop order. I needed expiration time...

Here is the code I used and it's working...

 

But, I now try to place a sell stop, and this code is not working... I get the 4756 error...

 

Any idea?

 Cam' 

I tried to run your code to place Sell Stop order. Order was placed successfully.

You got 4756 error in Experts tab. Were there any errors in Journal tab?

 
alexvd:

I tried to run your code to place Sell Stop order. Order was placed successfully.

You got 4756 error in Experts tab. Were there any errors in Journal tab; 

Indeed I didn't paid attention to journal errors...

I got the following message : "2010.06.29 14:39:51 Trades '2809' : failed sell stop 0.10 EURUSD at 1.22650 sl: 1.22700 tp: 1.22550 [Invalid stops]"

What does "invalid stops" mean?

Thanks for your help!

Cam' 

 
cameleon33:

Indeed I didn't paid attention to journal errors...

I got the following message : "2010.06.29 14:39:51 Trades '2809' : failed sell stop 0.10 EURUSD at 1.22650 sl: 1.22700 tp: 1.22550 [Invalid stops]"

What does "invalid stops" mean?


You try to specify Stop Loss or Take Profit too close to current price (for position) or to open price (for pending order).
 

Thanks for your help! It's working now!

Cam'

 
cameleon33:

Thanks for your help! It's working now!

Cam'

In the future try also to analyze not only last error code, but also function retcode: in your case result2.retcode. It might be more useful.

Also you can check whether order will be placed or not with OrderCheck function. It is very useful! 

OOP in MQL5 by Example: Processing Warning and Error Codes
  • 2010.05.26
  • KlimMalgin
  • www.mql5.com
The article describes an example of creating a class for working with the trade server return codes and all the errors that occur during the MQL-program run. Read the article, and you will learn how to work with classes and objects in MQL5. At the same time, this is a convenient tool for handling errors; and you can further change this tool according to your specific needs.
 
I dont know why when I try opening a BUY STOP without stoploss (sl=0) sometimes it opens right and sometimes it returns invalid stops. please help me I dont understand whats happening. 
 
Rosh:
You try to specify Stop Loss or Take Profit too close to current price (for position) or to open price (for pending order).
I received the invalid stops message trying to open a buystop with sl and tp in 0 (without them) sometimes I get the error sometimes doesnt, is very strange, any ideas?
 
Francisco Jose Castro Cabal #:
I received the invalid stops message trying to open a buystop with sl and tp in 0 (without them) sometimes I get the error sometimes doesnt, is very strange, any ideas?
Francisco, probably ask is bigger than your price at moment, slippage, or maybe, did you miss to sum your SL with your price before put as a parameter?
Reason: