Overall Statistics
Total Trades
21
Average Win
0%
Average Loss
-0.24%
Compounding Annual Return
-26.913%
Drawdown
49.400%
Expectancy
-1
Net Profit
-34.182%
Sharpe Ratio
-0.618
Probabilistic Sharpe Ratio
2.565%
Loss Rate
100%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0.006
Beta
0.916
Annual Standard Deviation
0.356
Annual Variance
0.127
Information Ratio
0.476
Tracking Error
0.056
Treynor Ratio
-0.24
Total Fees
$21.00
Estimated Strategy Capacity
$25000000.00
Lowest Capacity Asset
QQQ RIWIV7K5Z9LX
class FocusedSkyBlueGalago(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2008, 1, 1)
        self.SetEndDate(2009,5,1)

        self.InitCash = 10000
        self.SetCash(self.InitCash)
        self.AddEquity("SPY", Resolution.Minute)
        self.SetWarmUp(5)
        
        
        
        # Options Parameters ===================================
        spy = self.AddEquity("SPY", Resolution.Minute)
        qqq = self.AddEquity("QQQ", Resolution.Minute)
        tqqq = self.AddEquity("TQQQ", Resolution.Minute)
        spy.SetDataNormalizationMode(DataNormalizationMode.Raw)
        qqq.SetDataNormalizationMode(DataNormalizationMode.Raw)
        tqqq.SetDataNormalizationMode(DataNormalizationMode.Raw)
        self.spy = spy.Symbol
        self.qqq = qqq.Symbol
        self.tqqq = tqqq.Symbol
        self.spycontract = None
        self.qqqcontract = None

        
        # Rebalance beginning of every month =======================
        self.Schedule.On(self.DateRules.MonthStart("SPY"), self.TimeRules.AfterMarketOpen("SPY", 1), self.monthlyRebalance)
        self.Schedule.On(self.DateRules.EveryDay("SPY"), self.TimeRules.AfterMarketOpen("SPY", 1), self.captureSpy)
        
        #Variables used in stoploss=================================
        self.stoplosshold = 0
        self.dailythresh = 0
        
        #Graphing Benchmark 
        # Graphing SPY Benchmark =================
        
    def OnData(self, data):
        
        ''' OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
            Arguments:
                data: Slice object keyed by symbol containing the stock data
        '''
        if self.IsWarmingUp:
            return
        
        
        # SPY HEDGE
        if self.spycontract is None:
            self.spycontract = self.GetSpy()
        if self.qqqcontract is None:
            self.qqqcontract = self.GetQqq()
            return
        
        #Begin Hedging Logic
        if (self.spycontract.ID.Date - self.Time).days < 180:
            
            self.Liquidate(self.spycontract)
            self.RemoveSecurity(self.spycontract)
            self.spycontract = None
        
        if (self.tqqqcontract.ID.Date - self.Time).days < 180:
            if self.tqqqcontract is None:
                pass
            else:
                self.Liquidate(self.tqqqcontract)
                self.RemoveSecurity(self.tqqqcontract)
                self.tqqqcontract = None
                return
        
        if not self.Portfolio[self.spycontract].Invested:
            self.SetHoldings(self.spycontract, 0.04)
            if self.tqqqcontract is None:
                pass
            else:
                self.SetHoldings(self.qqqcontract,  0.01)
        
        #Exercixe our option when they increase in value 
        if  self.Securities[self.spy].Price < self.spycontract.ID.StrikePrice * 1.20:
            self.Liquidate(self.spycontract)
            self.RemoveSecurity(self.spycontract)
        
        if  self.Securities[self.qqq].Price < self.qqqcontract.ID.StrikePrice * 1.2:    
            '''
            if self.tqqqcontract is None:
                return
            '''
            self.Liquidate(self.qqqcontract)
            self.RemoveSecurity(self.qqqcontract)
        #End hedging Logic
    
        
        
        
        if self.stoplosshold == 1:
            return
        else:
            if not self.Portfolio.Invested:
                self.SetHoldings(self.spy, .60)
                self.SetHoldings(self.qqq, .35)
        
        
        if self.stoplosshold == 1: 
            return
        else:
            self.stoploss(data) # change 2
            self.Log('SetStoploss')


    def GetSpy(self):
        # Target strike as 40% OTM long put 
        targetStrike = (self.Securities[self.spy].Price * 0.60) - (self.Securities[self.spy].Price * 0.60)%5
        contracts = self.OptionChainProvider.GetOptionContractList(self.spy, self.Time)
        puts = [x for x in contracts if x.ID.OptionRight == OptionRight.Put]
        puts = sorted( sorted(puts, key = lambda x: x.ID.Date, reverse = True),
                       key = lambda x: x.ID.StrikePrice)
        puts = [x for x in puts if x.ID.StrikePrice == targetStrike]
        puts = [x for x in puts if 270 < (x.ID.Date - self.Time).days <= 420]
        if len(puts) == 0:
            self.Log("No SPY Puts")
            return None
        self.AddOptionContract(puts[0], Resolution.Minute)
        return puts[0]


    def GetQqq(self):
        # Target strike as 40% OTM long put 
        targetStrike = (self.Securities[self.qqq].Price * 0.60) - (self.Securities[self.qqq].Price * 0.60)%5
        contracts = self.OptionChainProvider.GetOptionContractList(self.qqq, self.Time)
        puts = [x for x in contracts if x.ID.OptionRight == OptionRight.Put]
        puts = sorted( sorted(puts, key = lambda x: x.ID.Date, reverse = True),
                       key = lambda x: x.ID.StrikePrice)
        
        '''
        This is where I have a question
        '''
        puts = [x for x in puts if x.ID.StrikePrice == targetStrike]
        # Instead of getting a put thats my direct cost, how do I instead sort
        # options to grab the one CLOSEST to my strike price, not exact
        if len(puts) ==0:
            x=0
            puts = sorted(puts, key = lambda x: x.ID.StrikePrice, reverse=True)
        # Try to grab puts that are in my date range, if there are none then sort by dates to expiration 
        # and grab the ones that are longest to expiration
        puts = [x for x in puts if 270 < (x.ID.Date - self.Time).days <= 420]
        if len(puts) == 0:
            puts = sorted(puts, key = lambda x: x.ID.Date, reverse=True)
        
        if len(puts) == 0:
            self.Log("No QQQ Puts")
            return None
        self.AddOptionContract(puts[0], Resolution.Minute)
        return puts[0]



    def captureSpy(self):
        if self.CurrentSlice.Bars.ContainsKey(self.spy):
            self.dailythresh = self.CurrentSlice[self.spy].Open
            self.stoplosshold = 0
            return
          
            
    def monthlyRebalance(self):
        '''
        Now I need to rebalance portfolio on a monthly basis
        '''
        if self.IsWarmingUp:
            return
        
        self.SetHoldings(self.spy, 0.60)
        self.SetHoldings(self.qqq, 0.35)
        return   
    
    
    def stoploss(self, data):
        '''
        Stoploss logic:
            1. If spy drops more than 5% liquidate entire equity portfolio
            2. Change stoplosshold value to 1, this indicates that the portfolios SL has been hit 
            and were going to hold until the next trading day
        '''
        if self.IsWarmingUp:
            return
        if self.CurrentSlice.Bars.ContainsKey(self.spy):
            #self.Debug((self.dailythresh - self.CurrentSlice[self.spy].Close)/self.CurrentSlice[self.spy].Close)
            if ((self.dailythresh - self.CurrentSlice[self.spy].Open)/self.dailythresh) < -.05:
                self.SetHoldings(self.spy, 0)
                self.SetHoldings(self.qqq, 0)
                self.stoplosshold = 1
                self.Log('HIT')