-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathputSpread.py
More file actions
137 lines (106 loc) · 4.1 KB
/
Copy pathputSpread.py
File metadata and controls
137 lines (106 loc) · 4.1 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
130
131
132
133
134
135
136
137
import ameritrade as am
import requests
# Calculate when spread return is zero
def getZeroProfit(stockPrice, sellStrike, buyStrike, sellPrice, buyPrice):
hitZero = 0
# Check that return is positive
data = calculateProfits(0, stockPrice,
sellStrike, buyStrike, sellPrice, buyPrice, 0)
if data is not None:
if data[6]>= 0:
spreadCost = data[5]
hitZero = (((buyStrike - spreadCost) / stockPrice) - 1) * 100
return hitZero
# Calculate max spread return
def calculateProfits(change, stockPrice, sellStrike, buyStrike, sellPrice,
buyPrice, hitsZero):
stock_Price = stockPrice * change
buyValue = stock_Price - buyStrike
sellValue = sellStrike - stock_Price
spreadValue = buyValue + sellValue
spreadCost = buyPrice - sellPrice
if buyPrice == 0:
spreadCost = 0.0
if sellPrice == 0:
spreadCost = 0.0
spreadProfit = spreadCost - spreadValue * -1.0
if spreadCost == 0:
percentReturn = 0.0
else:
percentReturn = spreadProfit / spreadCost * -1.0
return [stockPrice, buyStrike, sellStrike, buyPrice, sellPrice,
spreadCost, spreadProfit * -1, percentReturn * 100, hitsZero]
# Wrapper function returns Zero Profit and Expected Return
def getProfits(stock_Price, sell_Strike, buy_Strike, 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, buy_Strike, sell_Price,
buy_Price, hitsZero))
return outputs
# Pulls all relevant put spreads for a given stock
def putSpread(stock, expiration, protection, spreadReturn, spreadCost, openInterest, priceSpread):
data = am.callAPI('PUT', stock)
expDate = 'putExpDateMap'
selections = am.getDates(data, expDate)
x = expiration
outputs = []
# Check that Call has an Options Chain for Selected Date
try:
z = data[expDate][x]
except:
return
for y in data[expDate][x]:
bid = data[expDate][x][y][0]['bid']
ask = data[expDate][x][y][0]['ask']
underlyingPrice = data['underlyingPrice']
strikePrice = data[expDate][x][y][0]['strikePrice']
yPrice = (bid+ask)/2
if yPrice == 0.0:
continue
if strikePrice <= underlyingPrice:
continue
for s in data[expDate][x]:
# Only compare when sellstrike is higher
if s >= y:
continue
# Only use in the money calls
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
if sPrice == 0.0:
continue
if data[expDate][x][s][0]['strikePrice'] <= data['underlyingPrice']:
continue
#print(data[expDate][x][s][0]['description'])
if data[expDate][x][s][0]['strikePrice'] < data[expDate][x][y][0]['strikePrice']:
test = getProfits(data['underlyingPrice'],
data[expDate][x][s][0]['strikePrice'],
data[expDate][x][y][0]['strikePrice'],
sPrice, yPrice)
minProtection = protection
minPercentReturn = spreadReturn
minSpreadCost = spreadCost
naturalPrice = data[expDate][x][y][0]['ask'] - data[expDate][x][s][0]['bid']
midPrice = test[0][5]
# Refine program outputs
if test[0][8] is not None:
if test[0][8] > float(minProtection):
if test[0][7] >= float(minPercentReturn):
if test[0][5] >= float(minSpreadCost):
if data[expDate][x][y][0]['nonStandard'] == False:
if data[expDate][x][s][0]['openInterest'] >= int(openInterest):
if data[expDate][x][y][0]['openInterest'] >= int(openInterest):
if naturalPrice - midPrice <= float(priceSpread):
test[0][0] = str(data[expDate][x][s][0]['description'].split(" ")[0]) + " @ " + str(round(test[0][0], 2))
test[0][1] = str(test[0][1]) + " : " + str(data[expDate][x][y][0]['openInterest'])
test[0][2] = str(test[0][2]) + " : " + str(data[expDate][x][s][0]['openInterest'])
outputs.append(test)
flat_list = []
for sublist in outputs:
for item in sublist:
flat_list.append(item)
return am.Sort(flat_list)