使用 MT5 在 python 中获得 retcode=10021
got retcode=10021 in python with MT5
我正在尝试创建一个交易机器人来下订单,而不是从 MetaTrader5 应用程序手动下订单,我创建了以下功能
注意: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)
当我尝试将参数传递给这样的函数时:
MetaTrader_Order("GBPUSD", "SELL", 1.40001, 1.5, 1.3, 0.1)
它给我这样的输出:
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 Return 代码: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,
我正在尝试创建一个交易机器人来下订单,而不是从 MetaTrader5 应用程序手动下订单,我创建了以下功能
注意: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)
当我尝试将参数传递给这样的函数时:
MetaTrader_Order("GBPUSD", "SELL", 1.40001, 1.5, 1.3, 0.1)
它给我这样的输出:
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 Return 代码: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,