I want to list out the companies in NSE indices

6.6k Views Asked by At

I am using NSEPY API and I would like to list out the stocks which are included in nifty 50 indice.

I am getting history data for the individual stocks but not getting nifty 50 stocks (totally 50 stocks). I want to retrieve it .

import nsepy
from nsepy import get_history
from datetime import date
data = get_history(symbol="IOC", start=date(2017,1,1), end=date(2019,2,15))
data[['Close']].plot()

I want the results for 50 stocks but I am getting results for specific stick which I have given in symbol

1

There are 1 best solutions below

4
Daniel Labbe On BEST ANSWER

According to the NSEpy documentation, you don't have any method to list the companies from the index using the API.

However, you can download a csv that contains the 50 companies list here.

To read the file in Python and list the companies, using Pandas, the code is:

import pandas as pd
import io
import requests
url = 'https://www.nseindia.com/content/indices/ind_nifty50list.csv'
s = requests.get(url).content
df = pd.read_csv(io.StringIO(s.decode('utf-8')))
df.Symbol
>>> 0     ADANIPORTS
>>> 1     ASIANPAINT
>>> 2       AXISBANK
>>> 3     BAJAJ-AUTO
>>> 4     BAJFINANCE
>>> 5     BAJAJFINSV
>>> 6           BPCL
>>> 7     BHARTIARTL
>>> 8       INFRATEL
>>> 9          CIPLA
>>> 10     COALINDIA
>>> 11       DRREDDY
>>> 12     EICHERMOT
>>> 13          GAIL
>>> 14        GRASIM
>>> 15       HCLTECH
>>> 16      HDFCBANK
>>> 17    HEROMOTOCO
>>> 18      HINDALCO
>>> 19     HINDPETRO
>>> 20    HINDUNILVR
>>> 21          HDFC
>>> 22           ITC
>>> 23     ICICIBANK
>>> 24    IBULHSGFIN
>>> 25           IOC
>>> 26    INDUSINDBK
>>> 27          INFY
>>> 28      JSWSTEEL
>>> 29     KOTAKBANK
>>> 30            LT
>>> 31           M&M
>>> 32        MARUTI
>>> 33          NTPC
>>> 34          ONGC
>>> 35     POWERGRID
>>> 36      RELIANCE
>>> 37          SBIN
>>> 38     SUNPHARMA
>>> 39           TCS
>>> 40    TATAMOTORS
>>> 41     TATASTEEL
>>> 42         TECHM
>>> 43         TITAN
>>> 44           UPL
>>> 45    ULTRACEMCO
>>> 46          VEDL
>>> 47         WIPRO
>>> 48       YESBANK
>>> 49          ZEEL
>>> Name: Symbol, dtype: object