NSE Data · ML Classification · 2026

Nifty 50
UP ↑ or DOWN ↓
Predictor

A machine learning pipeline that predicts the next-day direction of India's Nifty 50 index using 4 years of NSE data, 9 engineered features, and two classification models.

51.1% RF Accuracy
9 Features
986 Trading Days
2 ML Models
NIFTY 50 ▲ 51.1% RF ACCURACY RANDOM FOREST ▲ BEAT BASELINE RSI · MA5 · MA20 · MA50 ▼ LR 47.8% 986 TRADING DAYS ▲ 9 FEATURES SCIKIT-LEARN · PANDAS · YFINANCE NIFTY 50 ▲ 51.1% RF ACCURACY RANDOM FOREST ▲ BEAT BASELINE RSI · MA5 · MA20 · MA50 ▼ LR 47.8% 986 TRADING DAYS ▲ 9 FEATURES SCIKIT-LEARN · PANDAS · YFINANCE

How It Works

The Pipeline

📥

1. Data Collection

Downloaded 4 years of real Nifty 50 OHLCV data (2021–2024) from Yahoo Finance using the yfinance library — no manual download needed.

⚙️

2. Feature Engineering

Created 9 financial features including RSI, moving averages (MA5/20/50), volatility, and volume momentum — signals real traders use.

🎯

3. Target Creation

Binary label — 1 if tomorrow's price is higher, 0 if lower. Time-series split (80/20) with no shuffle to prevent data leakage.

🤖

4. Model Training

Trained Logistic Regression (baseline) and Random Forest (200 trees). RF captured non-linear patterns and beat the 50% random baseline.

📊

5. Visualization

3 professional charts — price trends with MAs, feature importance analysis, and model comparison dashboard with confusion matrices.

💡

6. Key Insight

Daily Return was the most important feature (0.147). Nifty 50 is highly efficient — even beating 50% consistently is statistically meaningful.


Results

Visualizations

01 Nifty 50 Price with Moving Averages (2021–2024)
Price Chart with Moving Averages
02 Feature Importance & RSI Distribution
Feature Importance and RSI Analysis
03 Model Comparison Dashboard
Model Comparison Dashboard

Feature Engineering

9 Engineered Features

Feature Description Importance
ReturnDaily percentage price change🥇 0.147
High_Low_Pct(High - Low) / Close — daily price range🥈 0.121
Volatility10-day rolling std dev of returns🥉 0.120
Vol_ChangeDay-over-day volume momentum0.118
RSI14-day Relative Strength Index0.117
MA_5050-day moving average0.101
MA_55-day moving average0.098
MA_ratioMA5 / MA20 momentum signal0.091
MA_2020-day moving average0.087

Model Performance

Results

Random Baseline
50.0%
Coin flip — benchmark to beat
Logistic Regression
47.8%
Linear model — below baseline
Random Forest
51.1%
200 trees — beat the baseline ✓

WHY 51% IS MEANINGFUL

Stock markets are extremely efficient — millions of expert traders compete daily. Consistently beating the 50% random baseline demonstrates that the model has extracted real, statistically significant patterns from historical data. Interpretability and feature engineering quality matter as much as raw accuracy in financial ML.


Built With

Tech Stack

Python 3.12 Scikit-learn Pandas NumPy yFinance Matplotlib Seaborn Random Forest Logistic Regression NSE Data

About

Author

SR

Suraj Singh Rawat

B.Tech 3rd Year · MGM COET Noida · Aspiring ML Engineer