-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmain.py
398 lines (329 loc) · 12.3 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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
from mpl_toolkits.mplot3d import Axes3D
import yfinance as yf
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import cm
from PIL import Image
from GUI import *
import seaborn as sb
import numpy as np
import sys
# pre-set values
ticker = "AMD"
DateArray = yf.Ticker(ticker).options
strikeChoice = DateArray[2]
opt = yf.Ticker(ticker).option_chain(strikeChoice)
calls = opt.calls
puts = opt.puts
ArrayStore = None
def askForTicker():
# Prompt the user for a new ticker
global ticker, strikeChoice, opt
ticker = onButton()
print("Ticker was: " + ticker)
# pickAStrike2() # <----- causes ZSH error (toDO)
def getOptionsChain(inputString):
# Retrieve the Options Chain Expiration dates from Yahoo Finance
YFticker = yf.Ticker(inputString)
global DateArray
tempTuple = ("Pick a Strike Price",)
DateArray = tempTuple+YFticker.options
def pickAStrike():
global strikeChoice, opt
pickStrikePrice(DateArray)
strikeChoice = returnChoice()
opt = yf.Ticker(ticker).option_chain(strikeChoice)
print("Strike Choice was: " + strikeChoice)
def pickAStrike2():
global strikeChoice, opt
strikeChoice = DateArray[2]
opt = yf.Ticker(ticker).option_chain(strikeChoice)
print("Strike Choice was: " + strikeChoice)
def sortCallsandPuts():
global calls, puts, opt
calls = opt.calls
calls = cleaner(calls)
calls['Mid Price'] = calls.apply(lambda row: (row.ask + row.bid)/2, axis=1)
calls = calls.drop(columns=["ask", "bid"])
calls = calls[["strike", "Mid Price", "openInterest", "volume"]]
puts = opt.puts
puts = cleaner(puts)
puts['Mid Price'] = puts.apply(lambda row: (row.ask + row.bid)/2, axis=1)
puts = puts.drop(columns=["ask", "bid"])
puts = puts[["strike", "Mid Price", "openInterest", "volume"]]
def cleaner(object):
return object.drop(columns=["contractSymbol", "lastTradeDate", "lastPrice",
"change", "percentChange", "impliedVolatility", "inTheMoney", "contractSize", "currency"])
def heatCleaner(object):
object = object.drop(columns=["contractSymbol", "lastTradeDate", "lastPrice",
"change", "percentChange", "volume", "inTheMoney", "contractSize", "currency", "impliedVolatility", "ask", "bid"])
object = object[["strike", "openInterest"]]
return object
def heatCleanerVOLUME(object):
object = object.drop(columns=["contractSymbol", "lastTradeDate", "lastPrice",
"change", "percentChange", "openInterest", "inTheMoney", "contractSize", "currency", "impliedVolatility", "ask", "bid"])
object = object[["strike", "volume"]]
return object
def getCalls():
print("****************** Calls *********************")
print(calls)
def getPuts():
print("****************** Puts *********************")
print(puts)
def displayOptionsChain():
print("Length of the Chain: " + str(len(DateArray)) + "\n")
print("\n".join(DateArray))
def displayCleanOptionChain():
print("****************** Calls ********************** ---[" + ticker.upper(
)+"]--- ******************** Puts ********************* ")
temp = {' ': [""]}
tempPD = pd.DataFrame(data=temp)
merged = pd.concat([calls, tempPD, puts], axis=1)
merged = merged.fillna("")
# merged.style.hide_index()
print(merged.to_string(index=False))
############
def OIChart():
sortCallsandPuts()
callData = calls.drop(columns=['Mid Price', 'volume'])
putData = puts.drop(columns=['Mid Price', 'volume'])
finalFrame = pd.DataFrame(callData)
finalFrame.rename(columns={'openInterest': 'Calls'}, inplace=True)
tempFrame = pd.DataFrame(putData)
tempFrame.rename(columns={'openInterest': 'Puts'}, inplace=True)
# finalFrame = pd.concat([finalFrame, tempFrame])
finalFrame = pd.merge(finalFrame, tempFrame, on='strike')
finalFrame.plot.bar(figsize=(20, 8), x="strike", y=["Calls", "Puts"],
title="Open Interest for "+ticker.upper()+" all options at every strike on "+strikeChoice)
####################
# fig = plt.figure()
# a = ScrollableWindow(fig)
plt.savefig("out.png")
plt.clf()
img = Image.open('out.png')
img.show()
# plt.show(block=True)
print("***********************")
# dataframe place NaN with 0
def CallsOIMap(): # plt.style.use("dark_background")
callsArray = heatCleaner(opt.calls)
callsArray.rename(columns={'openInterest': DateArray[1]}, inplace=True)
for x in range(2, len(DateArray)-1):
opt2 = yf.Ticker(ticker).option_chain(DateArray[x])
callsArray2 = heatCleaner(opt2.calls)
callsArray2.rename(
columns={'openInterest': DateArray[x]}, inplace=True)
callsArray = pd.merge(callsArray, callsArray2, on='strike')
callsArray.set_index('strike', inplace=True)
callsArray = callsArray.fillna(0)
print(callsArray)
heat_map = sb.heatmap(callsArray, cmap="Reds", linewidths=0)
global ArrayStore
ArrayStore = callsArray
plt.yticks(rotation=0)
plt.xticks(rotation=50)
plt.gca().invert_yaxis()
plt.show()
plt.clf()
def PutsOIMap(): # plt.style.use("dark_background")
callsArray = heatCleaner(opt.puts)
callsArray.rename(columns={'openInterest': DateArray[1]}, inplace=True)
for x in range(2, len(DateArray)-1):
opt2 = yf.Ticker(ticker).option_chain(DateArray[x])
callsArray2 = heatCleaner(opt2.puts)
callsArray2.rename(
columns={'openInterest': DateArray[x]}, inplace=True)
callsArray = pd.merge(callsArray, callsArray2, on='strike')
callsArray.set_index('strike', inplace=True)
callsArray = callsArray.fillna(0)
print(callsArray)
heat_map = sb.heatmap(callsArray, cmap="Blues", linewidths=0)
global ArrayStore
ArrayStore = callsArray
plt.yticks(rotation=0)
plt.xticks(rotation=50)
plt.gca().invert_yaxis()
plt.show()
plt.clf()
def CallsVolumeMap():
callsArray = heatCleanerVOLUME(opt.calls)
callsArray.rename(columns={'volume': DateArray[1]}, inplace=True)
for x in range(2, len(DateArray)-1):
opt2 = yf.Ticker(ticker).option_chain(DateArray[x])
callsArray2 = heatCleanerVOLUME(opt2.calls)
callsArray2.rename(
columns={'volume': DateArray[x]}, inplace=True)
callsArray = pd.merge(callsArray, callsArray2, on='strike')
callsArray.set_index('strike', inplace=True)
callsArray = callsArray.fillna(0)
print(callsArray)
heat_map = sb.heatmap(callsArray, cmap="Reds", linewidths=0)
global ArrayStore
ArrayStore = callsArray
plt.yticks(rotation=0)
plt.xticks(rotation=50)
plt.gca().invert_yaxis()
plt.show()
plt.clf()
def PutsVolumeMap():
callsArray = heatCleanerVOLUME(opt.puts)
callsArray.rename(columns={'volume': DateArray[1]}, inplace=True)
for x in range(2, len(DateArray)-1):
opt2 = yf.Ticker(ticker).option_chain(DateArray[x])
callsArray2 = heatCleanerVOLUME(opt2.puts)
callsArray2.rename(
columns={'volume': DateArray[x]}, inplace=True)
callsArray = pd.merge(callsArray, callsArray2, on='strike')
callsArray.set_index('strike', inplace=True)
callsArray = callsArray.fillna(0)
print(callsArray)
heat_map = sb.heatmap(callsArray, cmap="Blues", linewidths=0)
global ArrayStore
ArrayStore = callsArray
plt.yticks(rotation=0)
plt.xticks(rotation=50)
plt.gca().invert_yaxis()
plt.show()
plt.clf()
def threedeegraph(object):
eg = object
# thickness of the bars
dx, dy = .8, .8
# prepare 3d axes
fig = plt.figure(figsize=(10, 6))
ax = Axes3D(fig)
# set up positions for the bars
xpos = np.arange(eg.shape[0])
ypos = np.arange(eg.shape[1])
# set the ticks in the middle of the bars
ax.set_xticks(xpos + dx/2)
ax.set_yticks(ypos + dy/2)
# create meshgrid
# print xpos before and after this block if not clear
xpos, ypos = np.meshgrid(xpos, ypos)
xpos = xpos.flatten()
ypos = ypos.flatten()
# the bars starts from 0 attitude
zpos = np.zeros(eg.shape).flatten()
# the bars' heights
dz = eg.values.ravel(order='F')
# plot and color
values = np.linspace(0.2, 1., xpos.ravel().shape[0])
colors = cm.rainbow(values)
ax.bar3d(xpos, ypos, zpos, dx, dy, dz, color=colors)
# put the column / index labels
ax.w_yaxis.set_ticklabels(eg.columns)
ax.w_xaxis.set_ticklabels(eg.index)
# name the axes
ax.set_xlabel('Strike')
# ax.set_ylabel('Date')
ax.set_zlabel('Open Interest / Volume')
# fig.colorbar(surf1, ax=ax1, shrink=0.5, aspect=5)
plt.show()
plt.clf()
def askForStrikePrice():
pickStrikePrice(calls['strike'].astype(str).tolist())
def setticker():
askForTicker()
getOptionsChain(ticker)
def optionchainMENU():
sortCallsandPuts()
displayOptionsChain()
def callOI3dmenu():
CallsOIMap()
threedeegraph(ArrayStore)
def putOI3dmenu():
PutsOIMap()
threedeegraph(ArrayStore)
def callVolumemenu():
CallsVolumeMap()
threedeegraph(ArrayStore)
def putVolumemenu():
PutsVolumeMap()
threedeegraph(ArrayStore)
def askForTickerMENU():
askForTicker()
getOptionsChain(ticker)
pickAStrike2()
def mainMENUswitch():
def switchBoard(arguement):
switcher = {
"setTicker": lambda: askForTickerMENU(),
"setStrike": lambda: pickAStrike(),
"optionChain": lambda: optionchainMENU(),
"OImage": lambda: OIChart(),
"CallVolumeMap": lambda: CallsVolumeMap(),
"CallOIMap": lambda: CallsOIMap(),
"PutVolumeMap": lambda: PutsVolumeMap(),
"PutsOIMap": lambda: PutsOIMap(),
"CallVolume3D": lambda: callVolumemenu(),
"CallOI3D": lambda: callOI3dmenu(),
"PutVolume3D": lambda: putVolumemenu(),
"PutOI3D": lambda: putOI3dmenu(),
"exit": lambda: sys.exit,
}
return switcher.get(arguement, lambda: "error")()
print("******* \n Welcome to Sam's Option Scanner \n *******")
while(True):
print("PICK ONE: setticker, setstrike, optionchain, OIimage, CallVolumeMap, CallOIMap, PutVolumeMap, PutOIMap, CallVolume3D, CallOI3D, PutVolume3D, PutOI3D, exit")
choice = input()
switchBoard(choice)()
def mainMENUnested():
def repeat():
print("PICK ONE: setticker, setstrike, optionchain, OIimage, CallsVolumeMap, CallsOIMap, PutsVolumeMap, PutsOIMap, CallVolume3D, CallOI3D, PutVolume3D, PutOI3D, exit")
choice = input()
if choice == "setticker":
# askForTicker() # get ticker of choice from user
# getOptionsChain(ticker) # get entire option chain from yFinance
askForTickerMENU()
repeat()
elif choice == "setstrike":
pickAStrike() # asks user for specific date
# askForStrikePrice() # prompts user to choose a strike from the table
# displayOptionsChain() #show entire option chain
repeat()
elif choice == "optionchain":
sortCallsandPuts() # breaks options chain into essential data and sorts by calls / puts
displayCleanOptionChain() # displays calls and puts as a clean table
repeat()
elif choice == "OIimage":
OIChart()
repeat()
elif choice == "CallsVolumeMap":
CallsVolumeMap()
repeat()
elif choice == "CallsOIMap":
CallsOIMap()
repeat()
elif choice == "PutsVolumeMap":
PutsVolumeMap()
repeat()
elif choice == "PutsOIMap":
PutsOIMap()
repeat()
elif choice == "CallVolume3D":
CallsVolumeMap()
threedeegraph(ArrayStore)
repeat()
elif choice == "CallOI3D":
CallsOIMap()
threedeegraph(ArrayStore)
repeat()
elif choice == "PutVolume3D":
PutsVolumeMap()
threedeegraph(ArrayStore)
repeat()
elif choice == "PutOI3D":
PutsOIMap()
threedeegraph(ArrayStore)
repeat()
elif choice == "exit":
sys.exit()
else:
print("unexpected choice")
repeat()
print("******* \n Welcome to Sam's Option Scanner \n *******")
repeat()
# mainMENUswitch()
mainMENUnested()
print("All done")