Overall Statistics
Total Trades
27
Average Win
0.43%
Average Loss
-1.30%
Compounding Annual Return
-28.892%
Drawdown
11.600%
Expectancy
-0.252
Net Profit
-5.627%
Sharpe Ratio
-1.359
Probabilistic Sharpe Ratio
10.613%
Loss Rate
44%
Win Rate
56%
Profit-Loss Ratio
0.33
Alpha
-0.285
Beta
-0.169
Annual Standard Deviation
0.209
Annual Variance
0.044
Information Ratio
-0.893
Tracking Error
0.313
Treynor Ratio
1.683
Total Fees
$31.00
Estimated Strategy Capacity
$1400000.00
Lowest Capacity Asset
SPY 310F3UV4FTZT2|SPY R735QTJ8XC9X
from datetime import timedelta
from AlgorithmImports import *



class FocusedSkyBlueGalago(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2018, 1, 2)
        self.SetEndDate(2018, 3, 1)
        #self.SetEndDate(2016, 2, 1)
        #self.SetEndDate(2009,5,1)

        self.InitCash = 10000
        self.SetCash(self.InitCash)
        # self.AddEquity("SPY", Resolution.Minute)
        self.SetWarmUp(5)

        
        # SHILE: I think I forgot the .Symbol?
        self.bnd = self.AddEquity('BND', Resolution.Minute).Symbol
        
        # Options Parameters ===================================
        spy = self.AddEquity("SPY", Resolution.Minute)
        spy.SetDataNormalizationMode(DataNormalizationMode.Raw)
        qqq = self.AddEquity("QQQ", Resolution.Minute)
        qqq.SetDataNormalizationMode(DataNormalizationMode.Raw)
        tqqq = self.AddEquity("TQQQ", Resolution.Minute)
        tqqq.SetDataNormalizationMode(DataNormalizationMode.Raw)
        self.spy = spy.Symbol
        self.qqq = qqq.Symbol
        self.tqqq = tqqq.Symbol
        self.spycontract: Symbol = None
        self.tqqqcontract: Symbol = 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
        
        # Setting Brokerage Model to allow for live deployment
        self.SetBrokerageModel(BrokerageName.AlphaStreams)

        self.weights = {
            self.spy: .3,
            self.qqq: .3,
            self.bnd: .3
        }
        
        #self.SetPortfolioConstruction(InsightWeightingPortfolioConstructionModel(lambda time: None))

    # not necessary, but it saves a lot of typing
    def InsightHelper(self, symbol, percentage):
        if abs(percentage) < 0.001:
            return Insight.Price(symbol, timedelta(1), InsightDirection.Flat)
        elif percentage > 0:
            return Insight.Price(symbol, timedelta(1), InsightDirection.Up, None, None, None, percentage)
        else:
            return Insight.Price(symbol, timedelta(1), InsightDirection.Down, None, None, None, abs(percentage))

    def CheckOptions(self):
        if self.spycontract and self.Securities[self.spycontract].IsTradable:
            if self.Portfolio[self.spycontract].UnrealizedProfitPercent > .1:
                self.MarketOrder(self.spy, -self.Portfolio[self.spycontract].Quantity * 100)
                self.ExerciseOption(self.spycontract, 1)
                self.RemoveSecurity(self.spycontract)
                self.spycontract = None
        if self.tqqqcontract and self.Securities[self.tqqqcontract].IsTradable:
            if self.Portfolio[self.tqqqcontract].UnrealizedProfitPercent > .1:
                self.MarketOrder(self.tqqq, -self.Portfolio[self.tqqqcontract].Quantity * 100)
                self.ExerciseOption(self.tqqqcontract, 1)
                self.RemoveSecurity(self.tqqqcontract)
                self.tqqqcontract = None
        
        # add checks for old contracts
        if self.spycontract and not self.Securities[self.spycontract].IsTradable:
            self.RemoveSecurity(self.spycontract)
            self.spycontract = None
        if self.spycontract and not self.Securities[self.tqqqcontract].IsTradable:
            self.RemoveSecurity(self.tqqqcontract)

    def ComputeInstrinsicValue(self, symbol:Symbol, underlying:Symbol):
        sign = 1 if symbol.ID.OptionRight == OptionRight.Call else -1
        instrinsic = max(0,
            sign * (self.Securities[symbol].Price - symbol.ID.StrikePrice)
        )
        return instrinsic

    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
        
        self.CheckOptions()

        # Begin Stoploss Logic
        self.stoploss(data)
        
        insights = []
        
        if self.stoplosshold == 1:
            for equity in self.weights:
                insights.append(self.InsightHelper(equity, 0))
        else:
            if not self.Portfolio.Invested:
                self.monthlyRebalance()
        #if insights:
        #   self.EmitInsights(insights)
        # End Stoploss Logic
        
        '''
        Begin Hedging On Data
        1. Purchase Hedges
        2. Liquidate and Purchase New Hedges for Time value
        3. Invest 
        '''
        # 1. Purchase Hedges

        if self.spycontract is None or self.tqqqcontract is None: 
            if self.spycontract is None:
                self.spycontract = self.GetSpy()
            if self.tqqqcontract is None:
                self.tqqqcontract = self.GetTqqq()
            return
            
        #Liquidate and Purchase New Hedges for Time value
        if (self.spycontract.ID.Date - self.Time).days < 180:
            self.Liquidate(self.spycontract)
            self.RemoveSecurity(self.spycontract)
            self.spycontract = None
            return
        
        if (self.tqqqcontract.ID.Date - self.Time).days < 180:
            self.Liquidate(self.tqqqcontract)
            self.RemoveSecurity(self.tqqqcontract)
            self.tqqqcontract = None
            return
        
        
        #Purchase LongPut Contracts
        if not self.Portfolio[self.spycontract].Invested:
            self.SetHoldings(self.spycontract, 0.06)
            
        if not self.Portfolio[self.tqqqcontract].Invested:    
                self.SetHoldings(self.tqqqcontract,  0.04)
        

        #Exercise Contracts when they increase a certain % in intrinsic value
        if self.Securities[self.spy].Price < self.spycontract.ID.StrikePrice * 1.2:
            self.Liquidate(self.spycontract)
            self.RemoveSecurity(self.spycontract)
        
        if self.Securities[self.tqqq].Price < self.tqqqcontract.ID.StrikePrice * 1.2:    
            self.Liquidate(self.tqqqcontract)
            self.RemoveSecurity(self.tqqqcontract)
        #End hedging Logic




    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 GetTqqq(self):
        # Target strike as 40% OTM long put 
        targetStrike = (self.Securities[self.tqqq].Price * 0.60) - (self.Securities[self.tqqq].Price * 0.60)%5
        contracts = self.OptionChainProvider.GetOptionContractList(self.tqqq, 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)
        # SHILE ADDITION
        # below will get exact strike price if possible, else get closest
        puts = sorted(puts, key=lambda x: abs(x.ID.StrikePrice - targetStrike))
        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:
            return None
        self.AddOptionContract(puts[0], Resolution.Minute)
        return puts[0]


    def captureSpy(self):
        #Grabs the daily opening price of spy for our stoploss method
        if self.CurrentSlice.Bars.ContainsKey(self.spy):
            self.dailythresh = self.CurrentSlice[self.spy].Open
            self.stoplosshold = 0
            return
          
            
    def monthlyRebalance(self):
        # Rebalance portfolio monthly basis
        if self.IsWarmingUp:
            return

        insights = []
        for symbol,weight in self.weights.items():
            insights.append(self.InsightHelper(symbol, weight))
            self.SetHoldings(symbol, weight)
        if insights:
            self.EmitInsights(insights)
         

    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')