-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter_options_strike.py
39 lines (32 loc) · 1.42 KB
/
filter_options_strike.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
# region imports
from AlgorithmImports import *
from datetime import timedelta
# endregion
class StrikeFilterAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2020, 1, 1)
self.SetEndDate(2020, 1, 31)
self.SetCash(100000)
# Add the underlying equity
self.equity = self.AddEquity("SPY", Resolution.Minute).Symbol
# Add options and set a filter
option = self.AddOption("SPY", Resolution.Minute)
option.SetFilter(self.StrikeFilter)
self.optionSymbol = option.Symbol
def StrikeFilter(self, universe):
# Filter options with strikes in a broad range (later refined in OnData)
return (
universe.IncludeWeeklys()
.Strikes(-10, +10)
.Expiration(timedelta(0), timedelta(30))
)
def OnData(self, data):
if self.optionSymbol in data.OptionChains:
chain = data.OptionChains[self.optionSymbol]
underlyingPrice = self.Securities[self.equity].Price
for contract in chain:
# Only select options with strike within 5% of the underlying price
if abs(contract.Strike - underlyingPrice) / underlyingPrice < 0.05:
self.Debug("Selected option by strike: " + str(contract.Symbol))
# For demo purposes, exit after the first match
break