Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close position from MT5 Python API not working

I'm trying to close an existing position in MT5 from Python but MT5 always claims an 'Unsupported Filling Mode'.
I tried all possible filling modes but always get the same result. Same, if I omit the filling mode in the request. I also tried to fetch the filling mode from the symbol info and used it in the request - without success. This happens on a live account.
Here is my function that takes an existing position and sends a close request for that position:

async def ClosePosition(position):
   if (position.type == mt5.POSITION_TYPE_BUY):
       order_type = mt5.ORDER_TYPE_SELL
       price = mt5.symbol_info_tick(position.symbol).bid
   else:
       order_type = mt5.ORDER_TYPE_BUY
       price = mt5.symbol_info_tick(position.symbol).ask

   request = {
           "action": mt5.TRADE_ACTION_DEAL,
           "symbol": position.symbol,
           "volume": position.volume,
           "type": order_type,
           "position": position.ticket,
           "price": price,
           "deviation": 10,
           "magic": 0,
           "comment": "Closed by Python",
           "type_time": mt5.ORDER_TIME_GTC,
           "type_filling": mt5.ORDER_FILLING_RETURN
       }

   result = mt5.order_send(request)

This is what the journal says:
Trades '#######': failed market buy 0.01 CHFJPY, close #109459760 sell 0.01 CHFJPY 116.807 [Unsupported filling mode]

Any help is greatly appreciated.

like image 610
hoga Avatar asked Oct 30 '25 12:10

hoga


1 Answers

Eventually I found that the values of SYMBOL_FILLING_MODE do not match those of ENUM_ORDER_TYPE_FILLING. When I translate SYMBOL_FILLING_MODE to the appropriate ENUM_ORDER_TYPE_FILLING it works.
So in my example using mt5.ORDER_FILLING_IOC successfully closes the position.

like image 190
hoga Avatar answered Nov 01 '25 04:11

hoga



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!