Analyzing World Stock Indices Performances with Python | by Intan Dea Yutami | Towards Data Science

Note from Towards Data Science’s editors: While we allow independent authors to publish articles in accordance with our rules and guidelines, we do not endorse each author’s contribution. You should not rely on an author’s works without seeking professional advice. See our Reader Terms for details.
The stock index is a list of selected companies, where its averaged price (or weighted-average) reflects the stock market. Stock indices also may reflect a certain industry or region that they cover. They are also often referred to as a benchmark to measure the performance of funds. The more important part of the stock index than its current price is the performance or price changes to a definitive previous moment, whether it is the day or three months before.
In the later part of this article is an introduction in how to evaluate stock indices performances in Python.
Data related to stock indices can be retrieved from Yahoo! Finance. In Python, there has been a popular module to retrieve data more easily from Yahoo! Finance. If it has not been installed yet, you can install it in your Jupyter Notebook by typing this chunk of codes, followed by importing the needed libraries/modules for processing.
Then, how do we get the stock data? Since this article is to analyze performances of world major indices, the next step to do is to get the list of world major indices from the Yahoo! Finance Website.
Now, here is the head/beginning content of the list taken from the website:
We can see that for each stock indices, there will be names and symbols in each row. The symbols will be important to retrieve the data. Now, it’s time to collect historical data for these stock indices.
To collect historical data per stock index, first, we need to call the Ticker module with the parameter of the symbol. For example, if we want to get the historical data of the S&P 500, we can type:
We also need to define the period of data retrieval. Should we get daily data or week data? It has to be inputted in the ‘period’ parameter. The start and end dates also have to be defined.
Here, the data will be retrieved daily (‘1d’) and started from 1 January 2020 until 30 September 2020. The end date stated in the parameter will not be included in the retrieved data (so, only retrieving until the day before that)
Now it’s time to collect historical data of ALL stock indices.
In the previous part, we have successfully collected historical stock data for world major stock indices from January 2010 until 30 September 2020. Now, it’s time to do some pre-processing for the analysis.
The first step that I would like to do is to categorize each index by the region. Here is my definition of the region:
Now, make a new column for the region.
def getRegion(ticker):    for k in region_idx.keys():        if ticker in region_idx[k]:            return kmsi['region']= msi.ticker.apply(lambda x: getRegion(x))
Since the stock prices are in different currencies, to get a general view of the performance, it would better to view that in the same unit or metric. Now, let’s see the price changes through the decade since 4 January 2010 for each of those indices. The changes will be represented percentage (%). The price change is using the Closing Price (‘Close’) for the day.
In the code chunk above, a new function is defined to calculate the price change. Then, for each row in the main dataset, that function is called to output the price change.
Now, for an easier process, let’s transform the dataset into tickers as columns, and rows as the price change for each date.
Now, the new data frame for price change will look like this:
It’s time to plot these price changes. For this part, the plots will be in regions.
Seen from the plot above, US-based stock indices have grown rapidly since 2016. NASDAQ stock price has been 5x higher since 2010. The other US-based stock indices such as S&P 500, Russell 2000, and Dow 30 have also multiplied their stock prices but not in the same performance as NASDAQ.
Some indices from other regions also have had their prices increased by 1–2.5x maximumly. Nikkei 225 (Japan), S&P/NZX 50 (New Zealand), S&P BSE SENSEX (India), and DAX (Germany) are the most profitable stock indices (if you invested somehow in them or some fund that reflects on them since January 2010) among the indices in their respective region.
We also can not leave to mention that all these stock indices were impacted by COVID-19. They all dropped significantly around the beginning of 2020, but generally, most of them have been crawling higher since then.
Short to Long Term Price Changes/Returns
When we are talking about stocks, usually we compare the current price to the historical prices in some previous months or years before. For this part, we are going to analyze the price changes in the short to long-term period.
To be clear and detailed, we compare on the same day but periods before. So, 6M (six months) on 30 September 2020 means the day 6 months before 30 September 2020, which is 30 March 2020. 1Y (one year) on the same date means 30 September 2019, and so on.
First, we subset the dataset to only 30 September 2020.
Now, to get historical prices, it’s a bit tricky. It can be that the day we want to refer to (past date) was not a business day. In addition to that, business days are not the same in every country. So, to handle that, let’s choose the last business day before that past date.
Now, let’s get the price changes in some definitive moments before.
Let’s plot the price change/return for those stock indices.
Referring to the plot above, all major stock indices performed positively (prices are increasing) in a period of 6 months. On the other hand, for other periods, there is a mix of performances among those indices. For 10 year period, most of the stock indices have been profitable, except for STI Index (but only a slight drop).
In conclusion, referring to the same plot, the most consistently profitable stock indices in general are:
S&P 500 (USA)
Dow 300 (USA)
NASDAQ (USA)
Nikkei 225 (Japan)
Shenzen Component Index (China)
S&P/NZX 50 Index Gross (New Zealand)
TSEC Weighted Index (Taiwan)
Please check the code for further reference in the link below: