- LSTM (RNN) models were trained on stock/ticker data from Yahoo Finance. The task: given X previous days of price data for ticker Y (and price data for various other tickers as supplementary features), predict whether the price of stock Y will be higher or lower Z days in the future.
- Keras, Tensorflow, Yahoo Finance API (
yfinance
)
- Keras, Tensorflow, Yahoo Finance API (
- An interface was then created to test a model's performance in generating returns by trading a specific ticker Y. If the model predicts ticker Y will be higher Z days in the future, the ticker is bought at the start, and sold at the end, of the day being predicted. Otherwise, it is shorted for that particular day. The return generated by the model's chosen action for each day are recorded. The model's returns over a time period are then determined by generating the cumulative product of returns for each day in the time period.
- pandas, numpy
- A web app was then created to backtest the models and visualize results compared to a benchmark.
- React, Flask, Fluent UI, visx
- Install dependencies.
pip install -r requirements.txt
cd src/app/
npm install
- (Optional) Specify the name of the model you would like to use with the
MODEL_NAME
key insrc/app/backend/config.ini
. Valid model names are listed as keys insrc/app/backend/model-info.json
. - Run the app (both the Vite frontend and the Flask API with concurrently).
cd src/app/
npm run app
- If the api fails to validate basic tickers properly (e.g. AAPL), then one of the following may be true
- the Yahoo Finance API that
yfinance
package is accessing may be temporarily down. This can be resolved by trying later - you may have to upgrade
yfinance
to the latest version (usingpip install -U yfinance
).
- the Yahoo Finance API that
- Additionally, the api might be overly strict as to what qualifies as a valid ticker. Change the dict keys checked for in
src/app/backend/api.py
if this is the case (setrequired_keys
in functionvalidate_ticker
).
Note: Models are listed in chronological order of creation. All models were trained to predict the price movement of 1 main (Yahoo Finance compatible) ticker (e.g. SPY), with price movement for additional tickers (QQQ, ^TNX, ^VIX, CL=F) provided as extra features to the model to base predictions off.
All models are stored in the src/model/models/
directory.
Model Name | Ticker* | Training Data End Date | Sequence Length** | Prediction Period Offset*** | Trainable Params | Notes |
---|---|---|---|---|---|---|
SPY | SPY | 2023-05-02 | 60 | 20 | 3162 | 1 layer LSTM with 20 units. |
SPY_lg_20230830 | SPY | 2023-08-29 | 250 | 20 | 139842 | 2 layer bidirectional LSTM with 64 units each. |
SPY_lg_20211231 | SPY | 2021-12-31 | 250 | 20 | 37314 | 2 layer bidirectional LSTM with 32 units each. |
* The main ticker the model was trained to predict price movement of.
** The length (in days) of previous price data the model uses to make a future prediction.
*** The number of days in the future (from the end of the data given to the model as input) to predict price movement on.
- Overall, the models are only somewhat successful in consistently generating returns above the benchmark (defined as the ticker the model is backtested on). The performance depends on the model being used, the dates the model are being backtested on, and the ticker being used to backtest.
- Models with more parameters, such as
SPY_lg_20230830
andSPY_lg_20211231
, perform better in general than those with less parameters, such asSPY
. - The models can recognize certain types of price movement (i.e. certain spikes, drops, etc.), allowing them to generate a good gains when they occur multiple times within a certain time period. This effect can be seen even when the models are backtested on tickers they were not trained on. This suggests that the predictions of the model can be used in conjunction with human judgement to make optimal decisions when trading.
- Trading off the model's predictions usually leads to very volatile changes in portfolio value, including cases when the underlying ticker being traded isn't very volatile itself (e.g. SPY). This, along with the model's non-guaranteed ability to generate above market returns, suggests that allowing this model to trade with large amounts of money might not be very desirable.
- Determine the statistical significance of the models' returns.
- Allow more tickers to be overlaid on the performance chart for more insightful performance comparisons.
- Analyze the model's trading risk to reward ratio and compare it with the risk to reward ratio of holding the underlying ticker being traded.
- Utilize data augmentation to increase the size of the training, validation and test sets used for each model.