Mean-variance portfolio optimization toolkit with a Streamlit dashboard. This project allows users to download stock data, estimate risk and return, compute optimal portfolio weights, backtest strategies, and analyze performance.
- Features
- Tech Stack
- Architecture
- Data Flow
- Installation 🛠️
- Usage 🚀
- Project Structure 📂
- Contributing 🤝
- License 📄
- Important Links 🔗
- Interactive Streamlit Dashboard: A user-friendly web interface for portfolio optimization tasks.
- Stock Data Ingestion: Downloads and cleans historical stock data using Yahoo Finance.
- Mean-Variance Optimization: Computes optimal portfolio weights based on expected returns and covariance matrices.
- Performance Metrics: Calculates key risk and return metrics like Sharpe ratio and maximum drawdown.
- Backtesting: Simulates historical portfolio performance under various strategies.
- Efficient Frontier Visualization: Plots the optimal risk-return tradeoff curve.
- Unit Tested: Comprehensive unit tests for core modules.
- CI/CD Integration: GitHub Actions for linting and testing.
- Languages: Python
- Frameworks/Libraries: Streamlit, Pandas, NumPy, SciPy, Plotly, yfinance, PyArrow, Ruff, Pytest
portfolio-optimization/
├── .github/workflows/ # CI/CD (tests + lint)
├── data/ # Local Parquet storage
├── src/
│ ├── __init__.py # Initializes the src package
│ ├── ingestion.py # Fetch and clean raw stock data
│ ├── math_engine.py # Returns and covariance matrices
│ ├── optimizer.py # Optimal portfolio weights
│ ├── backtester.py # Historical performance simulation
│ ├── analytics.py # Sharpe ratio, max drawdown
│ ├── mock_data.py # Generates synthetic price data for testing
│ ├── tickers.py # Manages the list of available tickers for the UI
│ └── visualization.py # Creates interactive plots (e.g., Efficient Frontier)
├── tests/ # Unit tests
├── app.py # Streamlit dashboard
└── pyproject.toml # Project metadata and dependencies
- Ingestion — Download or load price data into
data/as Parquet. - Math Engine — Compute daily returns and a covariance matrix.
- Optimizer — Solve for maximum-Sharpe (or other) weights via
scipy. - Backtester — Simulate portfolio value under fixed weights.
- Analytics — Report Sharpe ratio and maximum drawdown.
- Clone the repository:
git clone https://github.com/stikey1/portfolio-optimization.git
cd portfolio-optimization- Create and activate a virtual environment:
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate- Install dependencies:
pip install .- For development (including testing and linting):
pip install ".[dev]"- Launch the dashboard:
streamlit run app.py- Select tickers using the sidebar multiselect or text input.
- Set parameters — lookback window (in years) and risk-free rate.
- Run optimization by clicking "Run Optimization."
- Analyze results in the Efficient Frontier plot and Backtest comparison against benchmarks like the Equal-Weight portfolio.
pytest tests/ -vContributions are welcome! Please follow these steps:
- Fork the repository.
- Create a new branch (
git checkout -b feature/your-feature-name). - Make your changes.
- Commit your changes (
git commit -am 'Add some feature'). - Push to the branch (
git push origin feature/your-feature-name). - Open a Pull Request.
Please ensure all tests pass and follow the code style guidelines (enforced by Ruff).
This project is licensed under the MIT License - see the LICENSE file for details.
- Project Repository: https://github.com/stikey1/portfolio-optimization
- Live Demo: (Not available, but the Streamlit app can be run locally)
- Author Profile: stikey1 on GitHub & sliu247 on GitHub
© 2026 [portfolio optimizer]. All rights reserved. | Fork me on GitHub ⭐ | Like this project? Give it a star! 👍 | Report issues 🐞