Overall Statistics
Total Trades
21390
Average Win
0.07%
Average Loss
-0.06%
Compounding Annual Return
37.606%
Drawdown
23.600%
Expectancy
0.687
Net Profit
6304.328%
Sharpe Ratio
1.51
Probabilistic Sharpe Ratio
88.605%
Loss Rate
27%
Win Rate
73%
Profit-Loss Ratio
1.30
Alpha
0.311
Beta
0.22
Annual Standard Deviation
0.22
Annual Variance
0.048
Information Ratio
0.896
Tracking Error
0.261
Treynor Ratio
1.512
Total Fees
$35145.27
'''
Intersection of ROC comparison using OUT_DAY approach by Vladimir v1.2 (dynamic selector)

inspired by Peter Guenther, Tentor Testivis, Dan Whitnable, Thomas Chang, Miko M.

https://www.quantconnect.com/forum/discussion/10246/intersection-of-roc-comparison-using-out-day-approach/p1/comment-29043
Miko M version that dynamically selects top momentum stocks modified by Vladimir
'''
import numpy as np
# ------------------------------------------------------------------------------------------------
BONDS = ['TLT','TLH']; VOLA = 126; BASE_RET = 85; DV_N = 500; RETURN = 252; R_N = 10; LEV = 1.00; 
# ------------------------------------------------------------------------------------------------

class ROC_Comparison_IN_OUT(QCAlgorithm):

    def Initialize(self):

        self.SetStartDate(2008, 1, 1)
        self.SetEndDate(2021, 1, 7)
        self.InitCash = 100000
        self.SetCash(self.InitCash)  
        self.MKT = self.AddEquity("SPY", Resolution.Hour).Symbol
        self.mkt = []
        self.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage, AccountType.Margin)
        res = Resolution.Hour
        
        self.STOCKS = [] # Selected using the universe selection
        self.UniverseSettings.Resolution = res
        self.BONDS = [self.AddEquity(ticker, res).Symbol for ticker in BONDS]

        self.SLV = self.AddEquity('SLV', res).Symbol  
        self.GLD = self.AddEquity('GLD', res).Symbol  
        self.XLI = self.AddEquity('XLI', res).Symbol 
        self.XLU = self.AddEquity('XLU', res).Symbol
        self.DBB = self.AddEquity('DBB', res).Symbol  
        self.UUP = self.AddEquity('UUP', res).Symbol  

        self.pairs = [self.SLV, self.GLD, self.XLI, self.XLU, self.DBB, self.UUP]
        
        self.SetUniverseSelection(FineFundamentalUniverseSelectionModel(self.coarseSelector, self.fineSelector))
        self.UniverseSettings.Resolution = Resolution.Minute
        
        self.bull = 1        
        self.count = 0 
        self.outday = 0        
        self.wt = {}
        self.real_wt = {}
        self.universeMonth = -1

        self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.AfterMarketOpen('SPY', 60),
            self.daily_check)
        self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.AfterMarketOpen('SPY', 120),
            self.trade)    

        symbols = [self.MKT] + self.pairs
        for symbol in symbols:
            self.consolidator = TradeBarConsolidator(timedelta(days=1))
            self.consolidator.DataConsolidated += self.consolidation_handler
            self.SubscriptionManager.AddConsolidator(symbol, self.consolidator)
        
        self.history = self.History(symbols, VOLA + 1, Resolution.Daily)
        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[-(VOLA + 1):] 
        

    def daily_check(self):
        
        vola = self.history[[self.MKT]].pct_change().std() * np.sqrt(252)
        #wait_days = int(vola * BASE_RET)
        #period = int((1.0 - vola) * BASE_RET)        
        #r = self.history.pct_change(period).iloc[-1]
        r = self.history.pct_change(int((1.0 - vola) * BASE_RET)).iloc[-1]

        exit = ((r[self.SLV] < r[self.GLD]) and (r[self.XLI] < r[self.XLU]) and (r[self.DBB] < r[self.UUP]))

        if exit:
            self.bull = 0
            self.outday = self.count
        #if self.count >= self.outday + wait_days:
        if self.count >= self.outday + int(vola * BASE_RET):
            self.bull = 1
        self.count += 1
        
        
    def coarseSelector(self, coarse):
        
        if self.Time.month == self.universeMonth:
            return self.STOCKS

        eqs = [x for x in coarse if (x.HasFundamentalData == True)]
        dollar_volume_sorted = sorted(eqs, key=lambda x: x.DollarVolume, reverse = True)
        top = dollar_volume_sorted[:DV_N]
        top_eqs = [x.Symbol for x in top]
        
        return top_eqs
        

    def fineSelector(self, fine):

        if self.Time.month == self.universeMonth:
            return self.STOCKS

        tech = [x for x in fine if x.AssetClassification.MorningstarSectorCode == MorningstarSectorCode.Technology]
        selected_symbols = [str(x.Symbol) for x in tech]
        
        hist = self.History(selected_symbols, RETURN + 1, Resolution.Daily)
        o = hist['open'].unstack(level = 0)
        scores = o.ix[-1] / o.ix[0]
        top_eqs = scores.sort_values(ascending = False)[:R_N]
        self.STOCKS = [self.Symbol(str(x)) for x in top_eqs.index]

        self.universeMonth = self.Time.month
        
        return self.STOCKS 
        
        
    def trade(self):    

        for sym in self.STOCKS:
            if self.Securities[sym].IsTradable == False:
                self.STOCKS.remove(sym)

        for pi in self.Portfolio.Values:
            eq = self.Symbol(str(pi.Symbol))
            if eq not in self.STOCKS:
                self.wt[eq] = 0.

        for sec in self.STOCKS:
            self.wt[sec] = LEV/len(self.STOCKS) if self.bull else 0;
        for sec in self.BONDS: 
            self.wt[sec] = 0 if self.bull else LEV/len(self.BONDS);

        for sec, weight in self.wt.items():
            #if weight == 0 and self.Portfolio[sec].IsLong:
            if weight == 0:
                self.Liquidate(sec)
            #elif weight != 0:
            else:
                self.SetHoldings(sec, weight)
                
                    
    def OnWeekEnd(self): 
        
        mkt_price = self.Securities[self.MKT].Close
        self.mkt.append(mkt_price)
        mkt_perf = self.InitCash * self.mkt[-1] / self.mkt[0] 
        self.Plot('Strategy Equity', self.MKT, mkt_perf)     
        
        account_leverage = self.Portfolio.TotalHoldingsValue / self.Portfolio.TotalPortfolioValue
        
        self.Plot('Holdings', 'leverage', round(account_leverage, 2))
        self.Plot('Holdings', 'Target Leverage', LEV)