Overall Statistics
Total Trades
430
Average Win
1.10%
Average Loss
-1.01%
Compounding Annual Return
3.453%
Drawdown
46.700%
Expectancy
-0.212
Net Profit
52.900%
Sharpe Ratio
0.29
Probabilistic Sharpe Ratio
0.574%
Loss Rate
62%
Win Rate
38%
Profit-Loss Ratio
1.09
Alpha
-0.016
Beta
0.614
Annual Standard Deviation
0.173
Annual Variance
0.03
Information Ratio
-0.409
Tracking Error
0.141
Treynor Ratio
0.082
Total Fees
$62524.80
class TailHedge(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2008, 1, 1)
        self.SetEndDate(2020, 6, 30)
        self.SetCash(1000000)
        spy = self.AddEquity("SPY", Resolution.Minute)
        spy.SetDataNormalizationMode(DataNormalizationMode.Raw)
        self.SetSecurityInitializer(lambda x: x.SetMarketPrice(self.GetLastKnownPrice(x)))
        self.spy = spy.Symbol
        self.contract = None
        self.SetWarmUp(200)
        self.Schedule.On(self.DateRules.MonthStart("SPY"), self.TimeRules.AfterMarketOpen("SPY", 90), self.Rebalance)
    
    def OnData(self, data):
        if self.IsWarmingUp:
            return
    
    def Rebalance(self):
        #self.Log("SpecificTime: Fired at : {0}".format(self.Time))

        self.SetHoldings(self.spy, 0.99)
        #self.Debug(str(self.Portfolio[self.spy].HoldingsValue))
        
        if self.contract is None:
            self.contract = self.GetContract()
            if self.contract is not None:
                self.SetHoldings(self.contract, 0.01)
            #self.Debug(str(self.Portfolio[self.contract].HoldingsValue))
            return
        
        if not self.contract is None:
            self.Liquidate(self.contract)
            self.RemoveSecurity(self.contract)
            self.contract = None
            self.contract = self.GetContract()
            if self.contract is not None:
                self.SetHoldings(self.contract, 0.01)
            #self.Debug(str(self.Portfolio[self.contract].HoldingsValue))
            return

    def GetContract(self):
        targetStrike = self.Securities[self.spy].Price * 0.7
        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) >= 0
            and (x.ID.StrikePrice - targetStrike) < 6]
        puts = [x for x in puts if 50 < (x.ID.Date - self.Time).days <= 90]
        if len(puts) == 0:
            return None
        self.AddOptionContract(puts[0], Resolution.Minute)
        return puts[0]