├── .gitignore ├── README.md ├── Screenshot.png └── main.py /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_STORE 2 | 3 | data/ 4 | 5 | # Virtualenv related 6 | bin/ 7 | include/ 8 | pip-selfcheck.json 9 | 10 | # Django related 11 | # src//settings/local.py 12 | # static-cdn/ # any collected static files 13 | 14 | 15 | # Byte-compiled / optimized / DLL files 16 | __pycache__/ 17 | *.py[cod] 18 | *$py.class 19 | 20 | # C extensions 21 | *.so 22 | 23 | # Distribution / packaging 24 | .Python 25 | build/ 26 | develop-eggs/ 27 | dist/ 28 | downloads/ 29 | eggs/ 30 | .eggs/ 31 | lib/ 32 | lib64/ 33 | parts/ 34 | sdist/ 35 | var/ 36 | share/ 37 | wheels/ 38 | *.egg-info/ 39 | .installed.cfg 40 | *.egg 41 | pyvenv.cfg 42 | 43 | # PyInstaller 44 | # Usually these files are written by a python script from a template 45 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 46 | *.manifest 47 | *.spec 48 | 49 | # Installer logs 50 | pip-log.txt 51 | pip-delete-this-directory.txt 52 | 53 | # Unit test / coverage reports 54 | htmlcov/ 55 | .tox/ 56 | .coverage 57 | .coverage.* 58 | .cache 59 | nosetests.xml 60 | coverage.xml 61 | *.cover 62 | .hypothesis/ 63 | 64 | # Translations 65 | *.mo 66 | *.pot 67 | 68 | # Django stuff: 69 | *.log 70 | local_settings.py 71 | 72 | # Flask stuff: 73 | instance/ 74 | .webassets-cache 75 | 76 | # Scrapy stuff: 77 | .scrapy 78 | 79 | # Sphinx documentation 80 | docs/_build/ 81 | 82 | # PyBuilder 83 | target/ 84 | 85 | # Jupyter Notebook 86 | .ipynb_checkpoints 87 | 88 | # pyenv 89 | .python-version 90 | 91 | # celery beat schedule file 92 | celerybeat-schedule 93 | 94 | # SageMath parsed files 95 | *.sage.py 96 | 97 | # Environments 98 | .env 99 | .venv 100 | env/ 101 | venv/ 102 | ENV/ 103 | 104 | # Spyder project settings 105 | .spyderproject 106 | .spyproject 107 | 108 | # Rope project settings 109 | .ropeproject 110 | 111 | # mkdocs documentation 112 | /site 113 | 114 | # mypy 115 | .mypy_cache/ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Open-Interest-Data-Extractor 2 | 3 | This is boiler plate code in python to get Option Chain from NSE official website. 4 | Data is shown on cconsole in tabular format. 5 | 6 | ![Sample output](https://raw.githubusercontent.com/manddar/Open-Interest-Data-Extractor/main/Screenshot.png "Sample output") 7 | 8 | **Before running script** 9 | 10 | - 👉 Install `requests` library 11 | - 👉 Stop VPN if you have one, IPs from other countries are blocked (I guess) 12 | 13 | 14 | 📋 **Todo:** 15 | 16 | - 👉 Exception handling (if request is timed out, script thorws error on console) 17 | - 👉 JSON output 18 | - 👉 Restructure code and comment whereever possible 19 | 20 | 💾 **Credit:** 21 | 22 | - 👉 [VarunS2002](https://github.com/VarunS2002/) for his repo [Python-NSE-Option-Chain-Analyzer](https://github.com/VarunS2002/Python-NSE-Option-Chain-Analyzer). 23 | -------------------------------------------------------------------------------- /Screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manddar/Open-Interest-Data-Extractor/d672ee7b4b9336c0befc73627d224bf72c1b207a/Screenshot.png -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | import requests 2 | import json 3 | import math 4 | 5 | # Python program to print 6 | # colored text and background 7 | def strRed(skk): return "\033[91m {}\033[00m".format(skk) 8 | def strGreen(skk): return "\033[92m {}\033[00m".format(skk) 9 | def strYellow(skk): return "\033[93m {}\033[00m".format(skk) 10 | def strLightPurple(skk): return "\033[94m {}\033[00m".format(skk) 11 | def strPurple(skk): return "\033[95m {}\033[00m".format(skk) 12 | def strCyan(skk): return "\033[96m {}\033[00m".format(skk) 13 | def strLightGray(skk): return "\033[97m {}\033[00m".format(skk) 14 | def strBlack(skk): return "\033[98m {}\033[00m".format(skk) 15 | def strBold(skk): return "\033[1m {}\033[0m".format(skk) 16 | # Method to get nearest strikes 17 | def round_nearest(x,num=50): return int(math.ceil(float(x)/num)*num) 18 | def nearest_strike_bnf(x): return round_nearest(x,100) 19 | def nearest_strike_nf(x): return round_nearest(x,50) 20 | 21 | url_oc = "https://www.nseindia.com/option-chain" 22 | url_bnf = 'https://www.nseindia.com/api/option-chain-indices?symbol=BANKNIFTY' 23 | url_nf = 'https://www.nseindia.com/api/option-chain-indices?symbol=NIFTY' 24 | url_indices = "https://www.nseindia.com/api/allIndices" 25 | 26 | headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36', 27 | 'accept-language': 'en,gu;q=0.9,hi;q=0.8', 28 | 'accept-encoding': 'gzip, deflate, br'} 29 | 30 | sess = requests.Session() 31 | cookies = dict() 32 | 33 | # Local methods 34 | def set_cookie(): 35 | request = sess.get(url_oc, headers=headers, timeout=5) 36 | cookies = dict(request.cookies) 37 | 38 | def get_data(url): 39 | set_cookie() 40 | response = sess.get(url, headers=headers, timeout=5, cookies=cookies) 41 | if(response.status_code==401): 42 | set_cookie() 43 | response = sess.get(url_nf, headers=headers, timeout=5, cookies=cookies) 44 | if(response.status_code==200): 45 | return response.text 46 | return "" 47 | 48 | def set_header(): 49 | global bnf_ul 50 | global nf_ul 51 | global bnf_nearest 52 | global nf_nearest 53 | 54 | response_text = get_data(url_indices) 55 | data = json.loads(response_text) 56 | for index in data["data"]: 57 | if index["index"]=="NIFTY 50": 58 | nf_ul = index["last"] 59 | print("nifty") 60 | if index["index"]=="NIFTY BANK": 61 | bnf_ul = index["last"] 62 | print("banknifty") 63 | 64 | bnf_nearest=nearest_strike_bnf(bnf_ul) 65 | nf_nearest=nearest_strike_nf(nf_ul) 66 | 67 | def print_header(index="",ul=0,nearest=0): 68 | print(strPurple( index.ljust(12," ") + " => ")+ strLightPurple(" Last Price: ") + strBold(str(ul)) + strLightPurple(" Nearest Strike: ") + strBold(str(nearest))) 69 | 70 | def print_hr(): 71 | print(strYellow("|".rjust(70,"-"))) 72 | 73 | def print_oi(num,step,nearest,url): 74 | strike = nearest - (step*num) 75 | start_strike = nearest - (step*num) 76 | response_text = get_data(url) 77 | data = json.loads(response_text) 78 | currExpiryDate = data["records"]["expiryDates"][0] 79 | print(strPurple( "Expiry".ljust(12," ") + " => ")+strLightPurple(currExpiryDate)) 80 | print_hr() 81 | for item in data['records']['data']: 82 | if item["expiryDate"] == currExpiryDate: 83 | if item["strikePrice"] == strike and item["strikePrice"] < start_strike+(step*num*2): 84 | print(strCyan(str(item["strikePrice"])) + strGreen(" CE ") + "[ " + strBold(str(item["CE"]["openInterest"]).rjust(10," ")) + " ]" + strRed(" PE ")+"[ " + strBold(str(item["PE"]["openInterest"]).rjust(10," ")) + " ]") 85 | strike = strike + step 86 | 87 | set_header() 88 | print('\033c') 89 | print_hr() 90 | print_header("Nifty",nf_ul,nf_nearest) 91 | print_hr() 92 | print_oi(5,50,nf_nearest,url_nf) 93 | print_hr() 94 | print_header("Bank Nifty",bnf_ul,bnf_nearest) 95 | print_hr() 96 | print_oi(10,100,bnf_nearest,url_bnf) 97 | print_hr() --------------------------------------------------------------------------------