I want to open an order, no problem with that, but if I want to close this order, I need the ticket number, ticket that I can't write manually, it will be given after the order is opened.
From the documentation, I've got this:
but I can't pass anything else than 0 to "position": 0
(line 20), otherwise it will not open the order.
Meantime, if position = 0, it will open an order, I'll get a position ticket from result.order
, then I have to manually copy it and paste it in the position
from the closing order function, like that it will close the order.
So, is there a way of not manually copying the ticket number after each opened order and paste it in the closing function? Or writing an unique ticket in advance for both, open and close, order?
Thank you in advance!
import MetaTrader5 as mt5
if not mt5.initialize():
print("initialize() failed, error code =",mt5.last_error())
quit()
symbol = "EURUSD"
lot = 0.1
point = mt5.symbol_info(symbol).point
price = mt5.symbol_info_tick(symbol).ask
deviation = 20
def buy():
request = {
"action": mt5.TRADE_ACTION_DEAL,
"symbol": symbol,
"volume": lot,
"type": mt5.ORDER_TYPE_BUY,
"price": price,
"position": 0, # can't pass anything else than 0 here, otherwise it will not open the order!
"deviation": deviation,
"magic": 234000,
"comment": "python script open",
"type_time": mt5.ORDER_TIME_GTC,
"type_filling": mt5.ORDER_FILLING_IOC,
}
# send a trading request
result = mt5.order_send(request)
# check the execution result
print("2. order_send done, ", result)
print(result.order)
mt5.shutdown()
quit()
def close():
close_request={ "action": mt5.TRADE_ACTION_DEAL,
"symbol": symbol,
"volume": lot,
"type": mt5.ORDER_TYPE_SELL,
"position": 129950610,
"price": price,
"deviation": deviation,
"magic": 0,
"comment": "python script op",
"type_time": mt5.ORDER_TIME_GTC,
"type_filling": mt5.ORDER_FILLING_IOC,
}
# send a close request
result=mt5.order_send(close_request)
print(result)
# buy()
# close()