我对gym anytrading很陌生,我有这个python数据框,其中有一列包含不同长度的列表列表,我试图弄清楚如何将其放入gym anytrading环境中,下面是指向csv的链接数据帧中的示例数据和代码片段我不断收到此错误 TypeError: Cannot unpack non-iterable NoneType object
import gym
import pandas as pd
import numpy as np
from gym_anytrading.envs import TradingEnv
class CustomTradingEnv(TradingEnv):
def __init__(self, df):
super().__init__(df, window_size=10)
self.reward_range = (0, 1)
def _process_data(self):
# Process your DataFrame to ensure it's in the correct format
# Here, you can perform any necessary preprocessing steps
pass
def reset(self):
# Initialize the environment with the data from the DataFrame
self._process_data()
return super().reset()
env = CustomTradingEnv(df)
observation = env.reset()
for _ in range(100): # Run for 100 steps
action = env.action_space.sample() # Sample a random action
observation, reward, done, info = env.step(action)
if done:
break
https://docs.google.com/spreadsheets/d/1-LFNzZKXUG44smSYOy2rgVVnqiygLfs00lAl2vFdsxM/edit?usp=sharing
首先,您没有使用正确的
gym
套件:
import gym
需要
import gymnasium as gym
因为
gym_anytrading
也使用 gymnasium
(这与不再维护的旧版 gym
包略有不同)。
那么你的代码中的缩进是不正确的 - 我认为这只是一个错字。迭代应该是:
for _ in range(100): # Run for 100 steps
action = env.action_space.sample() # Sample a random action
observation, reward, done, info = env.step(action)
if done:
break
实际的错误来自
__init__
函数和 _process_data
函数。完整的回溯是:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "...", line 24, in test
env = CustomTradingEnv(df)
File "...", line 9, in __init__
super().__init__(df, window_size=10)
File "/.../anaconda3/envs/py310/lib/python3.10/site-packages/gym_anytrading/envs/trading_env.py", line 35, in __init__
self.prices, self.signal_features = self._process_data()
TypeError: cannot unpack non-iterable NoneType object
要解决此问题,您需要修改
process_data
,使其返回 numpy 数组的元组,prices
(1 维浮点数组)和 signal_features
(2 维浮点数组)。这在gym_anytrading README 中也有清楚的解释。
使用您的数据框,您可以执行以下操作:
def _process_data(self):
env = self.unwrapped
start = 0
end = len(env.df)
prices = env.df.loc[:, 'low'].to_numpy()[start:end]
signal_features = env.df.loc[:, ['close', 'open', 'high', 'low']].to_numpy()[start:end]
return prices, signal_features
当您这样做时,您的测试循环将正常工作,除了您没有提供任何奖励函数这一事实。自定义环境需要实现一个
_calculate_reward
函数,该函数为任何可能的操作返回奖励信号(在本例中只有两个)。
所以这应该类似于:
def _calculate_reward(self, action):
# I'm assuming 0 and 1 stand for sell and buy (or viceversa)
match action:
case 0: return 0.1 # or some other more suitable value
case 1: return -0.1 # or something more suitable
case _: raise Exception("bug")
还需要实现
_update_profit(self, action)
函数(它需要更新内部环境状态,self.unwrapped._total_profit
,但你也可以让它pass
)。
最后,代码中的
env.step
函数不正确(对于最新版本的gymnasium
)。它应该用作:
observation, reward, done, terminated, info = env.step(action)