Overall Statistics
Total Trades
662
Average Win
0.48%
Average Loss
-0.27%
Compounding Annual Return
14.846%
Drawdown
11.600%
Expectancy
0.543
Net Profit
70.352%
Sharpe Ratio
1.174
Probabilistic Sharpe Ratio
60.581%
Loss Rate
45%
Win Rate
55%
Profit-Loss Ratio
1.80
Alpha
0.051
Beta
0.395
Annual Standard Deviation
0.089
Annual Variance
0.008
Information Ratio
-0.267
Tracking Error
0.12
Treynor Ratio
0.265
Total Fees
$1939.73
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(2018, 1, 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(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)