How to close an open position in MQL5?


I place a trade with MqlTradeRequest structure and i want close my trade (Order = 123456 example)

When i write it :

MqlTradeRequest request={0};
   request.symbol = mysymbol;            
   request.volume = myvolume;       
   request.order =123456;         ;           ;               
   request.type= ORDER_TYPE_BUY_STOP;                  
   MqlTradeResult result={0};

my order is not close ?

do you know why

thank a lot

I’m not sure about this, but you can change this

I’m not sure about this, but you can change this for

closing buy position

request.type= ORDER_TYPE_SELL;

closing sell position

request.type= ORDER_TYPE_BUY; 

This it’s for placed order no?

For close my order i need write : TRADE_ACTION_MODIFY?

In the doc it’s not write how use MqlTradeRequest for close Order

It’s working

For close Buy => Use Sell

For close Sell => Use Buy

But how can i have two positions for the same Symbol?

example 1 volume Buy and 2 volume sell?

In MQL5 you can’t have 2 position (buy and sell). Only 1 position buy or sell…


I’m facing the same problem and tried to do as you said, open a sell to close a buy and vice versa but didn’t work. I keep just opening new positions instead of closing the old ones. Any idea what might be happening?

You are using an hedge account, while the technique described here is for a netting account.

For an hedge account you always need to specify the ticket of the position you want to close.