CTrade SetDeviationInPoints

what does this do?

if I set this up, when I use PositionOpen it should follow?

and why do I still need to input a deviation when I use PositionClose?

anyone know more info on ctrade?

I discovered the positionclose can omit the slippage. So how it the ctrade works?

You can have slippage when closing a position. Closing a position is only a trade request at a given price, there is technically no difference with opening a position.

Hi,

I ran into an issue concernig the class CTrade together with the method SetDeviationInPoints.

If I want to place a pending order without using the class CTrade I can do it together with allowing some deviation from the price.

This is described in MQL5 Reference / Constants, Enumerations and Structures / Trade Constants / Trade Operation Types.

There is an example of the TRADE_ACTION_PENDING trade operation for placing 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
...
   if(!OrderSend(request,result))

That is completely ok for me.

But if I want to use the class CTrade for placing a pending order and allow a certain deviation by the method SetDeviationInPoints this seems to have no effect.

#include Trade/Trade.mqh
CTrade trade;
...
trade.SetDeviationInPoints(5);
trade.OrderOpen (...);

I think the reason is a mssing line in MQL5/Include/Trade/Trade.mqh in the method OrderOpen (…)

If I add the line



m_request.deviation   =m_deviation;

in line 699 all is ok:

//+------------------------------------------------------------------+
//| Installation pending order                                       |
//+------------------------------------------------------------------+
bool CTrade::OrderOpen(const string symbol,const ENUM_ORDER_TYPE order_type,const double volume,const double limit_price,
                       const double price,const double sl,const double tp,
                       ENUM_ORDER_TYPE_TIME type_time,const datetime expiration,const string comment)
  {
...
//--- setting requestxox
   m_request.action      =TRADE_ACTION_PENDING;
   m_request.symbol      =symbol;
   m_request.magic       =m_magic;
   m_request.volume      =volume;
   m_request.type        =order_type;
   m_request.stoplimit   =limit_price;
   m_request.price       =price;
   m_request.sl          =sl;
   m_request.tp          =tp;
   m_request.type_time   =type_time;
   m_request.expiration  =expiration;
   m_request.deviation   =m_deviation;
...
   return(OrderSend(m_request,m_result));

What is your opinion?

BTW: I’m using MT5 Version 5.00 Build 2122

Some brokers have a limit on how far the deviation point can be.

Try using 10, work on most of them. I think 2 is a too small number.

You can check with your broker for the minimum accepted deviation point.

Deviation is of no use for pending orders. It’s only for market orders on a broker with instant execution (otherwise said a market maker).

Thanks for your answer.

I understand you correctly, even if the pending order has been triggered the deviation is of no use? Never?

If the deviation is of no use for pending orders why there is this line in

MQL5 Reference / Constants, Enumerations and Structures / Trade Constants / Trade Operation Types

in the code example of the TRADE_ACTION_PENDING trade operation for placing a pending order:

request.deviation=2;                              // allowed deviation from the price

???

Sincerely

Thanks for your answer.

I understand you correctly, even if the pending order has been triggered the deviation is of no use? Never?

No, never

If the deviation is of no use for pending orders why there is this line in

MQL5 Reference / Constants, Enumerations and Structures / Trade Constants / Trade Operation Types

in the code example of the TRADE_ACTION_PENDING trade operation for placing a pending order:

???

That’s an error in the documentation example (actually it’s not a problem to set the deviation, it’s just not used).

https://www.mql5.com/en/docs/constants/structures/mqltraderequest

Pending Order

Trade order to place a pending order. It requires to specify the following 11 fields:

  • action
  • symbol
  • volume
  • price
  • stoplimit
  • sl
  • tp
  • type
  • type_filling
  • type_time
  • expiration

Also it is possible to specify the “magic” and “comment” field values.

Try to open a pending order manually with MT5 GUI and you will see there is no deviation field.