Overall Statistics
Total Trades
3224
Average Win
0.32%
Average Loss
-0.07%
Compounding Annual Return
20.154%
Drawdown
15.400%
Expectancy
2.124
Net Profit
955.931%
Sharpe Ratio
1.62
Probabilistic Sharpe Ratio
95.432%
Loss Rate
42%
Win Rate
58%
Profit-Loss Ratio
4.37
Alpha
0.207
Beta
0.048
Annual Standard Deviation
0.131
Annual Variance
0.017
Information Ratio
0.428
Tracking Error
0.236
Treynor Ratio
4.423
Total Fees
$5497.85
"""
Based on 'In & Out' strategy by Peter Guenther 10-04-2020
expanded/inspired by Tentor Testivis, Dan Whitnable (Quantopian), Vladimir, and Thomas Chang.

https://www.quantopian.com/posts/new-strategy-in-and-out
"""

# Import packages
import numpy as np
import pandas as pd
import scipy as sc


class InOut(QCAlgorithm):

    def Initialize(self):

        self.SetStartDate(2008, 1, 1)  # Set Start Date
        self.cap = 100000
        self.SetCash(self.cap)  # Set Strategy Cash
        self.UniverseSettings.Resolution = Resolution.Minute

        # Feed-in constants
        self.INI_WAIT_DAYS = 15  # out for 3 trading weeks

        res = Resolution.Minute

        self.MRKT = self.AddEquity('SPY', res).Symbol
        self.TLT = self.AddEquity('TLT', res).Symbol
        self.IEF = self.AddEquity('IEF', res).Symbol

        # Market and list of signals based on ETFs
        self.PRDC = self.AddEquity('XLI', res).Symbol  # production (industrials)
        self.METL = self.AddEquity('DBB', res).Symbol  # input prices (metals)
        self.NRES = self.AddEquity('IGE', res).Symbol  # input prices (natural res)
        self.DEBT = self.AddEquity('SHY', res).Symbol  # cost of debt (bond yield)
        self.USDX = self.AddEquity('UUP', res).Symbol  # safe haven (USD)
        self.GOLD = self.AddEquity('GLD', res).Symbol  # gold
        self.SLVA = self.AddEquity('SLV', res).Symbol  # VS silver
        self.UTIL = self.AddEquity('XLU', res).Symbol  # utilities
        self.SHCU = self.AddEquity('FXF', res).Symbol  # safe haven (CHF)
        self.RICU = self.AddEquity('FXA', res).Symbol  # risk currency (AUD)
        self.INDU = self.PRDC  # vs industrials

        self.FORPAIRS = [self.GOLD, self.SLVA, self.UTIL, self.SHCU, self.RICU]
        self.SIGNALS = [self.PRDC, self.METL, self.NRES, self.DEBT, self.USDX]

        # 'In' and 'out' holdings incl. weights
        self.HLD_IN = {self.MRKT: 1.0}
        self.HLD_OUT = {self.TLT: .5, self.IEF: .5}

        # Initialize variables
        ## 'In'/'out' indicator
        self.be_in = 1
        ## Day count variables
        self.dcount = 0  # count of total days since start
        self.outday = 0  # dcount when self.be_in=0
        ## Flexi wait days
        self.WDadjvar = self.INI_WAIT_DAYS
        self.spy = []


        self.Schedule.On(
            self.DateRules.EveryDay(),
            self.TimeRules.AfterMarketOpen('SPY', 75),
            self.rebalance_when_out_of_the_market
        )


        self.Schedule.On(
            self.DateRules.WeekEnd(),
            self.TimeRules.AfterMarketOpen('SPY', 75),
            self.rebalance_when_in_the_market
        )

    def rebalance_when_out_of_the_market(self):
        # Returns sample to detect extreme observations
        hist = self.History(
            self.SIGNALS + [self.MRKT] + self.FORPAIRS, 252, Resolution.Daily)['close'].unstack(level=0).dropna()

        hist_shift = hist.rolling(66).apply(lambda x: x[:11].mean())

        returns_sample = (hist / hist_shift - 1)
        # Reverse code USDX: sort largest changes to bottom
        returns_sample[self.USDX] = returns_sample[self.USDX] * (-1)
        # For pairs, take returns differential, reverse coded
        returns_sample['G_S'] = -(returns_sample[self.GOLD] - returns_sample[self.SLVA])
        returns_sample['U_I'] = -(returns_sample[self.UTIL] - returns_sample[self.INDU])
        returns_sample['C_A'] = -(returns_sample[self.SHCU] - returns_sample[self.RICU])    
        self.pairlist = ['G_S', 'U_I', 'C_A']

        # Extreme observations; statist. significance = 1%
        pctl_b = np.nanpercentile(returns_sample, 1, axis=0)
        extreme_b = returns_sample.iloc[-1] < pctl_b

        # Determine waitdays empirically via safe haven excess returns, 50% decay
        self.WDadjvar = int(
            max(0.50 * self.WDadjvar,
                self.INI_WAIT_DAYS * max(1,
                                         returns_sample[self.GOLD].iloc[-1] / returns_sample[self.SLVA].iloc[-1],
                                         returns_sample[self.UTIL].iloc[-1] / returns_sample[self.INDU].iloc[-1],
                                         returns_sample[self.SHCU].iloc[-1] / returns_sample[self.RICU].iloc[-1]
                                         ))
        )
        adjwaitdays = min(60, self.WDadjvar)


        # Determine whether 'in' or 'out' of the market
        if (extreme_b[self.SIGNALS + self.pairlist]).any():
            self.be_in = False
            self.outday = self.dcount
        if self.dcount >= self.outday + adjwaitdays:
            self.be_in = True
        self.dcount += 1

        # Swap to 'out' assets if applicable
        if not self.be_in:
            # Close 'In' holdings
            for asset, weight in self.HLD_IN.items():
                self.SetHoldings(asset, 0)

            for asset, weight in self.HLD_OUT.items():
                self.SetHoldings(asset, weight)



        self.Plot("In Out", "in_market", int(self.be_in))
        self.Plot("In Out", "num_out_signals", extreme_b[self.SIGNALS + self.pairlist].sum())
        self.Plot("Wait Days", "waitdays", adjwaitdays)

        # to plot SPY on the same chart as the performance of our algo
        self.spy.append(hist[self.MRKT].iloc[-1])
        spy_perf = self.spy[-1] / self.spy[0] * self.cap
        self.Plot("Strategy Equity", "SPY", spy_perf)


    def rebalance_when_in_the_market(self):
        # Swap to 'in' assets if applicable
        if self.be_in:
            # Close 'Out' holdings
            for asset, weight in self.HLD_OUT.items():
                self.SetHoldings(asset, 0)

            for asset, weight in self.HLD_IN.items():
                self.SetHoldings(asset, weight)
"""
Based on 'In & Out' strategy by Peter Guenther 10-04-2020
expanded/inspired by Tentor Testivis, Dan Whitnable (Quantopian), Vladimir, and Thomas Chang.

https://www.quantopian.com/posts/new-strategy-in-and-out


# Import packages
import numpy as np
import pandas as pd
import scipy as sc

def initialize(context): 
    # Feed-in constants
    context.INI_WAIT_DAYS = 15 # out for 3 trading weeks
    
    # 'In' and 'out' holdings incl. weights
    context.HLD_IN = {symbol('SPY'): 1.0}
    context.HLD_OUT = {symbol('TLT'): .5, symbol('IEF'): .5}
    
    # Market and list of signals based on ETFs
    context.MRKT = symbol('SPY')
    context.PRDC = symbol('XLI') # production (industrials)
    context.METL = symbol('DBB') # input prices (metals)
    context.NRES = symbol('IGE') # input prices (natural res)
    context.DEBT = symbol('SHY') # cost of debt (bond yield)
    context.USDX = symbol('UUP') # safe haven (USD)
    context.SIGNALS = [context.PRDC, context.METL, context.NRES, context.DEBT, context.USDX]
    
    # Pairs for comparative returns signals
    context.GOLD = symbol('GLD') # gold
    context.SLVA = symbol('SLV') # VS silver
    context.UTIL = symbol('XLU') # utilities
    context.INDU = context.PRDC # vs industrials
    context.SHCU = symbol('FXF') # safe haven (CHF)
    context.RICU = symbol('FXA') # risk currency (AUD)
    context.FORPAIRS = [context.GOLD, context.SLVA, context.UTIL, context.SHCU, context.RICU]
    
    # Initialize variables
    ## 'In'/'out' indicator
    context.be_in = 1
    ## Day count variables
    context.dcount = 0  # count of total days since start
    context.outday = 0  # dcount when context.be_in=0
    ## Flexi wait days
    context.WDadjvar = context.INI_WAIT_DAYS
    
    # Commission
    set_commission(commission.PerShare(cost=0.0075, min_trade_cost=1.00))
    
    # Schedule functions  
    schedule_function(  
        # daily rebalance if OUT of the market
        rebalance_when_out_of_the_market,  
        date_rules.every_day(),
        time_rules.market_op en(minutes = 75)
    ) 
    schedule_function(  
        # weekly rebalance if IN the market
        rebalance_when_in_the_market,  
        date_rules.week_start(days_offset=4),
        time_rules.market_op en(minutes = 75)
    )

def rebalance_when_out_of_the_market(context, data):
    # Returns sample to detect extreme observations
    hist = data.history(context.SIGNALS+[context.MRKT]+context.FORPAIRS, 'close', 253, '1d').iloc[:-1]
    hist_shift = hist.apply(lambda x: (x.shift(65)+x.shift(64)+x.shift(63)+x.shift(62)+x.shift(61)+x.shift(60)+x.shift(59)+x.shift(58)+x.shift(57)+x.shift(56)+x.shift(55))/11)
    returns_sample = (hist/hist_shift-1)
    # Reverse code USDX: sort largest changes to bottom
    returns_sample[context.USDX] = returns_sample[context.USDX]*(-1)
    # For pairs, take returns differential, reverse coded
    returns_sample['G_S'] = -(returns_sample[context.GOLD] - returns_sample[context.SLVA])
    returns_sample['U_I'] = -(returns_sample[context.UTIL] - returns_sample[context.INDU])
    returns_sample['C_A'] = -(returns_sample[context.SHCU] - returns_sample[context.RICU])
    context.pairlist = ['G_S', 'U_I', 'C_A']
    
    # Extreme observations; statist. significance = 1%
    pctl_b = np.nanpercentile(returns_sample, 1, axis=0)
    extreme_b = returns_sample.iloc[-1] < pctl_b
 
    # Determine waitdays empirically via safe haven excess returns, 50% decay
    context.WDadjvar = int(max(0.50*context.WDadjvar, context.INI_WAIT_DAYS * max(1,returns_sample[context.GOLD].iloc[-1] / returns_sample[context.SLVA].iloc[-1],returns_sample[context.UTIL].iloc[-1] / returns_sample[context.INDU].iloc[-1],returns_sample[context.SHCU].iloc[-1] / returns_sample[context.RICU].iloc[-1])))
    adjwaitdays = min(60, context.WDadjvar)
    
    # Determine whether 'in' or 'out' of the market
    if (extreme_b[context.SIGNALS+context.pairlist]).any():
        context.be_in = False
        context.outday = context.dcount
    if context.dcount >= context.outday + adjwaitdays:
        context.be_in = True
    context.dcount += 1
    
    # Swap to 'out' assets if applicable
    if not context.be_in:
        for asset, weight in context.HLD_OUT.items(): 
            order_target_percent(asset, weight) 
        for asset in context.portfolio.positions:
            # Close 'In' holdings
            if asset not in context.HLD_OUT:
                order_target_percent(asset, 0) 
     
    # Record 
    record(in_market=context.be_in, num_out_signals=extreme_b[context.SIGNALS+context.pairlist].sum(), waitdays=adjwaitdays)
        
def rebalance_when_in_the_market(context, data):
    # Swap to 'in' assets if applicable
    if context.be_in:
        for asset, weight in context.HLD_IN.items(): 
            order_target_percent(asset, weight) 
        for asset in context.portfolio.positions:
            # Close 'Out' holdings
            if asset not in context.HLD_IN:
                order_target_percent(asset, 0)
"""