-
Notifications
You must be signed in to change notification settings - Fork 2
/
plot.py
206 lines (166 loc) · 5.65 KB
/
plot.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env python
import tkinter as tk
import matplotlib.pyplot as plt
import json
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
filename = "option_chain.json"
atm = 0
def plot_open_interest_data(
spot, strikes, expiry_list, expiry, index, container_window
):
global atm
content = {}
with open(filename, "r") as json_file:
content = json.load(json_file)
index_dict = {
"NIFTY": {"slicer": 25, "lot_size": 50},
"BANKNIFTY": {"slicer": 100, "lot_size": 25},
"FINNIFTY": {"slicer": 25, "lot_size": 40},
"MIDCPNIFTY": {"slicer": 25, "lot_size": 75},
"RELIANCE":{"slicer":10, "lot_size": 250},
"USDINR": {
"slicer": 0.1250,
"lot_size": 1,
}, # USDINR is leveraged for 1000 USD for 1 Qty
}
atm_slicer = index_dict[index]["slicer"]
lot_size = index_dict[index]["lot_size"]
f_strikes = []
call_oi = []
put_oi = []
call_change_oi = []
put_change_oi = []
for item in strikes:
if abs(item - spot) < atm_slicer:
atm = item
# os.system("cls")
print(f"{index} Spot is : {spot}")
print(f"{index} ATM strike is : {atm}")
# print(strikes)
for item in content["records"]["data"]:
if item["strikePrice"] in strikes:
if item["expiryDate"] == expiry:
# check for strikes
f_strikes.append(item["strikePrice"])
# Check and add Call OI, Call OI change
if "CE" in item.keys():
call_oi.append(item["CE"]["openInterest"])
call_change_oi.append(item["CE"]["changeinOpenInterest"])
else:
call_oi.append(0)
call_change_oi.append(0)
# Check and add Put OI
if "PE" in item.keys():
put_oi.append(item["PE"]["openInterest"])
put_change_oi.append(item["PE"]["changeinOpenInterest"])
else:
put_oi.append(0)
put_change_oi.append(0)
else:
pass
else:
print("fail")
# def near_Atm_Strikes(f_strikes, atm, n):
# index = f_strikes.index(atm)
# start = max(0, index-n)
# before = f_strikes[start:index]
# end = min(len(f_strikes), index + n+1)
# after = f_strikes[index+1:end]
# near_atm_strikes = before + [atm] + after
# return near_atm_strikes
# f_strikes=near_Atm_Strikes(f_strikes=f_strikes, atm=atm, n=5)
atm_oi_yval = max(max(call_oi), max(put_oi))
atm_change_oi_yval = max(max(call_change_oi), max(put_change_oi))
# Contract values calculation in million
calls = round(lot_size * sum(call_oi) / 1000000, 2)
puts = round(lot_size * sum(put_oi) / 1000000, 2)
calls_change = round(lot_size * sum(call_change_oi) / 1000000, 2)
puts_change = round(lot_size * sum(put_change_oi) / 1000000, 2)
# Plotting Begins here
# splitting into subplots with shared strike price x-axis
fig, axs = plt.subplots(2, 1, sharex=True)
canvas = FigureCanvasTkAgg(fig, master=container_window)
canvas.draw()
canvas.get_tk_widget().pack()
# creating the Matplotlib toolbar
toolbar = NavigationToolbar2Tk(canvas, container_window)
toolbar.update()
# placing the toolbar on the Tkinter window
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
# plotting OI and atm strike as a subplot
axs[0].bar(
f_strikes,
call_oi,
color="green",
width=45,
edgecolor="black",
linewidth=1,
label=f"CALL OI : {calls}M",
)
axs[0].bar(
f_strikes,
put_oi,
color="red",
width=30,
edgecolor="black",
linewidth=1,
label=f"PUT OI : {puts}M",
)
axs[0].bar(
atm,
(atm_oi_yval + 10000),
color="blue",
label=f"ATM Strike {atm}",
linestyle="dashed",
linewidth=1,
width=7,
)
axs[0].annotate(f"ATM Strike {atm}", xy=(atm + 50, atm_oi_yval), xycoords="data")
axs[0].set_title(f"{index} Open Interest")
# plotting change in OI and atm strike as another sub plot
axs[1].bar(
f_strikes,
call_change_oi,
color="green",
width=45,
edgecolor="black",
linewidth=1,
label=f"CALL OI change : {calls_change}M",
)
axs[1].bar(
f_strikes,
put_change_oi,
color="red",
width=30,
edgecolor="black",
linewidth=1,
label=f"PUT OI change : {puts_change}M",
)
axs[1].bar(
atm,
(atm_change_oi_yval + 2000),
color="blue",
label=f"ATM Strike {atm}",
linestyle="dashed",
linewidth=1,
width=7,
)
axs[1].annotate(
f"ATM Strike {atm}", xy=(atm + 50, atm_change_oi_yval), xycoords="data"
)
# axs[1].annotate(f'Calls : {(lot_size*sum(call_change_oi)/1000000)}M \n Puts : {(lot_size*sum(put_change_oi)/1000000)}M',xy=(atm+100, atm_change_oi_yval-10000),xycoords='data')
axs[1].set_title(f"{index} Change in Open Interest")
# show legends for the plot
axs[0].legend()
axs[1].legend()
# Define the size of window or frame
container_window.geometry("1200x800")
def destroyer():
container_window.quit()
container_window.destroy()
# Destroy windows on main window close detection and exit python
container_window.wm_protocol ("WM_DELETE_WINDOW",destroyer)
# function to show the plot
# plt.show()
return