Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bvsam/rnn-trader
A RNN based bot to trade tickers.
https://github.com/bvsam/rnn-trader
deep-learning keras lstm machine-learning python react rnn tensorflow visx
Last synced: 4 days ago
JSON representation
A RNN based bot to trade tickers.
- Host: GitHub
- URL: https://github.com/bvsam/rnn-trader
- Owner: bvsam
- License: mit
- Created: 2023-09-04T21:57:08.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2023-11-22T19:51:31.000Z (12 months ago)
- Last Synced: 2023-11-22T20:33:35.660Z (12 months ago)
- Topics: deep-learning, keras, lstm, machine-learning, python, react, rnn, tensorflow, visx
- Language: Jupyter Notebook
- Homepage:
- Size: 2.73 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# RNN Trading Bot
## About
- 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`)
- 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## Installation and Running
1. Install dependencies.
```
pip install -r requirements.txt
cd src/app/
npm install
```2. _(Optional)_ Specify the name of the model you would like to use with the `MODEL_NAME` key in `src/app/backend/config.ini`. Valid model names are listed as keys in `src/app/backend/model-info.json`.
3. Run the app (both the Vite frontend and the Flask API with concurrently).```
cd src/app/
npm run app
```### Tips for Running
- 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 (using `pip install -U yfinance`).
- 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 (set `required_keys` in function `validate_ticker`).## Model Zoo
**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.
## Conclusion/Learning Points
- 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` and `SPY_lg_20211231`, perform better in general than those with less parameters, such as `SPY`.
- 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.## Future Improvements
- 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.