QuantConnect / Lean

Lean Algorithmic Trading Engine by QuantConnect (Python, C#)
https://lean.io
Apache License 2.0
9.87k stars 3.27k forks source link

Option universe Strikes() filter returns too many contracts #6653

Closed salsasepp closed 2 years ago

salsasepp commented 2 years ago

Filtering option contracts with Strikes(-1, +1)

Expected Behavior

Filtered contracts contain two strikes, one below and one above the underlying's price: 2022-09-14 10:45:00 symbol MU, price 53.335, strikes [53.0, 54.0] as per the docs at https://www.quantconnect.com/docs/v2/writing-algorithms/universes/equity-options#11-Filter-Contracts

Actual Behavior

Filtered contracts contain two extraneous strikes below and above: 2022-09-14 10:45:00 symbol MU, price 53.335, strikes [52.5, 53.0, 54.0, 55.0]

Potential Solution

Unknown

Reproducing the Problem

from AlgorithmImports import *

class Issue(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(datetime(2022, 9, 14))
        self.SetEndDate(datetime(2022, 9, 14))

        self.udl = self.AddEquity('MU', Resolution.Minute, dataNormalizationMode=DataNormalizationMode.Raw)
        self.option = self.AddOption(self.udl.Symbol)
        self.option.SetFilter(-1, +1, timedelta(days=0), timedelta(days=7))

    def OnData(self, slice):
        if self.Time.time() != time(10, 45):
            return

        oc = slice.OptionChains.GetValue(self.option.Symbol)
        if oc is None:
            return

        strikes = sorted(set(c.Strike for _, c in oc.Contracts.items()))
        self.Debug(f"{self.Time} symbol {self.udl.Symbol}, price {oc.Underlying.Price}, strikes {strikes}")

Comments

Further to the above, filtering with a) Strikes(-1, 0) results in 2022-09-14 10:45:00 symbol MU, price 53.335, strikes [52.5, 53.0, 54.0, 55.0] b) Strikes(0, +1) results in 2022-09-14 10:45:00 symbol MU, price 53.335, strikes [53.0, 54.0, 55.0, 56.0] c) Strikes(0, 0) results in 2022-09-14 10:45:00 symbol MU, price 53.335, strikes [53.0, 54.0, 55.0] d) Strikes(-1, -1) results in 2022-09-14 10:45:00 symbol MU, price 53.335, strikes [52.5, 53.0, 54.0]

From the documentation it is not clear what the expected output for a) b) c) or d) would be.

System Information

master, Lean Version: 2.5.0.0.14632, cloud environment

Checklist

Martin-Molinero commented 2 years ago

Hey @salsasepp! Thanks for the report. We will fix this ASAP

salsasepp commented 2 years ago

@Martin-Molinero Thanks for the timely fix! Very much appreciated. The same goes for #6654.