Overall Statistics
Total Trades
0
Average Win
0%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
0%
Expectancy
0
Net Profit
0%
Sharpe Ratio
0
Probabilistic Sharpe Ratio
0%
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0
Beta
0
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
-9.684
Tracking Error
0.033
Treynor Ratio
0
Total Fees
$0.00
from QuantConnect.Securities.Option import OptionPriceModels

class BasicTemplateOptionsFilterUniverseAlgorithm(QCAlgorithm):
    UnderlyingTicker = "SPY"

    def Initialize(self):
        self.SetStartDate(2019, 12, 16)
        self.SetEndDate(2019, 12, 19)
        self.SetCash(100000)

        equity = self.AddEquity(self.UnderlyingTicker);
        option = self.AddOption(self.UnderlyingTicker)
        self.option_symbol = option.Symbol

        option.SetFilter(self.UniverseFunc) 

        option.PriceModel = OptionPriceModels.BaroneAdesiWhaley()
        self.SetWarmUp(timedelta(days=60))

        # use the underlying equity as the benchmark
        self.SetBenchmark(equity.Symbol)
        
        self.logged = 0

    def OnData(self,slice):
        if self.IsWarmingUp or self.Portfolio.Invested or self.Time.hour > 10:
            return

        for kvp in slice.OptionChains:
            if kvp.Key != self.option_symbol: 
                continue
            
            chain = kvp.Value
            # find the call options expiring today
            contracts = [i for i in chain if i.Right ==  OptionRight.Call and (i.Expiry.date() - self.Time.date()).days == 0]
            
            sorted_contracts = [i for i in sorted(contracts, key = lambda x:x.Strike, reverse = True) if i.Strike < chain.Underlying.Price]
            
            if len(sorted_contracts) == 0: 
                return
            
            if self.logged < 100:
                self.logged += 1
                self.Log(f"delta {sorted_contracts[0].Greeks.Delta}")


    def UniverseFunc(self, universe):
        return universe.IncludeWeeklys()\
                    .Strikes(-1, 1)\
                    .Expiration(0, 10)