This thread is meant to continue the development of the In & Out strategy started on Quantopian. The first challenge for us will probalbly be to translate our ideas to QC code.
I'll start by attaching the version Bob Bob kindly translated on Vladimir's request.
Vladimir:
About your key error, did you also initialize UUP like this?
self.UUP = self.AddEquity('UUP', res).Symbol
Peter Guenther
Vovik, we might be making a logical error with this comparison since the holdings are different (note FDN and TLH). It would be a bit like saying: "The Distilled Bear algo that Leandro posted here made 9,532%, so the "Distilled Bear" is better than the "Dual Momentum with Out Days" and the "In & Out".
In addition, I think that there could be value in moving beyond the comparison of individual in & outs and working on the question of whether it might be beneficial to combine them. Of course, be aware that this is 'rich data modelling' at its finest.
Liam Op
Peter,
Using definitions in your previous post:
> [parameter minimalists] Menno <---- Vladimir ----> Peter [rich data modelists]
I have the following questions/comments:
How did you determine that Menno belongs to "parameter minimalist" category?
Appreciate!
Peter Guenther
Liam Op, no worries. Check out page 2 of this thread, searching (Ctrl + F) for the term "Gedankenexperiment".
Vovik
Peter,
For comparison with Leandro Maia version Vladimir posted his v1.9
Peter Guenther
Interim stock taking
It’s been about half a year since the post below on Quantopian. Since then, much work has been done on multiple fronts. Merging this discussion into QuantConnect also has fired things up substantially.
This discussion has brought about multiple threads. An overview can be useful for people who want to explore all or some of the work that has been done. So here is a current list, approximately in chronological order:
- This thread. Focuses on developing in & out algos which then can be used to trade great stock selection strategies. It is an incubator for in & out-related ideas that are then further developed in dedicated threads.
- Amazing returns = superior stock selection strategy + superior in & out strategy (link). Focuses on combining in & outs with stock selection strategies, trying to find optimal combinations. Mostly uses the In & Out and Distilled Bear algos from this thread, combining it with leveraged ETFs (TQQQ and bonds), stock selections based on valuation (‘Valuation Rockets’), and stock selections based on quality fundamentals and momentum (‘Quality Companies in an Uptrend’).
- Dual Momentum with Out Days (link). Focuses on combining different in & outs (In & Out, Intersection of ROC comparison using Out_Day approach) with different equity ETFs (QQQ, FDN, IWF). Also, a focus on reducing the number of out signals (e.g. only using USD or Gold vs Silver and Utilities vs Industrials) as well as using leverage to boost returns.
- Intersection of ROC comparison using OUT_DAY approach (link). Focuses on combining the ‘Intersection of ROC comparison using OUT_DAY approach’ in & out algo with different equity holdings (e.g., combining QQQ, MSFT, and NFLX; FNGS; top 10 tech gainers). Also, discussion of sensitivity, leveraging, adding (trailing) stop losses, portfolio optimization, identifying 2x/3x bear and 2x/3x bull regimes.
- A very profitable version of IN and OUT, and why it is likely to fail in real life trading like its siblings (link). Argues that the in & out algos are overfitted and predicts that they will generate negative alpha in the future. Discusses strategies to test for overfitting and out-of-sampling testing. Also includes results from a parameter optimization run for the Dual Momentum In & Out.
- ROC comparison Utilities and Industrials (link). Focuses on minimizing the number of out signals to only one pair comparison (utilities vs industrials) and tests out-of-sample, compared to the other in & out backtests, by including the period from 1998-2007.
Great stuff, keep it up!
Strongs
Peter Guenther
Welcome to the discussion, Strongs. My two cents regarding whether the system will continue to work:
- of course, nobody can predict this with certainty
- one thing to consider: The critical component of the system in my view is not the bond side, but the equity side. You need to make a great selection here for the system to perform. For example, see our discussions in Amazing returns = superior stock selection strategy + superior in & out strategy.
- currently, bonds are going down since inflation and interest expectations are increasing (= economic recovery expectations). Currently, the algo is in equity not in bonds. When the algo does a solid job, then we only go in bonds when there is (substantial) jitter in the equity market. Jitter could occur because of doubts concerning the speed of the economic recovery. When there are these kinds of doubts, then bond prices will increase (= yields will fall) and, if the system switches accurately, we will benefit from this uptick to some extent.
Emiliano Fraticelli
Hi everybody! I deployed a slightly modified version of the algorithm for a client on IBKR US. But the client had an error some days ago and the strategy stopped.
Can anyone suggest the reason?
Thanks
Peter Guenther
Hi Emiliano Fraticelli, thanks for the note. Just speculating here: This could be an error at IB's end, that they could not unambiguously identify the stock that the algo is referring to (?). I know that some people are live-trading the algo, so they may have experienced something similar (I still trade them manually but may convert to auto-trading at some point). An additional thought: You could contact the QC team (via support) directly and they may be able to help. If you have a solution that worked, it would be great if you could share it here, so that others know what to do in a similar situation. Fingers crossed.
Jack Whisky
Hi Peter Guenther
I am new here. I read all your discussions with interest before I signed up, I still have to learn how to use the platform to try and make my contribution. Would you be so kind to explain to me what are the main differences between the various versions? I read that initially the exit signals came from retracements of XLI and other etfs. I did not understand the difference between the various versions '' Distilled Bear '', '' ROC '' and the latest version of IN & OUT. I did not understand exactly how the exit signals are calculated, nor the entry ones, from the backtests I saw that during the March collapse the algorithm held TLT for more than 15 days even if from the paper I understood that after 15 days you return in qqq or spy. Thanks in advance if you answer me, I think it might help other noobs like me :)Guy Fleury
The latest notebook is within the context of the IN & OUT trading strategies (all 5 versions). It states that the rebalancing schedule is responsible for most of the trading by these strategies and not necessarily the contraptions designed as trade triggering mechanism.
It is a different understanding from what is usually expressed in these 5 variants of the trading strategy. The math presented would also apply to other strategies using scheduled rebalancing as part of their trading design.
Hope some find it useful.
P.S.:
I am unable to attached a notebook or simply do not know how.
Here is that notebook as an HTML file: https://alphapowertrading.com/QC/Portfolio_Math.html
It can also be viewed in my latest article: Basic Portfolio Math
https://alphapowertrading.com/index.php/2-uncategorised/404-basic-stock-portfolio-math
Harsh Patel
Guy Fleury You are god damn right! I had been scratching my head past few days as irrespective of what 'stock selection' component I used in my modified version of 1.3 Intersection of ROC comparison using OUT_DAY approach by Vladimir and Leandro Maia , I more or less got same P/L with gains marginally within same range. It got me thinking that the ETF signals for IN/OUT and Volatility calculation for OUT DAYS played a nice role in defining the chart.
For example from period 2016-2021:
The Alpha I cooked past few weeks have stats like this with monthly periodic rebalance:
Adding the modified IN/OUT on varying timeframe rebalance gave something like this:
As you can see CAR nudged only by 6% but rest of the metrics seems to drastically fall under sweet spot or may be I might have overfitted my Alpha.
Thanks for sharing it was a good read.
Peter Guenther
Welcome to the discussion, Jack Whisky. You are right and this is still true regarding the In & Out: The exit signals are derived from substantial drops in the signal ETFs' prices. Specifically, we are looking for drops that are so extreme that they fall into the 1% most extreme percentile of a returns sample that we are pulling from the ETFs' price history. Regarding the list of signals and differences between the various in & out algos, the best approach to get our head around these differences probably is to check out the Initialize section and what ETFs are loaded there. And true: in the In & Out, we could be staying out for more than 15 days if there are negative return flips in certain asset pairs (i.e. Gold vs Silver, Utilities vs Industrials, and Safe haven currency vs Risky currency). These return flips temporarily increase the number of days that we would be staying out for. See the post here.
Peter Guenther
Thanks for sharing, Guy Fleury.
Harsh Patelan explanation for similar returns can be that the Intersection of ROC Comparison Using OUT_DAY was mainly mixed with tech stock selections (summary above). We did a similar thing in Amazing returns = superior stock selection strategy + superior in & out strategy. However, we also mixed in a value/quality stock selection (see page 2, QualUp). So, this might be of interest to get additional diversity regarding the stock selection component.
Jack Whisky
Hi Peter Guenther and everyone, as I wrote in my last comment, I'm still a nabbo in coding here on quantconnect. I had the idea of implementing a momentum filter on these stocks and using the same logic as the ROC, select in this container of shares, only the one with the most momentum of the last six months, hold it for a month and then run the process again. I tried to change the code but I can't understand what mistakes I make. Can someone help me?
''' Intersection of ROC comparison using OUT_DAY approach by Vladimir v1.1 (diversified static lists) inspired by Peter Guenther, Tentor Testivis, Dan Whitnable, Thomas Chang. ''' import numpy as np # ------------------------------------------------------------------------------------------- STOCKS = ['QQQ','MSFT','NFLX','AMZN','TSLA']; BONDS = ['TLT','TLH']; VOLA = 126; BASE_RET = 85; LEV = 0.99; # ------------------------------------------------------------------------------------------- class ROC_Comparison_IN_OUT(QCAlgorithm): def Initialize(self): self.SetStartDate(2008, 1, 1) # self.SetEndDate(2021, 1, 1) self.cap = 100000 self.STOCKS = [self.AddEquity('QQQ', Resolution.Minute).Symbol, self.AddEquity('MSFT', Resolution.Minute).Symbol, self.AddEquity('NFLX', Resolution.Minute).Symbol, self.AddEquity('AMZN', Resolution.Minute).Symbol, self.AddEquity('TSLA', Resolution.Minute).Symbol] self.BONDS = [self.AddEquity(ticker, Resolution.Minute).Symbol for ticker in BONDS] self.ASSETS = [self.STOCKS, self.BONDS] self.SLV = self.AddEquity('SLV', Resolution.Daily).Symbol self.GLD = self.AddEquity('GLD', Resolution.Daily).Symbol self.XLI = self.AddEquity('XLI', Resolution.Daily).Symbol self.XLU = self.AddEquity('XLU', Resolution.Daily).Symbol self.DBB = self.AddEquity('DBB', Resolution.Daily).Symbol self.UUP = self.AddEquity('UUP', Resolution.Daily).Symbol self.MKT = self.AddEquity('SPY', Resolution.Daily).Symbol self.pairs = [self.SLV, self.GLD, self.XLI, self.XLU, self.DBB, self.UUP] self.bull = 1 self.count = 0 self.outday = 0 self.wt = {} self.real_wt = {} self.mkt = [] self.SetWarmUp(timedelta(350)) 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] 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: self.bull = 1 self.count += 1 def trade(self): #MOMENTUM SELECTION TOP 1 self.SetWarmUp(6*21) for STOCKS in self.STOCKS: self.AddEquity(STOCKS, Resolution.Daily) self.data[STOCKS] = self.MOM(symbol, 6*21, Resolution.Minute) # shcedule the function to fire at the month start self.Schedule.On(self.DateRules.MonthStart(), self.TimeRules.AfterMarketOpen(), self.Rebalance) def OnData(self, data): pass def Rebalance(self): if self.IsWarmingUp: return top = pd.Series(self.data).sort_values(ascending = False)[:1] for kvp in self.Portfolio: security_hold = kvp.Value # liquidate the security which is no longer in the top momentum list if security_hold.Invested and (security_hold.Symbol.Value not in top.index): self.Liquidate(security_hold.Symbols) added_symbols = [] for symbols in top.index: if not self.Portfolio[symbol].Invested: added_symbols.append(symbol) for added in added_symbols: self.SetHoldings(added, 1/len(added_symbols)) 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: 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) account_leverage = self.Portfolio.TotalHoldingsValue / self.Portfolio.TotalPortfolioValue self.Plot('Holdings', 'leverage', round(account_leverage, 1)) 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))
Peter Guenther
Please find an implementation attached, Jack Whisky. I reckon, key lines are the following:
- Lines 26-28: variables for your return momentum lookback period (self.mom_lookback), for the selected stock(s) (to save the selection between rebalancing dates; self.stock_selection), and for saving the month (to see when it changes; self.ret_reb_month).
- Line 57: since they are fixed (i.e., not dynamically selected), add the stocks to the consolidator to continuously update their stock price data
- Lines 92-94: on month change, calculate the stocks' momentum and select the best stock (note: one can increase the 1 in line 93 to select more stocks)
- Lines 111-123: the function that performs the returns ranking and selects the best stock(s)
Clearly, the total return is incredible: +37,532%. Annual growth 56.59%. Of course, part of this is because the algo was riding on the substantial Tesla stock price increase.
Additional examples that might be of interest: For dynamic (instead of fixed) stock selections, see Amazing returns = superior stock selection strategy + superior in & out strategy.
Guy Fleury
The In & Out Trading Strategy - Analysis
This is a follow-up to my last article Basic Stock Portfolio Math, trying to provide a different look at the inner workings of the In & Out stock trading strategy which is freely available on QuantConnect where you can modify it at will. The intent is to show how this strategy is making its money. It should prove interesting. The strategy is composed of only a few parts: a stock selection process, a trend definition section, and a trade execution method. Nothing very complicated.
I examined and tested the 5 published versions of In & Out and some of their variations. Basically viewing them as templates where the math of the game could provide ways to raise performance almost at will. The strategies still have some weaknesses but they can be corrected or at least alleviated.
For the rest of the article, follow the link below. As just said, it should prove interesting.
https://alphapowertrading.com/index.php/2-uncategorised/406-the-in-out-trading-strategy-analysis
Cc cc
I have a free account and run this algo (cloned it) and want to know if you similar experiences - it is extremly slow (I am maybe spoiled for the quantopian times..).
I had to change the start time to 2020-1-1 and even then it run over 7 minutes (I think running it from 2008 it will run over an hour).
Do you have the same durations?
.ekz.
Sharing this research on Bond-Stock correlation, as it seems relevant to this topic.
Article Excerpt:
The correlation between stock and bond returns is an integral component of hedging strategies, risk assessment, and minimization of risk in allocation decisions. In the context of those strategies, the stock-bond correlation is typically estimated using monthly return data over a recent previous period. This is a reasonable approach but has turned out to be an unreliable indicator for forecasting purposes. .... In this paper, the authors conduct an incremental analysis of three innovations for reliably forecasting the long-term correlation of stocks and bonds.
Article Link:
https://alphaarchitect.com/2021/04/05/estimating-the-stock-bond-correlation/.ekz.
Guy Fleury , I finally got around to reading the article you shared above, and it was a great read! Thanks for taking the time to do, and document, the analysis.
This is timely for me because I am exploring OLPS (online portfolio selection), and these dynamics (rebalancing, ranking, etc) are critical to understand.
Hope to see more similar writing from you. Subscribed.
Tentor Testivis
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!