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
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.
Guy Fleury
@.ekz., thanks for the kind words.
Another look at the In & Out strategy. This time, trying to show rebalancing might be the most important line of code in this strategy. Also, that the trade mechanics has a major impact on final results.
Hope it will help some.
Link: Making Money with no Fault of Your Own
Jack Whisky
Thank you very much Peter Guenthern the last few weeks I've been practicing a lot on quantconnect and I'm slowly understanding how it works. I'm here one more time to ask for information.. Reading the initial description of the algos where there are written the reasons for the choice of some assets as a warning of a stock collapse. However, I cannot understand after the subsequent changes what is the underlying reason why if the performance of GLd> SLV should be a bearish market signal? in the same way also that of the pairs XLU> XLI and UUP> DBB.
Peter Guenther
That's great to hear, Jack Whisky. Keep on hacking!
I might not be the perfect person to speak to the economic interpretation of the pairs, since these logics are based on Vladimir's algo (Gold return > Silver return and Utilities returns > Industrials returns) and Dan Whitnable's algo version (adding Metals return < Dollar return) on Quantopian (for the discussion, see this link here).
Anyway, here is my take:
Investments in Gold and investments in Utilities can be safe-haven type of moves (= indications that something might be going pear-shaped in the equity market). However, these investments might also just indicate ordinary investments in a growing economy/market (i.e., more available capital then is invested into all kinds of assets that therefore increase in value). To tease out the safe-haven move, we compare with an alternative asset of the same type. For example, silver is similar to gold and industrials are similar to utilities. The logic is then: Usually the pair components should have similar returns. However, when a safe-haven move occurs, the safe-haven asset (i.e., gold or utilities) should generate a comparably higher return.
The third pair may be a bit more difficult to interpret. It combines falling metals prices (i.e., decreases in resource demand that may be early signs of drops in economic activity) and an increasing dollar (i.e., safe-haven move). Either move would create a signal in the third pair.
Let's see there might be different takes on the pairs and others may chip in with their own interpretations.
Jack Whisky
Hi Peter Guenther, Thank you for your reply. First of all I am interested in understanding the basic theoretical model, so as to be able to formalize it mathematically. For example, the one described by you explains in detail the reasons why you go into bond. Now I will try to write to Vlad another time.
Strongs
Hi Peter Guenther and everyone, after a few months I have been working on the various systems exposed here. I have some observations to make on the whole. I remain of the opinion that they are robust systems that can work. I have tried all the versions and I have formed an opinion. In & out classic or rather the version: Flex_5, it seems to have a really good timing for entries and exits, if we take into account the collapse of March 2020, the algorithm managed to exit correctly but what I think is even more incredible is the entry timing for the equity component that arrives in correspondence with the rises, while both the distilled bear version and the ROC remain in bonds until July, losing most of the rises. On balance the version I mentioned of in & out is also the one with the lowest drawdown and standard deviation, if we go and see, it would have been released on February 26, 2021 so lacking the timing of the decline on qqq and spy and still holds bonds, in reality it could be wrong to think that this entry is wrong, we should wait for the next few months which in my opinion may not be so bright. So we still don't know if this exit was completely wrong, what instead was objectively a problem is the bond counterpart that collapsed due to the inflation nightmare and therefore leading the system to a loss. On the other hand, the distilled bear and the ROC remained in equities, but suffered all the volatility of the last few months. So I have a few points I want to bring to attention. It is necessary to understand which is the best system among those exposed in terms of risk return, in terms of the number of correct exits. Another important thing is the macro situation of the last few months, namely inflation. A condition that Algos has never experienced so strongly during the backtest. I think it is interesting to consider a possible solution that is to impose an additional filter on the system: once the exit from the equity component has been validated, it might be a good idea to evaluate the situations of the interest rate, so as to have a third option of exit, not in bonds but in an alternative asset against inflation. Instead, returning to the discussion to find a selection of shares or ETFS, I believe that it is necessary to explore other possibilities, especially for ETFs, further brainstorming is needed. We could also create a telegram group for those who are interested in improving this system more actively which, as I repeat, I consider valid.
.ekz.
Thanks for the thoughtful comment, Strongs. I agree that an inflation-proof alternative is with pursuing. Interested to hear more from people on this.
My only suggestion: rather than telegram, we use the QuantConnect Slack group. Many of us are already there, and many of us, I'm sure, don't use Telegram (myself included). Ideally we don't want to splinter / fragment the community over this discussion.
Kamal G
Kamal G
Hi everyone,
I've learnt a lot from this thread and I've been trying to add a SPY Put Option, but I've getting an error on most backtests.
Specifically I think it's on this line.
Self.history.pct_change(period).iloc[-1]
And the error is,
ValueError : cannot convert float NaN to integer
Has anyone else been getting this error when backtesting the algorithms above?
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!