A Deep Reinforcement Learning Framework for Stock Market Trading

Overview

DQN-Trading

This is a framework based on deep reinforcement learning for stock market trading. This project is the implementation code for the two papers:

The deep reinforcement learning algorithm used here is Deep Q-Learning.

Acknowledgement

Requirements

Install pytorch using the following commands. This is for CUDA 11.1 and python 3.8:

pip install torch==1.9.0+cu111 torchvision==0.10.0+cu111 torchaudio==0.9.0 -f https://download.pytorch.org/whl/torch_stable.html
  • python = 3.8
  • pandas = 1.3.2
  • numpy = 1.21.2
  • matplotlib = 3.4.3
  • cython = 0.29.24
  • scikit-learn = 0.24.2

TODO List

  • Right now this project does not have a code for getting user hyper-parameters from terminal and running the code. We preferred writing a jupyter notebook (Main.ipynb) in which you can set the input data, the model, along with setting the hyper-parameters.

  • The project also does not have a code to do Hyper-parameter search (its easy to implement).

  • You can also set the seed for running the experiments in the original code for training the models.

Developers' Guidelines

In this section, I briefly explain different parts of the project and how to change each. The data for the project downloaded from Yahoo Finance where you can search for a specific market there and download your data under the Historical Data section. Then you create a directory with the name of the stock under the data directory and put the .csv file there.

The DataLoader directory contains files to process the data and interact with the RL agent. The DataLoader.py loads the data given the folder name under Data folder and also the name of the .csv file. For this, you should use the YahooFinanceDataLoader class for using data downloaded from Yahoo Finance.

The Data.py file is the environment that interacts with the RL agent. This file contains all the functionalities used in a standard RL environment. For each agent, I developed a class inherited from the Data class that only differs in the state space (consider that states for LSTM and convolutional models are time-series, while for other models are simple OHLCs). In DataForPatternBasedAgent.py the states are patterns extracted using rule-based methods in technical analysis. In DataAutoPatternExtractionAgent.py states are Open, High, Low, and Close prices (plus some other information about the candle-stick like trend, upper shadow, lower shadow, etc). In DataSequential.py as it is obvious from the name, the state space is time-series which is used in both LSTM and Convolutional models. DataSequencePrediction.py was an idea for feeding states that have been predicted using an LSTM model to the RL agent. This idea is raw and needs to be developed.

Where ever we used encoder-decoder architecture, the decoder is the DQN agent whose neural network is the same across all the models.

The DeepRLAgent directory contains the DQN model without encoder part (VanillaInput) whose data loader corresponds to DataAutoPatternExtractionAgent.py and DataForPatternBasedAgent.py; an encoder-decoder model where the encoder is a 1d convolutional layer added to the decoder which is DQN agent under SimpleCNNEncoder directory; an encoder-decoder model where encoder is a simple MLP model and the decoder is DQN agent under MLPEncoder directory.

Under the EncoderDecoderAgent there exist all the time-series models, including CNN (two-layered 1d CNN as encoder), CNN2D (a single-layered 2d CNN as encoder), CNN-GRU (the encoder is a 1d CNN over input and then a GRU on the output of CNN. The purpose of this model is that CNN extracts features from each candlestick, thenGRU extracts temporal dependency among those extracted features.), CNNAttn (A two-layered 1d CNN with attention layer for putting higher emphasis on specific parts of the features extracted from the time-series data), and a GRU encoder which extracts temporal relations among candles. All of these models use DataSequential.py file as environment.

For running each agent, please refer to the Main.py file for instructions on how to run each agent and feed data. The Main.py file also has code for plotting results.

The Objects directory contains the saved models from our experiments for each agent.

The PatternDetectionCandleStick directory contains Evaluation.py file which has all the evaluation metrics used in the paper. This file receives the actions from the agents and evaluate the result of the strategy offered by each agent. The LabelPatterns.py uses rule-based methods to generate buy or sell signals. Also Extract.py is another file used for detecting wellknown candlestick patterns.

RLAgent directory is the implementation of the traditional RL algorithm SARSA-λ using cython. In order to run that in the Main.ipynb you should first build the cython file. In order to do that, run the following script inside it's directory in terminal:

python setup.py build_ext --inplace

This works for both linux and windows.

For more information on the algorithms and models, please refer to the original paper. You can find them in the references.

If you had any questions regarding the paper, code, or you wanted to contribute, please send me an email: [email protected]

References

@article{taghian2020learning,
  title={Learning financial asset-specific trading rules via deep reinforcement learning},
  author={Taghian, Mehran and Asadi, Ahmad and Safabakhsh, Reza},
  journal={arXiv preprint arXiv:2010.14194},
  year={2020}
}

@article{taghian2021reinforcement,
  title={A Reinforcement Learning Based Encoder-Decoder Framework for Learning Stock Trading Rules},
  author={Taghian, Mehran and Asadi, Ahmad and Safabakhsh, Reza},
  journal={arXiv preprint arXiv:2101.03867},
  year={2021}
}
Comments
  • No module named 'RLAgent.Agent'

    No module named 'RLAgent.Agent'

    I have a problem, i cannot run my code. According to this project, I need to use Python version 3.9.8 but it still wont work even when i have all the packages in requirements.txt installed. I don't know how to solve this, can someone please help?

    Screen Shot 2022-03-27 at 19 55 03

    And when i run this line, i get an error:

    pip install torch==1.9.0+cu111 torchvision==0.10.0+cu111 torchaudio==0.9.0 -f https://download.pytorch.org/whl/torch_stable.html

    Screen Shot 2022-03-27 at 19 57 50
    opened by dekacypher 1
  • [WinError 206] The filename or extension is too long.

    [WinError 206] The filename or extension is too long.

    Hi, when I run main.py passes through these two files . the function os.makedirs( ) points out these filename or extension is too long to create. "[WinError 206] The filename or extension is too long. "

    • EncoderDecoderAgent/BaseTrain.py (line 89) self.PATH = os.path.join(Path(os.path.abspath(os.path.dirname(file))).parent, f'Results/{self.DATASET_NAME}/' f'{self.model_kind}; ' f'DATA_KIND({self.data_train.data_kind}); ' f'BEGIN_DATE({self.begin_date}); ' f'END_DATE({self.end_date}); ' f'SPLIT_POINT({self.split_point}); ' f'WindowSize({self.window_size}); ' f'BATCH_SIZE{self.BATCH_SIZE}; ' f'GAMMA{self.GAMMA}; ' f'REPLAY_MEMORY_SIZE{self.ReplayMemorySize}; ' f'TARGET_UPDATE{self.TARGET_UPDATE}; ' f'N_STEP{self.n_step}')

        if not os.path.exists(self.PATH):
            os.makedirs(self.PATH)
      
    • DeepRLAgent/BaseTrain.py (line 100) self.PATH = os.path.join(Path(os.path.abspath(os.path.dirname(file))).parent, f'Results/{self.DATASET_NAME}/' f'{self.model_kind}; ' f'DATA_KIND({self.data_train.data_kind}); ' f'BEGIN_DATE({self.begin_date}); ' f'END_DATE({self.end_date}); ' f'SPLIT_POINT({self.split_point}); ' f'StateMode({self.state_mode}); ' f'WindowSize({self.window_size}); ' f'BATCH_SIZE{self.BATCH_SIZE}; ' f'GAMMA{self.GAMMA}; ' f'REPLAY_MEMORY_SIZE{self.ReplayMemorySize}; ' f'TARGET_UPDATE{self.TARGET_UPDATE}; ' f'N_STEP{self.n_step}')

        if not os.path.exists(self.PATH):
            os.makedirs(self.PATH)
      

    I would appreciate it if you can tell me where the problem is. THANKS!

    opened by Tired-Longer 1
  • ZeroDivisionError: division by zero in Main.ipynb

    ZeroDivisionError: division by zero in Main.ipynb

    Does anyone have a solution? I tried to run the return statement only if the length of rate_of_return is not null, but then i get a followup error like IndexError.

    I'm trying to run this code: `#Read RL Agent experiment data_test_rlagent = dataTest_patternBased.data if dataTest_patternBased is not None else None

    rlAgent = RLTrain(dataTrain_patternBased.data, data_test_rlagent, data_loader.patterns, DATASET_NAME, n=n, num_iteration=num_iteration, gamma=gamma, alpha=alpha, epsilon=epsilon)

    rlAgent.training() rlAgent.write_to_file()

    #rlAgent.read_from_file('GOOGL-TRAIN_TEST_SPLIT(True)-NUM_ITERATIONS10000-N_STEP10-GAMMA1-ALPHA0.1-EPSILON0.1-EXPERIMENT(1).pkl') #rlAgent.read_from_file('AAPL-TRAIN_TEST_SPLIT(True)-NUM_ITERATIONS1000-N_STEP10-GAMMA1-ALPHA0.1-EPSILON0.1-EXPERIMENT.pkl') #rlAgent.read_from_file('BTC-USD-TRAIN_TEST_SPLIT(True)-NUM_ITERATIONS10000-N_STEP10-GAMMA0.9-ALPHA0.1-EPSILON0.1-EXPERIMENT.pkl') #rlAgent.read_from_file('KSS-TRAIN_TEST_SPLIT(True)-NUM_ITERATIONS10000-N_STEP10-GAMMA0.9-ALPHA0.1-EPSILON0.1-EXPERIMENT(1).pkl')

    ev_rlAgent = rlAgent.test(test_type= 'train') print('train') ev_rlAgent.evaluate() rlAgent_portfolio_train = ev_rlAgent.get_daily_portfolio_value() ev_rlAgent = rlAgent.test(test_type= 'test') print('test') ev_rlAgent.evaluate() rlAgent_portfolio_test = ev_rlAgent.get_daily_portfolio_value()

    model_kind = f'RL' #model_kind = f'RL-{n}'

    add_train_portfo(model_kind, rlAgent_portfolio_train) add_test_portfo(model_kind, rlAgent_portfolio_test)` Screen Shot 2022-03-28 at 21 36 50

    opened by dekacypher 0
  • It seems training not working(rewards don't converge at all)

    It seems training not working(rewards don't converge at all)

    This repo is pretty awesome. I'm trying to run a basic demo, but the training process seems not working at all (rewards don't converge at all). However, the agent still outperforms B&H a lot.(even when the reward is negative! ) I'm confused by this situation. Is there an explanation about this?

    The graph is rewards with training epochs=50. image

    opened by zhiyiZeng 2
  • How to make data_processed.csv plot data into a visual chart (jpg file)?

    How to make data_processed.csv plot data into a visual chart (jpg file)?

    I'm trying to run the Main.py, it runs perfectly, the only issue here is that it writes new data(BTC-USD) to the file data_processed.csv but it does not plot it into a visual chart (jpg file). I've set load_from_file=False and it still doesn't work, what am I doing wrong?

    `

    BTC-USD

    DATASET_NAME = 'BTC-USD' DATASET_FOLDER = r'BTC-USD' FILE = r'BTC-USD.csv' data_loader = YahooFinanceDataLoader(DATASET_FOLDER, FILE, '2021-01-10', load_from_file=False) transaction_cost = 0.0 `

    opened by dekacypher 0
  • MinMaxScaler() usage

    MinMaxScaler() usage

    Warning Risk of data leak Do not use minmax_scale unless you know what you are doing. A common mistake is to apply it to the entire data before splitting into training and test sets. This will bias the model evaluation because information would have leaked from the test set to the training set. In general, we recommend using MinMaxScaler within a Pipeline in order to prevent most risks of data leaking: pipe = make_pipeline(MinMaxScaler(), LogisticRegression()).

    from https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.minmax_scale.html .

    opened by formidiable 0
  • def get_reward(self, action)

    def get_reward(self, action)

    def get_reward(self, action):
        """
        @param action: based on the action taken it returns the reward
        @return: reward
        """
    
        reward_index_first = self.current_state_index + self.start_index_reward
        reward_index_last = self.current_state_index + self.start_index_reward + self.n_step \
            if self.current_state_index + self.n_step < len(self.states) else len(self.close_price) - 1
    
        p1 = self.close_price[reward_index_first]
        p2 = self.close_price[reward_index_last]
    
        reward = 0
        if action == 0 or (action == 1 and self.own_share):  # Buy Share or Hold Share
            reward = ((1 - self.trading_cost_ratio) ** 2 * p2 / p1 - 1) * 100  # profit in percent
        elif action == 2 or (action == 1 and not self.own_share):  # Sell Share or No Share
            # consider the transaction in the reverse order
            reward = ((1 - self.trading_cost_ratio) ** 2 * p1 / p2 - 1) * 100
    
        return reward
    

    Sorry, the function is true.

    opened by formidiable 0
Owner
null
Technical Indicators implemented in Python only using Numpy-Pandas as Magic - Very Very Fast! Very tiny! Stock Market Financial Technical Analysis Python library . Quant Trading automation or cryptocoin exchange

MyTT Technical Indicators implemented in Python only using Numpy-Pandas as Magic - Very Very Fast! to Stock Market Financial Technical Analysis Python

dev 34 Dec 27, 2022
Use deep learning, genetic programming and other methods to predict stock and market movements

StockPredictions Use classic tricks, neural networks, deep learning, genetic programming and other methods to predict stock and market movements. Both

Linda MacPhee-Cobb 386 Jan 3, 2023
Technical experimentations to beat the stock market using deep learning :chart_with_upwards_trend:

DeepStock Technical experimentations to beat the stock market using deep learning. Experimentations Deep Learning Stock Prediction with Daily News Hea

Keon 449 Dec 29, 2022
This project provides a stock market environment using OpenGym with Deep Q-learning and Policy Gradient.

Stock Trading Market OpenAI Gym Environment with Deep Reinforcement Learning using Keras Overview This project provides a general environment for stoc

Kim, Ki Hyun 769 Dec 25, 2022
:boar: :bear: Deep Learning based Python Library for Stock Market Prediction and Modelling

bulbea "Deep Learning based Python Library for Stock Market Prediction and Modelling." Table of Contents Installation Usage Documentation Dependencies

Achilles Rasquinha 1.8k Jan 5, 2023
Trading Gym is an open source project for the development of reinforcement learning algorithms in the context of trading.

Trading Gym Trading Gym is an open-source project for the development of reinforcement learning algorithms in the context of trading. It is currently

Dimitry Foures 535 Nov 15, 2022
A resource for learning about deep learning techniques from regression to LSTM and Reinforcement Learning using financial data and the fitness functions of algorithmic trading

A tour through tensorflow with financial data I present several models ranging in complexity from simple regression to LSTM and policy networks. The s

null 195 Dec 7, 2022
This is a simple backtesting framework to help you test your crypto currency trading. It includes a way to download and store historical crypto data and to execute a trading strategy.

You can use this simple crypto backtesting script to ensure your trading strategy is successful Minimal setup required and works well with static TP a

Andrei 154 Sep 12, 2022
Stock-history-display - something like a easy yearly review for your stock performance

Stock History Display Available on Heroku: https://stock-history-display.herokua

LiaoJJ 1 Jan 7, 2022
Deep Reinforcement Learning based Trading Agent for Bitcoin

Deep Trading Agent Deep Reinforcement Learning based Trading Agent for Bitcoin using DeepSense Network for Q function approximation. For complete deta

Kartikay Garg 669 Dec 29, 2022
Time-series-deep-learning - Developing Deep learning LSTM, BiLSTM models, and NeuralProphet for multi-step time-series forecasting of stock price.

Stock Price Prediction Using Deep Learning Univariate Time Series Predicting stock price using historical data of a company using Neural networks for

Abdultawwab Safarji 7 Nov 27, 2022
Forecasting directional movements of stock prices for intraday trading using LSTM and random forest

Forecasting directional movements of stock-prices for intraday trading using LSTM and random-forest https://arxiv.org/abs/2004.10178 Pushpendu Ghosh,

Pushpendu Ghosh 270 Dec 24, 2022
A general-purpose, flexible, and easy-to-use simulator alongside an OpenAI Gym trading environment for MetaTrader 5 trading platform (Approved by OpenAI Gym)

gym-mtsim: OpenAI Gym - MetaTrader 5 Simulator MtSim is a simulator for the MetaTrader 5 trading platform alongside an OpenAI Gym environment for rein

Mohammad Amin Haghpanah 184 Dec 31, 2022
Trading and Backtesting environment for training reinforcement learning agent or simple rule base algo.

TradingGym TradingGym is a toolkit for training and backtesting the reinforcement learning algorithms. This was inspired by OpenAI Gym and imitated th

Yvictor 1.1k Jan 2, 2023
Reinforcement Learning for Automated Trading

Reinforcement Learning for Automated Trading This thesis has been realized for the obtention of the Master's in Mathematical Engineering at the Polite

Pierpaolo Necchi 80 Jun 19, 2022
Predict stock movement with Machine Learning and Deep Learning algorithms

Project Overview Stock market movement prediction using LSTM Deep Neural Networks and machine learning algorithms Software and Library Requirements Th

Naz Delam 46 Sep 13, 2022
Conservative Q Learning for Offline Reinforcement Reinforcement Learning in JAX

CQL-JAX This repository implements Conservative Q Learning for Offline Reinforcement Reinforcement Learning in JAX (FLAX). Implementation is built on

Karush Suri 8 Nov 7, 2022
Reinforcement-learning - Repository of the class assignment questions for the course on reinforcement learning

DSE 314/614: Reinforcement Learning This repository containing reinforcement lea

Manav Mishra 4 Apr 15, 2022
Implementation of the bachelor's thesis "Real-time stock predictions with deep learning and news scraping".

Real-time stock predictions with deep learning and news scraping This repository contains a partial implementation of my bachelor's thesis "Real-time

David Álvarez de la Torre 0 Feb 9, 2022