A data-driven portfolio management system leveraging Modern Portfolio Theory to construct and optimize equity portfolios using NSE-listed stocks.
This project builds an intelligent portfolio optimization system that helps retail investors and wealth managers construct optimal portfolios from India's top NSE-listed stocks. By applying Modern Portfolio Theory and Monte Carlo simulation, it identifies portfolios that maximize risk-adjusted returns.
β
Real-Time Data Integration - Fetches live stock prices, fundamentals, and market data from NSE
β
Portfolio Optimization - Monte Carlo simulation with 10,000 random portfolios
β
Efficient Frontier Visualization - Interactive risk-return tradeoff analysis
β
Risk Analytics - Sharpe ratio, volatility, correlation, and diversification metrics
β
Interactive Portfolio Builder - User inputs investment amount and risk tolerance to get personalized allocations
β
Sector Diversification - Automated sector allocation analysis
β
CSV Export - Downloadable portfolio allocation ready for broker execution
Challenge:
Retail investors struggle to construct diversified, risk-optimized portfolios from thousands of NSE stocks without quantitative tools or financial expertise. Traditional investing often leads to:
- Over-concentration risk
- Suboptimal risk-adjusted returns
- Lack of systematic diversification
Solution:
An automated portfolio optimization system that:
- Selects top stocks by market capitalization
- Calculates optimal weights using Modern Portfolio Theory
- Provides personalized allocations based on risk tolerance
- Generates actionable investment recommendations
| Category | Technologies |
|---|---|
| Language | Python 3.8+ |
| Data Sources | yfinance, nsepython (NSE API) |
| Analysis | pandas, numpy, scipy |
| Optimization | Monte Carlo Simulation, Markowitz Mean-Variance |
| Visualization | plotly, matplotlib, seaborn |
| Environment | Jupyter Notebook |
- Fetch 2,000+ NSE equity symbols via
nsepython - Retrieve fundamentals: price, market cap, P/E ratio, sector
- Select top 50 stocks by market capitalization
- Download 3 years of historical daily prices
- Calculate daily returns and covariance matrix
- Generate 10,000 random portfolios via Monte Carlo simulation
- Compute for each portfolio:
- Expected annual return
- Annual volatility (risk)
- Sharpe ratio (risk-adjusted return)
- Identify optimal portfolios:
- Maximum Sharpe Ratio (best risk-adjusted returns)
- Minimum Volatility (lowest risk)
- Correlation heatmap - Stock relationship analysis
- Sector allocation - Diversification across industries
- Sharpe ratio - Return per unit of risk (target > 1.0)
- Efficient frontier - Visualization of optimal portfolios
User inputs:
- Investment amount (βΉ)
- Risk tolerance (Low/Medium/High)
System outputs:
- Personalized portfolio allocation
- Exact number of shares to buy for each stock
- Sector diversification breakdown
- Future portfolio value projections (1, 3, 5, 10 years)
- Actionable recommendations
- CSV export for broker execution
Python 3.8 or higher
Jupyter Notebook or JupyterLab- Clone the repository
git clone https://github.com/parthnemaa/nse-portfolio-builder.git
cd nse-portfolio-builder- Install dependencies
pip install -r requirements.txt- Launch Jupyter Notebook
jupyter notebook NSE-2.ipynbyfinance>=0.2.0
nsepython>=2.0.0
pandas>=1.5.0
numpy>=1.23.0
scipy>=1.9.0
matplotlib>=3.6.0
plotly>=5.11.0
seaborn>=0.12.0
| Portfolio Type | Annual Return | Volatility | Sharpe Ratio |
|---|---|---|---|
| Equal Weight (Baseline) | 15.2% | 22.1% | 0.68 |
| Maximum Sharpe | 18.7% | 20.3% | 0.92 |
| Minimum Volatility | 12.4% | 16.8% | 0.74 |
Sample data - actual results vary based on market conditions
| Company | Sector | Weight | Shares | Investment |
|---|---|---|---|---|
| Reliance Industries | Energy | 8.2% | 15 | βΉ20,500 |
| TCS | IT Services | 7.5% | 6 | βΉ18,750 |
| HDFC Bank | Banking | 6.8% | 12 | βΉ17,000 |
| Infosys | IT Services | 6.3% | 10 | βΉ15,750 |
Financial Services 28.5% ββββββββββββββββββββββββββββ
IT Services 22.3% ββββββββββββββββββββββ
Energy 15.2% βββββββββββββββ
Consumer Goods 12.8% ββββββββββββ
Healthcare 8.7% ββββββββ
Others 12.5% ββββββββββββ
- Construct scientifically optimized portfolios
- Get exact share allocations for any investment amount
- Understand risk-return tradeoffs
- Download ready-to-execute trading lists
- Generate client-specific portfolio recommendations
- Match portfolios to client risk tolerance
- Automate portfolio rebalancing calculations
- Provide data-backed investment rationale
- Learn Modern Portfolio Theory implementation
- Understand Monte Carlo simulation
- Practice with real market data
- Build portfolio analysis skills
Finance Domain Knowledge:
- Modern Portfolio Theory (Markowitz optimization)
- Risk metrics: Sharpe ratio, volatility, correlation
- Portfolio construction and diversification principles
- Efficient frontier analysis
Technical Skills:
- Python programming (pandas, numpy, scipy)
- API integration and data fetching
- Statistical analysis and Monte Carlo simulation
- Data visualization (plotly, matplotlib, seaborn)
- Interactive Jupyter notebook development
Business Acumen:
- Translating financial theory into practical tools
- User-centric design (investment amount input)
- Actionable recommendations and insights
- Production-ready output (CSV export)
Parth Nema
- GitHub: @parthnemaa
- LinkedIn: Connect with me
- Email: parthnemaa@gmail.com
- Data Sources: Yahoo Finance API, NSE Python Library
- Inspiration: Modern Portfolio Theory by Harry Markowitz
- Community: Python finance and data science communities
IMPORTANT NOTICE:
This project is for educational and research purposes only. It is NOT investment advice.
- Past performance does not guarantee future results
- All investments carry risk, including potential loss of principal
- Portfolio optimization based on historical data may not predict future performance
- This analysis does not consider individual financial situations, risk tolerance, or investment objectives
- Transaction costs, taxes, and market impact are not included in calculations
Before making any investment decisions:
- Consult with a qualified financial advisor
- Conduct your own due diligence
- Understand your risk tolerance and investment horizon
- Consider all costs and tax implications
The author assumes no liability for any financial losses resulting from the use of this analysis.
Version 1.0 - October 25, 2025
- Initial release
- Author: Parth Nema
If you found this project useful, please consider giving it a star! It helps others discover this work and motivates further development.
Built with β€οΈ for the finance and data science community