Your Page Title
🔍

    nsetools in Python

    nsetools is a Python package which one uses for fetching Indian NSE data. One can access the real-time price, index of the stock, and so on with its help programmatically.
    This section goes into great detail regarding the features of the product along with how it could be used.

    Installation

    To use nsetools, you need to install it. You can do this using pip:

    pip install nsetools

    Main Features

    Easy access through the nsetools library for:

    1. Stock Quotes: Current data for any individual stock.
    2. Market Indices: All NSE indices such as NIFTY50, BANKNIFTY, etc.
    3. Top Gainers/Losers: A list of stocks that gain/lose the most for the day.
    4. Sectors Data: Data on sectoral indices.
    5. Stock List: Returns all stocks listed on NSE.

    Key Classes and Methods

    The library revolves around the Nse class. Below are its most commonly used methods:

    1. Initialize the NSE object

    from nsetools import Nse
    nse = Nse()

    2. Fetching Stock Details

    stock_data = nse.get_quote('INFY') # Fetch data for Infosys
    print(stock_data)

    Input: Stock symbol (e.g., INFY for Infosys, RELIANCE for Reliance).Output: A dictionary containing:

    • Current Price: lastPrice
    • Day’s High/Low: dayHigh, dayLow
    • 52-Week High/Low: high52, low52
    • Volume: quantityTraded

    3. Get Index Data

    nifty_data = nse.get_index_quote('nifty 50')
    print(nifty_data)
    • Input: Index name (e.g., nifty 50, bank nifty).
    • Output: A dictionary with key information about the index.

    4. Top Gainers and Losers

    Top Gainers

    gainers = nse.get_top_gainers()
    print(gainers)

    Output: A list of dictionaries containing:

    • symbol: Stock symbol
    • ltp: Last traded price
    • netPrice: Percentage change
    • tradedQuantity: Total quantity traded

    Top Losers

    losers = nse.get_top_losers()
    print(losers)

    5. Verify if the Stock Market is Open

    market_status = nse.is_valid_code('INFY')
    print(market_status) # Returns True if market is open

    6. List of All Stocks

    stock_list = nse.get_stock_codes()
    print(stock_list)
    • Output: A dictionary of stock symbols and their full names.

    Sample Use Case: Fetching and Displaying Stock Prices

    Here’s an example of fetching the stock price of Reliance:

    from nsetools import Nse
    
    # Initialize the NSE object
    nse = Nse()
    
    # Get stock data
    reliance_data = nse.get_quote('RELIANCE')
    
    # Display relevant details
    print("Company Name:", reliance_data['companyName'])
    print("Last Traded Price:", reliance_data['lastPrice'])
    print("Day's High:", reliance_data['dayHigh'])
    print("Day's Low:", reliance_data['dayLow'])
    print("52-Week High:", reliance_data['high52'])
    print("52-Week Low:", reliance_data['low52'])

    Advantages

    • Simple and straightforward API for accessing NSE data.
    • Real-time data fetching.
    • Useful for building financial tools, stock analysis systems, and dashboards.

    Limitations

    1. Unstable Updates: The library relies on NSE’s APIs, which are publicly available and can be changed anytime without notice.
    2. Compliance Issues: Please refer to NSE’s terms of service while using the data programatically.
    3. Not Actively Maintained: nsetools may not function with the latest updates on NSE’s API endpoints.

    Alternatives

    • yfinance: For broader financial market data (not limited to NSE).
    • NSEpy: A more versatile library for Indian stock market analysis, supporting historical data retrieval.