### Install gym-mtsim Source: https://github.com/aminhp/gym-mtsim/blob/main/README.md Installs the gym-mtsim library via pip or directly from the repository. Dependencies like stable-baselines3 are also mentioned for example execution. ```bash pip install gym-mtsim ``` ```bash git clone https://github.com/AminHP/gym-mtsim cd gym-mtsim pip install -e . ``` ```bash pip install --upgrade --no-deps --force-reinstall https://github.com/AminHP/gym-mtsim/archive/main.zip ``` -------------------------------- ### Train and Test Execution Example Source: https://github.com/aminhp/gym-mtsim/blob/main/examples/SB3_a2c_ppo.ipynb Demonstrates the execution of the `train_test_model` function. It sets up the environment, defines parameters like seed and number of episodes, runs the simulation with random actions, prints the resulting statistics, and prepares data for plotting. ```python seed = 2024 # random seed total_num_episodes = 50 print ("env_name :", env_name) print ("seed :", seed) # INIT matplotlib plot_settings = {} plot_data = {'x': [i for i in range(1, total_num_episodes + 1)]} # Random actions model = None total_learning_timesteps = 0 rewards = train_test_model(model, env, seed, total_num_episodes, total_learning_timesteps) min_value, avg_value, max_value = print_stats(rewards) class_name = f'Random actions' label = f'Avg. {avg_value:>7.2f} : {class_name}' plot_data['rnd_rewards'] = rewards plot_settings['rnd_rewards'] = {'label': label} learning_timesteps_list_in_K = [25] # learning_timesteps_list_in_K = [50, 250, 500] # learning_timesteps_list_in_K = [500, 1000, 3000, 5000] ``` -------------------------------- ### Complete Example with Stable-Baselines Source: https://github.com/aminhp/gym-mtsim/blob/main/README.md Demonstrates a complete example of using the gym-mtsim environment with the stable-baselines library. It sets up the environment, trains an A2C model, and then uses the trained model to interact with the environment. The final state is rendered in advanced_figure mode. ```python 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, ) from stable_baselines3 import A2C from stable_baselines3.common.vec.env import DummyVecEnv import random import numpy as np import torch env_name = 'forex-hedge-v0' # reproduce training and test seed = 2024 random.seed(seed) np.random.seed(seed) torch.manual_seed(seed) env = gym.make(env_name) model = A2C('MultiInputPolicy', env, verbose=0) model.learn(total_timesteps=1000) 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') ``` -------------------------------- ### Create Default Forex Hedge Environment Source: https://github.com/aminhp/gym-mtsim/blob/main/README.md Initializes a default Forex hedging environment using gymnasium. This is a quick way to get started with a pre-configured trading environment. ```python import gymnasium as gym import gym_mtsim env = gym.make('forex-hedge-v0') ``` -------------------------------- ### Place Orders and Get State Source: https://github.com/aminhp/gym-mtsim/blob/main/README.md This code segment illustrates how to place buy and sell orders on specific currency pairs with defined volumes and fees. It also shows how to advance the simulation time using the tick method and retrieve the current state of the simulator, including balance, equity, margin, and open orders. ```python sim.current_time = datetime(2021, 8, 30, 0, 17, 52, tzinfo=pytz.UTC) order1 = sim.create_order( order_type=OrderType.Buy, symbol='GBPCAD', volume=1., fee=0.0003, ) sim.tick(timedelta(days=2)) order2 = sim.create_order( order_type=OrderType.Sell, symbol='USDJPY', volume=2., fee=0.01, ) sim.tick(timedelta(days=5)) state = sim.get_state() print( f"balance: {state['balance']}, equity: {state['equity']}, margin: {state['margin']} " f"free_margin: {state['free_margin']}, margin_level: {state['margin_level']} ") state['orders'] ``` -------------------------------- ### Close Orders and Get Final State Source: https://github.com/aminhp/gym-mtsim/blob/main/README.md This section demonstrates how to close all open orders in the simulator and then retrieve the final state. It shows the impact of closing orders on the simulator's balance, equity, margin, and free margin. The output includes a table detailing the closed orders. ```python order1_profit = sim.close_order(order1) order2_profit = sim.close_order(order2) # alternatively: # for order in sim.orders: # sim.close_order(order) state = sim.get_state() print( f"balance: {state['balance']}, equity: {state['equity']}, margin: {state['margin']} " f"free_margin: {state['free_margin']}, margin_level: {state['margin_level']} ") state['orders'] ``` -------------------------------- ### Environment Creation Source: https://github.com/aminhp/gym-mtsim/blob/main/examples/SB3_a2c_ppo.ipynb Initializes a Gymnasium environment for trading simulations. The code allows selection from various market types (forex, stocks, crypto, mixed) and strategies (hedge, unhedge) by uncommenting the desired environment name. ```python # env_name = 'forex-hedge-v0' env_name = 'stocks-hedge-v0' # env_name = 'crypto-hedge-v0' # env_name = 'mixed-hedge-v0' # env_name = 'forex-unhedge-v0' # env_name = 'stocks-unhedge-v0' # env_name = 'crypto-unhedge-v0' # env_name = 'mixed-unhedge-v0' env = gym.make(env_name) ``` -------------------------------- ### Initialize MtSimulator and Load/Download Data Source: https://github.com/aminhp/gym-mtsim/blob/main/README.md This snippet demonstrates how to initialize the MtSimulator with custom parameters such as unit, balance, leverage, stop-out level, and hedging. It also shows how to load existing forex data or download and save new data if it's not available. ```python import pytz from datetime import datetime, timedelta from gym_mtsim import MtSimulator, OrderType, Timeframe, FOREX_DATA_PATH sim = MtSimulator( unit='USD', balance=10000., leverage=100., stop_out_level=0.2, hedge=False, ) if not sim.load_symbols(FOREX_DATA_PATH): sim.download_data( symbols=['EURUSD', 'GBPCAD', 'GBPUSD', 'USDCAD', 'USDCHF', 'GBPJPY', 'USDJPY'], time_range=( datetime(2021, 5, 5, tzinfo=pytz.UTC), datetime(2021, 9, 5, tzinfo=pytz.UTC) ), timeframe=Timeframe.D1 ) sim.save_symbols(FOREX_DATA_PATH) ``` -------------------------------- ### Create Custom Forex Hedge Environment Source: https://github.com/aminhp/gym-mtsim/blob/main/README.md Creates a custom trading environment with a specified simulator configuration, trading symbols, window size, thresholds, fees, and multiprocessing settings. This allows for fine-grained control over the trading simulation. ```python import pytz from datetime import datetime, timedelta import numpy as np from gym_mtsim import MtEnv, MtSimulator, FOREX_DATA_PATH sim = MtSimulator( unit='USD', balance=10000., leverage=100., stop_out_level=0.2, hedge=True, symbols_filename=FOREX_DATA_PATH ) env = MtEnv( original_simulator=sim, trading_symbols=['GBPCAD', 'EURUSD', 'USDJPY'], window_size=10, # time_points=[desired time points ...], hold_threshold=0.5, close_threshold=0.5, fee=lambda symbol: { 'GBPCAD': max(0., np.random.normal(0.0007, 0.00005)), 'EURUSD': max(0., np.random.normal(0.0002, 0.00003)), 'USDJPY': max(0., np.random.normal(0.02, 0.003)), }[symbol], symbol_max_orders=2, multiprocessing_processes=2 ) ``` -------------------------------- ### Import Libraries Source: https://github.com/aminhp/gym-mtsim/blob/main/examples/SB3_a2c_ppo.ipynb Imports necessary libraries for the project, including data manipulation (numpy, pandas), plotting (seaborn, matplotlib), reinforcement learning (gymnasium, stable_baselines3), and progress bars (tqdm). ```python from tqdm import tqdm import random import numpy as np import pandas as pd import seaborn as sns import matplotlib.pyplot as plt import gymnasium as gym import gym_mtsim from stable_baselines3 import A2C, PPO from stable_baselines3.common.callbacks import BaseCallback import torch ``` -------------------------------- ### MtEnv Properties and Methods Source: https://github.com/aminhp/gym-mtsim/blob/main/README.md This section outlines the key properties and methods of the MtEnv Gym environment. Properties include simulator instances, trading parameters, and data structures like prices and signal features. Methods cover standard Gym functionalities (seed, reset, step, render, close) and internal virtual methods for data processing and reward calculation. ```python class MtEnv: # Properties: original_simulator: MtSim simulator: MtSim trading_symbols: list time_points: list hold_threshold: float close_threshold: float fee: float or callable symbol_max_orders: int multiprocessing_processes: int prices: dict signal_features: np.ndarray window_size: int features_shape: tuple action_space: gym.spaces.Box observation_space: gym.spaces.Dict history: list # Methods: def seed(self, seed=None): # Typical Gym seed method pass def reset(self): # Typical Gym reset method pass def step(self, action): # Typical Gym step method # Applies expit to probability values in action pass def render(self, mode='human'): # Renders in 'human', 'simple_figure', or 'advanced_figure' modes pass def close(self): # Typical Gym close method pass # Virtual Methods: def _get_prices(self): # Calculates symbol prices pass def _process_data(self): # Calculates signal features pass def _calculate_reward(self): # Reward function for the RL agent pass ``` -------------------------------- ### Train and Plot RL Algorithms Source: https://github.com/aminhp/gym-mtsim/blob/main/examples/SB3_a2c_ppo.ipynb This snippet trains different RL algorithms (A2C, PPO) for a specified number of timesteps and plots their reward statistics. It iterates through learning timesteps and model classes, trains the model, prints statistics, and stores rewards for plotting. Error handling is included for training failures. ```python from stable_baselines3 import A2C, PPO import pandas as pd import seaborn as sns import matplotlib.pyplot as plt # Assuming env, learning_timesteps_list_in_K, seed, total_num_episodes, train_test_model, print_stats are defined elsewhere model_class_list = [A2C, PPO] plot_data = {'x': list(range(1, total_num_episodes + 1))} plot_settings = {} for timesteps in learning_timesteps_list_in_K: total_learning_timesteps = timesteps * 1000 step_key = f'{timesteps}K' for model_class in model_class_list: policy_dict = model_class.policy_aliases policy = policy_dict.get('MultiInputPolicy') try: model = model_class(policy, env, verbose=0) class_name = type(model).__qualname__ plot_key = f'{class_name}_rewards_'+step_key rewards = train_test_model(model, env, seed, total_num_episodes, total_learning_timesteps) min_value, avg_value, max_value = print_stats(rewards) label = f'Avg. {avg_value:>7.2f} : {class_name} - {step_key}' plot_data[plot_key] = rewards plot_settings[plot_key] = {'label': label} except Exception as e: print(f"ERROR: {str(e)}") continue ``` -------------------------------- ### Render Environment State in Human Mode Source: https://github.com/aminhp/gym-mtsim/blob/main/README.md This Python code snippet demonstrates how to render the environment state in human mode and print key financial metrics such as balance, equity, margin, and free margin. It also accesses the 'orders' key from the state dictionary. ```python state = env.render() print( f"balance: {state['balance']}, equity: {state['equity']}, margin: {state['margin']} " f"free_margin: {state['free_margin']}, margin_level: {state['margin_level']} " ) state['orders'] ``` -------------------------------- ### ProgressBarCallback for Stable-Baselines3 Source: https://github.com/aminhp/gym-mtsim/blob/main/examples/SB3_a2c_ppo.ipynb A custom callback class that extends Stable-Baselines3's BaseCallback to display a progress bar during model training. It initializes a tqdm progress bar and updates it based on the `check_freq` parameter during the training steps. ```python # ProgressBarCallback for model.learn() class ProgressBarCallback(BaseCallback): def __init__(self, check_freq: int, verbose: int = 1): super().__init__(verbose) self.check_freq = check_freq def _on_training_start(self) -> None: """ This method is called before the first rollout starts. """ self.progress_bar = tqdm(total=self.model._total_timesteps, desc="model.learn()") def _on_step(self) -> bool: if self.n_calls % self.check_freq == 0: self.progress_bar.update(self.check_freq) return True def _on_training_end(self) -> None: """ This event is triggered before exiting the `learn()` method. """ self.progress_bar.close() ``` -------------------------------- ### Print Environment Information Source: https://github.com/aminhp/gym-mtsim/blob/main/README.md Displays the shapes of the price data and signal features within the environment. This helps in understanding the structure and dimensions of the data used for training and simulation. ```python print("env information:") for symbol in env.prices: print(f"> prices[{symbol}].shape:", env.prices[symbol].shape) print("> signal_features.shape:", env.signal_features.shape) print("> features_shape:", env.features_shape) ``` -------------------------------- ### Perform Random Trades in Environment Source: https://github.com/aminhp/gym-mtsim/blob/main/README.md Executes random actions within the environment until the episode terminates. It prints key trading metrics such as balance, equity, margin, and reward at the end of the episode. ```python observation = env.reset() while True: action = env.action_space.sample() observation, reward, terminated, truncated, info = env.step(action) done = terminated or truncated if done: # print(info) print( f"balance: {info['balance']}, equity: {info['equity']}, margin: {info['margin']} " f"free_margin: {info['free_margin']}, margin_level: {info['margin_level']} " f"step_reward: {info['step_reward']}" ) break ``` -------------------------------- ### Render in Simple Figure Mode Source: https://github.com/aminhp/gym-mtsim/blob/main/README.md Renders the trading environment in a simple figure mode. Each symbol is illustrated with a separate color, with green/red triangles indicating successful buy/sell actions, gray triangles for errors, and black vertical bars for close actions. ```python env.render('simple_figure') ``` -------------------------------- ### Display Trade Data Table Source: https://github.com/aminhp/gym-mtsim/blob/main/README.md This HTML snippet, using scoped CSS for styling, displays trade data in a structured table format. The table includes columns for trade ID, symbol, type, volume, entry/exit times and prices, balance, equity, profit, margin, fee, and closed status. ```html
| Id | Symbol | Type | Volume | Entry Time | Entry Price | Exit Time | Exit Price | Exit Balance | Exit Equity | Profit | Margin | Fee | Closed | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 14 | EURUSD | Buy | 9.95 | 2021-08-27 00:00:00+00:00 | 1.17955 | 2021-08-31 00:00:00+00:00 | 1.18083 | 18179.652195 | 18179.652195 | 1052.554631 | 11736.522500 | 0.000222 | True |
| 1 | 13 | EURUSD | Buy | 0.22 | 2021-08-26 00:00:00+00:00 | 1.17515 | 2021-08-31 00:00:00+00:00 | 1.18083 | 17127.097565 | 18179.652195 | 120.009649 | 258.533000 | 0.000225 | True |
| 2 | 12 | GBPCAD | Buy | 7.10 | 2021-08-24 00:00:00+00:00 | 1.72784 | 2021-08-26 00:00:00+00:00 | 1.73770 | 17007.087916 | 17007.087916 | 5140.996853 | 9746.529273 | 0.000675 | True |
| 3 | 11 | EURUSD | Sell | 3.33 | 2021-08-20 00:00:00+00:00 | 1.16996 | 2021-08-23 00:00:00+00:00 | 1.17457 | 11866.091062 | 11866.091062 | -1610.650324 | 3895.966800 | 0.000227 | True |
| 4 | 10 | GBPCAD | Buy | 6.65 | 2021-07-30 00:00:00+00:00 | 1.73335 | 2021-08-02 00:00:00+00:00 | 1.73577 | 13476.741387 | 13476.741387 | 868.941338 | 9248.130601 | 0.000786 | True |
| 5 | 9 | EURUSD | Sell | 0.26 | 2021-07-21 00:00:00+00:00 | 1.17946 | 2021-07-22 00:00:00+00:00 | 1.17707 | 12607.800048 | 12607.800048 | 56.809064 | 306.659600 | 0.000205 | True |
| 6 | 8 | USDJPY | Buy | 7.11 | 2021-07-12 00:00:00+00:00 | 110.34900 | 2021-07-16 00:00:00+00:00 | 110.08100 | 12550.990984 | 12550.990984 | -1850.301309 | 7110.000000 | 0.018474 | True |
| 7 | 7 | EURUSD | Buy | 4.23 | 2021-07-07 00:00:00+00:00 | 1.17903 | 2021-07-09 00:00:00+00:00 | 1.18774 | 14401.292293 | 14401.292293 | 3618.699910 | 4987.296900 | 0.000155 | True |
| 8 | 6 | GBPCAD | Sell | 2.77 | 2021-07-02 00:00:00+00:00 | 1.70511 | 2021-07-05 00:00:00+00:00 | 1.70716 | 10782.592383 | 10782.592383 | -612.337927 | 3831.428119 | 0.000678 | True |
| 9 | 5 | EURUSD | Sell | 6.07 | 2021-06-21 00:00:00+00:00 | 1.19185 | 2021-06-22 00:00:00+00:00 | 1.19413 | 11394.930310 | 11394.930310 | -1512.813611 | 306.659600 | 0.000205 | True |