gpt4 book ai didi

python - MT5开单返回 "None"

转载 作者:行者123 更新时间:2023-12-04 09:30:48 26 4
gpt4 key购买 nike

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

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

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/data preparation.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

最佳答案

volume 应该是 Double所以:

'volume': 10

应该是:

'volume': 10.0

关于python - MT5开单返回 "None",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62857379/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com