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:

JavaScript
JavaScript

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

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

JavaScript
JavaScript

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.

JavaScript
JavaScript

and:

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement