[英]MT5/Metatrader 5 connect to different MT5 terminals using python
[英]got retcode=10021 in python with MT5
我正在嘗試創建一個交易機器人來下訂單而不是從 MetaTrader5 應用程序手動下訂單,我在下面制作了 function
注:BoS:買入或賣出 | sl: 止損 | tp: 獲利 | 批號:體積
def MetaTrader_Order(symbol, BoS, price, sl, tp, lot):
# connect to MetaTrader 5
if not mt5.initialize():
print("initialize() failed")
mt5.shutdown()
# prepare the buy request structure
symbol_info = mt5.symbol_info(symbol)
if symbol_info is None:
print(symbol, " not found, can not call order_check()")
# if the symbol is unavailable in MarketWatch, add it
if not symbol_info.visible:
print(symbol, " is not visible, trying to switch on")
if not mt5.symbol_select(symbol,True):
print("symbol_select({}) failed, exit",symbol)
deviation = 20
if BoS == "SELL":
request = {
"action": mt5.TRADE_ACTION_DEAL,
"symbol": symbol,
"volume": lot,
"type": mt5.ORDER_TYPE_SELL,
"price": price,
"sl": sl,
"tp": tp,
"deviation": deviation,
"magic": 234000,
"comment": "python script open",
"type_time": mt5.ORDER_TIME_GTC,
"type_filling": mt5.ORDER_FILLING_RETURN,
}
elif BoS == "BUY":
request = {
"action": mt5.TRADE_ACTION_DEAL,
"symbol": symbol,
"volume": lot,
"type": mt5.ORDER_TYPE_BUY,
"price": price,
"sl": sl,
"tp": tp,
"deviation": deviation,
"magic": 234000,
"comment": "python script open",
"type_time": mt5.ORDER_TIME_GTC,
"type_filling": mt5.ORDER_FILLING_RETURN,
}
else:
request = None
# send a trading request
result = mt5.order_send(request)
# check the execution result
print("1. order_send(): {} {} at price {}, with lot {}, with deviation {}".format(BoS, symbol, price, lot, deviation))
if result.retcode != mt5.TRADE_RETCODE_DONE:
print("2. order_send failed, retcode={}".format(result.retcode))
# request the result as a dictionary and display it element by element
result_dict=result._asdict()
for field in result_dict.keys():
print(" {}={}".format(field,result_dict[field]))
# if this is a trading request structure, display it element by element as well
if field=="request":
traderequest_dict=result_dict[field]._asdict()
for tradereq_filed in traderequest_dict:
print(" traderequest: {}={}".format(tradereq_filed,traderequest_dict[tradereq_filed]))
else:
print("2. order_send done, ", result)
當我嘗試像這樣將 arguments 傳遞給 function 時:
MetaTrader_Order("GBPUSD", "SELL", 1.40001, 1.5, 1.3, 0.1)
它給了我一個 output 像這樣:
1. order_send(): SELL GBPUSD at price 1.40001, with lot 0.01, with deviation 20
2. order_send failed, retcode=10021
retcode=10021
deal=0
order=0
volume=0.0
price=0.0
bid=0.0
ask=0.0
comment=No prices
request_id=0
retcode_external=0
request=TradeRequest(action=1, magic=234000, order=0, symbol='GBPUSD', volume=0.01, price=1.40001, stoplimit=0.0, sl=1.5, tp=1.3, deviation=20, type=1, type_filling=2, type_time=0, expiration=0, comment='python script open', position=0, position_by=0)
traderequest: action=1
traderequest: magic=234000
traderequest: order=0
traderequest: symbol=GBPUSD
traderequest: volume=0.01
traderequest: price=1.40001
traderequest: stoplimit=0.0
traderequest: sl=1.5
traderequest: tp=1.3
traderequest: deviation=20
traderequest: type=1
traderequest: type_filling=2
traderequest: type_time=0
traderequest: expiration=0
traderequest: comment=python script open
traderequest: position=0
traderequest: position_by=0
如果我嘗試像這樣自動設置價格: price = mt5.symbol_info_tick(symbol).ask
它可以正常工作並將訂單發送到 MetaTrader5 沒有問題
但實際上我不會以我輸入的特定價格發送訂單,而不是市場價格
.
.
.
MT5 文檔: https://www.mql5.com/en/docs/integration/python_metatrader5 MT5
order_send: https://www.mql5.com/en/docs/integration/python_metatrader5/mt5ordersend_py
MT5 返回代碼: https://www.mql5.com/en/docs/constants/errorswarnings/enum_trade_return_codes
另請參閱此頁面上的TRADE_ACTION_PENDING
:https://www.mql5.com/en/docs/constants/structures/mqltraderequest (它是另一種語言)
我通過編寫這部分代碼解決了這個問題:
if BoS == "SELL":
if price < mt5.symbol_info_tick(symbol).bid:
price = mt5.symbol_info_tick(symbol).bid
request = {
"action": mt5.TRADE_ACTION_PENDING,
"symbol": symbol,
"volume": lot,
"type": mt5.ORDER_TYPE_SELL_LIMIT,
"price": price,
"sl": sl,
"tp": tp,
"deviation": deviation,
"magic": 234000,
"comment": "python script open",
"type_time": mt5.ORDER_TIME_GTC,
"type_filling": mt5.ORDER_FILLING_RETURN,
}
elif BoS == "BUY":
if price > mt5.symbol_info_tick(symbol).ask:
price = mt5.symbol_info_tick(symbol).ask
request = {
"action": mt5.TRADE_ACTION_PENDING,
"symbol": symbol,
"volume": lot,
"type": mt5.ORDER_TYPE_BUY_LIMIT,
"price": price,
"sl": sl,
"tp": tp,
"deviation": deviation,
"magic": 234000,
"comment": "python script open", ## may delete some items
"type_time": mt5.ORDER_TIME_GTC,
"type_filling": mt5.ORDER_FILLING_RETURN,
}
注意: "action": mt5.TRADE_ACTION_PENDING,
並且: "type": mt5.ORDER_TYPE_SELL_LIMIT,
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.