A lightweight and modular Python framework for backtesting trading strategies using historical stock market data. This project includes components for data loading, strategy execution, portfolio management, and result visualization.
- Plug-and-play trading strategies (e.g., Moving Average Crossover)
- Modular design for extensibility
- Portfolio and cash management
- Uses Yahoo Finance for historical data
- Command-line interface for quick testing
.
├── core
│ ├── backtester.py # Main backtester logic
│ ├── data_feed.py # Iterates over data
│ ├── data_loader.py # Loads data from CSV or DataFrame
│ ├── display.py # Plotting utilities
│ ├── portfolio.py # Portfolio and trade execution logic
│ └── strategy.py # Base strategy class
├── strategies
│ └── simple_MA.py # Example strategy: Simple Moving Average
├── main.py # Entry point
└── README.md # This file
- Python 3.8+
pandasmatplotlibyfinanceccxtpyarrow
Install dependencies with:
poetry installRun the backtest from the command line:
python main.py --ticker AAPL --period 60d--ticker: Stock ticker (e.g., AAPL, MSFT, TSLA)--period: Time range (e.g., 60d, 1y, 5y)
The script will:
- Download historical price data from Yahoo Finance.
- Apply the
SimpleMovingAverageStrategy. - Execute trades via the portfolio manager.
- Display a price chart.
Simple Moving Average Crossover:
- Buy when short MA crosses above long MA.
- Sell when short MA crosses below long MA.
- Holds otherwise.
You can define your own strategies by subclassing Strategy.
This project is open-source and free to use under the MIT License.