-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallSpread.py
More file actions
129 lines (105 loc) · 4.27 KB
/
Copy pathcallSpread.py
File metadata and controls
129 lines (105 loc) · 4.27 KB
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
import ameritrade as am
import requests
# Calculates where the Covered Call Spread stops being profitable
def getZeroProfit(stockPrice, sellStrike, buyStrike, sellPrice, buyPrice):
hitZero = 0
# Check that return is positive
data = calculateProfits(0, stockPrice,
sellStrike, 0, buyStrike, 0, sellPrice, buyPrice, 0)
if data is not None:
if data[5]>= 0:
spreadCost = data[5]
hitZero = ((stockPrice - (buyStrike + spreadCost)) / stockPrice) * 100
return hitZero
# Identifies profit return for the Covered Call Spread
def calculateProfits(change, stockPrice, sellStrike, sellInterest, buyStrike,
buyInterest, sellPrice, buyPrice, hitsZero):
stock_Price = stockPrice * change
buyValue = float(stock_Price) - float(buyStrike)
sellValue = sellStrike - stock_Price
if sellStrike >= stock_Price:
sellValue = 0.00
spreadValue = buyValue + sellValue
spreadCost = buyPrice - sellPrice
if buyPrice == 0:
spreadCost = 0.0
if sellPrice == 0:
spreadCost = 0.0
spreadProfit = spreadValue - spreadCost
if spreadCost == 0:
percentReturn = 0.0
else:
percentReturn = spreadProfit / spreadCost
return [stockPrice, str(buyStrike) + " : " + str(buyInterest), str(sellStrike) + " : " + str(sellInterest),
buyPrice, sellPrice, spreadCost, spreadProfit, percentReturn * 100, hitsZero]
# Joins the Options Spread profit and durability into a final table
def getProfits(stock_Price, sell_Strike, sell_interest, buy_Strike, buy_interest, sell_Price, buy_Price):
outputs = []
hitsZero = getZeroProfit(stock_Price, sell_Strike, buy_Strike,
sell_Price, buy_Price)
outputs.append(calculateProfits(1.1, stock_Price, sell_Strike, sell_interest,
buy_Strike, buy_interest, sell_Price, buy_Price, hitsZero))
return outputs
# Combines and calls all functions for Covered Call Spreads
def callSpread(stock, expiration, protection, spreadReturn, spreadCost, openInterest, priceSpread):
data = am.callAPI('CALL', stock)
expDate = 'callExpDateMap'
selections = am.getDates(data, expDate)
x = expiration
# Array used to store all final outputs
outputs = []
# Check that the Call has an Options Chain for Selected Date
try:
y = data[expDate][x]
except:
return
# Iterate all strikes
for y in data[expDate][x]:
bid = data[expDate][x][y][0]['bid']
ask = data[expDate][x][y][0]['ask']
yPrice = (bid+ask)/2
underlyingPrice = data['underlyingPrice']
strikePrice = data[expDate][x][y][0]['strikePrice']
# Second loop for comparing all strikes against eachother
for s in data[expDate][x]:
if s >= y: # Only compare when SellStrike is higher for Debit spreads
continue
if data[expDate][x][s][0]['inTheMoney'] == 'false':
continue
bid = data[expDate][x][s][0]['bid']
ask = data[expDate][x][s][0]['ask']
sPrice = (bid+ask) / 2
#print(data[expDate][x][s][0]['description'])
test = getProfits(data['underlyingPrice'],
data[expDate][x][y][0]['strikePrice'],
data[expDate][x][y][0]['openInterest'],
data[expDate][x][s][0]['strikePrice'],
data[expDate][x][s][0]['openInterest'],
yPrice, sPrice)
# Narrowing parameters
minProtection = protection
minPercentReturn = spreadReturn
minSpreadCost = spreadCost
naturalPrice = data[expDate][x][s][0]['ask'] - data[expDate][x][y][0]['bid']
midPrice = test[0][5]
# Refine program outputs
if test[0][8] is not None:
if float(test[0][8]) >= float(minProtection):
if float(test[0][7]) >= float(minPercentReturn):
if float(test[0][5]) >= float(minSpreadCost):
if data[expDate][x][s][0]['openInterest'] >= int(openInterest):
if data[expDate][x][y][0]['openInterest'] >= int(openInterest):
if data[expDate][x][y][0]['nonStandard'] == False:
if naturalPrice - midPrice <= float(priceSpread):
# Add stock symbol to table rows
test[0][0] = str(data[expDate][x][s][0]['description'].split(" ")[0]) + " @ " + str(round(test[0][0], 2))
outputs.append(test)
# If no results after refining, don't print
if outputs == []:
return
# Flatten, Sort, and Tabulate final outputs
flat_list = []
for sublist in outputs:
for item in sublist:
flat_list.append(item)
return am.Sort(flat_list)