Overall Statistics
Total Trades
2
Average Win
0%
Average Loss
-0.54%
Compounding Annual Return
6.107%
Drawdown
1.000%
Expectancy
-1
Net Profit
0.483%
Sharpe Ratio
1.234
Probabilistic Sharpe Ratio
52.377%
Loss Rate
100%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
-0.147
Beta
0.161
Annual Standard Deviation
0.049
Annual Variance
0.002
Information Ratio
-7.704
Tracking Error
0.159
Treynor Ratio
0.376
Total Fees
$1.00
from QuantConnect.Algorithm import *
from QuantConnect.Securities.Option import OptionPriceModels

class BasicTemplateOptionsFilterUniverseAlgorithm(QCAlgorithm):
    def Initialize(self):
        self.SetStartDate(2019, 1, 1)
        self.SetEndDate(2019, 1, 30)
        self.SetCash(100000)

        self.option = self.AddOption("AAPL")
        
        self.option.SetFilter(-1, +1, 1, 30) 
        self.option.PriceModel = OptionPriceModels.CrankNicolsonFD()
        self.SetWarmup(30, Resolution.Daily)
        
        self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.Every(timedelta(minutes=10)), self.PlotGreek)
        
    def OnData(self, slice):
        
        if self.IsWarmingUp or self.Portfolio.Invested:
            return 
        
        for symbol, chain in slice.OptionChains.items():
            contracts = [c for c in chain if c.Right == OptionRight.Call]
            if len(contracts) == 0:
                return

            sorted_contracts = sorted(contracts, key = lambda x: (x.Strike, x.Expiry), reverse=True)
            contract = sorted_contracts[0]
            self.MarketOrder(contract.Symbol, 1)
            
            
    def get_option_for_option_holding(self, symbol):
        for chain in self.CurrentSlice.OptionChains.Values:
            for opt in chain:
                if opt.Symbol == symbol:
                    return opt
                
    def get_option_holdings(self):
        options = []
        for holding in self.Portfolio.Values:
            if holding.Invested and holding.Type == SecurityType.Option:
                option = self.get_option_for_option_holding(holding.Symbol)
                if option is not None:
                    options.append((holding.Quantity, option))
        return options

    def PlotGreek(self):
        options = self.get_option_holdings()
        
        for quantity, option in options:
            self.Plot("Greeks", "Values", option.Greeks.Delta)