Overall Statistics
Total Trades
7
Average Win
1.21%
Average Loss
-0.26%
Compounding Annual Return
29.657%
Drawdown
30.000%
Expectancy
2.746
Net Profit
93.538%
Sharpe Ratio
1.301
Probabilistic Sharpe Ratio
60.283%
Loss Rate
33%
Win Rate
67%
Profit-Loss Ratio
4.62
Alpha
0.041
Beta
0.944
Annual Standard Deviation
0.203
Annual Variance
0.041
Information Ratio
0.625
Tracking Error
0.045
Treynor Ratio
0.28
Total Fees
$7.00
Estimated Strategy Capacity
$69000000.00
Lowest Capacity Asset
QQQ RIWIV7K5Z9LX
class FocusedSkyBlueGalago(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2019, 1, 1)
        #self.SetEndDate(2018, 1, 3)
        self.InitCash = 10000
        self.SetCash(self.InitCash)
        self.AddEquity("SPY", Resolution.Minute)

        # ETF's we're going to trade===================================
        spy = self.AddEquity("SPY", Resolution.Minute)
        QQQ = self.AddEquity("QQQ", Resolution.Minute)
        tqqq = self.AddEquity("TQQQ", Resolution.Minute)
        spy.SetDataNormalizationMode(DataNormalizationMode.Raw)
        tqqq.SetDataNormalizationMode(DataNormalizationMode.Raw)
        
        self.spy = spy.Symbol
        self.qqq = QQQ.Symbol
        self.tqqq = tqqq.Symbol
        
        
        #Option Contract =================================================
        self.contract = None
        
        # Rebalance beginning of every month =======================
        self.Schedule.On(self.DateRules.MonthStart("SPY"), 
        self.TimeRules.AfterMarketOpen("SPY"), self.MonthlyRebalance)
        
        # Graphing SPY Benchmark =================
        self.MKT = self.AddEquity("SPY", Resolution.Daily).Symbol
        self.mkt = []

        #Stoploss
        self.priceSPY = 0
        self.Count = 0

    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
        '''
        #Start of day
        if not self.Portfolio[self.spy].Invested:
           self.SetHoldings(self.spy, .6)
           self.SetHoldings(self.qqq, .36)
        
        #do at start of day
        if self.Count == 0:
            if data.ContainsKey("SPY"):
                self.priceSPY = data.Bars["SPY"].High
                #do at start of day
            self.Count = 1
        
        #Stoploss
        else:
            if data.ContainsKey("SPY"):
                    if ( (data.Bars["SPY"].High - self.priceSPY) / self.priceSPY ) < -.05:
                        #self.Debug(self.Time)
                        #self.Debug((data.Bars["SPY"].High - self.priceSPY) / self.priceSPY )
                        self.SetHoldings(self.spy, 0)
                        self.SetHoldings(self.qqq, 0)
                        return
        
        #Reset trailing stoploss parameter
        self.Count = 0

        
        # DO HEDGE
        if self.contract is None:
            self.contract = self.GetContract()
            return
        
        if (self.contract.ID.Date - self.Time).days < 180:
            self.Liquidate(self.contract)
            self.RemoveSecurity(self.contract)
            self.contract = None
            return
        
        if not self.Portfolio[self.contract].Invested:
            self.SetHoldings(self.contract, 0.05)
        
        if self.Securities[self.spy].Price < self.contract.ID.StrikePrice * 1.2:
            self.Liquidate(self.contract)
            self.RemoveSecurity(self.contract)
    
    def GetContract(self):
        targetStrike = (self.Securities[self.spy].Price * 0.6) - (self.Securities[self.spy].Price * 0.6)%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 Puts")
            return None
        self.AddOptionContract(puts[0], Resolution.Minute)
        puts[0]
    
    
           
    def MonthlyRebalance(self):
        
        #Now I need to rebalance portfolio on a monthly basis
        self.SetHoldings(self.spy, 0.5)
        self.SetHoldings(self.qqq, 0.45)
        return   
        
        
    def OnEndOfDay(self):  
    
        mkt_price = self.History(self.MKT, 2, Resolution.Daily)['close'].unstack(level= 0).iloc[-1]
        self.mkt.append(mkt_price)
        mkt_perf = self.InitCash * self.mkt[-1] / self.mkt[0] 
        self.Plot('Strategy Equity', self.MKT, mkt_perf)