使用 MT5 在 python 中得到 retcode=10021

问题描述

我正在尝试创建一个交易机器人来下订单,而不是从 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,"type": mt5.ORDER_TYPE_BUY,} 


    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,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
订单发送: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_PENDINGhttps://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,"type": mt5.ORDER_TYPE_BUY_LIMIT,## may delete some items
        "type_time": mt5.ORDER_TIME_GTC,} 

注意:"action": mt5.TRADE_ACTION_PENDING,
并且:"type": mt5.ORDER_TYPE_SELL_LIMIT,