Environments#
- class gym_trading_env.environments.TradingEnv(df: ~pandas.core.frame.DataFrame, positions: list = [0, 1], dynamic_feature_functions=[<function dynamic_feature_last_position_taken>, <function dynamic_feature_real_position>], reward_function=<function basic_reward_function>, windows=None, trading_fees=0, borrow_interest_rate=0, portfolio_initial_value=1000, initial_position='random', max_episode_duration='max', verbose=1, name='Stock', render_mode='logs')#
An easy trading environment for OpenAI gym. It is recommended to use it this way :
import gymnasium as gym import gym_trading_env env = gym.make('TradingEnv', ...)
- Parameters
df (pandas.DataFrame) – The market DataFrame. It must contain ‘open’, ‘high’, ‘low’, ‘close’. Index must be DatetimeIndex. Your desired inputs need to contain ‘feature’ in their column name : this way, they will be returned as observation at each step.
positions (optional - list[int or float]) – List of the positions allowed by the environment.
dynamic_feature_functions (optional - list) –
The list of the dynamic features functions. By default, two dynamic features are added :
the last position taken by the agent.
the real position of the portfolio (that varies according to the price fluctuations)
reward_function (optional - function<History->float>) – Take the History object of the environment and must return a float.
windows (optional - None or int) – Default is None. If it is set to an int: N, every step observation will return the past N observations. It is recommended for Recurrent Neural Network based Agents.
trading_fees (optional - float) – Transaction trading fees (buy and sell operations). eg: 0.01 corresponds to 1% fees
borrow_interest_rate (optional - float) – Borrow interest rate per step (only when position < 0 or position > 1). eg: 0.01 corresponds to 1% borrow interest rate per STEP ; if your know that your borrow interest rate is 0.05% per day and that your timestep is 1 hour, you need to divide it by 24 -> 0.05/100/24.
portfolio_initial_value (float or int) – Initial valuation of the portfolio.
initial_position (optional - float or int) – You can specify the initial position of the environment or set it to ‘random’. It must contained in the list parameter ‘positions’.
max_episode_duration (optional - int or 'max') – If a integer value is used, each episode will be truncated after reaching the desired max duration in steps (by returning truncated as True). When using a max duration, each episode will start at a random starting point.
verbose (optional - int) – If 0, no log is outputted. If 1, the env send episode result logs.
name (optional - str) – The name of the environment (eg. ‘BTC/USDT’)
- class gym_trading_env.environments.MultiDatasetTradingEnv(dataset_dir, *args, preprocess=<function MultiDatasetTradingEnv.<lambda>>, episodes_between_dataset_switch=1, **kwargs)#
(Inherits from TradingEnv) A TradingEnv environment that handles multiple datasets. It automatically switches from one dataset to another at the end of an episode. Bringing diversity by having several datasets, even from the same pair from different exchanges, is a good idea. This should help avoiding overfitting.
It is recommended to use it this way :
import gymnasium as gym import gym_trading_env env = gym.make('MultiDatasetTradingEnv', dataset_dir = 'data/*.pkl', ... )
- Parameters
dataset_dir (str) – A glob path that needs to match your datasets. All of your datasets needs to match the dataset requirements (see docs from TradingEnv). If it is not the case, you can use the
preprocess
param to make your datasets match the requirements.preprocess (function<pandas.DataFrame->pandas.DataFrame>) –
This function takes a pandas.DataFrame and returns a pandas.DataFrame. This function is applied to each dataset before being used in the environment.
For example, imagine you have a folder named ‘data’ with several datasets (formatted as .pkl)
import pandas as pd import numpy as np import gymnasium as gym from gym_trading_env # Generating features. def preprocess(df : pd.DataFrame): # You can easily change your inputs this way df["feature_close"] = df["close"].pct_change() df["feature_open"] = df["open"]/df["close"] df["feature_high"] = df["high"]/df["close"] df["feature_low"] = df["low"]/df["close"] df["feature_volume"] = df["volume"] / df["volume"].rolling(7*24).max() df.dropna(inplace= True) return df env = gym.make( "MultiDatasetTradingEnv", dataset_dir= 'examples/data/*.pkl', preprocess= preprocess, )
episodes_between_dataset_switch (optional - int) – Number of times a dataset is used to create an episode, before moving on to another dataset. It can be useful for performances when max_episode_duration is low.