Giter VIP home page Giter VIP logo

stock_backtesting_analysis's Introduction

stock_backtesting_analysis

Do mean reversion and/or trend following strategies work better than buy and hold for stocks?

plot

Summary

I was curious to see if trend following and/or mean reversion strategies outperformed buy and hold with the S&P 500. I repurposed some backtesting scripts that I had written for cryptocurrency (https://github.com/hansenrhan/crypto_backtesting_analysis). The analyses were ran assuming 0 trading fees, but did apply a 30% tax each year if the strategy was profitable that year (since a major advantage of buy and hold is no taxes except when selling). The scripts also assume fractional shares as needed (which isn't quite realistic, but is a carryover from cryptocurrency backtesting).

I looked at hourly data from the past two years using the yfinance library (since hourly data further past than that is not readily available). It would be interesting to look at that in the future.

I looked at four strategies: Mean Reversion w/ Shorting, Mean Reversion w/o Shorting, SMA Crossover w/ Shorting and SMA Crossover w/o Shorting. SMA Crossover strategies performed much higher across the board than Mean Reversion strategies, and only SMA Crossover w/ Shorting beat S&P 500 buy and hold in more than 50% of the runs.

The code can be generalized to any other ticket available with yfinance.

plot

Contents

comparison_analysis.html: R Markdown Analysis Output
comparison_analysis.Rmd: R Markdown Analysis
mean_reversion_analysis.ipynb: Mean Reversion Backtester Scripts
sma_crossover_analysis.ipynb: Simple Moving Average (SMA) Crossover Scripts
run_output/: Backtesting Results from Random Parameters

Requirements

Python:

  • yfinance
  • seaborn
  • matplotlib
  • pandas
  • numpy
  • tqdm

R:

  • tidyverse
  • ggpubr

Implementing Strategies with the Alpaca API

Once you've analyzed and discovered the most promising parameters using this backtesting repository, you may want to actually implement the strategies in live trading.

I have developed another script for this purpose: the Alpaca SMA Crossover Trader (https://github.com/hansenrhan/alpaca_sma_crossover_trader). This Python script uses the Alpaca API to execute trades based on the Simple Moving Average (SMA) crossover strategy.

With slight modifications to the code, you can substitute in the parameters for the SMA periods that you discovered using this backtesting analysis.

Please be reminded that this script is for illustrative and educational purposes only. It should not be used for making actual investment decisions without understanding and adjusting it to your specific needs and risk tolerance. Please check out the Alpaca SMA Crossover Trader for more information.

stock_backtesting_analysis's People

Contributors

hansen-han 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.