Overall Statistics
Total Trades
30
Average Win
52.32%
Average Loss
-2.47%
Compounding Annual Return
290.016%
Drawdown
41.900%
Expectancy
7.857
Net Profit
597.658%
Sharpe Ratio
5.943
Probabilistic Sharpe Ratio
96.534%
Loss Rate
60%
Win Rate
40%
Profit-Loss Ratio
21.14
Alpha
3.587
Beta
0.471
Annual Standard Deviation
0.626
Annual Variance
0.392
Information Ratio
5.459
Tracking Error
0.63
Treynor Ratio
7.889
Total Fees
$159.14
Estimated Strategy Capacity
$180000000.00
Lowest Capacity Asset
TSLA UNU3P8Y3WFAD
# Last N Hour bars above a moving average
# ------------------------------------------
STOCK = 'TSLA'; MA_F = 1; MA_S = 200; N = 5;
# ------------------------------------------
class MAC(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2020, 1, 1)
        self.SetEndDate(2021, 6, 4)
        self.SetCash(100000)
        res = Resolution.Hour
        self.stock = self.AddEquity(STOCK, res).Symbol 
        self.SetWarmUp(MA_S, res)
        self.sma_fast = self.SMA(self.stock, MA_F, res)
        self.sma_slow = self.SMA(self.stock, MA_S, res)
        self.count = 0
        

    def OnData(self, data):
        if self.IsWarmingUp: return 
        if not self.sma_fast.IsReady: return 
        if not self.sma_slow: return 

        self.Plot("SMA", "sma_fast", self.sma_fast.Current.Value)
        self.Plot("SMA", "sma_slow", self.sma_slow.Current.Value)
        
        if self.sma_fast.Current.Value > self.sma_slow.Current.Value:  
            self.count += 1
            
        elif self.sma_fast.Current.Value < self.sma_slow.Current.Value: 
            self.count = 0
                
        if self.count > N and not self.Portfolio[self.stock].IsLong:
            self.SetHoldings(self.stock, 1)  
            
        elif self.count <= N and self.Portfolio[self.stock].IsLong: 
            self.SetHoldings(self.stock, 0) 
            
        self.Plot("Above MA", "count", self.count)
        self.Plot("Above MA", "threshold", N)