| Overall Statistics |
|
Total Trades 6891 Average Win 0.59% Average Loss -0.09% Compounding Annual Return 77.923% Drawdown 34.400% Expectancy 2.939 Net Profit 170563.180% Sharpe Ratio 2.239 Probabilistic Sharpe Ratio 98.240% Loss Rate 45% Win Rate 55% Profit-Loss Ratio 6.20 Alpha 0.864 Beta 0.671 Annual Standard Deviation 0.422 Annual Variance 0.178 Information Ratio 2.043 Tracking Error 0.404 Treynor Ratio 1.406 Total Fees $77804.84 |
"""
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.SetCash(10000) # Set Strategy Cash
self.UniverseSettings.Resolution = Resolution.Daily
res = Resolution.Minute
#margin_leverage = 1 == No margin
self.max_margin_leverage = 2
self.margin_leverage = 1.5
# stock selection and enter condition
self.STKSEL = {
self.AddEquity('QLD', res).Symbol: {"margin_leverage": 1.95, "holding": .5, "last_exit_price": 0},
self.AddEquity('SSO', res).Symbol: {"margin_leverage": 1.95, "holding": .5, "last_exit_price": 0}
}
self.STKSEL_FOR_HEDGE = {
self.AddEquity('TLT', res).Symbol: {"margin_leverage": 1.95, "holding": .35, "last_exit_price": 0},
self.AddEquity('IEF', res).Symbol: {"margin_leverage": 1.95, "holding": .35, "last_exit_price": 0},
self.AddEquity('IEI', res).Symbol: {"margin_leverage": 1.95, "holding": .2, "last_exit_price": 0},
self.AddEquity('UUP', res).Symbol: {"margin_leverage": 1.95, "holding": .1, "last_exit_price": 0}
}
# Calcuate current price
self.cal_current_price = True
self.max_drop_percent = 0.15
self.max_drop_check = False
self.max_drop_in = False
self.current_price = {}
# Feed-in constants
self.INI_WAIT_DAYS = 15 # out for 3 trading weeks
self.MRKT = self.AddEquity('SPY', 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 = {}
for symbol in self.STKSEL:
self.HLD_IN[symbol] = self.STKSEL[symbol]['holding'] * self.STKSEL[symbol]['margin_leverage']
self.HLD_OUT = {}
for symbol in self.STKSEL_FOR_HEDGE:
self.HLD_OUT[symbol] = self.STKSEL_FOR_HEDGE[symbol]['holding'] * self.STKSEL_FOR_HEDGE[symbol]['margin_leverage']
# 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
## Setup Margin ##
self.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage, AccountType.Margin)
## Add 2x Margin
for symbol in self.HLD_IN:
self.Securities[symbol].SetLeverage(self.max_margin_leverage)
#self.Securities[symbol].MarginModel = PatternDayTradingMarginModel()
for symbol in self.HLD_OUT:
self.Securities[symbol].SetLeverage(self.max_margin_leverage)
#self.Securities[symbol].MarginModel = PatternDayTradingMarginModel()
self.Schedule.On(
self.DateRules.EveryDay(),
self.TimeRules.AfterMarketOpen('SPY', 120),
self.rebalance_when_out_of_the_market
)
self.Schedule.On(
self.DateRules.WeekEnd(),
self.TimeRules.AfterMarketOpen('SPY', 120),
self.rebalance_when_in_the_market
)
def OnData(self, data):
if self.cal_current_price and data != None:
for symbol in self.HLD_IN:
if data.ContainsKey(symbol) and data[symbol] != None:
self.current_price[symbol] = data[symbol].Close
for symbol in self.HLD_OUT:
if data.ContainsKey(symbol) and data[symbol] != None:
self.current_price[symbol] = data[symbol].Close
for symbol in self.SIGNALS + self.FORPAIRS:
if data.ContainsKey(symbol) and data[symbol] != None:
self.current_price[symbol] = data[symbol].Close
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())
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[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']
# 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]
np.where((returns_sample[self.GOLD].iloc[-1]>0) & (returns_sample[self.SLVA].iloc[-1]<0) & (returns_sample[self.SLVA].iloc[-2]>0), self.INI_WAIT_DAYS, 1),
np.where((returns_sample[self.UTIL].iloc[-1]>0) & (returns_sample[self.INDU].iloc[-1]<0) & (returns_sample[self.INDU].iloc[-2]>0), self.INI_WAIT_DAYS, 1),
np.where((returns_sample[self.SHCU].iloc[-1]>0) & (returns_sample[self.RICU].iloc[-1]<0) & (returns_sample[self.RICU].iloc[-2]>0), self.INI_WAIT_DAYS, 1)
))
)
#self.Debug('Wait Day Before: {}'.format(self.WDadjvar))
adjwaitdays = min(60, self.WDadjvar)
#self.Debug('Wait Day After: {}'.format(self.WDadjvar))
# Remove unrelated pairs first
returns_sample[self.FORPAIRS] = 0
# Extreme observations; statist. significance = 1%
pctl_b = np.nanpercentile(returns_sample, 1, axis=0)
extreme_b = returns_sample.iloc[-1] < pctl_b
# 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 and not self.max_drop_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)
#Calculate last exit price
if self.cal_current_price:
for symbol in self.STKSEL:
self.STKSEL[symbol]['last_exit_price'] = self.current_price[symbol]
#self.Plot( "Stock Out Price", "Price ({})".format(symbol), self.STKSEL[symbol]['last_exit_price'])
#for symbol in self.STKSEL_FOR_HEDGE:
# self.Plot( "Stock In Price", "Price ({})".format(symbol), self.current_price[symbol])
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)
for symbol in self.HLD_IN:
self.Plot("Stock Price", "Price ({})".format(symbol), self.current_price[symbol])
for symbol in self.HLD_OUT:
self.Plot("Stock Price", "Price ({})".format(symbol), self.current_price[symbol])
for symbol in self.SIGNALS:
self.Plot("Signal Index", "Index ({})".format(symbol), self.current_price[symbol])
for symbol in self.FORPAIRS:
self.Plot("Compare Index", "Index ({})".format(symbol), self.current_price[symbol])
def rebalance_when_in_the_market(self):
#Max Drop In Hold for 1 week and check again
if self.max_drop_check and self.max_drop_in:
self.max_drop_in = False
# Enter the market if any sel stock drop more than xxx%
if self.max_drop_check and self.cal_current_price and not self.be_in and not self.max_drop_in:
for symbol in self.STKSEL:
if self.current_price[symbol] != 0:
if self.STKSEL[symbol]['last_exit_price'] == 0:
self.STKSEL[symbol]['last_exit_price'] = self.current_price[symbol]
if (1- (self.current_price[symbol] / self.STKSEL[symbol]['last_exit_price'])) >= self.max_drop_percent:
self.be_in = True
self.max_drop_in = True
self.Plot("In Out", "max_drop_in", int(True))
else:
self.Plot("In Out", "max_drop_in", int(False))
# 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)
#if self.cal_current_price:
#for symbol in self.STKSEL:
# self.Plot( "Stock In Price", "Price ({})".format(symbol), self.current_price[symbol])
#for symbol in self.STKSEL_FOR_HEDGE:
# self.Plot( "Stock Out Price", "Price ({})".format(symbol), self.current_price[symbol])