Git Product home page Git Product logo

sp500-stock-strategy-tester's Introduction

sp500-stock-strategy-tester

Summary

This repository contains code for calculating the historical returns of stock strategies calculated with daily data, as well as a generated dataset that contains the values of four portfolios over time following four different strategies.

The strategies included are simple-moving-average based strategies which are traded using stocks that are members of the S&P500 (Standard and Poor's 500) Index. This is a list of the 500 "leading" publicly-traded companies. For more information on S&P500 inclusion criteria, see this article

Guide to this Readme

Below are some descriptions of each file. If you want, you can skip to the "How to Use This Repository" section for a quick-start guide.

sp500-get-info.py

This python file is responsible for fetching some basic information about each stock currently in the S&P500. It fetches the data from a Wikipedia table That is regularly updated.

It creates a csv called "sp500-info.py" with quite a few features. The most notable features are the 'tickers' and 'dates_added' features, which tell you the stock symbol and date it was added to the S&P500 index, respectively.

sp500_create_10yr_hist_data_csvs.py

This python file creates the historical daily data for each stock based on the stocks listed in the csv generated by the program in sp500-info.py.

It uses the yfinance api to fetch the data and creates a separate csv with the stocks price data history, up to 10 years but maximally back to the date it was added to the S&{ (if it was added in the last 10 years).

Note that running this program may take a while.

See the next section for the features this data has.

sp500-10yr-hist-data/

This is a folder where the historical price data for the S&P stocks will be stored.

This historical data has some useful features that can be used to make indicator calculations:

  • 'Date' - the date in which the stock price data corresponds to
  • 'Open' - the price at the start of the trading day
  • 'High' - the highest price of the given stock during the trading day
  • 'Low' - the lowest price of the given stock during the trading day
  • 'Close' - the price at the end of the trading day
  • 'Volume' - the amount of shares traded during the trading day
  • 'Dividend' - indicates whether a dividend was paid out, and how much was paid out per share (0 for no dividend, or an amount per share)
  • 'Stock Splits' - Indicates whether a stock split occurred, with 0 indicating no stock split and a different number indicating that the number of shares in circulation was multiplied by that number

How to Use This Repository

First, make sure your environment is configured with the necessary packages installed. Here is a list of the ones you will need:

  • requests
  • bs4
  • pandas
  • time
  • yfinance

Second, run the SP500-get-info to generate sp500-info.csv, a csv which will have information on each S&P stock.

Third, you need to run sp500_create_10yr_hist_data_csvs.py to create the csvs with the historical data of stocks that are currently in the S&P500 index. This will create historical data (up to 10 years) for each of the stocks in their own csv files within the sp500-10-yr-hist-data folder. Note that this may take a while.

Fourth, you can add indicators or compose new strategies based on the class templates in the sp500_strategy_tester.py, and your selected strategies' portfolio equity data will be calculated over time and stored as a feature for each strategy you define in a csv called "sp500-sma-strategy-performance-data.csv (you can change the name depending on what types of strategies you are testing). This may take a while to run, possibly up to an hour or so.

Limitations

The strategy calculations do not take into account historical data for companies that were previously on the S&P500 index but were removed. They only tracks the performance of historically trading stocks that are currently a part of the S&P500 index.

sp500-stock-strategy-tester's People

Contributors

connerdrake98 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.