Overall Statistics
Total Trades
657
Average Win
0.06%
Average Loss
-0.03%
Compounding Annual Return
2.959%
Drawdown
0.600%
Expectancy
0.295
Net Profit
2.956%
Sharpe Ratio
2.847
Probabilistic Sharpe Ratio
95.663%
Loss Rate
59%
Win Rate
41%
Profit-Loss Ratio
2.16
Alpha
0.021
Beta
-0.005
Annual Standard Deviation
0.007
Annual Variance
0
Information Ratio
-1.839
Tracking Error
0.105
Treynor Ratio
-4.468
Total Fees
$361.00
Estimated Strategy Capacity
$180000000.00
Lowest Capacity Asset
SPY 31966X63E2YFA|SPY R735QTJ8XC9X
class RetrospectiveRedOrangeHornet(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2019, 1, 1)  # Set Start Date
        self.SetEndDate(2019, 12, 31)  # 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(0)))
        self.symbol = option.Symbol
        #self.vix_symbol = self.AddIndex('VIX', Resolution.Minute).Symbol
        self.SetBenchmark("SPY")
        self.lastDay=None
        self.openPrice=None
        #self.openVix=None
        self.atr = self.ATR("SPY", 4, Resolution.Daily)
        self.spread=10
        self.SetWarmup(1)
        #self.Consolidate("SPY", Resolution.Daily, self.OnDailyData)
    
            
    def OnData(self, slice):
        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)