Skip to main content

No project description provided

Project description

📈 Portfolio Optimization & Backtesting

A lightweight and modular framework for backtesting quantitative portfolio strategies using configurable rebalancing (annual / 6‑month / quarterly), efficient frontier analysis, and Sharpe ratio optimization.


📖 Documentation

Explore the full documentation here: 📘 Read the Docs

📦 Installation

pip install portfolio-opt
# or, with Poetry
poetry add portfolio-opt

🚀 Features

  • ✅ Downloads historical stock data from Yahoo Finance via vectorbt
  • ✅ Samples 10,000 random portfolios per rebalance period
  • ✅ Selects the maximum Sharpe ratio portfolio each rebalance period
  • ✅ Flexible --rebalance flag (A, 6M, Q/ 3M) to control rebalancing frequency
  • ✅ Computes and plots the Efficient Frontier using cvxpy
  • ✅ Tracks performance vs. benchmark (SPY)
  • ✅ Exports detailed reports: PNG plots and CSV summaries

🗂️ Project Structure

portfolio-opt/
├── src/
│   └── portfolio_opt/
│       ├── __init__.py
│       ├── main.py               # CLI + library entry points
│       ├── portfolio_backtester.py
│       └── portfolio_plotter.py
├── reports/                      # Auto‑generated plots (.png)
└── exports/                      # Auto‑generated summaries (.csv)
Module Description
main.py Coordinates data loading, optimization, backtest
portfolio_backtester.py Runs backtests and computes risk/return metrics
portfolio_plotter.py All portfolio and asset visualizations

🛠️ Tools & Libraries

Tool Role
Python 3.13 Core language
Poetry Dependency & environment management
vectorbt Market data ingestion & helpers
cvxpy Portfolio optimization engine
pandas / numpy Data analysis
matplotlib Chart rendering

⚡ Quick Start

# Run via the CLI (recommended)
portfolio-opt --tickers AAPL MSFT NVDA --start-date 2020-01-01 --end-date 2024-12-31 --rebalance 6M

# Or call programmatically
python - << 'PY'
from portfolio_opt.main import run_annual_rebalanced_backtest

run_annual_rebalanced_backtest(
    tickers=["AAPL", "MSFT", "NVDA"],
    start_date="2020-01-01",
    end_date="2024-12-31",
)
PY

Output Files:

Folder Output
reports/ Efficient frontier charts, equity curves, drawdown
exports/ CSV files for annual summaries, weights, benchmark

⚙️ Configuration Tips

Feature How to change
Tickers --tickers CLI flag or pass tickers=[...] to run_annual_rebalanced_backtest
Risk‑Free Rate --rf CLI flag or risk_free_rate= param
Portfolio Samples --num-ports CLI flag
Rebalance Frequency --rebalance CLI flag (A, 6M, Q/ 3M) or function param rebalance_freq=
Date Range --start-date / --end-date flags or function params

🔧 Possible Extensions

  1. Live Trading – Integrate with live modules from vectorbt or QuantConnect
  2. Factor Models – Score stocks on valuation, momentum, etc., instead of random
  3. Risk Constraints – Add CVaR, max drawdown, or concentration limits
  4. Visualization Dashboard – Use Streamlit, Dash, or Jupyter for dynamic charts

📜 License

MIT License – Free to use and modify. Attribution appreciated.


"In investing, what is comfortable is rarely profitable." – Robert Arnott

Enjoy building your own quantitative strategies! 🎯

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

portfolio_opt-0.1.0.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

portfolio_opt-0.1.0-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file portfolio_opt-0.1.0.tar.gz.

File metadata

  • Download URL: portfolio_opt-0.1.0.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.13.2 Darwin/23.6.0

File hashes

Hashes for portfolio_opt-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b988cd8a4cb2205de78cbce12b8cabb2f1dd4810646572c8cde5a7c78ba3fcfa
MD5 0ea16ede129141f863600b777951cf05
BLAKE2b-256 f9c3db2479cf2bdcba469074a72f7d13a6ef6fa083a6b37a90f1f107be5b92b7

See more details on using hashes here.

File details

Details for the file portfolio_opt-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: portfolio_opt-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.13.2 Darwin/23.6.0

File hashes

Hashes for portfolio_opt-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9fe59f29ecf3f922ed2134ccba4ec656f0ca445201bd30e49c11ede671e89389
MD5 4f5241151622afbcd87189accc70df02
BLAKE2b-256 0042a2a369d5e75bd2b65988bc9fd426c77775f87eefe3e4043878240c0e17b0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page