kernc / backtesting.py

:mag_right: :chart_with_upwards_trend: :snake: :moneybag: Backtest trading strategies in Python.
https://kernc.github.io/backtesting.py/
GNU Affero General Public License v3.0
5.55k stars 1.07k forks source link

Backtest a portfolio of stocks (cross-sectionally rather than one stock at a time) #969

Closed tjf2007 closed 1 year ago

tjf2007 commented 1 year ago

Did this ever get addressed? It seems like a great tool, but the ability to simulate a strategy on a portfolio of stocks is essential for it to be truly useful. For example, how would we build a portfolio of relative value strategies that are buying and shorting multiple single-name stocks and hedging against that via sector ETFs and SPY? How do we address position sizing too?

Discussed in https://github.com/kernc/backtesting.py/discussions/895

Originally posted by **vchopra9** February 1, 2023 Is it possible to run and evaluate cross-sectional backtests using backtesting.py (or another resource)? For example, I want to calculate a simple moving average crossover strategy signal (20 days vs 200 days), apply it to every stocks in the Dow Jones index, and restrict my Buy universe to only those stocks that meet the buy condition (sma(20)>=sma(200)). From within this Universe, I want the rank every stock using a measure of momentum (3 month price change) and pick the top 10 stocks to create a portfolio. Each stock is equal-weighted. Then I want to evaluate the performance of this portfolio over time and compare it with the performance of the Dow Jones index itself. What is the best Python resource for doing this?