Overall Statistics
Total Trades
1936
Average Win
0.42%
Average Loss
0%
Compounding Annual Return
25.890%
Drawdown
34.500%
Expectancy
0
Net Profit
1914.560%
Sharpe Ratio
1.06
Probabilistic Sharpe Ratio
45.277%
Loss Rate
0%
Win Rate
100%
Profit-Loss Ratio
0
Alpha
0.158
Beta
0.356
Annual Standard Deviation
0.181
Annual Variance
0.033
Information Ratio
0.488
Tracking Error
0.197
Treynor Ratio
0.539
Total Fees
$0.00
Estimated Strategy Capacity
$33000.00
Lowest Capacity Asset
SPXW 323S3PHZN0TWU|SPX 31
from AlgorithmImports import *

class BasicTemplateSPXWeeklyIndexOptionsAlgorithm(QCAlgorithm):
    def Initialize(self):
        self.SetStartDate(2010, 1, 1)
        self.SetEndDate(datetime.now())
        self.SetCash(10000)

        self.spx = self.AddIndex("SPX").Symbol

        # regular option SPX contracts
        self.spxOptions = self.AddIndexOption(self.spx);
        self.spxOptions.SetFilter(lambda u: (u.Strikes(0, 1).Expiration(0, 30)))

        # weekly option SPX contracts
        spxw = self.AddIndexOption(self.spx, "SPXW")
        # set our strike/expiry filter for this option chain
        spxw.SetFilter(lambda u: (u.Strikes(-2, -1)
                                     .Expiration(0, 7)
                                     .IncludeWeeklys()))

        self.spxw_option = spxw.Symbol
        self.Schedule.On(self.DateRules.EveryDay("SPX"), self.TimeRules.At(9, 30), self.CheckOpeningPrice)
       

    def CheckOpeningPrice(self):
        underlyingPrice = self.spxOptions.Underlying.Open
        self.openingPrice = underlyingPrice
        self.Plot("Opening Price: ","value", self.openingPrice)

    
       

        # we sort the contracts to find at the money (ATM) contract with closest expiration
       

      

    def OnData(self,slice):
        if self.Portfolio.Invested: return
        
        if self.Time.hour == 15 and self.Time.minute > 30:
            chain = slice.OptionChains.GetValue(self.spxw_option)
            if chain is None:
                return
            underlyingPrice = self.spxOptions.Underlying.Close
            if underlyingPrice > (1 * self.openingPrice):
                
                contracts = sorted(sorted(sorted(chain, \
                    key = lambda x: x.Expiry), \
                    key = lambda x: abs(x.Strike < self.openingPrice)), \
                    key = lambda x: x.Right == OptionRight.Put, reverse=True)
                
                if len(contracts) == 0: 
                    return
                symbol = contracts[0].Symbol
                self.Sell(symbol, 1)
                

    def OnOrderEvent(self, orderEvent):
        pass