Thursday, March 7, 2019

Explore NSE Stock API using Python

Explore Stock API using Python



We need to install Python setup first. We are using nsetools which uses www.nseindia.com as a data source.

Configure Python in Eclipse using the link

Get Live quote in JSON:link


Case 1: List of Traded Stock Codes & Names

 NSE1.py


from nsetools import Nse
nse = Nse()
print(nse)
all_stock_codes = nse.get_stock_codes()
from pprint import pprint
pprint (all_stock_codes)



Case 2: Top Losers & Gainers

 NSE2.py


from nsetools import Nse
nse = Nse()
print(nse)
top_gainers = nse.get_top_gainers()
from pprint import pprint
pprint(top_gainers)
  

Case 3: Checking If the Code is Valid

 NSE3.py


from nsetools import Nse
nse = Nse()
print(nse)
pprint(nse.is_valid_code('infy'))


Case 4: List of Traded Stock Codes in loop

 NSE4.py

from nsetools import Nse
nse = Nse()
print(nse)
lst = nse.get_stock_codes()
for i in lst:
    try :
        from urllib.parse import quote
        nse.get_quote(quote(i,safe=''))
    except Exception as e:
        print(i)
    continue   
 

To be continue.. 

0 comments:

Post a Comment