Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

8 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ“ˆ Sector-wise Performance & Risk Analysis of NIFTY Stocks using Python & Machine Learning

An end-to-end financial analytics and machine learning project that analyzes historical performance of NIFTY stocks, predicts next-day stock price movement using technical indicators, and presents the results through an interactive Streamlit dashboard.

๐ŸŒ Live Demo:
https://sector-wise-performance-and-risk-analysis-ml.streamlit.app/

๐Ÿ–ฅ๏ธ Dashboard Preview

Dashboard

Disclaimer:
This project is for educational purposes only and should not be considered financial or investment advice.

โœจ Features

  • ๐Ÿ“ฅ Automated historical stock data collection using Yahoo Finance
  • ๐Ÿ“Š Statistical analysis of daily stock returns
  • ๐Ÿ“ˆ Annual return and volatility calculations
  • โš–๏ธ Sharpe Ratio computation for risk-adjusted performance
  • ๐Ÿฆ Sector-wise performance comparison
  • ๐Ÿ”— Correlation analysis between stocks
  • ๐Ÿ“‰ Professional financial visualizations
  • โš™๏ธ Technical indicator generation
  • ๐Ÿค– Machine Learning pipeline for stock movement prediction
  • ๐Ÿง  Automatic model comparison and best model selection
  • ๐Ÿ’พ Model persistence using Joblib
  • ๐Ÿ”ฎ Next-day stock movement prediction with confidence scores
  • ๐Ÿ“„ Automatic CSV report generation

๐Ÿš€ Key Highlights

  • Analysed 24 NIFTY stocks across 5 sectors
  • Built an end-to-end ML pipeline
  • Engineered 13 technical indicators
  • Trained and compared multiple classification models
  • Developed an interactive multi-page Streamlit dashboard
  • Automated prediction generation for all supported stocks

๐Ÿ›๏ธ Project Architecture

Historical Stock Data
          โ”‚
          โ–ผ
Data Collection (Yahoo Finance)
          โ”‚
          โ–ผ
Data Cleaning & Processing
          โ”‚
          โ–ผ
Financial Metrics Calculation
          โ”‚
          โ–ผ
Visualization Generation
          โ”‚
          โ–ผ
Feature Engineering
          โ”‚
          โ–ผ
Machine Learning Models
          โ”‚
          โ–ผ
Model Evaluation
          โ”‚
          โ–ผ
Best Model Selection
          โ”‚
          โ–ผ
Prediction Generation
          โ”‚
          โ–ผ
Interactive Streamlit Dashboard

๐Ÿ—‚๏ธ Project Structure

Sector-Performance-Analysis-ML/
โ”‚
โ”œโ”€โ”€ assets/                     # Images used in the README
โ”‚
โ”œโ”€โ”€ dashboard/                  # Reusable Streamlit components
โ”‚   โ”œโ”€โ”€ chart_style.py
โ”‚   โ”œโ”€โ”€ charts.py
โ”‚   โ”œโ”€โ”€ components.py
โ”‚   โ”œโ”€โ”€ config.py
โ”‚   โ”œโ”€โ”€ indicators.py
โ”‚   โ”œโ”€โ”€ load_data.py
โ”‚   โ”œโ”€โ”€ sidebar.py
โ”‚   โ”œโ”€โ”€ styles.py
โ”‚   โ”œโ”€โ”€ technical_indicators.py
โ”‚   โ””โ”€โ”€ theme.py
โ”‚
โ”œโ”€โ”€ data/
โ”‚   โ”œโ”€โ”€ raw/                    # Historical stock data
โ”‚   โ””โ”€โ”€ processed/              # Processed datasets
โ”‚
โ”œโ”€โ”€ figures/                    # Generated visualizations
โ”‚
โ”œโ”€โ”€ models/                     # Trained ML models
โ”‚
โ”œโ”€โ”€ outputs/
โ”‚   โ”œโ”€โ”€ model_summary.csv
โ”‚   โ”œโ”€โ”€ stock_summary.csv
โ”‚   โ”œโ”€โ”€ descriptive_statistics.csv
โ”‚   โ””โ”€โ”€ predictions/            # Model prediction outputs
โ”‚
โ”œโ”€โ”€ pages/                      # Streamlit application pages
โ”‚   โ”œโ”€โ”€ 1_Dashboard.py
โ”‚   โ”œโ”€โ”€ 2_Sector_Analysis.py
โ”‚   โ”œโ”€โ”€ 3_Stock_Analysis.py
โ”‚   โ”œโ”€โ”€ 4_Model_Analysis.py
โ”‚   โ”œโ”€โ”€ 5_Prediction.py
โ”‚   โ””โ”€โ”€ 6_About.py
โ”‚
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ config.py
โ”‚   โ”œโ”€โ”€ data_ingestion.py
โ”‚   โ”œโ”€โ”€ feature_engineering.py
โ”‚   โ”œโ”€โ”€ metrics.py
โ”‚   โ”œโ”€โ”€ visualize.py
โ”‚   โ””โ”€โ”€ ml/
โ”‚       โ”œโ”€โ”€ evaluation.py
โ”‚       โ”œโ”€โ”€ ml_pipeline.py
โ”‚       โ”œโ”€โ”€ model_training.py
โ”‚       โ”œโ”€โ”€ prediction.py
โ”‚       โ”œโ”€โ”€ prediction_pipeline.py
โ”‚       โ”œโ”€โ”€ preprocessing.py
โ”‚       โ””โ”€โ”€ utils.py
โ”‚
โ”œโ”€โ”€ app.py                      # Streamlit entry point
โ”œโ”€โ”€ main.py                     # Data processing pipeline
โ”œโ”€โ”€ requirements.txt
โ”œโ”€โ”€ LICENSE
โ””โ”€โ”€ README.md

๐Ÿ“Š Dataset

The project analyzes 24 large-cap NIFTY stocks across five major sectors:

Sector Companies
Banking HDFC Bank, ICICI Bank, SBI, Axis Bank, Kotak Bank
Information Technology TCS, Infosys, Wipro, HCLTech, Tech Mahindra
Pharmaceuticals Sun Pharma, Dr. Reddy's, Cipla, Divi's Labs, Lupin
Automobile Maruti Suzuki, M&M, Bajaj Auto, Hero MotoCorp
FMCG Hindustan Unilever, ITC, Nestlรฉ India, Britannia, Dabur

Historical data is downloaded automatically using the yfinance API.


๐Ÿ“ˆ Financial Metrics

The project computes:

  • Daily Returns
  • Descriptive Statistics
  • Annual Returns
  • Annual Volatility
  • Sharpe Ratio
  • Correlation Matrix
  • Sector Performance
  • Investment Growth

โš™๏ธ Feature Engineering

The machine learning pipeline creates the following technical indicators:

Feature
Daily Return
5-Day Moving Average
10-Day Moving Average
20-Day Moving Average
Volume Change
Rolling Volatility
High-Low Percentage
Open-Close Percentage
RSI (Relative Strength Index)
MACD
MACD Signal
Bollinger Band Upper
Bollinger Band Lower

These features are used to predict whether the stock price will move UP or DOWN on the following trading day.


๐Ÿค– Machine Learning Models

Three supervised learning algorithms are trained and evaluated.

Model Purpose
Logistic Regression Baseline linear classifier
Decision Tree Non-linear decision model
Random Forest Ensemble learning model

Each model is evaluated using:

  • Accuracy
  • Precision
  • Recall
  • F1 Score

The model with the highest F1 Score is automatically selected and saved.


๐Ÿ“Š Visualizations

The project automatically generates:

  • Risk vs Return Scatter Plot
  • Correlation Heatmap
  • Sector-wise Returns
  • Sector-wise Volatility
  • Investment Growth Comparison
  • Top 5 Performing Stocks
  • Bottom 5 Performing Stocks
  • Sector Growth Comparison
  • Sharpe Ratio Comparison

All plots are saved inside the figures/ directory.


๐Ÿ“ท Sample Visualizations

Correlation Heatmap

Correlation Heatmap


Risk vs Return Analysis

Risk vs Return


Sector-wise Returns

Sector Returns


Sharpe Ratio Comparison

Sharpe Ratio

๐Ÿ“„ Generated Outputs

After execution, the project automatically creates:

outputs/
โ”‚
โ”œโ”€โ”€ descriptive_statistics.csv
โ”œโ”€โ”€ stock_summary.csv
โ”œโ”€โ”€ model_summary.csv

It also generates:

  • trained machine learning models
  • prediction CSV files
  • processed datasets
  • financial visualizations

๐Ÿš€ Installation

git clone https://github.com/abhiminav/Sector-Performance-Analysis-ML.git

cd Sector-Performance-Analysis-ML

pip install -r requirements.txt

Generate datasets and train models

python main.py

Launch the dashboard

streamlit run app.py

๐Ÿ› ๏ธ Technologies Used

Category Technology
Programming Python
Data Analysis Pandas, NumPy
Machine Learning Scikit-learn
Dashboard Streamlit
Interactive Charts Plotly
Static Visualisation Matplotlib
Data Source yfinance
Model Persistence Joblib

๐Ÿ“Œ Future Improvements

Potential enhancements include:

  • Cross-validation
  • Hyperparameter tuning using GridSearchCV
  • XGBoost and LightGBM models
  • LSTM-based time-series forecasting
  • Portfolio optimization
  • Real-time stock prediction
  • Model explainability using SHAP values

๐Ÿ‘จโ€๐Ÿ’ป Author

Abhinav Mishra

B.Tech Computer Science Engineering

Built as a portfolio project showcasing skills in:

  • Data Analysis
  • Financial Analytics
  • Machine Learning
  • Streamlit Dashboard Development

๐Ÿ“„ License

This project is licensed under the MIT License.

About

End-to-end financial analytics and machine learning project for analyzing NIFTY stocks with an interactive Streamlit dashboard.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages