env
Submodules
env.EnvMultipleStock_trade
- class env.EnvMultipleStock_trade.StockEnvTrade(df, day=0, turbulence_threshold=140, initial=True, previous_state=None, model_name='', iteration='')
Bases:
EnvA stock trading environment for OpenAI Gym.
- day
The current trading day.
- Type:
int
- df
DataFrame containing stock data.
- Type:
pd.DataFrame
- initial
Whether this is the initial state.
- Type:
bool
- previous_state
The state from the previous step.
- Type:
list
- model_name
Name of the model for saving results.
- Type:
str
- iteration
Iteration identifier for saving results.
- Type:
str
- metadata: Dict[str, Any] = {'render.modes': ['human']}
- render(mode='human')
Render the environment (simply returns the current state).
- Parameters:
mode (str) – Rendering mode.
- Returns:
Current state.
- Return type:
np.ndarray
- reset()
Reset the environment.
- Returns:
The initial state of the environment.
- Return type:
list
- step(actions)
Take a step in the environment.
- Parameters:
actions (np.ndarray) – Actions for each stock.
- Returns:
(state, reward, terminal, info)
- Return type:
tuple
env.EnvMultipleStock_train
- class env.EnvMultipleStock_train.StockEnvTrain(df, day=0)
Bases:
EnvA stock trading environment for OpenAI Gym, designed to simulate stock trading activities. The environment allows an agent to perform buy and sell actions on a portfolio of stocks and rewards it based on the change in the total asset value.
- metadata
Metadata about the environment (e.g., render modes).
- Type:
dict
- action_space
Action space representing the proportion of stocks to buy or sell.
- Type:
gym.spaces.Box
- observation_space
Observation space containing the environment’s state.
- Type:
gym.spaces.Box
- df
Dataframe containing historical stock data (e.g., adjusted close prices, technical indicators).
- Type:
pd.DataFrame
- day
Current day in the simulation.
- Type:
int
- state
Current state of the environment, including account balance and stock data.
- Type:
list
- terminal
Boolean flag indicating whether the episode has ended.
- Type:
bool
- reward
Reward calculated based on asset change.
- Type:
float
- cost
Cost of transactions (fees).
- Type:
float
- trades
Count of executed trades.
- Type:
int
- asset_memory
List tracking the account balance over time.
- Type:
list
- rewards_memory
List tracking the rewards over time.
- Type:
list
- metadata: Dict[str, Any] = {'render.modes': ['human']}
- render(mode='human')
Renders the current state of the environment.
- Parameters:
mode (str, optional) – The mode in which to render the state. Defaults to “human”.
- Returns:
The current state of the environment.
- Return type:
list
- reset()
Resets the environment to its initial state.
- Returns:
The initial state of the environment.
- Return type:
list
- step(actions)
Executes a single step in the trading environment.
- Parameters:
actions (np.array) – Array of actions taken by the agent (scaled between -1 and 1 for each stock).
- Returns:
state (list): The updated state after the actions.
reward (float): The reward based on the change in total asset value.
terminal (bool): Whether the episode is over.
info (dict): Additional information, empty in this case.
- Return type:
tuple
env.EnvMultipleStock_validation
- class env.EnvMultipleStock_validation.StockEnvValidation(df, day=0, turbulence_threshold=140, iteration='')
Bases:
EnvA stock trading environment for OpenAI Gym where agents simulate stock trading.
This environment allows agents to interact with historical stock data to take actions such as buying, selling, or holding stocks. The environment includes stock prices, technical indicators, and market turbulence as features for the agent to consider when making decisions.
- day
The current day in the stock data.
- Type:
int
- df
DataFrame containing historical stock data for simulation.
- Type:
pandas.DataFrame
- action_space
The action space of the agent, representing buy, sell, or hold actions.
- Type:
gym.spaces.Box
- observation_space
The observation space representing the state of the environment.
- Type:
gym.spaces.Box
- state
The current state of the environment, including balance, stock prices, and technical indicators.
- Type:
list
- reward
The reward after each action taken by the agent.
- Type:
float
- turbulence_threshold
Threshold value for market turbulence.
- Type:
float
- turbulence
Current turbulence value from stock data.
- Type:
float
- cost
The transaction cost incurred from buying or selling stocks.
- Type:
float
- trades
The number of trades made during the episode.
- Type:
int
- asset_memory
History of the total asset value during the episode.
- Type:
list
- rewards_memory
History of rewards earned during the episode.
- Type:
list
- metadata: Dict[str, Any] = {'render.modes': ['human']}
- render(mode='human', close=False)
Renders the current state of the environment.
- Parameters:
mode (str, optional) – The mode for rendering. Default is “human”.
close (bool, optional) – Whether to close the rendering. Default is False.
- Returns:
The current state of the environment.
- Return type:
list
This method provides a way to visualize the current state of the environment, useful for debugging or monitoring.
- reset()
Resets the environment to its initial state.
- Returns:
The initial state of the environment, including balance, stock prices, and technical indicators.
- Return type:
list
This method reinitializes the environment and prepares it for the next episode.
- step(actions)
Advances the environment by one time step based on the agent’s actions.
- Parameters:
actions (np.ndarray) – An array of actions where each value represents the amount to buy or sell for each stock.
- Returns:
A tuple containing the updated state, reward, a boolean indicating if the episode is done, and additional information.
- Return type:
tuple
This method performs the buy or sell actions, calculates rewards, and updates the environment’s state. It ends the episode if the agent has traded through all available days.