I am very new to gym anytrading i have this pandas dataframe where there is a column with a list of lists that are different lengths I am trying to figure out how to put that into the gym anytrading environment. Below is a link to a csv of the sample data in the dataframe and a code snippet. I keep getting this error 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
First of all, you are not using the right
gympackage:needs to be
since
gym_anytradingalso usesgymnasium(which is subtly different from the no-longer-maintained, oldergympackage).Then the indentation in your code is incorrect - I assume this is just a typo. The iteration should be:
The actual error is coming from the
__init__function and the_process_datafunction. The full traceback is:To fix this you need to modify
process_dataso that it returns a tuple of numpy arrays,prices(a 1-dim array of floats) andsignal_features(a 2-dim array). This is also clearly explained in the gym_anytrading README. With your dataframe you could do something like:When you do that your test loop will work fine, apart from the fact that you didn't provide any reward function. A custom env needs to implement a
_calculate_rewardfunction that returns a reward signal for any of the possible actions (in this case there are only two). So that should look similar to:Also the
_update_profit(self, action)function needs to be implemented (it needs to update the internal env state,self.unwrapped._total_profit, but you can let itpassas well).Finally, the
env.stepfunction in your code is incorrect (for the latest version ofgymnasium). It should be used as: