AminHP / gym-anytrading

The most simple, flexible, and comprehensive OpenAI Gym trading environment (Approved by OpenAI Gym)
MIT License
2.09k stars 459 forks source link

New version of gym ERROR: Environment stocks doesn't exist #93

Closed QuantumCrazy closed 10 months ago

QuantumCrazy commented 10 months ago

Hi Amin,

I recently upgraded by computer and had to re-install all my models including the Python packages. Gym and Gym-Anytrading were updated to the latest versions, I have: gym version 0.26.2 gym-anytrading version 2.0.0 gymnasium version 0.29.1

When I run my models I get an error where the environments do not exist.

Old system was working had: gym version 0.19.0 gym-anytrading version 1.2.0

I downgraded gym to version 0.22.0 and gym-anytrading to version 1.2.0. I keep getting the following errors:

'init() got an unexpected keyword argument 'unit-side' 'gym.error.NameNotFound: Environment stocks doesn't exist'

Do you have any idea how to fix this? Something is going on where the gym environments are not getting recognized. What versions are compatible?

AminHP commented 10 months ago

Could you provide a minimal code to reproduce the error?

QuantumCrazy commented 10 months ago

I'll try not to be verbose and relating to gym only:

import gym
import gym_anytrading
from gym_anytrading.envs import StocksEnv, ForexEnv

.......

env = gym.make('stocks-v0', df=df, window_size=testing_window, frame_bound=(testing_lower_bound,testing_upper_bound), unit_side='right')

The line above is where the problem arises. When making the gym environment, the environment type = stocks-v0 is not recognized. Neither is forex-v0.

AminHP commented 10 months ago

Please try import gymnasium as gym

QuantumCrazy commented 10 months ago
import gym
import gymnasium as gym
import gym_anytrading
from gym_anytrading.envs import StocksEnv, ForexEnv
import gymnasium as gym
import gym_anytrading
from gym_anytrading.envs import StocksEnv, ForexEnv

I tried the above imports, getting same error. On the second one, I removed gym and tried only gymnasium and gym_anytrading, nothing still.

QuantumCrazy commented 10 months ago

Few years ago when I was running the system, I only had gym and gym_anytrading packages installed. Recently however, I noticed gymnasium was installed by default either in Python or a new version of gym. And this package might be clashing

QuantumCrazy commented 10 months ago

This is turning into one of those headaches where new package updates mess things up working before with older package versions. I might have to downgrade across the board, not sure what else I can do

QuantumCrazy commented 10 months ago

Amin, before I downgraded packages across the board I tried once more, import gymnasium as gym as you advised, the training runs but the model cannot save due to a new error

        while True:
            obs = obs[np.newaxis, ...]
            action, _states = model.predict(obs, deterministic=True)
            obs, rewards, done, info = env.step(action)

That line -->> obs = obs[np.newaxis, ...] produces the error

TypeError: tuple indices must be integers or slices, not tuple

Any ideas? This did not happen before with old packages

AminHP commented 10 months ago

This code works with the older versions of stable-baselines. Installing the latest version of stable-baselines3 should fix your issue. Some useful examples are also available here.

QuantumCrazy commented 10 months ago

Hi Amin, its been a frustrating journey for me but I finally solved my issue. This issue I believe is related to the issue I flagged before where I noticed performance of RL models varies on hardware specs on the machine...

So anyway, I ended up downgrading the following packages to versions I had on my old machine:

pip install --upgrade gym==0.22.0
pip install --upgrade gym_anytrading==1.2.0
pip install --upgrade torch==1.9.0
pip install --upgrade stable_baselines3==1.1.0
pip install --upgrade scikit-learn==0.24.2

Then I added this line of code to my machine learning script. I tested it both during training and loading saved models.

os.environ['CUDA_VISIBLE_DEVICES'] = '-1'

I mentioned I bought a new machine. Its NVDIA GPU came with CUDA drivers. I noticed during training it defaults to CUDA device but on my old machine I was using CPU device. This caused various problems with the code because the old models were saved by another machine running on another CPU device and the observation data was not loading properly. I had to re-train with the new CPU device, save new models and load them again. This is why performance cannot be replicated across different devices (in my experience at least). So the code above forces the machine to use CPU instead of CUDA drivers. There is an argument about whether CUDA (GPU) or CPU is better for ML algos. I am forcing CPU because my machine is not a gaming machine with very good GPU, instead i'm relying on its powerful CPU.

I'll close off this bug, thanks for your help.