-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.py
97 lines (84 loc) · 3.65 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import requests
import json
import math
# Python program to print
# colored text and background
def strRed(skk): return "\033[91m {}\033[00m".format(skk)
def strGreen(skk): return "\033[92m {}\033[00m".format(skk)
def strYellow(skk): return "\033[93m {}\033[00m".format(skk)
def strLightPurple(skk): return "\033[94m {}\033[00m".format(skk)
def strPurple(skk): return "\033[95m {}\033[00m".format(skk)
def strCyan(skk): return "\033[96m {}\033[00m".format(skk)
def strLightGray(skk): return "\033[97m {}\033[00m".format(skk)
def strBlack(skk): return "\033[98m {}\033[00m".format(skk)
def strBold(skk): return "\033[1m {}\033[0m".format(skk)
# Method to get nearest strikes
def round_nearest(x,num=50): return int(math.ceil(float(x)/num)*num)
def nearest_strike_bnf(x): return round_nearest(x,100)
def nearest_strike_nf(x): return round_nearest(x,50)
url_oc = "https://www.nseindia.com/option-chain"
url_bnf = 'https://www.nseindia.com/api/option-chain-indices?symbol=BANKNIFTY'
url_nf = 'https://www.nseindia.com/api/option-chain-indices?symbol=NIFTY'
url_indices = "https://www.nseindia.com/api/allIndices"
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',
'accept-language': 'en,gu;q=0.9,hi;q=0.8',
'accept-encoding': 'gzip, deflate, br'}
sess = requests.Session()
cookies = dict()
# Local methods
def set_cookie():
request = sess.get(url_oc, headers=headers, timeout=5)
cookies = dict(request.cookies)
def get_data(url):
set_cookie()
response = sess.get(url, headers=headers, timeout=5, cookies=cookies)
if(response.status_code==401):
set_cookie()
response = sess.get(url_nf, headers=headers, timeout=5, cookies=cookies)
if(response.status_code==200):
return response.text
return ""
def set_header():
global bnf_ul
global nf_ul
global bnf_nearest
global nf_nearest
response_text = get_data(url_indices)
data = json.loads(response_text)
for index in data["data"]:
if index["index"]=="NIFTY 50":
nf_ul = index["last"]
print("nifty")
if index["index"]=="NIFTY BANK":
bnf_ul = index["last"]
print("banknifty")
bnf_nearest=nearest_strike_bnf(bnf_ul)
nf_nearest=nearest_strike_nf(nf_ul)
def print_header(index="",ul=0,nearest=0):
print(strPurple( index.ljust(12," ") + " => ")+ strLightPurple(" Last Price: ") + strBold(str(ul)) + strLightPurple(" Nearest Strike: ") + strBold(str(nearest)))
def print_hr():
print(strYellow("|".rjust(70,"-")))
def print_oi(num,step,nearest,url):
strike = nearest - (step*num)
start_strike = nearest - (step*num)
response_text = get_data(url)
data = json.loads(response_text)
currExpiryDate = data["records"]["expiryDates"][0]
print(strPurple( "Expiry".ljust(12," ") + " => ")+strLightPurple(currExpiryDate))
print_hr()
for item in data['records']['data']:
if item["expiryDate"] == currExpiryDate:
if item["strikePrice"] == strike and item["strikePrice"] < start_strike+(step*num*2):
print(strCyan(str(item["strikePrice"])) + strGreen(" CE ") + "[ " + strBold(str(item["CE"]["openInterest"]).rjust(10," ")) + " ]" + strRed(" PE ")+"[ " + strBold(str(item["PE"]["openInterest"]).rjust(10," ")) + " ]")
strike = strike + step
set_header()
print('\033c')
print_hr()
print_header("Nifty",nf_ul,nf_nearest)
print_hr()
print_oi(5,50,nf_nearest,url_nf)
print_hr()
print_header("Bank Nifty",bnf_ul,bnf_nearest)
print_hr()
print_oi(10,100,bnf_nearest,url_bnf)
print_hr()