AminHP / gym-mtsim

A general-purpose, flexible, and easy-to-use simulator alongside an OpenAI Gym trading environment for MetaTrader 5 trading platform (Approved by OpenAI Gym)
MIT License
412 stars 101 forks source link

Gymnasium support #40

Closed Alex2782 closed 5 months ago

Alex2782 commented 1 year ago

The render() function is not yet compatible with SB3 + Gymnasium. (https://github.com/DLR-RM/stable-baselines3/pull/1327)

Training works with: A2C, PPO, RecurrentPPO, TRPO (added gym-mtsim/examples/train_SB3_gymnasium.py)

Random actions vs. SB3 - Agents x [50K, 250K, 500K] learning_timesteps

random_sb3_agents

AminHP commented 10 months ago

Hi @Alex2782 , is this PR ready to review?

Alex2782 commented 10 months ago

The render function is not yet compatible with SB3 + Gymnasium. I don't know how. advanced_figure does not fit into the concept of gymnasium.

train_SB3_gymnasium.py (without render) shows only the training result at the end image

Alex2782 commented 10 months ago

@AminHP image

5 months ago I already changed your 'README.ipynb'. I find 'Jupyter Notebook' terrible. I can no longer track what I have customized and what changes are still needed.

AminHP commented 10 months ago

You could use VSCode for editing the notebooks. It highlights the changes and provides some other useful features.

AminHP commented 10 months ago

I'll check the render function.

Alex2782 commented 10 months ago

i just added new comment here #new line, VS code changes the whole file after save

image

my VS Code Extensions image

Alex2782 commented 10 months ago

ok the changes look like this, but you have to scroll a lot and look closely, is not so well solved.

image

The arrows to navigate through the changes do not work either image

AminHP commented 10 months ago

How about the Github changes page here?

Alex2782 commented 10 months ago

it is enough only if ipython is executed, then already very much changes at the structure. image

I have found my changes, but you have to search longer than usual, some changes are not comprehensible. image

AminHP commented 10 months ago

It seems that Gymnasium doesn't allow passing additional args to the render method. I managed to fix it by the below code, however, I don't know if it is a correct solution based on Gymnasium rules.

import numpy as np
import random
import torch
from stable_baselines3 import A2C

import gymnasium as gym
from gym_mtsim import (
    Timeframe, SymbolInfo,
    MtSimulator, OrderType, Order, SymbolNotFound, OrderNotFound,
    MtEnv,
    FOREX_DATA_PATH, STOCKS_DATA_PATH, CRYPTO_DATA_PATH, MIXED_DATA_PATH,
)

env_name = 'forex-hedge-v0'
env = gym.make(env_name)

# reproduce training and test
seed = 42
env.reset(seed=seed)
torch.manual_seed(seed)
random.seed(seed)
np.random.seed(seed)

model = A2C('MultiInputPolicy', env, verbose=0)
model.learn(total_timesteps=10000)

observation, info = env.reset(seed=seed)

while True:
    action, _states = model.predict(observation)
    observation, reward, terminated, truncated, info = env.step(action)
    done = terminated or truncated

    if done:
        break

env.unwrapped.render('advanced_figure', time_format='%Y-%m-%d')
Alex2782 commented 10 months ago

I think that is okay

https://gymnasium.farama.org/api/env/#gymnasium.Env.render Compute the render frames is not possible in project mtsim -> advanced_figure

AminHP commented 10 months ago

Great! Let's continue with this then. Thanks @Alex2782 for checking the docs.

AminHP commented 9 months ago

Hi @Alex2782 When do you think you will have the time to finish this PR?

Alex2782 commented 9 months ago

Hi @AminHP,

which changes exactly are still necessary? only adjust the examples?

AminHP commented 9 months ago

Yes, the examples should probably use the changes we discussed in the above code. It is also better to have them in ipynb format, like the one in anytrading.

Alex2782 commented 9 months ago

Hi @AminHP,

I have checked and adjusted your file 'README.ipynb'.

image

Note: without the first change this error comes, it may also be due to my python or numpy version. image

Alex2782 commented 9 months ago

I think the documentation would also have to be adapted, for example there is still the file README.md. Currently I have unfortunately no time to check it more closely.

My example train_SB3_gymnasium.py does not use 'render()'. In the 'matplotlib' output only one model comparison is shown: random actions, A2C and PPO.

AminHP commented 9 months ago

Thanks for the changes. I will update and revise the docs later.

The last change is providing the examples in ipynb format.

AminHP commented 9 months ago

Any updates @Alex2782 ?

Alex2782 commented 9 months ago

Should I remove the file 'train_SB3_gymnasium.py'? I think the 'README.ipynb' is enough. Currently I have no time to convert my example to ipynb.

AminHP commented 9 months ago

No, there is no need to remove the example. I will try to change it to ipynb version. The example is beneficial as it compares A2C and PPO.

Alex2782 commented 9 months ago

@AminHP https://github.com/AminHP/gym-anytrading/blob/master/examples/SB3_a2c_ppo.ipynb

I had 'train_SB3_gymnasium.py' also in the project 'gym-anytrading' to test 'gymnasium' support.

Snoup97 commented 5 months ago

hello ✋🏿 any updates on the merge of this feature?

AminHP commented 5 months ago

I have finally fixed the minor issues, updated the examples, and merged the PR :)) Thanks a lot @Alex2782 for your contribution.

Snoup97 commented 5 months ago

thanks guys, appreciate the effort 👍🏿