Skip to content
Advertisement

Python InvestPy package to get data of ‘Nifty50’ index, get_stock_historical_data function not working

In my use of the InvestPy package, I am able to get stock ticker data easily, using the in-built function ‘get_stock_historical_data’. But not having the same luck in trying to get Index data of Nifty50, for example. A quick look at all the Indian tickers available from the function <investpy.get_stocks(country=’india’)> reveals nothing related to the Index.

Is their a way to get it using the package? My alternative is web-scraping stuff for index. Couldn’t find any thing relevant in the official documentation here.

Advertisement

Answer

There are a few methods that investpy has implemented to gather information regarding indices. Unfortunately, I could not find any function that returns the performances of each individual member of the index, but you can, for example, get the historical data regarding an index:

df = investpy.get_index_historical_data(index="Nifty 50",
                                        country="India",
                                        from_date='01/01/2018',
                                        to_date='01/01/2019')
                Open      High       Low     Close     Volume Currency
Date                                                                  
2018-01-01  10531.70  10537.85  10423.10  10435.55  134532000      INR
2018-01-02  10477.55  10495.20  10404.65  10442.20  158092000      INR
2018-01-03  10482.65  10503.60  10429.55  10443.20  172516992      INR
2018-01-04  10469.40  10513.00  10441.45  10504.80  180256992      INR
2018-01-05  10534.25  10566.10  10520.10  10558.85  186470000      INR
...              ...       ...       ...       ...        ...      ...
2018-12-26  10635.45  10747.50  10534.55  10729.85     271943      INR
2018-12-27  10817.90  10834.20  10764.45  10779.80     470160      INR
2018-12-28  10820.95  10893.60  10817.15  10859.90     253087      INR
2018-12-31  10913.20  10923.55  10853.20  10862.55     186495      INR
2019-01-01  10881.70  10923.60  10807.10  10910.10     159405      INR

[247 rows x 6 columns]

More on Index Data Retrieval is found in the documentation here.

You can also retrieve a list of all stocks in a certain country:

df = investpy.get_stocks_list(country="india") # returns 'list'
df = investpy.get_stocks(country="india") # returns 'pandas.DataFrame' (shown below)
     country                     name  ... currency symbol
0      india     Aditya Birla Capital  ...      INR   ADTB
1      india                  Hubtown  ...      INR   HUBT
2      india              3i Infotech  ...      INR   TIIN
3      india                 3M India  ...      INR   TMIN
4      india                ABB India  ...      INR    ABB
...      ...                      ...  ...      ...    ...
1706   india           G K P Printing  ...      INR   GKPP
1707   india         Vivid Mercantile  ...      INR   VIVD
1708   india            White Organic  ...      INR   WHIE
1709   india      Parshva Enterprises  ...      INR   PAHV
1710   india  SK International Export  ...      INR   SKIN

[1711 rows x 6 columns]

Because investpy doesn’t offer something to get all stocks from a certain index, you will have to sort through them yourself. Fortunately, investpy does give you functions that allow you to get the recent, historical, etc. data about a specific index (mentioned above). If you are looking for data on each of those stocks, you could:

  1. Gather symbols of each stock in the Nifty 50 index as a list
  2. Set up a loop that gets the historical data for each stock
  3. Perform operations on data such as writing to CSV, etc.

Edit

You mentioned not being able to find data for MSCI Emerging Markets, which is a world index. Unfortunately, I could not specify world as a country. I do not know what the reasoning behind this is, but I did dive into the source code to find out what was happening:

It turns out that the world indices do exist inside of the investpy/resources/indices.csv, but they are filtered out in index_countries_as_list() because the world country does not exist inside of the variable INDEX_COUNTRIES in investpy/utils/constants.py. You may want to submit an issue here. Here are a few things I was able to do that verify that MSCIEF exists, but again, I am not sure there’s a way to get data regarding this index.

investpy.indices.get_indices_dict(country=None, columns=None, as_json=False)
{'country': 'world', 'name': 'MSCI Emerging Markets', 'full_name': 'MSCI Emerging Markets', 'symbol': 'MSCIEF', 'currency': 'USD', 'class': 'other_indices', 'market': 'global_indices'}

and:

investpy.indices.search_indices(by="name", value="MSCI Emerging Markets")
  country                   name  ...          class          market
0   world  MSCI Emerging Markets  ...  other_indices  global_indices

[1 rows x 7 columns]
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement