Take a look

Finance

How to pull live option chain data from NSE using python?

If you are trading in options whether stock or index, an unavoidable thing to watch before entry is the open interest data. Open interest data is updated by the exchange at a regular interval of time(3-4 mins).  Change in open interest data tells us the change in the market sentiment in real-time. By analyzing the change in the open interest it’s not much harder to identify the trend of the market whether it’s weekly or monthly. Fetching open interest data frequently for various analyses is a common practice performed by FIIs and DIIs and other Prop firms. As a retail trader trading in options, it is always advised to check the rate of change of open interest in call and put options at least till 300-500 strikes out of the money and In the money. Please find the below code snippet which will fetch the options data from National Stock Exchange in JSON  format. 

import requests
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; '
            'x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36'}


main_url = "https://www.nseindia.com/"
response = requests.get(main_url, headers=headers)
print(response.status_code)
cookies = response.cookies

url = "https://www.nseindia.com/api/option-chain-indices?symbol=BANKNIFTY"
bank_nifty_oi_data = requests.get(url, headers=headers, cookies=cookies)
print(bank_nifty_oi_data.status_code)
print("BN OI data", bank_nifty_oi_data.text)

url = "https://www.nseindia.com/api/option-chain-indices?symbol=NIFTY"
nifty_oi_data = requests.get(url, headers=headers, cookies=cookies)
print(nifty_oi_data.status_code)
print("Nifty OI data", nifty_oi_data.text)

You will get the data for both in the below format, further modifications can be performed according to your requirement.

A cumulative sum of options data from 9:15 to 11:30 AM can give us a better idea about the positions made by the real players with Big money. Also, watch out for the difference in the cumulative difference of each strike, for eg. for a strike of 39000 if the cumulative sum of call subtracted by the cumulative sum of a put is positive then you can consider buying put options or selling call options and vice versa. Always make sure the entry is either at the break of any important peak or trough.

Leave a Reply