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:

  1. class MySpreadOptionAlgorithm(QCAlgorithm):
  2. def initialize(self):
  3. ...
  4. equity = self.algorithm.add_equity("SOME EQUITY SYMBOL", Resolution.HOUR)
  5. equity.set_data_normalization_mode(DataNormalizationMode.RAW)
  6. self.symbol_equity = equity.symbol
  7. option = self.add_option(self.symbol_equity, Resolution.HOUR)
  8. option.set_filter(filter_function)
  9. ...

Then, implement the filter_function:

  1. def filter_function(universe:OptionFilterUniverse):
  2. current_equity_price = universe.underlying.price
  3. # 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.

 

 

Author

Stacey Ramos

October 2024