Overall Statistics
class BounceBack(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2015, 1, 1)
        self.SetEndDate(2015, 2, 10)
        self.SetCash(100000)
        
        # Volatile ETFs
        tickers = 'UVXY,XIV,NUGT,DUST,JNUG,JDUST,LABU,LABD,GUSH,DRIP,TVIX,GASL,GASX,DWTI,UWTI,DGAZ,UGAZ,UBIO,ZBIO,BRZU,RUSS,SCO,UCO,RUSL,ERY,ERX,BIOL,SVXY,VXX,SILJ,BIB,BIS,VIXY,SOXL,VIIX,SOXS,BZQ,USLV,SLVP,DSLV,GDXJ,GLDX'
        
        resolution = Resolution.Hour
        security = self.AddEquity('TQQQ', resolution)
        security.FeeModel = ConstantFeeModel(0)
        self.sym = security.Symbol
        self.ma = self.SMA(self.sym, 50, resolution)
        self.priceWindow = RollingWindow[float](2)
      
        self.SetWarmUp(50)
        
        self.threshold = 0.005
        
       
        self.limitTicket = None
        self.stopTicket = None
        
    def OnData(self, data):
        if self.IsWarmingUp or not self.ma.IsReady:
            return
        
        if self.sym not in data or data[self.sym] is None:
            return
        
        price = data[self.sym].Price
        
        self.priceWindow.Add(price)
        
        if not self.priceWindow.IsReady:
            return
        
        ma = self.ma.Current.Value
       
        # long crossover
        if self.priceWindow[0] > ma * (1 + self.threshold) and self.priceWindow[1] < ma * (1 - self.threshold):
            if not self.Portfolio.Invested:
                limit = 0.03
                stop = 0.01
                quant = self.CalculateOrderQuantity(self.sym, 0.80)
                self.MarketOrder(self.sym, quant)
                self.stopTicket = self.StopMarketOrder(self.sym, -quant, price * (1 - stop))    
                self.limitTicket = self.LimitOrder(self.sym, -quant, price * (1 + limit))
                
        self.Plot('Chart', 'MA', ma)
        self.Plot('Chart', 'Price', price)
        
    def OnOrderEvent(self, orderevent):
        if orderevent.Status != OrderStatus.Filled:
            return
         
        
        if self.stopTicket != None and orderevent.OrderId == self.stopTicket.OrderId:
            self.Debug(f"STOP FILLED, LIMIT CANCELLED @ {self.Time}")
            response = self.limitTicket.Cancel()
           
            
       
        if self.limitTicket != None and orderevent.OrderId == self.limitTicket.OrderId:
            self.Debug(f"LIMIT FILLED, STOP CANCELLED @ {self.Time}")
            response = self.stopTicket.Cancel()