Hi,
how can I open an opposite position with ORDER_TYPE_CLOSE_BY using Python?
Error is always 13200 failed cancel order #0 buy 0 at market [Invalid request] regardless of what inputs I with the request send.
According to the mql5 documentation I need request.position_by=PositionGetInteger(POSITION_TICKET)
However in the python module, the function PositionGetInteger doesnt exist!
Using a random number or converting the position_ticket into int() doesnt work apparently.
And let's say there is a way to use it...how to combine it with Pending Stop-Orders or Stop Loss?
So what I actually want to do is, once a position hits the StopLoss, I want to open an opposite position.
I know, I could just close one position with a StopLoss and have another opposite pending Stopp-Order ready on the same price, but I want to save the spread (if it is possible like suggested on hedging accounts).
You cannot "open" a position using closeby. Closeby is to be used when you already have offsetting orders open on a hedging account and you need to reconcile them as one order closing other.
import MetaTrader5 as mt5 import pprint def closeby_all(symbol, **kwargs): results = kwargs.get('results', []) positions = mt5.positions_get(symbol=symbol) it = iter(positions) try: first_position = next(it) second_position = next(p for p in it if p.type != first_position.type) except StopIteration: return (True, results) res = mt5.order_send(dict( action=mt5.TRADE_ACTION_CLOSE_BY, position=first_position.identifier, position_by=second_position.identifier )) results.append(res) if res.retcode == mt5.TRADE_RETCODE_DONE: return closeby_all(symbol, results=results) else: return (False, results) def main(): account = mt5.account_info() assert account.margin_mode == mt5.ACCOUNT_MARGIN_MODE_RETAIL_HEDGING symbol = mt5.symbol_info('EURUSD') tick = mt5.symbol_info_tick(symbol.name) buy_req = { 'action' : mt5.TRADE_ACTION_DEAL, 'type' : mt5.ORDER_TYPE_BUY, 'symbol' : 'EURUSD', 'volume' : 0.1, 'price' : tick.ask, 'deviation': 200, } sell_req = { **buy_req, 'type' : mt5.ORDER_TYPE_SELL, 'price': tick.bid } for order in [buy_req, sell_req]: mt5.order_send(order) is_done, trade_results = closeby_all(symbol.name) for res in trade_results: pprint.pp(res._asdict()) if __name__ == '__main__': try: assert mt5.initialize(), f'Failed to initialized: {mt5.last_error()}' main() finally: mt5.shutdown()

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi,
request = { "action": mt5.ORDER_TYPE_CLOSE_BY, "symbol": 'USDEUR', "volume": 2, 'position': 123456, 'position_by':???, # "price": 0.9, "sl":0.9, } } result = mt5.order_send(request)
how can I open an opposite position with ORDER_TYPE_CLOSE_BY using Python?
Error is always 13200 failed cancel order #0 buy 0 at market [Invalid request] regardless of what inputs I with the request send.
According to the mql5 documentation I need request.position_by=PositionGetInteger(POSITION_TICKET)
However in the python module, the function PositionGetInteger doesnt exist!
Using a random number or converting the position_ticket into int() doesnt work apparently.
And let's say there is a way to use it...how to combine it with Pending Stop-Orders or Stop Loss?
So what I actually want to do is, once a position hits the StopLoss, I want to open an opposite position.
I know, I could just close one position with a StopLoss and have another opposite pending Stopp-Order ready on the same price, but I want to save the spread (if it is possible like suggested on hedging accounts).