This repository contains codes and documents of my AI project about an application of reinforcement learning for trading. The AI trader uses a very simplified represenation of the stock market (state variables) and is trained to achieve a trading strategy that maximizes profit based on the devised reward formula.
The project allows to obtain a trading strategy based on historial price data of a stock symbol, which tends to maximize profitability over the given timeframe. It's noteworhtly that long-term profitablity and short-term profitablity may lead to different strategy and they are highly dependent on the latent variables. As a result, a trader trained over a specific time period may perform very badly for another time period (overfitting problem). To prevent this, it's important to obtain a generalized strategy by training the agent over various time periods that cover various price regimes (stock situations).
The codes provide functionalities for training and testing trading agents in a simulated environment. It includes:
- Reading input files containing historical stock data.
- Training a trading agent using reinforcement learning (DQN RL).
- Testing the trained agent and benchmarking against several baseline trading strategies.
- Plotting the trading logs and performance metrics.
osrandomstock_analysisIPython.displaymatplotlibpandastrader_classes
- Ensure all dependencies are installed.
- Run the script using Python 3. You can either run the dqn_trader.py or dqn_trader.ipynb.
- Adjust parameters such as the number of episodes (
n_ep), batch size (batch_size), and reset probability (reset_pr) as needed. - Monitor the training process through printed episode statistics and plotted rewards mean and standard deviation.
- After training, test the agent's performance and visualize the trading logs and benchmark comparisons.
- Reads input CSV files containing historical stock data.
- Merges the dataframes based on the date column.
- Returns a single merged dataframe.
- Plots various trading metrics over time, including close price, number of shares, balance, portfolio value, and actions taken.
- Saves the plot as a PDF file.
- Plots benchmark comparison logs alongside the trained agent's logs.
- Compares close price, number of shares, total portfolio value, and actions taken.
- Saves the plot as a PDF file.
- Trains a trading agent in a given environment using reinforcement learning.
- Conducts training over a specified number of episodes (
n_ep), with a specified batch size for replay memory (batch_size) and reset probability (reset_pr). - Returns the trained agent.
-
Tests a trained agent in a simulated trading environment.
-
Compares the agent's performance against several baseline trading strategies (RSI, CCI, Donchian, Return).
-
Visualizes the trading logs and benchmark comparisons.
A sample of the features, comprised of indicator signals, used to train the model is illustrated below.

The performance of the trained trading agent vs the performance of the individual technical indicator signals is provided below.

- Adjust hyperparameters and environment settings based on specific requirements and characteristics of the trading scenario.
- Ensure proper understanding of reinforcement learning concepts and potential risks associated with live trading applications.