Overall Statistics
Total Trades
382
Average Win
0.52%
Average Loss
-0.28%
Compounding Annual Return
19.400%
Drawdown
11.700%
Expectancy
0.648
Net Profit
49.491%
Sharpe Ratio
1.358
Probabilistic Sharpe Ratio
68.299%
Loss Rate
42%
Win Rate
58%
Profit-Loss Ratio
1.85
Alpha
0.065
Beta
0.399
Annual Standard Deviation
0.103
Annual Variance
0.011
Information Ratio
-0.356
Tracking Error
0.137
Treynor Ratio
0.351
Total Fees
$1041.15
Estimated Strategy Capacity
$12000000.00
Lowest Capacity Asset
BND TRO5ZARLX6JP
# Price - Moving Average(4 Hour bar) Cross 

# -----------------------------------
STOCK = "SPY"; BOND = "BND"; MA = 20;
# -----------------------------------

class PMAC_FourHourBar(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2019, 8, 1)
        self.SetEndDate(2021, 11, 5)
        self.SetCash(100000) 
        self.stock = self.AddEquity(STOCK, Resolution.Hour).Symbol
        self.bond = self.AddEquity(BOND, Resolution.Hour).Symbol
        consolidator = TradeBarConsolidator(timedelta(hours = 4)) 
        self.SubscriptionManager.AddConsolidator(self.stock, consolidator)
        self.sma = SimpleMovingAverage(MA)    
        self.RegisterIndicator(self.stock, self.sma, consolidator) 
        self.SetWarmUp(4*MA, Resolution.Hour)
        self.uptrend = 0
        
        
    def OnData(self, data):
        if self.IsWarmingUp: return 
        if not self.sma.IsReady or self.stock not in data or self.bond not in data:
            return
        if data[self.stock].Price >= self.sma.Current.Value and not self.uptrend:
                self.SetHoldings(self.stock, 0.8)
                self.SetHoldings(self.bond, 0.2)
                self.uptrend = 1
                
        elif data[self.stock].Price < self.sma.Current.Value and self.uptrend:
            self.SetHoldings(self.stock, 0.2)
            self.SetHoldings(self.bond, 0.8)
            self.uptrend = 0

        self.Plot("Benchmark", "SMA", self.sma.Current.Value)