【问题标题】:sending order to open a position via MetaTrader5 python module and nothing happens通过 MetaTrader5 python 模块发送订单开仓,没有任何反应
【发布时间】:2021-03-02 09:51:58
【问题描述】:

我关注了Metatrader5 python documentationthis 在堆栈溢出中的回答

我尝试开仓:

import MetaTrader5 as mt5


ea_magic_number = 9986989 # if you want to give every bot a unique identifier

def get_info(symbol):
    '''https://www.mql5.com/en/docs/integration/python_metatrader5/mt5symbolinfo_py
    '''
    # get symbol properties
    info=mt5.symbol_info(symbol)
    return info

def open_trade(action, symbol, lot, sl_points, tp_points, deviation):
    '''https://www.mql5.com/en/docs/integration/python_metatrader5/mt5ordersend_py
    '''
    # prepare the buy request structure
    symbol_info = get_info(symbol)

    if action == 'buy':
        trade_type = mt5.ORDER_TYPE_BUY
        price = mt5.symbol_info_tick(symbol).ask
    elif action =='sell':
        trade_type = mt5.ORDER_TYPE_SELL
        price = mt5.symbol_info_tick(symbol).bid
    point = mt5.symbol_info(symbol).point

    buy_request = {
        "action": mt5.TRADE_ACTION_DEAL,
        "symbol": symbol,
        "volume": lot,
        "type": trade_type,
        "price": price,
        "sl": price - sl_points * point,
        "tp": price + tp_points * point,
        "deviation": deviation,
        "magic": ea_magic_number,
        "comment": "sent by python",
        "type_time": mt5.ORDER_TIME_GTC, # good till cancelled
        "type_filling": mt5.ORDER_FILLING_RETURN,
    }
    # send a trading request
    result = mt5.order_send(buy_request)        
    return result, buy_request 

def close_trade(action, buy_request, result, deviation):
    '''https://www.mql5.com/en/docs/integration/python_metatrader5/mt5ordersend_py
    '''
    # create a close request
    symbol = buy_request['symbol']
    if action == 'buy':
        trade_type = mt5.ORDER_TYPE_BUY
        price = mt5.symbol_info_tick(symbol).ask
    elif action =='sell':
        trade_type = mt5.ORDER_TYPE_SELL
        price = mt5.symbol_info_tick(symbol).bid
    position_id=result.order
    lot = buy_request['volume']

    close_request={
        "action": mt5.TRADE_ACTION_DEAL,
        "symbol": symbol,
        "volume": lot,
        "type": mt5.ORDER_TYPE_SELL,
        "position": position_id,
        "price": price,
        "deviation": deviation,
        "magic": ea_magic_number,
        "comment": "python script close",
        "type_time": mt5.ORDER_TIME_GTC, # good till cancelled
        "type_filling": mt5.ORDER_FILLING_RETURN,
    }
    # send a close request
    result=mt5.order_send(close_request)


# This is how I would execute the order
result, buy_request = open_trade('buy', 'USDJPY', 0.1, 50, 50, 10)
close_trade('sell', buy_request, result, 10)

没有任何反应,应用程序终端也没有反应。我还检查了 Metatrader5 中的交易和历史部分以查找一些相关信息,但我什么也没找到。

如何监控 Metatrader5 中的日志以调试代码并解决问题?

【问题讨论】:

    标签: python python-3.x debugging mql5 metatrader5


    【解决方案1】:

    在 MetaTrader 中,必须开启算法交易才能建立买入/卖出头寸。

    【讨论】:

      【解决方案2】:

      仅适用于达到此主题并且算法交易按钮已打开且发送订单时没有发生任何事情的人。

      就我而言,这是因为我将请求的“音量”设置为整数,并且它必须是浮点数。没有显示错误,没有生命迹象, order_send(...) 返回 None 就是这样。只需将传入的值转换为浮动即可。

      这不是问题作者的情况,但我希望它对将来有冒险精神的人有所帮助。

      【讨论】:

        猜你喜欢
        • 2021-08-04
        • 2022-01-20
        • 1970-01-01
        • 2015-06-05
        • 2010-12-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-23
        相关资源
        最近更新 更多