Overall Statistics
Total Trades
8096
Average Win
0.14%
Average Loss
-0.15%
Compounding Annual Return
38.300%
Drawdown
39.400%
Expectancy
0.514
Net Profit
2952.908%
Sharpe Ratio
1.447
Probabilistic Sharpe Ratio
80.273%
Loss Rate
22%
Win Rate
78%
Profit-Loss Ratio
0.94
Alpha
0.351
Beta
-0.053
Annual Standard Deviation
0.238
Annual Variance
0.056
Information Ratio
0.733
Tracking Error
0.288
Treynor Ratio
-6.488
Total Fees
$32646.00
Estimated Strategy Capacity
$1300000.00
Lowest Capacity Asset
TMF UBTUG7D0B7TX
class SleepyYellowBee(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2010, 12, 23)
        self.SetCash(100000) 
        self.AddEquity("SPY", Resolution.Daily)
        self.SetBenchmark("SPY")
        # Variable to hold the last calculated benchmark value
        self.lastBenchmarkValue = None
        # Our inital benchmark value scaled to match our portfolio
        self.BenchmarkPerformance = self.Portfolio.TotalPortfolioValue
        
        #self.Float = self.GetParameter("self.Float")
        self.Float = 0.01
        self.non_float = 1 - self.Float
        self.bear = []
        
        # target allocations
        self.TQQQ_target = 0.20*self.non_float
        self.UPRO_target = 0.20*self.non_float
        self.TMF_target = 0.60*self.non_float
        
        #import equities 
        self.AddEquity("TQQQ", Resolution.Daily)
        self.AddEquity("UPRO", Resolution.Daily)
        self.AddEquity("TYD", Resolution.Daily)
        self.AddEquity("TMF", Resolution.Daily)
        
        #EMAs for bear filter
        fast = 5
        med = 40
        slow = 100
        self.ema_fast = self.EMA("SPY", fast)
        self.ema_medium = self.EMA("SPY", med)
        self.ema_slow = self.EMA("SPY", slow)
        
        # Slope and Concavity
        self.slope = IndicatorExtensions.Of(ExponentialMovingAverage(fast), self.ROCP("SPY", med, Resolution.Daily)) # slope
        self.concavity = IndicatorExtensions.Of(ExponentialMovingAverage(fast), IndicatorExtensions.Of(RateOfChange(fast), self.ROCP("SPY", slow, Resolution.Daily))) #concavity
        
        #
        self.SetWarmUp(200, Resolution.Daily)
        
        self.rebalance_date = self.Time + timedelta(days = 100)
        
        
        

    def OnData(self, data):
        if self.IsWarmingUp:
            return
        
        # when bull market
        if self.ema_fast > self.ema_slow:
            self.TQQQ_target = 0.30*self.non_float
            self.UPRO_target = 0.30*self.non_float
            self.TMF_target = 0.40*self.non_float
            self.Plot("BULL - BEAR", "Bull", 1)
            self.Plot("BULL - BEAR", "Bear", 0)
            self.bear=0
            self.Rebalance(data)
        # Bear Market
        if self.ema_fast < self.ema_slow:# or (self.slope.Current.Value < 0 and self.concavity.Current.Value < 0):
            self.TQQQ_target = 0.15*self.non_float
            self.UPRO_target = 0.15*self.non_float
            self.TMF_target = 0.50*self.non_float
            self.Plot("BULL - BEAR", "Bull", 0)
            self.Plot("BULL - BEAR", "Bear", 1)
            self.bear=1
            self.Rebalance(data)
        
        # I guess these kind of work but trigger less often:
        # Bear exit early
        if self.ema_fast > self.ema_medium and self.bear == 1: #and self.ema_slow > self.ema_medium:
            self.TQQQ_target = 0.40*self.non_float
            self.UPRO_target = 0.40*self.non_float
            self.TMF_target = 0.20*self.non_float
            self.Plot("BULL - BEAR", "Bull", 3)
            self.Plot("BULL - BEAR", "Bear", 0)
            self.bear=0
            self.Rebalance(data)
        # Concave up
        if self.ema_fast < self.ema_slow and (self.slope.Current.Value > 0 and self.concavity.Current.Value > 0):
            self.TQQQ_target = 0.50*self.non_float
            self.UPRO_target = 0.30*self.non_float
            self.TMF_target = 0.2*self.non_float
            self.Plot("BULL - BEAR", "Bull", 2)
            self.Plot("BULL - BEAR", "Bear", 0)
            self.Rebalance(data)
        
        # Plot EMAs
        self.Plot("Benchmark", "Fast", self.ema_fast.Current.Value)
        self.Plot("Benchmark", "Medium", self.ema_medium.Current.Value)
        self.Plot("Benchmark", "Slow", self.ema_slow.Current.Value)
        
        # Plot Bear
        self.Plot("Bear", "Bear", self.bear)
        
        # Plot slope/concavity
        self.Plot("Derivatives", "Slope", self.slope.Current.Value)
        self.Plot("Derivatives", "Concavity", self.concavity.Current.Value)
        
        # Plot assets 
        self.Plot("Assets", "TMF", self.Securities["TMF"].Close)
        self.Plot("Assets", "UPRO", self.Securities["UPRO"].Close)
        self.Plot("Assets", "TQQQ", self.Securities["TQQQ"].Close)
        #self.Plot("Assets", "TYD", self.Securities["TYD"].Close)
        
        # Check if we're not invested and then put portfolio 100% in the SPY ETF.      
        if not self.Portfolio.Invested:
           self.SetHoldings("TQQQ", self.TQQQ_target)
           self.SetHoldings("UPRO", self.UPRO_target)
           self.SetHoldings("TMF", self.TMF_target)
        
        #rebalance_date = self.Time + timedelta(days = 370)
        
        if self.Time == self.rebalance_date:
            self.Rebalance(data)
            
            
            
        # store the current benchmark close price
        benchmark = self.Securities["SPY"].Close
        # enter our strategy
        if not self.Portfolio.Invested:
            self.Rebalance(data)
        # Calculate the performance of our benchmark and update our benchmark value for plotting
        if self.lastBenchmarkValue is not  None:
           self.BenchmarkPerformance = self.BenchmarkPerformance * (benchmark/self.lastBenchmarkValue)
        # store today's benchmark close price for use tomorrow
        self.lastBenchmarkValue = benchmark
        # make our plots
        self.Plot("Strategy vs Benchmark", "Portfolio Value", self.Portfolio.TotalPortfolioValue)
        self.Plot("Strategy vs Benchmark", "Benchmark", self.BenchmarkPerformance)
        
          
    def Rebalance(self, data):
        self.SetHoldings("TQQQ", self.TQQQ_target)
        self.SetHoldings("UPRO", self.UPRO_target)
        self.SetHoldings("TMF", self.TMF_target)
        
        self.rebalance_date = self.Time + timedelta(days = 100)
        self.Log("Rebalanced")