Skip to content

Latest commit

Β 

History

1 Commit

Folders and files

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

Repository files navigation

πŸ“Š NSE India Market Data API

Python β€’ Selenium β€’ REST APIs

A Python-based REST API service that uses Selenium automation to fetch live market data from NSE India and expose it as HTTP APIs.

This project is designed for personal, research, and internal trading tools, where NSE data is required programmatically.


πŸš€ Features

  • Live NSE data using Selenium
  • REST APIs using Python http.server
  • Centralized browser management
  • Supports Index & Equity data
  • JSON-based responses
  • Clean modular architecture

πŸ“ Project Structure

NseIndia/
β”‚
β”œβ”€β”€ .venv/                     # Virtual environment
β”‚
β”œβ”€β”€ utils/                     # Selenium scraping logic
β”‚   β”œβ”€β”€ browser_manager.py
β”‚   β”œβ”€β”€ get_option_chain.py
β”‚   β”œβ”€β”€ get_option_chain_v1.py
β”‚   β”œβ”€β”€ get_option_chain_v2.py
β”‚   β”œβ”€β”€ get_oi_spurts.py
β”‚   β”œβ”€β”€ get_MACEquityTable.py
β”‚   β”œβ”€β”€ get_top_20_gainers_losers.py
β”‚   β”œβ”€β”€ get_index_performance.py
β”‚   β”œβ”€β”€ get_volume_gainers.py
β”‚   β”œβ”€β”€ get_52_week_high_low.py
β”‚   β”œβ”€β”€ get_advances_or_declines.py
β”‚   β”œβ”€β”€ get_large_deals.py
β”‚   └── ...
β”‚
β”œβ”€β”€ main.py                    # API Server Entry Point
β”œβ”€β”€ test.py
β”œβ”€β”€ setup.txt
└── Readme.md

βš™οΈ Requirements

  • Python 3.9+
  • Google Chrome (latest)
  • ChromeDriver (matching Chrome version)

πŸ“¦ Python Dependencies

pip install selenium beautifulsoup4

(Optional – Recommended)

pip install webdriver-manager

πŸ”§ ChromeDriver Setup

Option 1: Manual

  1. Download ChromeDriver https://chromedriver.chromium.org/downloads

  2. Add to system PATH or place in:

    C:\Windows\System32
    

Option 2: Auto (Recommended)

Use webdriver-manager inside browser_manager.py.


▢️ How to Run the Project

1️⃣ Activate Virtual Environment

D:\TMSS\Project\StockPredict\Python\src\NseIndia> venv\Scripts\activate

2️⃣ Start the API Server

python main.py

Server starts at:

http://localhost:8000

🌐 API Base URL

http://localhost:8000

πŸ“š API Endpoints

πŸ”Ή API Documentation

GET /

πŸ“ˆ Top 20 Gainers & Losers

GET /top-20-gainers-losers?index=NIFTY

index values

NIFTY, BANKNIFTY, NIFTYNEXT50, SecGtr20, SecLwr20, FOSec, all

πŸ“Š Option Chain (Index)

GET /option-chain?index=NIFTY

πŸ“Š Option Chain (Stock)

GET /option-chain?stock=RELIANCE

Optional

expiry-date-index=0

πŸ“‰ Index Performance

GET /index-performance?page=broadMarket

Values:

broadMarket | sectoralIndices | otherIndices | fixedIncomeIndices | all

πŸ“¦ Volume Gainers

GET /volume-gainers

πŸ“ˆ Open Interest Spurts

GET /oi-spurts

πŸ“Š Most Active Contracts

GET /most-active-contracts

πŸ“… 52 Week High / Low

GET /52-week-high-low

πŸ“‰ Advances / Declines

GET /advances-or-declines?page_type=advance

πŸ’Ό Large Deals

GET /large-deals

πŸ”’ Version

GET /version

❌ Error Handling

  • Missing parameters β†’ 400
  • Invalid values β†’ 400
  • Unknown endpoint β†’ 404

Example:

{
  "detail": "Wrong parameter. Available values are NIFTY, BANKNIFTY"
}

🧠 Architecture Overview

  • Selenium β†’ Live NSE data
  • BrowserManager β†’ Single shared Chrome instance
  • Utils Modules β†’ One module per NSE page
  • main.py β†’ Lightweight REST server

πŸ“„ Utility Module Highlight (20%)

get_option_chain.py – Option Chain Scraper

Path

utils/get_option_chain.py

Purpose

Fetches NSE Option Chain data for indices or stocks, parses CALL / STRIKE / PUT data, and returns structured JSON-ready output.


Function Signature

def get_option_chain(
    dropdown_id,
    index_name,
    dropdown_id_expiry_date,
    expiry_date_idx=0
)

Parameters

Name Description
dropdown_id Dropdown ID for index/stock
index_name Index or equity symbol
dropdown_id_expiry_date Expiry dropdown ID
expiry_date_idx Expiry index (0 = nearest)

Example Usage

get_option_chain(
    "equity_optionchain_select",
    "NIFTY",
    "expirySelect",
    0
)

Returned Data Format

{
  "STRIKE": 22000,
  "CALLS": [...],
  "PUTS": [...]
}
  • "-" values converted to 0.0
  • Data aligned exactly with NSE table layout

NSE Page Used

https://www.nseindia.com/option-chain

Error Handling

  • Invalid expiry index β†’ returns empty list
  • NSE layout change β†’ safe exit

⚠️ Important Notes

  • NSE blocks aggressive scraping
  • Avoid high-frequency API calls
  • Intended for educational / personal use
  • Not affiliated with NSE India

πŸš€ Future Improvements

  • FastAPI migration
  • Async scraping
  • Redis caching
  • Docker support
  • Swagger / OpenAPI docs

πŸ‘¨β€πŸ’» Author

NSE India Selenium API Project Built with Python & Selenium ❀️


If you want next:

  • πŸ“¦ Dockerfile
  • πŸš€ FastAPI version
  • πŸ“˜ Swagger UI
  • πŸ§ͺ Unit tests

Just say the word πŸ‘

About

NSE India Market Data API built using Python and Selenium. Provides REST APIs for live option chain, gainers & losers, OI spurts, index performance, volume movers, and more. Designed for personal trading tools, research, and NSE market analysis automation.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages