Giter VIP home page Giter VIP logo

rnn-trader's Introduction

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
  1. (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.
  2. 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.

rnn-trader's People

Contributors

bvsam avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.