nselib Python Library

repository·main·Indexed 41 days ago

https://github.com/ruchitanmay/nselib

A Python library for fetching publicly available financial data from NSE India. It provides modules for Capital Markets, Derivatives (F&O), Cash Markets (FPI and AMFI reports), and Indices, allowing users to retrieve price-volume data, option chains, and index constituents using specific date ranges or shorthand periods.

Tokens
2.4K
Snippets
8
Records
9
Agent score
2%

What's inside nselib

  1. Configure date parameters for API calls

    main

    Most functions in nselib accept time ranges using one of two methods. You must provide either a specific date range or a shorthand period, but never both.

    ParameterFormatExample
    from_date / to_datedd-mm-YYYY'01-06-2024'
    periodShorthand code'1D', '1W', '1M', '6M', '1Y'

    Common shorthand codes include:

    • 1D: 1 Day
    • 1W: 1 Week
    • 1M: 1 Month
    • 6M: 6 Months
    • 1Y: 1 Year
  2. Enable logging for debugging

    main

    By default, nselib is silent to avoid polluting your application logs. To debug network requests, API responses, or errors, use nselib.enable_logging() with a standard Python logging level.

    import nselib
    import logging
    
    # Enable the logger to output to the console
    nselib.enable_logging(level=logging.DEBUG)
    
    # Now, function calls will emit helpful trace logs
    df = nselib.capital_market.price_volume_data('SBIN', period='1W')
  3. Get stock price, volume, and deliverable position data for a custom date range

    main

    To retrieve data for a specific timeframe, use capital_market.price_volume_and_deliverable_position_data. You must provide the symbol, from_date, and to_date in 'DD-MM-YYYY' format.

    from nselib import capital_market
    
    # Specify a custom date range
    df = capital_market.price_volume_and_deliverable_position_data(
        symbol='SBIN',
        from_date='01-01-2024',
        to_date='31-01-2024'
    )
    print(df)
  4. Get stock price volume data

    main

    Use the capital_market.price_volume_data function to retrieve price and volume information for a specific stock symbol. You can specify a predefined period like '1M' (1 month) to get the last month of data.

    from nselib import capital_market
    
    # Get price volume data for a stock (last 1 month)
    df = capital_market.price_volume_data(symbol='SBIN', period='1M')
    print(df.head())
  5. Use the Derivatives module

    main

    The nselib.derivatives module provides data for Futures and Options (F&O).

    Instrument Types:

    • FUTIDX: Future Index
    • FUTSTK: Future Stock
    • OPTIDX: Option Index
    • OPTSTK: Option Stock

    Key Functions:

    • future_price_volume_data(symbol, instrument, dates): Futures price & volume.
    • option_price_volume_data(symbol, instrument, option_type, dates): Options price & volume. option_type can be 'PE' or 'CE'.
    • nse_live_option_chain(symbol, expiry_date=None, oi_mode=None): Live option chain. Use oi_mode='compact' for fewer columns.
    • fii_derivatives_statistics(trade_date): FII derivatives stats.
    from nselib import derivatives
    
    # Futures price data
    df = derivatives.future_price_volume_data(
        symbol='SBIN', instrument='FUTSTK', period='1M'
    )
    
    # Live option chain
    df = derivatives.nse_live_option_chain(symbol='BANKNIFTY', expiry_date='27-03-2025')
    
    # Compact option chain (fewer columns)
    df = derivatives.nse_live_option_chain(symbol='NIFTY', oi_mode='compact')
    
    # FII derivatives statistics
    df = derivatives.fii_derivatives_statistics(trade_date='20-12-2025')
    
    # F&O daily volatility report
    df = derivatives.daily_volatility(trade_date='17-04-2026')
  6. Use the Indices module

    main

    The nselib.indices module allows you to explore index categories and their constituents.

    Index Categories:

    • BroadMarketIndices
    • SectoralIndices
    • ThematicIndices
    • StrategyIndices

    Key Functions:

    • index_list(index_category): Returns available indices by category.
    • constituent_stock_list(index_category, index_name): Returns stocks in a given index.
    • live_index_performances(): Returns live performance of all indices.
    from nselib import indices
    
    # List all broad market indices
    index_names = indices.index_list(index_category='BroadMarketIndices')
    
    # Get Nifty 50 constituents
    df = indices.constituent_stock_list(index_category='BroadMarketIndices', index_name='Nifty 50')
    
    # Live index performances
    df = indices.live_index_performances()
  7. Use the Capital Market module

    main

    The nselib.capital_market module provides access to equity data, including price/volume, delivery positions, bulk/block deals, and index-specific data.

    Key Functions:

    • price_volume_and_deliverable_position_data(symbol, from_date/to_date or period): Returns OHLCV + delivery data.
    • price_volume_data(symbol, from_date/to_date or period): Returns OHLCV price volume data.
    • deliverable_position_data(symbol, from_date/to_date or period): Returns delivery position data.
    • bhav_copy_with_delivery(trade_date): Daily bhav copy with delivery.
    • india_vix_data(from_date/to_date or period): India VIX historical data.
    • index_data(index, from_date/to_date or period): Historical index OHLC data.
    • top_gainers_or_losers(to_get): Returns top gainers or losers. Use 'gainers' or 'loosers' for to_get.
    from nselib import capital_market
    
    # Bhav copy for a specific date
    df = capital_market.bhav_copy_with_delivery(trade_date='20-06-2024')
    
    # India VIX for last 1 week
    df = capital_market.india_vix_data(period='1W')
    
    # CM daily volatility report
    df = capital_market.daily_volatility(trade_date='17-04-2026')
    
    # Historical index data
    df = capital_market.index_data(index='NIFTY 50', from_date='01-01-2024', to_date='31-03-2024')
    
    # Financial results (quarterly, F&O securities only)
    df = capital_market.financial_results_for_equity(period='6M', fo_sec=True, fin_period='Quarterly')
    
    # Top gainers in live market
    df = capital_market.top_gainers_or_losers('gainers')
  8. Use the Cash Market module

    main

    The nselib.cash_market module focuses on FPI activity and AMFI monthly reports.

    Key Functions:

    • nsdl_fpi_investment_activity(trade_date): NSDL FPI investment activity for a reporting date.
    • nsdl_fpi_latest_derivative_activity(): Latest NSDL FPI derivative activity.
    • amfi_monthly_report_links(): List of available AMFI monthly archive links.
    • amfi_monthly_data(report_month, file_type_priority): Parse a single AMFI monthly report.
    • amfi_monthly_historical_data(from_month, to_month, file_type_priority): Parse AMFI monthly reports across a range.
    from nselib import cash_market
    
    # NSDL FPI investment activity for a specific reporting date
    df = cash_market.nsdl_fpi_investment_activity(trade_date='30-10-2025')
    
    # Latest NSDL FPI derivative activity
    df = cash_market.nsdl_fpi_latest_derivative_activity()
    
    # List available AMFI archive reports
    links = cash_market.amfi_monthly_report_links()
    
    # Parse a single AMFI monthly report
    df = cash_market.amfi_monthly_data(report_month='01-03-2026')
    
    # Parse AMFI history for a month range
    history = cash_market.amfi_monthly_historical_data(from_month='01-01-2024', to_month='01-03-2026')