Overall Statistics
class TransdimensionalNadionAtmosphericScrubbers(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2015, 1, 1)  # Set Start Date
        self.SetCash(10000)
        
        self.tsla = self.AddEquity("TSLA", Resolution.Minute).Symbol
        
        self.trailing_stop_distance = 10
        self.lookback_max = self.MAX(self.tsla, 22, Resolution.Daily)
        self.last_close = 0
        self.trailing_stop = None
        self.highest_tsla_price = 0


    def OnData(self, data):
        c = data[self.tsla].Close
        
        if self.Portfolio.Invested:
            if c > self.highest_tsla_price:
                self.highest_tsla_price = c
                
                update_fields = UpdateOrderFields()
                update_fields.StopPrice = c - self.trailing_stop_distance
                self.trailing_stop.Update(update_fields)
        else:
            prev_high = self.lookback_max.Current.Value
            if self.last_close <= prev_high and c > prev_high:
                quantity = self.CalculateOrderQuantity(self.tsla, 1)
                if quantity:
                    entry_price = self.MarketOrder(self.tsla, quantity).AverageFillPrice
                    self.highest_tsla_price = entry_price
                    
                    exit_price = entry_price - self.trailing_stop_distance
                    self.trailing_stop = self.StopMarketOrder(self.tsla, -quantity, exit_price)
        
        self.last_close = c
    
    def OnOrderEvent(self, orderEvent):
        if orderEvent.Status != OrderStatus.Filled:
            return
        if not self.Portfolio.Invested:
            self.highets_tsla_price = 0