forked from mmarose14/options
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.py
367 lines (277 loc) · 12.5 KB
/
options.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
import api
import files
import util
from datetime import datetime, timedelta
#Options criteria
MIN_VOLUME = 1 #Adjust for liquidity
MAX_BID_ASK_SPREAD = .10 #Adjust for liquidity
MIN_OPEN_INT = 1 #Minimum open interest
MAX_STRIKES_WIDTH = 5 #Minimize vertical spread loss
MAX_DELTA = -.11 #Delta threshold (short)
MIN_DELTA = .1 #Delta threshold (long) - Abs
MAX_THETA = -.1 #Theta threshold (long)
MIN_PREMIUM = .21 #Minimum credit received
MAX_OPTION_ASK = .03 #For buying options
def message(str):
print(str)
def findPutSpreads(ListOfSymbols):
matching_options = []
for symbol in ListOfSymbols:
print(f"Processing {symbol}...")
expirations_list = util.listOfLimitedExpirations(symbol,20,50)
numOptions = 0
for expiration in expirations_list:
options = api.getOptionsChain(symbol, expiration)
prev_option_strike = 0
prev_option_prem = 0
for option_item in options:
#Ignore weeklys?
if (option_item['expiration_type'] == "weeklys"):
break
option = util.gatherOptionData(option_item)
if (option['bid'] is None):
continue
#Estimated premium (mid price)
premium = round((option['bid'] + option['ask']) / 2,2)
#Figure out net credit from credit spread
net_credit = round ((premium - prev_option_prem),2)
if ('delta' in option):
delta = option['delta']
#Criteria here
if (option['type'] == "put"
and option['bid'] > 0.0
and premium >= MIN_PREMIUM
and delta >= MAX_DELTA
and (option['ask'] - option['bid']) <= MAX_BID_ASK_SPREAD
and option['volume'] > MIN_VOLUME
):
option_output = '{}, {}, BID:{}, ASK:{}, {}, {}(D), Premium: {}'\
.format(
option['expiration'],
option['strike'],
option['bid'],
option['ask'],
option['volume'],
option['delta'],
premium)
if (numOptions == 0):
matching_options.append(f"Symbol: {symbol}")
numOptions += 1
#Mark a strike where the width between the current strike and the previous strike meets the criteria
if (net_credit >= MIN_PREMIUM
and prev_option_prem > 0
and option['strike'] - prev_option_strike <= MAX_STRIKES_WIDTH
):
option_output = option_output + " <<<<<< "
option_output = option_output + f"{net_credit}"
#Print the screen when a match is found
print(f"Found: {option_output} - ({datetime.now()})")
matching_options.append(option_output)
if (option['type'] == "put"):
prev_option_prem = premium
prev_option_strike = option['strike']
return matching_options
def findCoveredCalls(ListOfSymbols):
matching_options = []
for symbol in ListOfSymbols:
last_price = api.getLastStockPrice(symbol)
expirations_list = util.listOfLimitedExpirations(symbol,20,50)
numOptions = 0
for expiration in expirations_list:
options = api.getOptionsChain(symbol, expiration)
for option_item in options:
option = util.gatherOptionData(option_item)
if (option['strike'] <= last_price):
continue
premium = round((option['bid'] + option['ask']) / 2,2)
profit = round(100 * ((option['strike'] - last_price) + premium))
debit = round ( (100 * last_price) - (100 * premium) )
if (option['bid'] > 0
and debit <= 1000
and option['type'] == "call"
and last_price <= 15
and option['volume'] > 0):
if (numOptions == 0):
matching_options.append(f"Symbol: {symbol}, Last: {last_price}")
numOptions += 1
option_output = '{} {}, {}, BID:{}, ASK:{}, {}, {}(D), Premium: {}, Debit: {}, Profit: {}' \
.format(option['type'],
option['expiration'],
option['strike'],
option['bid'],
option['ask'],
option['open_int'],
option['delta'],
premium,
debit,
profit
)
#Print the screen when a match is found
print(f"Found: {option_output}")
matching_options.append(option_output)
return matching_options
def findHigherRiskCreditSpreads(ListOfSymbols):
for symbol in ListOfSymbols:
expirations_list = util.listOfLimitedExpirations(symbol,0,90)
for expiration in expirations_list:
options = api.getOptionsChain(symbol, expiration)
cheapest_option = 0
highest_premium = 0
for option_item in options:
option = util.gatherOptionData(option_item)
#Estimated premium (mid price)
premium = round((option['bid'] + option['ask']) / 2,2)
if ('delta' in option):
delta = option['delta']
#Criteria
if (option['type'] == "put"
and option['open_int'] > 0
and option['bid'] > 0.0
):
if (cheapest_option == 0):
cheapest_option = option
cheapest_option['premium'] = premium
if (option['type'] == "put"
and option['bid'] > 0.0
and option['volume'] > MIN_VOLUME
and delta >= -.16
):
highest_premium = option
highest_premium['premium'] = premium
if (cheapest_option == 0):
continue
if (highest_premium == 0):
continue
### OUTPUT
high_spread = highest_premium['ask'] - highest_premium['bid']
net = round(highest_premium['premium'] - cheapest_option['premium'],2)
max_loss = ((highest_premium['strike'] - cheapest_option['strike']) - net)
if (max_loss > 0 and net > 0):
risk = round(max_loss / net, 2)
#Only print the options worth our while
if (net >= .3
and max_loss <= 8
and high_spread <= .15
):
#Print LONG option
output = 'Buy: {}, {}, {}'\
.format(
cheapest_option['expiration'],
cheapest_option['strike'],
cheapest_option['premium']
)
print(f"{symbol} {output}")
#Print SHORT option
output = 'Sell: {}, {}, {}'\
.format(
highest_premium['expiration'],
highest_premium['strike'],
highest_premium['premium']
)
print(f"{symbol} {output}")
#Print net profit
print(f"Net: {net}")
#Print Max loss
print(f"Max Loss: {max_loss}")
print(f"Risk: {risk}")
print("--")
def checkWheelPositions(Positions):
for position in Positions:
symbol = position['pos_symbol']
trade_price = position['pos_trade_price']
options = api.getOptionsChain(symbol,position['pos_expiration'])
for option_item in options:
if (option_item['strike'] == position['pos_strike']
and option_item['option_type'] == position['pos_type']
):
mid = (option_item['ask'] + option_item['bid']) / 2
gain = round(((trade_price - mid) * 100),2)
print("---")
print(f"Position {symbol} gain: {gain}")
post_data = {"value1":symbol, "value2":gain}
if (gain > 0):
profit_target = 40
profit = gain / trade_price
if (profit >= profit_target):
print(f"Profit: {profit}")
api.sendPush(post_data)
print("---")
def checkSpreads():
position = {}
position['symbol'] = "BIDU"
position['option_type'] = "put"
position['expiration'] = "2020-09-18"
position['leg1_strike'] = 105
position['leg1_trade_price'] = -53
position['leg2_strike'] = 100
position['leg2_trade_price'] = 30
"""
position['symbol'] = "DOCU"
position['option_type'] = "put"
position['expiration'] = "2020-09-18"
position['leg1_strike'] = 155
position['leg1_trade_price'] = -92
position['leg2_strike'] = 150
position['leg2_trade_price'] = 64
"""
symbol = position['symbol']
leg1_bid = 0
leg1_ask = 0
leg2_bid = 0
leg2_ask = 0
options = api.getOptionsChain(symbol,position['expiration'])
for option in options:
if (option['option_type'] == "put"):
if (option['strike'] == position['leg1_strike']):
leg1_bid = option['bid']
leg1_ask = option['ask']
if (option['strike'] == position['leg2_strike']):
leg2_bid = option['bid']
leg2_ask = option['ask']
short_mid = (leg1_ask + leg1_bid) / 2
long_mid = (leg2_ask + leg2_bid) / 2
long_mark = long_mid
short_mark = (-1 * short_mid)
mark = (long_mark + short_mark) * 100
trade_price = position['leg1_trade_price'] + position['leg2_trade_price']
gain = (-1 * trade_price) - (-1 * mark)
gain_rounded = round(gain, 2)
print(f"Symbol: {symbol} gain: {gain_rounded}")
def findWheels(ListOfSymbols):
matching_options = []
for symbol in ListOfSymbols:
print(f"Processing {symbol}...")
expirations_list = util.listOfLimitedExpirations(symbol,1,15)
numOptions = 0
for expiration in expirations_list:
options = api.getOptionsChain(symbol, expiration)
for option_item in options:
option = util.gatherOptionData(option_item)
if (option['bid'] is None):
continue
#Estimated premium (mid price)
premium = round((option['bid'] + option['ask']) / 2,2)
if ('delta' in option):
delta = option['delta']
if (option['type'] == "put"
and option['bid'] > 0
and delta >= -.16
and premium >= .19
and (option['ask'] - option['bid']) <= MAX_BID_ASK_SPREAD
and option['volume'] > 0
):
option_output = '{}, {}, BID:{}, ASK:{}, {}, {}(D), Premium: {}'\
.format(
option['expiration'],
option['strike'],
option['bid'],
option['ask'],
option['volume'],
option['delta'],
premium)
if (numOptions == 0):
matching_options.append(f"Symbol: {symbol}")
numOptions += 1
#Print the screen when a match is found
print(f"Found: {option_output} - ({datetime.now()})")
return matching_options