zhwei820 / RNN_LSTM_trading_model

This project explores stock trading modelling with the use recurrent neural network (RNN) with long-short term memory (LSTM) architecture. This is for single stock prediction and backtesting, another RNN LSTM network and backtester for multiple-stock portfolio will be added soon.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Recurrent neural network - Long-Short Term Memory Trading Modelling

This project explores stock trading modelling with the use of recurrent neural network (RNN) - Long-short term memory (LSTM) architecture. CNN+LSTM hybrid architecture was tried. Tensorflow and Keras frameworks are adopted for implementation.

Data preprocessing

Data comprises of the asset's OHLCV data and technical data derive from its OHLCV data. A feature selection algorithm are then used to select the data series based on importance of varied factors. The selected feature data are visualized below. This visualization of pre-processed data demonstrates data richness.

Selected features' visualization

Model Construction & Training

Two machine learning algorithms were tested for price prediction application: A multi-layer recurrent neural network with Long-short term memory (LSTM) cells and a hybrid consists of 2 convolutional neural network layers & 1 LSTM neural network layers.

With CAPM model in mind, future stock price (the next day) is predicted with today and past market (S&P 500) and stock data. Each set of stock and market data consists of indicator time series derived from their respective price and volume data.

Depends on user's computational resource, it may take sometime to train the neural network models. rnn_modelling.py program takes about 15 minutes to train on both RNN-LSTM and CNN + RNN-LSTM models on a machine equipped with two Nvidia Geforce 1080Ti GPUs. For user convenience, their pre-trained models are included in this repository: best_cnn_model_AAPL.h5 & best_lstm_model_AAPL.h5 for which rnn_modelling.py will skip training and predict from the loaded pre-trained models.

Whereas for rnn_modelling.ipynb notebook, if pre-trained models are available the loading sequence will retrain existing pre-trained models with the hope of improved learning. User may rename the pre-trained models for this notebook to start training from fresh. Or proceed directly to the respecitve model loading cell to get instant prediction.

All training and the pre-trained models in this notebook and rnn_modelling.py are done for the stock AAPL. In rnn_modelling.py, user is also given the option to choose other stocks, the program will check if enough historical data is available for training, otherwise user will be prompted to enter a different stock quote.

Results

Prediction on unseen data after model training

Training LSTM_AAPL

Testing LSTM_IBM

Testing LSTM_CNN_AALP

Testing LSTM_CNN_IBM

Backtesting result using trained model to generate trade signals

Backtesting trade signals

Backtesting cummulative returns

Conclusion

After trying a number of model architectures, optimizers, hyperparameter adjustment and normalization techniques, the unseen data prediction and backtesting results shows that it is possible to use RNN-LSTM for stock trading. I reckon real world trading result will discount the backtesting result even further. So these models will need to have better generalization ability in order to get pass backtesting and eventually became a workable real world trading algorithm.

For improvements, I suggest:
More context and fundamental data.
Predict returns instead of price.
Try a hybrid of deep learning and reinforcement learning algorithm

About

This project explores stock trading modelling with the use recurrent neural network (RNN) with long-short term memory (LSTM) architecture. This is for single stock prediction and backtesting, another RNN LSTM network and backtester for multiple-stock portfolio will be added soon.


Languages

Language:Jupyter Notebook 94.7%Language:Python 5.3%