How to partially close a position on MT5 hedging account

Hi guys, I have faced a problem which I really worked on but I couldn’t solve it.

I want to partially close a position by pending order, the reason I want to use pending order is I want it as an SL but Partial SL.

I wrote the code below but pending order results new position an doesn’t affect the active position.

MqlTradeRequest PrimaryPendingRequest;
MqlTradeResult PrimaryPendingResult;
ZeroMemory(PrimaryPendingRequest);
ZeroMemory(PrimaryPendingResult);
PrimaryPendingRequest.action = TRADE_ACTION_PENDING;
PrimaryPendingRequest.magic = 1998;
PrimaryPendingRequest.symbol = _Symbol;
PrimaryPendingRequest.volume = 0.9;
PrimaryPendingRequest.price = NormalizeDouble(PrimaryTradeResult.price + 0.00005, 5);
PrimaryPendingRequest.type = ORDER_TYPE_BUY_STOP;
PrimaryPendingRequest.type_filling = ORDER_FILLING_FOK;
PrimaryPendingRequest.position = PrimaryTradeResult.order;
OrderSend(PrimaryPendingRequest, PrimaryPendingResult);

I want this pending to close 0.9 lot of the active position which is 1 lot and make it 0.1 lot position but it opens a new position and I will have 1 lot position + 0.9 lot position.

I thought it’s broker problem and check it with 3 brokers but the result was the same.

it’s the same thing, Normal close partially or with pending

It’s only possible on a netting account. On an hedging account, it will always result in a new position.

Unfortunately It’s not the same thing, I tested it and new position opened for me albeit I had given value to MqlTradeRequest.Position

It seems complicated to me Alain, I can affect my current position by “deal” orders even on a hedge account with giving my current position ticket value to MqlTradeRequest.position but when it comes to “pending” orders, I can’t affect my position by them even when I give value to “Position” variable in MqlTradeRequest structure.

It’s not complicated it’s how it works, you will have to deal with it.

On an hedging account, you can :

open new positions (with market or pending orders)
close existing position (fully or partially) with market orders, not with pending orders.
you can not add to an existing position, only open a new one.

So if you want a partial close you have 2 solutions :

Monitor the price and send a market order to partially close your position with the needed volume.
Open 2 positions (0.9 and 0.1) instead of one, and set your sl/tp accordingly.

In all cases, pending orders can’t be used for that purpose.

Got it completely, Thank you for your generosity.