Overall Statistics
Total Trades
3524
Average Win
1.15%
Average Loss
-0.95%
Compounding Annual Return
17.374%
Drawdown
35.500%
Expectancy
0.222
Net Profit
3356.721%
Sharpe Ratio
1.074
Probabilistic Sharpe Ratio
49.997%
Loss Rate
45%
Win Rate
55%
Profit-Loss Ratio
1.21
Alpha
0.114
Beta
0.513
Annual Standard Deviation
0.142
Annual Variance
0.02
Information Ratio
0.549
Tracking Error
0.139
Treynor Ratio
0.297
Total Fees
$80745.74
## T. Smith - Reductionist ROC comparison XLI-XLU - Faster trading - Long SPY only - Sharpe >1 since 1999 - Inspired by Vladimir & Peter Guenther

import numpy as np

class DualMomentumInOut(QCAlgorithm):

    def Initialize(self):

        self.SetStartDate(1999, 1, 1) 
        self.cap = 100000
        self.RETURN = 10 
       
        self.STK = self.AddEquity('SPY', Resolution.Hour).Symbol
        self.XLI = self.AddEquity('XLI', Resolution.Hour).Symbol 
        self.XLU = self.AddEquity('XLU', Resolution.Hour).Symbol
        self.MKT = self.AddEquity('SPY', Resolution.Daily).Symbol 

        self.pairs = [self.XLI, self.XLU]
       
        self.wt = {}
        self.real_wt = {}
        self.mkt = []
        self.SetWarmUp(timedelta(350))

        self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.Every(TimeSpan.FromMinutes(240)),
            self.daily_check)
            
        symbols = self.pairs
        for symbol in symbols:
            self.consolidator = TradeBarConsolidator(timedelta(hours=1))
            self.consolidator.DataConsolidated += self.consolidation_handler
            self.SubscriptionManager.AddConsolidator(symbol, self.consolidator)
        
        self.history = self.History(symbols, self.RETURN + 1, Resolution.Hour)
        if self.history.empty or 'close' not in self.history.columns:
            return
        self.history = self.history['close'].unstack(level=0).dropna()
        
    def consolidation_handler(self, sender, consolidated):
        self.history.loc[consolidated.EndTime, consolidated.Symbol] = consolidated.Close
        self.history = self.history.iloc[-(self.RETURN + 1):] 
        
    def daily_check(self):
        
        r = self.history.pct_change(self.RETURN).iloc[-1]

        if (r[self.XLI] < r[self.XLU]):
            self.wt[self.STK] = 0 
            self.trade()
        else:
            self.wt[self.STK] = 1 
            self.trade() 
             
    def trade(self):

        for sec, weight in self.wt.items():
            if weight == 0 and self.Portfolio[sec].IsLong:
                self.Liquidate(sec)
                
            cond1 = weight == 0 and self.Portfolio[sec].IsLong
            cond2 = weight > 0 and not self.Portfolio[sec].Invested
            if cond1 or cond2:
                self.SetHoldings(sec, weight)
            
    def OnEndOfDay(self):                
        mkt_price = self.Securities[self.MKT].Close
        self.mkt.append(mkt_price)
        mkt_perf = self.mkt[-1] / self.mkt[0] * self.cap
        self.Plot('Strategy Equity', 'SPY', mkt_perf)
        
        for sec, weight in self.wt.items(): 
            self.real_wt[sec] = round(self.ActiveSecurities[sec].Holdings.Quantity * self.Securities[sec].Price / self.Portfolio.TotalPortfolioValue,4)
            self.Plot('Holdings', self.Securities[sec].Symbol, round(self.real_wt[sec], 3))