Overall Statistics
# region imports
from AlgorithmImports import *
# endregion

class BasicOptionAlgorithm(QCAlgorithm):
    
    def initialize(self):
        self.set_start_date(2024, 9, 10)
        self.set_end_date(2024, 9, 11)
        self.universe_settings.asynchronous = True
        option = self.add_option("SPY")
        option.set_filter(self._filter)
        self._symbol = option.symbol

    def _filter(self, universe):
        # 0DTE and positive delta at the previous day close
        return universe.include_weeklys().delta(0,.5).expiration(0, 0)

    def on_data(self, data):
        chain = data.option_chains.get(self._symbol)
        if chain:
            expiry = min([x.expiry for x in chain])
            contracts = sorted([x for x in chain if x.expiry==expiry], key=lambda x: abs(0.3-x.greeks.delta))
            message = f'{self.time} :: {contracts[0].greeks.delta}'
            #self.log(message)