I'm trying to implement an algorithm using option spreads. To decrease the memory usages and manual filtering in “on_data”, I would like to create an option filter with 2 strike price ranges. For example, I want all the call options that have a strike price between 0.7 and 0.8 of the underlying stock price AND all the call options that have a strike price between 1.03 and 1.05 of the underlying stock price. I don't want to see any other options in the option chain.
It feels like the right idea is to use “.set_filter” in the “initialize” method:
class MySpreadOptionAlgorithm(QCAlgorithm):
def initialize(self):
...
equity = self.algorithm.add_equity("SOME EQUITY SYMBOL", Resolution.HOUR)
equity.set_data_normalization_mode(DataNormalizationMode.RAW)
self.symbol_equity = equity.symbol
option = self.add_option(self.symbol_equity, Resolution.HOUR)
option.set_filter(filter_function)
...
Then, implement the filter_function:
def filter_function(universe:OptionFilterUniverse):
current_equity_price = universe.underlying.price
# Logic to reduce the option universe to only the call options with the specific strike price ranges
I've scoured code examples, documentation and exhausting dialogs with Mia, but I can't figure out how to implement that logic.
Can anyone point me in the right direction?
Thank you.
Mia Alissi
To implement an option filter with two strike price ranges for call options, you can use the
set_filter
method in theinitialize
method of your algorithm. Here's how you can set up the filter function to achieve your goal:Initialize the Algorithm: In the
initialize
method, add the equity and option for which you want to filter the option contracts.Set Data Normalization Mode: Ensure the equity data normalization mode is set to
RAW
to handle options data correctly.Add Option and Set Filter: Add the option to your algorithm and use the
set_filter
method to apply your custom filter function.Define the Filter Function: Implement the
filter_function
to filter the option universe based on your specific strike price ranges.Filter Call Options: Use the
calls_only()
method to select only call options.Apply Strike Price Ranges: Use the
contracts
method with a lambda function to filter contracts based on the desired strike price ranges relative to the underlying price.Return Filtered Universe: Ensure the filter function returns the filtered
OptionFilterUniverse
.Here's a sample implementation of the filter function:
Complete Example
This setup will filter the option chain to include only call options with strike prices within the specified ranges relative to the underlying stock price.
Sources: Index Options - QuantConnect.com, Equity Options - QuantConnect.com, Universes - QuantConnect.com, Universes - QuantConnect.com, Basic Template Options Filter Universe Algorithm
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Stacey Ramos
Hello,
Mia emailed me a solution to this question that does not work. Mia's solution is:
This solution throws the error:
'OptionUniverse' object has no attribute 'strike' because the parameter to the lambda function is NOT a list of OptionContract instances, but, rather, an list of OptionUniverse instances.
The documentation indicates that .contracts takes as its parameter a callable with one parameter. Some of the documentation says that its a list of Symbols, other documentation says its a single OptionContract.
This makes no sense to me.
I am not the first person to algorithmically trade option spreads. This seems like a trivial request and the answer should not be this difficult.
Can anyone provide an feasible solution?
Stacey Ramos
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!