How to calculate NIFTY 50 value from the stock prices in Python

Calculating The NIFTY Index

In this discussion, We will attempt to calculate the NIFTY Index in program directly from the stock prices. Till date, there has been many attempt to calculate the NIFTY index by various programmers but it all ended in non conclusive discussions because of lack of clarity. 

 

As We are aiming the impossible, Keep an open mind and think this as a constructive discussion rather than some validated assertion. 

Section I

Here is a snapshot of NIFTY as we are discussing. This image will act as a good reference point. 

Lets refer to this document – NIFTY 50 Methodology

In this document shared, in the page 11, You can see this formula – 

  • Free Float Market Capitalization = Shares outstanding * Price * IWF
  • Index Value = Current Market Value / Base Market Capital * Base Index Value (1000)

Also, in the Page 6, it is written that the Base capital was 2.06 trillion. 

df["free_float_market_cap"] = df['sharesOutstanding']*df['ltP']*df['investableWeightFactor']
net_free_float_market_cap =df["free_float_market_cap"].sum()
net_free_float_market_cap/2060000000000 * 1000

Output – 

39177.60991419329

Well, the value of NIFTY is not nowhere near 39177! What went wrong? 

Section II

In the previous article, while constructing an index fund, We were using the variable Net_Indexmcap_today because the marketcap was directly given in the API itself. Why calculate it again? Anyways, Lets compare Net_Indexmcap_today and net_free_float_market_cap out of curiocity.
print(net_free_float_market_cap)
print(Net_Indexmcap_today)

Output – 

80705876423238.16
80684368062351
The difference is not that much at all! It looks like snapshot at different time.
  • As net_free_float_market_cap is constructed out of LTP directly. It entails the immediate and realtime value.
  • Net_Indexmcap_today is made out of Indexmcap_today which is updated at the beginning of the day only.
Anyways, Let’s move forward with Net_Indexmcap_today.

Section III

Now, the only thing that can be changed here is the assumption i.e. value of base market capital using back calculation. 

Net_Indexmcap_today/18244.2 * 1000

Output – 

4422466759975.828
Now, Let’s test this hypothesis on the column "Indexmcap_yst" and if it comes all correct then it is more than enough for moment being!
today_nifty = Net_Indexmcap_today/4422466759975.828 * 1000
yst_nifty = df["Indexmcap_yst"].sum()/4422466759975.828 * 1000
print(today_nifty)
print(round(yst_nifty,2))

Output – 

18244.2
18159.95

Our hypothesized value of base capital gives the proper output of NIFTY50’s yesterday’s price! 

That's all.

This is a part of the live discussion that happened on Unofficed Discussion Forum. Feel free to participate and engage in future discussions. Here are some agenda to be discussed later –

  • Will there be an arbitrage opportunity if there is a difference in the value?

Let’s discuss this further in the next part of this discussion. Don’t shy away to share your code snippets or variations in the forum if You trying something 🙂

1 Comment
Collapse Comments

Wonderfull content!!!

Leave a Comment

Your email address will not be published. Required fields are marked *