A flexible framework for backtesting trading strategies against historical market data, with a focus on SPY500 comparisons. The system is built with modularity in mind - each strategy exists as an independent module while leveraging shared utilities for data processing, performance metrics, and visualization.
Backtests/
strategies/ # Strategy implementations
ma200.py # 200-day moving average strategy
monday_reversal.py # Monday market reversal strategy
overnight_swing.py # Overnight holding strategy
sellmay.py # Sell in May and go away strategy
spy_tlt_rotation.py # SPY/TLT rotation strategy
turn_of_month.py # Turn of the month strategy
berkshire.py # Berkshire Hathaway vs SPY comparison
core/
backtest_utils.py # Utility functions for data handling, caching, metrics
plot_utils.py # Plotting functions for equity curves and stats
strategy_base.py # Base strategy interface class
figures/ # Output figures directory
main.py # Entry point for running strategies
requirements.txt # Project dependencies
README.md # This documentation
- Modular strategy implementation with a common interface
- Efficient data caching to avoid redundant downloads
- Standardized benchmark comparison across all strategies
- Consistent metrics and visualization
- Install dependencies:
pip install -r requirements.txt - Run a specific strategy or all strategies:
# Run a single strategy python main.py --strategy ma200 # Run all registered strategies python main.py --all # Get help on available options python main.py --help
-
Create a new Python file in the
strategies/directory -
Implement a class that inherits from
StrategyBasewith:run(self, data, initial_investment, **kwargs)- Runs the strategy and returns resultssummarize(self, results, data=None)- Prints summary statistics and creates plots
-
Register your strategy in
main.py:STRATEGIES = { # Add your strategy to this dictionary: "your_strategy_key": ( "strategies.your_module", "YourStrategyClass", "SYMBOL", years_back, initial_investment ), # ...existing strategies... }
- Data Management: Automatic caching of downloaded data
- Benchmark Standardization: Consistent benchmark calculations across strategies
- Performance Metrics: CAGR, max drawdown, trade statistics
- Visualization: Equity curve plots with benchmark comparison
Each strategy generates:
- Summary statistics in the console (CAGR, drawdown, trade metrics, etc.)
- Comparison to Buy & Hold benchmark (SPY by default)
- Equity curve plots saved in the
figures/directory
MIT License