Overall Statistics
Total Trades
224
Average Win
3.31%
Average Loss
-1.89%
Compounding Annual Return
6.734%
Drawdown
30.100%
Expectancy
0.201
Net Profit
43.879%
Sharpe Ratio
0.512
Probabilistic Sharpe Ratio
11.419%
Loss Rate
56%
Win Rate
44%
Profit-Loss Ratio
1.75
Alpha
0.074
Beta
0.05
Annual Standard Deviation
0.158
Annual Variance
0.025
Information Ratio
-0.191
Tracking Error
0.237
Treynor Ratio
1.606
Total Fees
$224.00
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