# Understanding and Building A Market Index With Python

In this post we are going to talk about market indexes and how to build a market index with Python. A market index is made of individual securities. And it intends to represent a given market segment, asset class, geography region, etc. (e.g. a country, a particular industry, etc.). Market indexes are widely used in order to:

• Measure performance of investment managements,
• Gauge the current trend of the market
• Estimate market risk

All of us are probably familiar with some very popular indexes such as NASDAQ or the S&P500 among others. I even have some other post showing how to calculate S&P500 returns using Python. In this post, I will not go into details of each of the famous indexes. Instead, we are going to focus more on learning how they are built and we will build our own indexes using Python.

## How to Build a Market Index?

Building a market index is quite easy. We can basically do it by following below three steps

1. First, we need to select our market target. E.g. what industry or regions we want our index to represent
2. Second, we need to select securities representing such index. Depending on how many securities we want to include, we can include the whole population or just a sample of them.
3. Determine the weight of each security.

Point one and two is up to us and it is quite easy to do. For example, the S&P500 represents the stock performance of 500 large companies listed on exchanges in the US.

Point three, determining the weighting of each security in the index is a bit more tricky. Depending on the weighting type selected, we will find advantages and disadvantages. Let’s have a look at each of them:

## Market Index Weighting

#### Price Weighted Index

This type of weighting is very simple. It is simply weighting each of the securities according to their current share price.

An example of index using price weighting is the Dow Jones Industrial Average.

Weight of a single security = Price of security / Price of all securities in the index.

Problem with this weighting type is that securities with the higher share prices will be overrepresented on the index.

#### Similar(Equal) Weighted Index

This is probably the easiest index to build. It simply gives the same weight to each of the securities included in an index.

The problem with this weighting type is that securities with lower value are overrepresented. It favours small companies.

Daily price changes impacts the weighting of each security in the index. Therefore, daily rebalancing is required to keep an equally weighting. Rebalancing leads to open and sell stocks in the market daily to keep the weight constant and therefore, transaction costs may be quite high.

Weight of a single security = 1 / Number of securities in the index

#### Market Capitalisation Weighting

This weighting type is very similar to the price weighting. It simply takes the market capitalisation of each security divided by the total capitalisation of all securities.

The problem with this kind of weighting is that if a security is overpriced, it will have a bigger weight on the index. Therefore, the index value will be bias toward longer companies performance.

Weight of a single security = Market Capitalisation of single security / total Market Capitalisation

where Market Capitalization = price * quantity

## Build a Market Index with Python

Let’s now build in Python three indexes with the same securities using the three weighting types mentioned above. That will make clearer how different results we obtain by just changing the weighting type.

To do so, we are going to build a 3 stock index. The selected stocks will be Google, Apple and Microsoft. To build our indexes, we need to get prices and market capitalisation for each of the securities.

We will get the financial data from financialmodelingprep. It offers 250 API calls free per month which should be more than enough for this exercise.

#### Building a Price Weighting with Python

Below code will do the work for us. It is pretty easy code, so no further explanation is required. The script simply extracts the stock prices for each of the securities and compute the weighting according to the current share price:

``````import requests
your_api_key = 'your api key from financial modeling prep'

#get quote data from API
AAPL = requests.get(f'https://financialmodelingprep.com/api/v3/quote/AAPL?apikey={your_api_key}').json()
MSFT = requests.get(f'https://financialmodelingprep.com/api/v3/quote/MSFT?apikey={your_api_key}').json()
GOOG = requests.get(f'https://financialmodelingprep.com/api/v3/quote/GOOG?apikey={your_api_key}').json()

#Parse PRice
AAPL_price = AAPL[0]['price']
MSFT_price = MSFT[0]['price']
GOOG_price = GOOG[0]['price']

#calculate Weights
Weight_AAPL = AAPL_price / (AAPL_price+MSFT_price+GOOG_price)
Weight_MSFT = MSFT_price / (AAPL_price+MSFT_price+GOOG_price)
Weight_GOOG = GOOG_price / (AAPL_price+MSFT_price+GOOG_price)

print('Apple Weight is: ' + str(Weight_AAPL) + '  Microsoft Weight is: ' + str(Weight_MSFT) + '  Google Weight is: ' + str(Weight_GOOG)  )``````

The outcome of our price index is the following:

Apple Weight is: 0.056, Microsoft weight is: 0.10, Google Weight is: 0.84

I do not really like this weighting type since the current share price is not indicative of anything. For example, Google has the highest price in the market and that is the reason why it has a bigger weight in our index. Google stock price changes will affect a lot the index value.

#### Equal Weighted Index with Python

This is the easiest to build. Since, we have three stocks, each stock will represent 33% of the index. That is our weight.

There is no need to calculate anything on Python. However, take into account that as soon as the price of the security changes, we will need to rebalance the portfolio in order to keep the weight constant.

#### Market Cap Weighting with Python

This is my favourite index. Each stock will be reprented according to the current market capitalisation. Let’s see the weight of each of our three securities if we were to use this index:

``````import requests

#get quote data from API
AAPL = requests.get(f'https://financialmodelingprep.com/api/v3/quote/AAPL?apikey={your_api_key}').json()
MSFT = requests.get(f'https://financialmodelingprep.com/api/v3/quote/MSFT?apikey={your_api_key}').json()
GOOG = requests.get(f'https://financialmodelingprep.com/api/v3/quote/GOOG?apikey={your_api_key}').json()

#Parse MarketCap
AAPL_marketCap = AAPL[0]['marketCap']
MSFT_marketCap = MSFT[0]['marketCap']
GOOG_marketCap = GOOG[0]['marketCap']

#calculate Weights
Weight_AAPL = AAPL_marketCap / (AAPL_marketCap+MSFT_marketCap+GOOG_marketCap)
Weight_MSFT = MSFT_marketCap / (AAPL_marketCap+MSFT_marketCap+GOOG_marketCap)
Weight_GOOG = GOOG_marketCap / (AAPL_marketCap+MSFT_marketCap+GOOG_marketCap)

print('Apple Weight is: ' + str(Weight_AAPL) + '  Microsoft Weight is: ' + str(Weight_MSFT) + '  Google Weight is: ' + str(Weight_GOOG)  )``````

As shown below, the weight of each of the securities is quite different compared to the other two indexes types. Since Apple is the stock with the highest market capitalisation of the three, it also has the biggest weight in the market cap index type.

Apple Weight is: 0.41 Microsoft Weight is: 0.33 Google Weight is: 0.24

## Wrapping Up

Each weighting type give us different weighting for the three securities in our index. This obviously has an impact on the index value. Below is a quick comparison of the values of the index using the latest price data:

Price Weighting Index Value = 0.056 * Apple Price (117) + 0.10 * Microsoft Price(210) + 0.84 * Google Price(1742) = 1490

Equal Weighting Index Value = 0.33 * Apple Price (117) + 0.33 * Microsoft Price(210) + 0.33 * Google Price(1742) = 682

Market Cap Weighting Index Value = 0.41 * Apple Price (117) + 0.33 * Microsoft Price(210) + 0.24 * Google Price(1742) = 534

If we were to buy one stock in the Price Weighted market index, we would need to pay 1,490\$. On the other hand, 682\$ are required to buy one stock in the equal weighted index value and only 534\$ to get one stock in the market cap weighting index value.

As shown in this post, the weighting type is a crucial aspect to determine the value of an index.

Hope that you have enjoyed the post on how to build a market index with Python. Looking forward for your comments and follows.

Check out my other posts on Python for Finance to learn more how great is Python applied to Finance.

## 2 thoughts on “Understanding and Building A Market Index With Python”

1. I was examining some of your blog posts on this website and I conceive this web site is rattling informative ! Retain posting . Betti Rowen Bigler

2. With thanks for sharing your superb website!