Overall Statistics
Total Trades
21
Average Win
10.70%
Average Loss
-1.64%
Compounding Annual Return
31.486%
Drawdown
15.000%
Expectancy
2.764
Net Profit
47.800%
Sharpe Ratio
1.882
Probabilistic Sharpe Ratio
76.389%
Loss Rate
50%
Win Rate
50%
Profit-Loss Ratio
6.53
Alpha
0.262
Beta
0.263
Annual Standard Deviation
0.178
Annual Variance
0.032
Information Ratio
0.201
Tracking Error
0.268
Treynor Ratio
1.278
Total Fees
$49.76
Estimated Strategy Capacity
$87000000.00
Lowest Capacity Asset
QQQ RIWIV7K5Z9LX
# Last N Hour bars above a moving average
# ------------------------------------------
STOCK = 'QQQ'; 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)