Skip to content

Latest commit

Β 

History

25 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ“Š Portfolio Builder & Optimizer

A data-driven portfolio management system leveraging Modern Portfolio Theory to construct and optimize equity portfolios using NSE-listed stocks.

Python License Jupyter


🎯 Project Overview

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.

Key Features

βœ… 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


πŸ’Ό Business Problem

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

πŸ› οΈ Tech Stack

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

πŸ“ˆ Methodology

1. Data Collection

  • 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

2. Portfolio Optimization

  • 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)

3. Risk Analysis

  • 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

4. Interactive Portfolio Builder

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

πŸš€ Getting Started

Prerequisites

Python 3.8 or higher
Jupyter Notebook or JupyterLab

Installation

  1. Clone the repository
git clone https://github.com/parthnemaa/nse-portfolio-builder.git
cd nse-portfolio-builder
  1. Install dependencies
pip install -r requirements.txt
  1. Launch Jupyter Notebook
jupyter notebook NSE-2.ipynb

Dependencies

yfinance>=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

πŸ“Š Sample Results

Portfolio Performance Metrics

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

Top Holdings Example

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

Sector Allocation

Financial Services    28.5% β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ
IT Services          22.3% β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ
Energy               15.2% β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ
Consumer Goods       12.8% β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ
Healthcare            8.7% β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ
Others               12.5% β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ

🎯 Use Cases

For Retail Investors

  • Construct scientifically optimized portfolios
  • Get exact share allocations for any investment amount
  • Understand risk-return tradeoffs
  • Download ready-to-execute trading lists

For Wealth Managers

  • Generate client-specific portfolio recommendations
  • Match portfolios to client risk tolerance
  • Automate portfolio rebalancing calculations
  • Provide data-backed investment rationale

For Finance Students

  • Learn Modern Portfolio Theory implementation
  • Understand Monte Carlo simulation
  • Practice with real market data
  • Build portfolio analysis skills

πŸ’‘ Key Insights

What This Project Demonstrates

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)

πŸ“« Contact

Parth Nema


πŸ™ Acknowledgments

  • Data Sources: Yahoo Finance API, NSE Python Library
  • Inspiration: Modern Portfolio Theory by Harry Markowitz
  • Community: Python finance and data science communities

⚠️ Disclaimer

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 History

Version 1.0 - October 25, 2025

  • Initial release
  • Author: Parth Nema

⭐ Star This Repository

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

About

Intelligent portfolio optimization system using Modern Portfolio Theory and Monte Carlo simulation for NSE stocks. Features interactive portfolio builder with risk-return analysis.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages