【问题标题】:MT5 opening order returns "None"MT5 开仓单返回“无”
【发布时间】:2020-12-30 04:46:37
【问题描述】:

我是新手,正在尝试为 MT5 构建一个 python 机器人。我试图通过 python 在 MT5 中发送交易,但它总是返回“None”。

我已经输入了所有必需的信息,可能是因为小数错误?

import os
from datetime import datetime
import MetaTrader5 as mt5
import requests
import datetime as dt  # for dealing with times
import numpy as np
import json
import pandas as pd
import pytz

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

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 = mt5.symbol_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


    action_request = {
        "action": mt5.TRADE_ACTION_DEAL,
        "symbol": symbol,
        "volume": lot,
        "type": trade_type,
        "price": price,
        "sl": round(price - sl_points * point,5),
        "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(action_request)
    return result, action_request

def close_trade(action, action_request, result, deviation):
    '''https://www.mql5.com/en/docs/integration/python_metatrader5/mt5ordersend_py
    '''
    # create a close request
    symbol = action_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 = action_request['volume']

    action_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(action_request)

currency_pair = "GBPUSD"


# establish connection to MetaTrader 5 terminal
if not mt5.initialize(login=31706337, server="MetaQuotes-Demo",password="mzhjm8hi"):
    print("initialize() failed, error code =", mt5.last_error())
    quit()


trade = open_trade('buy', currency_pair, 10, 500, 2000, 10)
print(trade)

结果: C:\Users\andyq\PycharmProjects\BinanceTrading\venv\Scripts\python.exe "C:/Users/andyq/PycharmProjects/BinanceTrading/数据准备.py"

(None, {'action': 1, 'symbol': 'GBPUSD', 'volume': 10, 'type': 0, 'price': 1.26224, 'sl': 1.25724, 'tp': 1.28224, 'deviation': 10, 'magic': 9986989, 'comment': 'sent by python', 'type_time': 0, 'type_filling': 2})

进程以退出代码 0 结束

【问题讨论】:

标签: python metatrader5


【解决方案1】:

volume 应该是 Double 所以:

'volume': 10

应该是:

'volume': 10.0

【讨论】:

  • 这为我节省了很多时间。赞一个!
  • 谢谢没注意
【解决方案2】:

我遇到了同样的问题,要解决这个问题,您应该检查请求字典中的所有数据类型。 交易量、止损、止盈和价格应该是浮动值,并记住订单应该是整数

【讨论】:

  • 请提供有助于 OP 了解需要做什么的代码。
猜你喜欢
  • 2022-11-18
  • 1970-01-01
  • 2017-03-11
  • 2023-01-12
  • 2022-11-09
  • 2022-12-05
  • 2018-02-24
  • 2021-03-09
  • 2016-12-18
相关资源
最近更新 更多