This is the pine script which calculate the nifty 50 .
Take each stock contribute to nifty 50 and find it's .
Multiply the same with contribution percentage of the same on Nifty 50
Add up all of them and find the total .
There is a similar script by @daytraderph which is built for Bank Nifty (custom ) . I took the same and built for Nfity.
Nifty has 50 stocks and you cant call security method more than 40 times from one Pine script, so this is the limitation of this script. It consider top 40 stocks and find the (which contribute pretty much around 95% of the ) and convert the same to 100 %
In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in a publication is governed by House Rules. You can favorite it to use it on a chart.