{"id":28765338,"url":"https://github.com/ishtufinquant/portfolio-optimizer","last_synced_at":"2026-04-12T18:37:23.021Z","repository":{"id":298962922,"uuid":"1000457265","full_name":"ishtufinquant/portfolio-optimizer","owner":"ishtufinquant","description":"Optimizes stock portfolios using simulation and real-world constraints (SLSQP).","archived":false,"fork":false,"pushed_at":"2025-06-13T20:56:08.000Z","size":725,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-06-13T21:29:35.545Z","etag":null,"topics":["backtesting","finance","numpy","pandas","portfolio-optimization","python","quant","scipy","yfinance"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ishtufinquant.png","metadata":{"files":{"readme":"ReadMe.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-06-11T20:15:30.000Z","updated_at":"2025-06-13T21:05:37.000Z","dependencies_parsed_at":"2025-06-13T21:40:11.094Z","dependency_job_id":null,"html_url":"https://github.com/ishtufinquant/portfolio-optimizer","commit_stats":null,"previous_names":["ishtufinquant/portfolio-optimizer"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ishtufinquant/portfolio-optimizer","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ishtufinquant%2Fportfolio-optimizer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ishtufinquant%2Fportfolio-optimizer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ishtufinquant%2Fportfolio-optimizer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ishtufinquant%2Fportfolio-optimizer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ishtufinquant","download_url":"https://codeload.github.com/ishtufinquant/portfolio-optimizer/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ishtufinquant%2Fportfolio-optimizer/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260346849,"owners_count":22995142,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["backtesting","finance","numpy","pandas","portfolio-optimization","python","quant","scipy","yfinance"],"created_at":"2025-06-17T11:01:04.219Z","updated_at":"2026-04-12T18:37:23.014Z","avatar_url":"https://github.com/ishtufinquant.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 📈 Portfolio Optimization with YFinance and Python\n\nThis project demonstrates how to use Python to perform **portfolio optimization** on a set of stocks using historical price data. It includes:\n\n- Downloading stock price data with `yfinance`\n- Calculating daily returns, mean returns, and the covariance matrix\n- Simulating 1,000 random portfolios to visualize the risk/return trade-off\n- Optimizing the portfolio using **two methods**:\n  - **Method 1**: Simulation-based (brute-force)\n  - **Method 2**: SLSQP-based (constrained optimization using `scipy.optimize.minimize`)\n\n---\n\n## 🛠️ Technologies Used\n\n- Python\n- `yfinance` – for fetching historical stock data\n- `pandas` and `numpy` – for data handling\n- `matplotlib` – for plotting the results\n- `scipy.optimize` – for portfolio optimization\n\n---\n\n## 📊 Stocks Used\n\nThis analysis includes the following tickers:\n\n- Johnson \u0026 Johnson (`JNJ`)\n- Amazon (`AMZN`)\n- JPMorgan Chase (`JPM`)\n- Apple (`AAPL`)\n- Exxon Mobil (`XOM`)\n\nHistorical data is downloaded for the period from **2020-01-01** to **2024-12-31**.\n\n---\n\n# ⚙️ Method 1: Portfolio Simulation (Random Portfolios)\n\nThis approach:\n- Randomly generates 1,000 portfolio weight combinations\n- Calculates expected return, volatility, and Sharpe ratio for each\n- Identifies:\n  - 📍 **Max Sharpe Ratio Portfolio**\n  - 📍 **Minimum Volatility Portfolio**\n\n---\n\n## 🔄 Workflow Overview\n\n1. **Download Price Data**  \n   Use `yfinance` to fetch historical closing prices.\n\n2. **Calculate Daily Returns**  \n   Compute daily percentage change.\n\n3. **Estimate Statistics**  \n   - Mean returns  \n   - Covariance matrix\n\n4. **Simulate 1,000 Portfolios**  \n   - Random weight combinations  \n   - Portfolio return, volatility, Sharpe ratio\n\n5. **Visualize Efficient Frontier**  \n   - Risk vs return plot  \n   - Highlight max Sharpe and min volatility\n\n---\n\n## ✅ Sample Output (Method 1 – Simulation)\n\n📌 Output from **1,000 simulated portfolios**:\n\n```\nOptimized Portfolio Weights: [0.20, 0.15, 0.30, 0.25, 0.10]\nOptimized Portfolio Return: 0.00091\nOptimized Portfolio Volatility: 0.01345\n```\n\n---\n\n## 📊 Visualization\n\nThis plot shows:\n\n- 1,000 simulated portfolios, colored by Sharpe ratio  \n- 🔴 Red star = Portfolio with **max Sharpe ratio**  \n- 🔵 Blue star = Portfolio with **min volatility**\n\n![Efficient Frontier](assets/efficient_frontier.png)\n\n---\n\n# ⚙️ Method 2: Portfolio Optimization with SLSQP\n\nThis approach:\n- Uses `scipy.optimize.minimize` (Sequential Least Squares Programming)  \n- Solves for **maximum Sharpe ratio** under real-world constraints\n\n### Constraints:\n- Portfolio weights must sum to 1  \n- No stock may exceed 40% of the portfolio\n\n---\n\n## ✅ Sample Output (Method 2 – SLSQP)\n\n📌 Output from `scipy.optimize.minimize`:\n\n```\nOptimized Portfolio Weights:\nJNJ  : 0.0000\nAMZN : 0.4000\nJPM  : 0.2000\nAAPL : 0.4000\nXOM  : 0.0000\n\nExpected Portfolio Return    : 0.000997\nExpected Portfolio Volatility: 0.017179\n```\n\n---\n\n## 👤 Real-World Use Case (Client-Style)\n\nA client wants to allocate capital across 5 stocks (JNJ, AMZN, JPM, AAPL, XOM) with these goals:\n\n- **Maximize risk-adjusted return (Sharpe ratio)**\n- **Limit any single stock to ≤ 40% of the portfolio**\n- **Keep the total investment sum to 100%**\n\nThis project uses `scipy.optimize.minimize` (SLSQP method) to:\n\n- Apply these real-world constraints\n- Find the optimal allocation\n- Return exact weights, expected return, and volatility\n\n📊 This approach is highly practical for asset managers and analysts building production-grade portfolios.\n\n---\n\n## 🚀 How to Run and Customize\n\n### 1. Clone the repository\n\n```bash\ngit clone https://github.com/ishtufinquant/portfolio-optimizer.git\ncd portfolio-optimizer\n```\n\n### 2. Install dependencies\n\n```bash\npip install yfinance pandas numpy matplotlib scipy\n```\n\n### 3. Run the script\n\n```bash\npython portfolio_optimization.py\n```\n\n📌 The script runs both the simulation and optimization methods in a straightforward top-down flow.  \nFuture versions will modularize the code and may include CLI support or a `main()` wrapper.\n\n---\n\n## 🔧 TODO (Planned Improvements)\n\n- Refactor into separate modules  \n- Add input flexibility via CLI or config  \n- Include unit tests  \n- Add export to CSV/PDF\n\n---\n\n## 📁 File Overview\n\n```\nportfolio-optimizer/\n├── portfolio_optimization.py     # Main script (initial version)\n├── README.md                     # Project documentation\n├── requirements.txt              # Dependencies\n├── assets/\n│   └── efficient_frontier.png    # Output plot (automatically generated)\n```\n\n---\n\n## 📄 License\n\nThis project is licensed under the **MIT License**. You are free to use, modify, and distribute it.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fishtufinquant%2Fportfolio-optimizer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fishtufinquant%2Fportfolio-optimizer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fishtufinquant%2Fportfolio-optimizer/lists"}