-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaxPainHistory.py
80 lines (63 loc) · 2.87 KB
/
MaxPainHistory.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
import pandas as pd
import yfinance as yf
import datetime
def options_chain(tk):
# Builds the Options chain of tk
opt = pd.DataFrame()
opt_calls = tk.option_chain().calls
opt_puts = tk.option_chain().puts
opt = pd.concat([opt_calls, opt_puts])
# Boolean column if the option is a CALL
opt['CALL'] = opt['contractSymbol'].str[4:].apply(lambda x: "C" in x)
opt[['bid', 'ask', 'strike','openInterest']] = opt[['bid', 'ask', 'strike', 'openInterest']].apply(pd.to_numeric)
# Drop unnecessary and/or meaningless columns
opt = opt.drop(columns = ['contractSize', 'currency', 'change', 'percentChange',
'lastTradeDate', 'lastPrice', 'contractSymbol', 'bid', 'ask', 'impliedVolatility',
'inTheMoney'])
return opt
def total_loss_on_strike(chain, strike):
callChain = chain[chain['CALL'] == True]
callChain = callChain.dropna()
in_money_calls = callChain[callChain['strike'] < strike][["openInterest", "strike"]]
in_money_calls["CLoss"] = (strike - in_money_calls['strike']) * in_money_calls["openInterest"]
putChain = chain[chain['CALL'] != True]
putChain = putChain.dropna()
in_money_puts = putChain[putChain['strike'] > strike][["openInterest", "strike"]]
in_money_puts["PLoss"] = (in_money_puts['strike'] - strike) * in_money_puts["openInterest"]
total_loss = in_money_calls["CLoss"].sum() + in_money_puts["PLoss"].sum()
return total_loss
# YFinance Object of the Ticker
ticker = "GME"
tk = yf.Ticker(ticker)
# Export all Strikes
chain = options_chain(yf.Ticker(ticker))
# Create a list of the Strikes to Itterate over
strikes = chain['strike'].values.tolist()
# Remove Duplicates (eg. A Call and A Put at $10 is only used once), Arrange by Value
strikes = sorted(list(dict.fromkeys(strikes)))
# Initialize an Array for the Losses at Each Strike
losses = []
for expiry_strike in strikes:
losses.append([total_loss_on_strike(chain, expiry_strike)])
# Un-comment below to audit the losses at each strike price
#print("Losses at the expirary price of: " + str(expiry_strike) + " were: " + str(losses[-1]))
# Max pain is the minimum loss to option writers
max_pain = strikes[losses.index(min(losses))]
print(f"Max Pain: {max_pain}")
print(f"Price of {ticker}: {round(tk.history()['Close'].iloc[-1], 4)}")
#####
# Add the Values into a SQL Database at current date/time
# Either use multiple tables (ie. use maxpain.Connor, maxpain.Chives, etc.)
# or use a "personalID" number to differentiate which theory is recorded in that value
#
# CREATE TABLE maxPain.ConnorMethod (
# personalID int,
# date DATE,
# maxPain FLOAT,
# price FLOAT,
# );
#
# After some time of regular capture, use the database to pull up graphs of the max pain of the ticker over time
#
#
#####