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

Example seems to be broken #68

Closed aheagel closed 2 years ago

aheagel commented 2 years ago

It seems that the example code at https://github.com/AminHP/gym-anytrading#a-complete-example is broken. Running the code returns: Perhaps I've missed something?

AssertionError: The observation returned by the `reset()` method is not contained with the 
observation space (Box(-inf, inf, (10, 2), float32))
import gym
import gym_anytrading
from gym_anytrading.envs import TradingEnv, ForexEnv, StocksEnv, Actions, Positions 
from gym_anytrading.datasets import FOREX_EURUSD_1H_ASK, STOCKS_GOOGL
import matplotlib.pyplot as plt

env = gym.make('forex-v0', frame_bound=(50, 100), window_size=10)
# env = gym.make('stocks-v0', frame_bound=(50, 100), window_size=10)

observation = env.reset()
while True:
    action = env.action_space.sample()
    observation, reward, done, info = env.step(action)
    # env.render()
    if done:
        print("info:", info)
        break

plt.cla()
env.render_all()
plt.show()
aheagel commented 2 years ago

After debugging for a while I found out that the reset function needs to be in float32 whereas the returned value was float64. Hence changing the observation_space from https://github.com/AminHP/gym-anytrading/blob/09108ebb1f75c548b7987dbd4b493b70f1c6f7cb/gym_anytrading/envs/trading_env.py#L37

to

self.observation_space = spaces.Box(low=-np.inf, high=np.inf, shape=self.shape, dtype=np.float64)

seems to solve the problem in my case

AminHP commented 2 years ago

Hi @aheagel ,

It works for me too. Thanks for the fix. Do you want to create a pull request?

aheagel commented 2 years ago

Hi @aheagel ,

It works for me too. Thanks for the fix. Do you want to create a pull request?

I'm still very new to this community and I haven't yet figured out how to use git. So i think it is better if you do it in case i mess up.

AminHP commented 2 years ago

Alright. As you wish :)