Overall Statistics
Total Trades
24
Average Win
0.54%
Average Loss
0%
Compounding Annual Return
18.979%
Drawdown
18.200%
Expectancy
0
Net Profit
25.016%
Sharpe Ratio
1.232
Probabilistic Sharpe Ratio
56.640%
Loss Rate
0%
Win Rate
100%
Profit-Loss Ratio
0
Alpha
0.173
Beta
-0.079
Annual Standard Deviation
0.133
Annual Variance
0.018
Information Ratio
0.152
Tracking Error
0.307
Treynor Ratio
-2.062
Total Fees
$24.00
class FrameworkAlgorithm(QCAlgorithm):
    
    def Initialize(self):
        self.SetStartDate(2019, 1, 1)   
        self.SetEndDate(2020, 4, 13)    
        self.SetCash(10000)           
            
        # Add securities
        self.spy = self.AddEquity("SPY", Resolution.Daily).Symbol
        self.tlt = self.AddEquity("TLT", Resolution.Daily).Symbol
        
        # Setup highest high indicator
        lookback = 252
        self.highest_high = self.MAX(self.spy, lookback, Resolution.Daily, Field.High)
        self.SetWarmup(lookback, Resolution.Daily)
        
        
    def OnData(self, data):
        if not data.Bars.ContainsKey("SPY"):
            return
        
        # First entry
        if not self.Portfolio.Invested:
            self.SetHoldings("SPY", 0.60)
            self.SetHoldings("TLT", 0.40)
            return
        
        # Calculate hwm and drawdown
        hwm = self.highest_high.Current.Value
        drawdown = (data.Bars["SPY"].Close - hwm) / hwm

        # Rebalancing        
        if drawdown < -0.10:
            self.SetHoldings("SPY", 0.60)
            self.SetHoldings("TLT", 0.40)