Saturday, February 18, 2023

Python Program to fetch Live Stock Data from NSE

To process live data from NSE using python we need two libraries nsepy and nsetools. We can extract all the details like Last Traded price, Total Traded Quantity, Delivery Quantity, Upper Price Band and all other values on real time basis for a particular stock from NSE. In the below example we have shown an example to fetch last traded price and close price. 

In the next program we will use this data to evaluate technical indicator values.


import os

os.system('pip install nsepy')

os.system('pip install nsetools')


import pandas as pd


from nsepy.live import get_holidays_list

from datetime import date, timedelta


from nsetools import Nse

import nsepy as NSE


nse = Nse()


all_stock_codes = nse.get_stock_codes()

symbols = all_stock_codes.keys()

series = "EQ"

for data_symbol in symbols:

    tradingSymbol = data_symbol

    series = "EQ"

    quote = NSE.live.get_quote(symbol=tradingSymbol)

    stock_data = quote["data"]

    #print(pd.DataFrame(quote["data"]).T)

    if(len(stock_data) == 0):

      continue

    Close_Price = stock_data[0]["closePrice"]

    Last_Price = stock_data[0]["lastPrice"]

    print('symbol: {}, LastPrice: {}, ClosePrice: {}'.format(tradingSymbol, Last_Price, Close_Price))


Source Code can be downloaded from GitHub

No comments:

Post a Comment

Operations on Python Dictionaries