QuantConnect / Lean

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

Option universe Expiration(0, 0) filter (i.e. 0-DTE filter) returns expired options sometimes #6654

Closed salsasepp closed 2 years ago

salsasepp commented 2 years ago

Filtering for 0-DTE options results in expired options being returned. Possibly related to #6235 ?

Expected Behavior

When using a filter of Expiration(timedelta(days=0), timedelta(days=0)) on 2022-01-28, only SPY options expiring on 2022-01-28 should be returned

Actual Behavior

When using a filter of Expiration(timedelta(days=0), timedelta(days=0)) on 2022-01-28, SPY options expiring on 2022-01-28 and 2022-01-26 are returned

Log excerpt:

2022-01-26 09:45:00 2022-01-26 09:45:00, expirations with 0-DTE filter: {datetime.date(2022, 1, 26)}
2022-01-28 09:45:00 2022-01-28 09:45:00, expirations with 0-DTE filter: {datetime.date(2022, 1, 26), datetime.date(2022, 1, 28)}

Potential Solution

Unknown

Reproducing the Problem

from AlgorithmImports import *
from datetime import datetime, time, timedelta

class Issue(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2022, 1, 26)
        self.SetEndDate(2022, 1, 28)

        self.udl = self.AddEquity('SPY', Resolution.Minute, dataNormalizationMode=DataNormalizationMode.Raw)
        self.option = self.AddOption(self.udl.Symbol)
        self.option.SetFilter(lambda u: u.IncludeWeeklys().Expiration(timedelta(days=0), timedelta(days=0)).Strikes(-5, +5))

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

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

        expirations = set(c.Value.Expiry.date() for c in oc.Contracts)
        self.Debug(f'{self.Time}, expirations with 0-DTE filter: {expirations}')
        for c in oc.Contracts:
            self.Debug(f'  {c.Key} {c.Value.Expiry} {c.Value.Strike} {c.Value.Right}')

System Information

master, Lean Version: 2.5.0.0.14637, cloud environment

Checklist

Martin-Molinero commented 2 years ago

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