期货量化交易软件:如何在 MQL5 中使用 ONNX 模型
1. 构建模型
Python 提供了一组专门的函数库,因此它提供了处理机器学习模型的广泛功能。 函数库极大地方便了数据的准备和处理。 我们建议使用 GPU 资源来最大限度地提高机器学习项目的效率。 许多 Windows 用户在尝试安装当前的 TensorFlow 版本时遇到了问题(请参阅视频指南,及其文本版本的注释)。 故此,我们已经测试了TensorFlow 2.10.0,并建议使用此版本。 GPU 计算是在 NVIDIA GeForce RTX 2080 Ti 图形卡上使用 CUDA 11.2 和 CUDNN 8.1.0.7 函数库进行的。

1.1. 安装 Python 和函数库如果您还没有 Python,请您先安装它。 我们使用的是 3.9.16 版。 另外,还要安装函数库(如果您使用的是 Conda/Anaconda,请在 Anaconda 提示符下运行以下命令): python.exe -m pip install --upgrade pip pip install --upgrade pandas pip install --upgrade scikit-learn pip install --upgrade matplotlib pip install --upgrade tqdm pip install --upgrade metatrader5 pip install --upgrade onnx==1.12 pip install --upgrade tf2onnx pip install --upgrade tensorflow==2.10.0 1.2. 检查 TensorFlow 版本和 GPU下面的代码检查已安装的 TensorFlow 版本,并验证是否可以使用 GPU 来计算模型: #check tensorflow version print(tf.__version__) #check GPU support print(len(tf.config.list_physical_devices('GPU'))>0) 如果正确安装了所需的版本,您将看到以下结果: 2.10.0 True 我们使用 Python 脚本来构建和训练模型。 下面简要介绍此过程的步骤。1.3. 构建和训练模型该脚本首先导入将在模型中使用的 Python 函数库。 #Python libraries import matplotlib.pyplot as plt import MetaTrader5 as mt5 import tensorflow as tf import numpy as np import pandas as pd import tf2onnx from sklearn.model_selection import train_test_split from sys import argv 检查 TensorFlow 版本和 GPU 可用性: #check tensorflow version print(tf.__version__) 2.10.0 #check GPU support print(len(tf.config.list_physical_devices('GPU'))>0) True 初始化赫兹量化 以执行来自 Python 的操作: #initialize 赫兹量化 for history data if not mt5.initialize(): print("initialize() failed, error code =",mt5.last_error()) quit() 有关 赫兹量化 终端的信息: #show terminal info terminal_info=mt5.terminal_info() print(terminal_info) TerminalInfo(community_account=True, community_connection=True, connected=True, dlls_allowed=False, trade_allowed=False, tradeapi_disabled=False, email_enabled=False, ftp_enabled=False, notifications_enabled=False, mqid=False, build=3640, maxbars=100000, codepage=0, ping_last=58768, community_balance=1.0, retransmission=0.015296317559440137, company='MetaQuotes Software Corp.', name='MetaTrader 5', language='English', path='C:\\Program Files\\MetaTrader 5', data_path='C:\\Users\\user\\AppData\\Roaming\\MetaQuotes\\Terminal\\D0E8209F77C8CF37AD8BF550E51FF075', commondata_path='C:\\Users\\user\\AppData\\Roaming\\MetaQuotes\\Terminal\\Common') #show file path file_path=terminal_info.data_path+"\\MQL5\\Files\\" print(file_path) C:\Users\user\AppData\Roaming\MetaQuotes\Terminal\D0E8209F77C8CF37AD8BF550E51FF075\MQL5\Files\ 打印模型(在此示例中,脚本在 Jupyter 笔记本中运行)的保存路径: #data path to save the model data_path=argv[0] last_index=data_path.rfind("\\")+1 data_path=data_path[0:last_index] print("data path to save onnx model",data_path) data path to save onnx model C:\Users\user\AppData\Roaming\Python\Python39\site-packages\ 准备请求历史数据的日期。 在我们的示例中,我们请求从当前日期开始 120 根 EURUSD H1 的柱线: #set start and end dates for history data from datetime import timedelta,datetime end_date = datetime.now() start_date = end_date - timedelta(days=120) #print start and end dates print("data start date=",start_date) print("data end date=",end_date) data start date= 2022-11-28 12:28:39.870685 data end date= 2023-03-28 12:28:39.870685 请求 EURUSD 历史数据: #get EURUSD rates (H1) from start_date to end_date eurusd_rates = mt5.copy_rates_range("EURUSD", mt5.TIMEFRAME_H1, start_date, end_date) 输出下载的数据: #check print(eurusd_rates)

#create dataframe df = pd.DataFrame(eurusd_rates) 显示数据帧的开始和结束: #show dataframe head df.head()

#show dataframe tail df.tail()

#show dataframe shape (the number of rows and columns in the data set) df.shape