下面的 Python 模块会使用辅助函数 backtest 生成数据,以便对深度 Q 学习交易机器人进 行向量化回测。该代码在第 11 章中也使用过。 # # 交易机器人向量化回测 # (Q学习金融智能体) # # (c) Dr. Yves J. Hilpisch # Artificial Intelligence in Finance # import numpy as np import pandas as pd pd.set_option('mode.chained_assignment', None) def reshape(s, env): return np.reshape(s, [1, env.lags, env.n_features]) def backtest(agent, env): done = False env.data['p'] = 0 308 | 第 12 章 state = env.reset() while not done: action = np.argmax( agent.model.predict(reshape(state, env))[0, 0]) position = 1 if action == 1 else -1 env.data.loc[:, 'p'].iloc[env.bar] = position state, reward, done, info = env.step(action) env.data['s'] = env.data['p'] * env.data['r']
下面是带有 OandaTradingBot 类和部署该类的代码的 Python 脚本。 # # Oanda交易机器人 # 和部署代码 # # (c) Dr. Yves J. Hilpisch # Artificial Intelligence in Finance # import sys import tpqoa import pickle impo