如何正确关闭已打开的订单?(发送订单时不能将票号传递给“位置”)
我想开一个订单,没问题,但是如果我想关闭这个订单,我需要票号,票我不能手动写,开单后会给。
从文档中,我得到了这个:
但我不能将 0 以外的任何东西传递给"position": 0(第 20 行),否则它不会打开订单。
同时,如果 position = 0,它将打开一个订单,我将从 中获得一个持仓单result.order,然后我必须手动将其复制并粘贴到position来自关闭订单功能中,就像它将关闭订单一样。
那么,有没有一种方法可以不在每个打开的订单后手动复制票号并将其粘贴到关闭功能中?或者提前为打开和关闭订单写一张独特的票?
先感谢您!
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()