Is possible to close a position with a limit order in HEDGING system?

 

Hi,

I would like know if is possible to close a position (or invert it) with a limit order (TRADE_ACTION_PENDING) in HEDGING system?
Even when I set the order.position to the position id (ticket) that I would like to close, I'm getting the error 10013 "invalid request".
Since this error is vague, it don't help me to identify the real problem.
So I don't know if is possible and I'm set some other thing wrong or if is really impossible to close a position with a limit order in hedging system.
For now, I'm only able to close with a market order (TRADE_ACTION_DEAL).

MqlTradeRequest request = {0};
MqlTradeResult result = {0};
request.action = TRADE_ACTION_PENDING;
request.magic = m_magic;
request.symbol = m_symbol.Name();
request.sl = 0;
request.tp = 0;
request.deviation = 0;
request.type_time = ORDER_TIME_DAY;
request.type_filling = ORDER_FILLING_RETURN;
request.expiration = TimeLocal() + PeriodSeconds(PERIOD_D1);
request.volume = m_position.Volume();
request.comment = IntegerToString(m_magic);

request.position = positionId;

if(m_position.PositionType() == POSITION_TYPE_BUY) {            
  request.price = m_symbol.NormalizePrice(m_position.PriceOpen() + _distance);
  request.type = ORDER_TYPE_SELL_LIMIT;
}
else {
  request.price = m_symbol.NormalizePrice(m_position.PriceOpen() - _distance);
  request.type = ORDER_TYPE_BUY_LIMIT;
}
if(!OrderSend(request, result)) {
  Print(__FUNCTION__,":", result.comment, "-", result.retcode, "-", result.retcode_external); 
}
ZeroMemory(request);
ZeroMemory(result);
 
No, it's not possible.
 
Look at Profit Targets and Trailing Stops for ways of closing any order.  There are also scripts that you can run to set these exits. 
 
Alain, you know why?
Thank you.
 
Rodrigo Pandini:
Alain, you know why?
Thank you.
Because in hedging system, a pending order will always result in a new position (or nothing if not triggered).
 

Hi Alain, but how to ensure that the position will close no matter what?

I tried to close a long position with a limit order 5% below the bid price, but it happened what you said, opened a new short position, even I setting position ticket on the request.

The problem on stockmarket (real stocks and futures, not CFD), is that MT5 market orders is sent on ask/bid prices, but if there is no enough volume on this level (if my order is bigger) or even if market moves faster until my order reaches the stockmarket, my market order stays at book, pending as limit order, waiting for execution.

I had some issues with this already, even asked my broker for logs on their side, and they showed me why my market order wasn't executed. And this is serious, that could lead to heavy losses.

So this market order on MT5 is not really a market order, it is a top of the book order only. A real market order would execute my order no matter what the price was (or having a implicit price order of 10% ou 20% beyond the ask/bid price).

For what I saw, deviation (slippage) don't work on stockmarket environment, only on forex exchanges. So even if I set deviation to 100, it always execute on ask/bid prices only, and not one cent more.

So my question (again) is: how to ensure that the position will be closed no matter what? Taking several levels of the book if needed.

Thanks in advance!

 
@rodsibin #:

Hi Alain, but how to ensure that the position will close no matter what?

I tried to close a long position with a limit order 5% below the bid price, but it happened what you said, opened a new short position, even I setting position ticket on the request.

The problem on stockmarket (real stocks and futures, not CFD), is that MT5 market orders is sent on ask/bid prices, but if there is no enough volume on this level (if my order is bigger) or even if market moves faster until my order reaches the stockmarket, my market order stays at book, pending as limit order, waiting for execution.

I had some issues with this already, even asked my broker for logs on their side, and they showed me why my market order wasn't executed. And this is serious, that could lead to heavy losses.

So this market order on MT5 is not really a market order, it is a top of the book order only. A real market order would execute my order no matter what the price was (or having a implicit price order of 10% ou 20% beyond the ask/bid price).

For what I saw, deviation (slippage) don't work on stockmarket environment, only on forex exchanges. So even if I set deviation to 100, it always execute on ask/bid prices only, and not one cent more.

So my question (again) is: how to ensure that the position will be closed no matter what? Taking several levels of the book if needed.

Thanks in advance!


I don't really understand your reasoning or explanation, but if you still want to use a limit order, here are your options:

  • Use a "netting" account instead of a "hedging" account, which is the simplest option, or ...
  • close both of the opposite positions using a close-by operation.
    This can be done after your limit order is triggered so as to close both open positions simultaneously without any further loss or slippage.
    The net result will be the equivalent of the "netting" account.
    Please note however, that not all brokers allow the "close-by" operation.
    To achieve this operation in code, use the TRADE_ACTION_CLOSE_BY action request.
Executing Trades - Trading Operations - MetaTrader 5 Help
Executing Trades - Trading Operations - MetaTrader 5 Help
  • www.metatrader5.com
The trading activity in the platform implies forming and sending market and pending orders to be executed by a broker, as well as managing current...
 
Fernando Carreiro #:

I don't really understand your reasoning or explanation, but if you still want to use a limit order, here are your options:

  • Use a "netting" account instead of a "hedging" account, which is the simplest option, or ...
  • close both of the opposite positions using a close-by operation.
    This can be done after your limit order is triggered so as to close both open positions simultaneously without any further loss or slippage.
    The net result will be the equivalent of the "netting" account.
    Please note however, that not all brokers allow the "close-by" operation.
    To achieve this operation in code, use the TRADE_ACTION_CLOSE_BY action request.

Hi Fernando, I think the full reasoning had the motives: 1) Tell until what I know on MT5/MQL5; 2) Someone see if I'm saying something wrong; 3) Give full context about MT5 on stockmarket so can help others too searching for some keywords; and finally 4) some words about this frustrated experience of (not so) market orders, different from other platforms.

Thanks for the quick reply. Netting is not an option, since I execute multiple EAs per symbol. Close-by operation I have already seen on documentation but never had the courage to use. Well, I will take look. Another option is to check if position was closed on the next tick, and if not, cancel pending order and send a close again. It would be much simpler to execute a real market order to close the position, but... let's work with what we have!

Thanks again!