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
0
Tracking Error
0
Treynor Ratio
0
Total Fees
$0.00
Estimated Strategy Capacity
$0
Lowest Capacity Asset
class RetrospectiveRedOrangeHornet(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2019, 1, 1)  # Set Start Date
        self.SetEndDate(2019, 1, 2)  # Set Start Date
        self.SetCash(100000)  # Set Strategy Cash
        equity = self.AddEquity("SPY", Resolution.Minute)
        option = self.AddOption("SPY", Resolution.Minute)
        option.SetFilter(lambda universe: universe.IncludeWeeklys().Strikes(-12,12).Expiration(timedelta(0), timedelta(90)))
        option.PriceModel = OptionPriceModels.CrankNicolsonFD()
        self.symbol = option.Symbol
        #self.vix_symbol = self.AddIndex('VIX', Resolution.Minute).Symbol
        self.SetBenchmark("SPY")
        self.lastDay=None
        self.openPrice=None
        self.breakCode = False
        #self.openVix=None
        self.atr = self.ATR("SPY", 4, Resolution.Daily)
        self.spread=10
        self.SetWarmup(timedelta(days=90), Resolution.Minute)
        #self.Consolidate("SPY", Resolution.Daily, self.OnDailyData)
    
            
    def OnData(self, slice):
        if self.breakCode == True:
            return
        
        for chain in slice.OptionChains.Values:
            for contract in chain:
                if contract.Greeks.Delta != 0:
                    self.Debug(contract.Greeks.Delta)
                    self.breakCode = True
                else:
                    self.Debug("no contracts with delta")

        if self.Portfolio.Invested:
            self.SetHoldings("SPY", 0)
        day = self.Time.weekday()
        if( day != 0 and day != 2 and day != 4):
            return
        if self.lastDay == self.Time.day :
            return
            
        if self.openPrice == None and slice.ContainsKey("SPY"):
            try:
                self.openPrice = slice.Bars["SPY"].Close
            except Exception:
                self.openPrice = None
        #if self.openVix == None and slice.ContainsKey("VIX"):
        #    try:
        #        self.openVix = slice.Bars["VIX"].Close
        #    except Exception:
        #        self.openVix = None
        
        if(self.Time.hour >= 10 and self.openPrice != None): # and self.openVix != None
            if(not (slice.ContainsKey("SPY"))): #slice.ContainsKey("VIX") and 
                return
            tenPrice = slice.Bars["SPY"].Close
            self.lastDay = self.Time.day
            diff = tenPrice - self.openPrice
            #tenVix = slice.Bars["VIX"].Close
            #vixdiff = tenVix - self.openVix
            if(diff > 0):# and vixdiff<0
                #bullish
                sellStrikePrice = tenPrice #+ 3 * self.atr.Current.Value
                self.TradeOption(slice,sellStrikePrice, self.Time, 0, OptionRight.Put)
                buyStrikePrice = sellStrikePrice - self.spread
                self.TradeOption(slice,buyStrikePrice, self.Time, 1, OptionRight.Put)
            elif (diff < 0):# and vixdiff>0
                #bearish
                sellStrikePrice = tenPrice #+ 3 * self.atr.Current.Value
                self.TradeOption(slice,sellStrikePrice, self.Time, 0, OptionRight.Call)
                buyStrikePrice = sellStrikePrice + self.spread
                self.TradeOption(slice,buyStrikePrice, self.Time, 1, OptionRight.Call)
            self.openPrice=None
            #self.openVix=None
            
    def TradeOption(self,slice,strike, exp, isBuy, isPut):
        if slice.OptionChains.Count == 0: return
        for i in slice.OptionChains:
            if i.Key != self.symbol: continue
            chain = i.Value
            call = [x for x in chain if x.Right == isPut] 
            contracts = sorted(sorted(call, key = lambda x: abs(x.Strike - strike)),
                                            key = lambda x: abs(x.Expiry - exp))
            if len(contracts) == 0: return
            contract = contracts[0]
            if isBuy:
                self.Buy(contract.Symbol, 1)
            else:
                self.Sell(contract.Symbol, 1)