Skip to content

suman-gs/momentum_backtest_script

Repository files navigation

📊 Momentum-Based Portfolio Backtest

A comprehensive backtesting system for a momentum-based portfolio strategy using NIFTYBEES and GOLDBEES ETFs.

🎯 Strategy Overview

  • 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

🚀 Quick Start

Method 1: UV Package Manager (Recommended)

# 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

Method 2: Traditional pip/venv

# 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-01

Method 3: Interactive Mode

python run_backtest.py

📋 Command Line Options

python 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

📅 Common Date Ranges

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

📊 Sample Results

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%

🎨 Customization

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%

📁 File Structure

├── 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

⚠️ Disclaimer

This is for educational and research purposes only. Past performance does not guarantee future results. Always consult with financial advisors before making investment decisions.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published