How Close an MT5 Order from Python?

How Close an MT5 Order from Python?

Manage alerts

Loading saved threads...

Swepy · External communityPost link
External question — Stack Overflow Stack Exchange Author: Swepy Original post: https://stackoverflow.com/questions/60971841 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. I'm trying to send orders from a python script to my MT5 terminal via the MT5-python API. I can open a position but how can I close it from Python mt5-API? I open a BUY ticket it like this: import MetaTrader5 as mt5 lot = 0.1 request = { "action": mt5.TRADE_ACTION_DEAL, "symbol": symbol, "volume": lot, "type": mt5.ORDER_TYPE_BUY, "price": price, ..... "type_time": mt5.ORDER_TIME_GTC, "type_filling": mt5.ORDER_FILLING_RETURN, } enter copoint = mt5.symbol_info(symbol).point But what command and form to send a "close"? Find more info: https://www.mql5.com/en/docs/integration/python_metatrader5 Many thanks!
Quote
Report
Edison · External communityPost link
External answer — Stack Overflow Stack Exchange Author: Edison Original post: https://stackoverflow.com/a/61160531 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. The structure of the order is the same, Only that during close, you have to specify the position_id that you got from the result object returned by the first order. And obviously, if closing a buy order, the close request is a sell order. Here is the code that you could use to do both a buy and sell. import MetaTrader5 as mt5 ea_magic_number = 9986989 # if you want to give every bot a unique identifier def get_info(symbol): '''https://www.mql5.com/en/docs/integration/python_metatrader5/mt5symbolinfo_py ''' # get symbol properties info=mt5.symbol_info(symbol) return info def open_trade(action, symbol, lot, sl_points, tp_points, deviation): '''https://www.mql5.com/en/docs/integration/python_metatrader5/mt5ordersend_py ''' # prepare the buy request structure symbol_info = get_info(symbol) if action == 'buy': trade_type = mt5.ORDER_TYPE_BUY price = mt5.symbol_info_tick(symbol).ask elif action =='sell': trade_type = mt5.ORDER_TYPE_SELL price = mt5.symbol_info_tick(symbol).bid point = mt5.symbol_info(symbol).point buy_request = { "action": mt5.TRADE_ACTION_DEAL, "symbol": symbol, "volume": lot, "type": trade_type, "price": price, "sl": price - sl_points * point, "tp": price + tp_points * point, "deviation": deviation, "magic": ea_magic_number, "comment": "sent by python", "type_time": mt5.ORDER_TIME_GTC, # good till cancelled "type_filling": mt5.ORDER_FILLING_RETURN, } # send a trading request result = mt5.order_send(buy_request) return result, buy_request def close_trade(action, buy_request, result, deviation): '''https://www.mql5.com/en/docs/integration/python_metatrader5/mt5ordersend_py ''' # create a close request symbol = buy_request['symbol'] if action == 'buy': trade_type = mt5.ORDER_TYPE_BUY price = mt5.symbol_info_tick(symbol).ask elif action =='sell': trade_type = mt5.ORDER_TYPE_SELL price = mt5.symbol_info_tick(symbol).bid position_id=result.order lot = buy_request['volume'] close_request={ "action": mt5.TRADE_ACTION_DEAL, "symbol": symbol, "volume": lot, "type": trade_type, "position": position_id, "price": price, "deviation": deviation, "magic": ea_magic_number, "comment": "python script close", "type_time": mt5.ORDER_TIME_GTC, # good till cancelled "type_filling": mt5.ORDER_FILLING_RETURN, } # send a close request result=mt5.order_send(close_request) # This is how I would execute the order result, buy_request = open_trade('buy', 'USDJPY', 0.1, 50, 50, 10) close_trade('sell', buy_request, result, 10)
Quote
Report
Waleed Alenazi · External communityPost link
External answer — Stack Overflow Stack Exchange Author: Waleed Alenazi Original post: https://stackoverflow.com/a/68346739 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. an easier way to do it mt5.Close(symbol,ticket=ticket) note: ticket is the same as position_id
Quote
Report
Farzad Amirjavid · External communityPost link
External answer — Stack Overflow Stack Exchange Author: Farzad Amirjavid Original post: https://stackoverflow.com/a/76876100 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. mt5.Close(symbol,ticket=ticket), and pay attention the ticket is a number not a string type.
Quote
Report
user3409500 · External communityPost link
External answer — Stack Overflow Stack Exchange Author: user3409500 Original post: https://stackoverflow.com/a/78875302 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. as above said .. You can Simply use mt5.Close(symbol,ticket) Such as this : import MetaTrader5 as mt5 # Initialize MetaTrader 5 mt5.initialize() # Get open positions positions = mt5.positions_get() # Close all open positions for position in positions: result = mt5.Close(symbol='XAUUSD.',ticket=position.ticket) if result != True: print(f"Failed to close position {position.ticket}: {result.comment}") else: print(f"Position {position.ticket} closed successfully") # Shut down MetaTrader 5 mt5.shutdown() Good Luck
Quote
Report

Post Reply

Quoted from Forex.com.bd-Editorial External answer — Stack Overflow Stack Exchange Author: user3409500 Source score (net votes, not local likes): 2 Original post: https://stackoverflow.com/a/78875302 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. as above said .. You can Simply use mt5.Close(symbol,ticket) Such as this : import MetaTrader5 as mt5 # Initialize MetaTrader 5 mt5.initialize() # Get open positions positions = mt5.positions_get() # Close all open positions for position in positions: result = mt5.Close(symbol='XAUUSD.',ticket=position.ticket) if result != True: print(f"Failed to close position {position.ticket}: {result.comment}") else: print(f"Position {position.ticket} closed successfully") # Shut down MetaTrader 5 mt5.shutdown() Good Luck

Cancel quote

Checking account access…