A comprehensive backtesting system for a momentum-based portfolio strategy using NIFTYBEES and GOLDBEES ETFs.
- Initial Allocation: 70% NIFTYBEES, 30% GOLDBEES
- Rebalancing Rule: If NIFTYBEES 3-month return < 2%, switch to 30% NIFTYBEES, 70% GOLDBEES
- Rebalancing Frequency: Monthly (first trading day of each month)
- Transaction Cost: 0.05% per trade
# Install UV (if not already installed)
curl -LsSf https://astral.sh/uv/install.sh | sh
# Install dependencies
uv sync
# Run backtest
uv run python script.py --from 2024-01-01
# Or use helper script
./uv_run.sh backtest --from 2024-01-01# Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies
pip install pandas yfinance numpy python-dateutil holidays
# Run backtest
python script.py --from 2024-01-01python run_backtest.pypython script.py [OPTIONS]
Options:
--from, --start DATE Start date in YYYY-MM-DD format
--to, --end DATE End date in YYYY-MM-DD format
--list-examples Show example date ranges
--help Show help message
Examples:
python script.py --from 2020-01-01
python script.py --from 2018-01-01 --to 2023-12-31
python script.py --list-examples| Period | Command | Description |
|---|---|---|
| Full Period | python script.py |
2017-09-05 to 2025-07-15 (Default) |
| COVID Period | python script.py --from 2020-01-01 --to 2021-12-31 |
Market volatility period |
| Post-COVID | python script.py --from 2022-01-01 |
Recovery and recent performance |
| Recent Period | python script.py --from 2024-01-01 |
Latest performance |
Initial Capital: ₹100,000.00
Final Portfolio Value: ₹341,498.96
Total Return: 241.50%
CAGR: 16.92%
Maximum Drawdown: 92.64%
Strategy Comparison:
- Dynamic Portfolio CAGR: 16.92%
- NIFTYBEES Buy & Hold CAGR: 13.84%
- GOLDBEES Buy & Hold CAGR: 15.10%
- Static 70/30 Portfolio CAGR: 14.22%
Edit configuration in script.py:
INITIAL_CAPITAL = 100000
NIFTYBEES_INITIAL_ALLOCATION = 0.70 # 70%
GOLDBEES_INITIAL_ALLOCATION = 0.30 # 30%
TRANSACTION_COST_PERCENT = 0.0005 # 0.05%├── script.py # Main backtesting script
├── run_backtest.py # Interactive wrapper
├── uv_run.sh # UV helper script (Linux/Mac)
├── pyproject.toml # Project configuration
├── uv.lock # Dependency lock file
└── README.md # This file
This is for educational and research purposes only. Past performance does not guarantee future results. Always consult with financial advisors before making investment decisions.