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
Vovik
Peter Guenther,
Can you share a secret how you pick up symbols for signals in your In_out_flex_v5_disambiguate_v2?
The last one was "RINF".
Have you ever looked at it's intraday chart?
For the last week there were only 3 trades 500, 200, 200 shares.
Last year average daily volume of 100 shares per day.
Here is its monthly chart.
Do you think this is a reliable source for TQQQ trading?
Peter Guenther
Stream: Rich data modelists
Thanks for your question, Vovik!
I had hoped that it’s no “secret” why indicators are in the algo since these are individually introduced (use Ctrl + F “RINF”) plus there is an explanation in the code. What sometimes happens is that people cut away the in-code explanations to “save words” in the code; not a good idea, look at a code in a few months’ or years’ time from now and you know what I mean.
The RINF is there to gauge inflation expectations.
Now, how is the indicator specifically used in the code? Check out self.INFL in the code. We want to know, on a particular day, whether inflation expectations are above or below their median. Below is the ~three-month chart for RINF. Does this chart help me to know, on a particular day, whether expectations are above or below their median? I would say yes. I do not need a minute-sharp high-frequency trade price to make that call. Note that close to the median you will always have noise, frequently updated prices won’t make that go away and could even aggravate the issue. It’s also important to note: we do not trade RINF itself (in that case, we would be more highly concerned about liquidity/volume). What we are after via RINF is a coarse indication: above or below median inflation expectations.
However, I think a real downside of RINF is that it only started in Jan 2012 (see my original post). Therefore, the post notes that: “However, RINF could be manually constructed by comparing treasuries with inflation-protected treasuries. I have to explore that option a bit more and may give it a go later unless somebody else might be faster (?) ...”
Vovik, are you maybe that somebody who might be faster and thereby contribute to our Rich data modelists stream?
Vovik
Peter Guenther,,
self.INFL is a member of self.SIGNALS
self.SIGNALS = [self.PRDC, self.METL, self.NRES, self.DEBT, self.USDX, self.INFL]
symbols = self.SIGNALS + [self.MRKT] + self.FORPAIRS
returns_sample = (self.history / self.history_shift - 1)
extreme_b = returns_sample.iloc[-1] < pctl_b
if (extreme_b[self.SIGNALS + self.pairlist]).any(): self.be_in = False
As I understand it, if any of [self.SIGNALS self.pairlist] falls below pctl_b,
self.HLD_OUT will be generated.
Do you think "RINF" hitting lowest low after lowest low is a reliable source for TQQQ trading?
Frank DiGiacomo
I've been readng through this entire thread and I find it very interesting! I'm a little confused on one part though.
mom = (self.history / self.history_shift_mean - 1)
What is this exactly caclulating? I don't understand the -1 in there. Is it some rule in matrices?
Peter Guenther
Vovik you have a very good eye there! In the Initialize part, self.INFL should be in the self.FORPAIRS (line 51) bracket instead of the self.SIGNALS bracket (line 52). The intention actually is not to use self.INFL as a signal by itself but to disambiguate the GOLD pair according to the table in an earlier post (Additional musing about signal ambiguity). Updated version attached.
Regarding TQQQ, I reckon trading 3 x leveraged ETFs is anyway a hell of a wild rodeo ride. I would take that risk only with a very measured investment (maybe <10-15% wealth or less). In my view, no matter which in & out strategy you use, or indicators you have included or have not included, drawdowns can be brutal in 3 x leverage ETFs and the in & out precision won't be sufficiently accurate to completely prevent these. One then hopes for a stellar increase in the TQQQ to more than make up for the drawdown.
Updated version:
Total returns: 2092% (worse)
Sharpe ratio: 1.87 (worse); Max drawdown: 13.6% (better); Compounding annual return: 26.8% (worse)
Peter Guenther
Frank DiGiacomo thanks for joining in and no worries. The code snippet calculates a return. self.history contains day-specific prices. self.history_shift_mean contains past averaged prices (i.e. for a particular stock and day, it would be the stock's average price in the range 55 days ago to 65 days ago). For an example stock, say the day-specific price is 110, the shifted_mean price is 100, then we calculate 110/100 - 1 = 10% return (the -1 is required to calculate the return).
Vovik
Peter Guenther,
Do you think "RINF" hitting lowest low after lowest low is a reliable source for QQQ trading?
Peter Guenther
Vovik , thanks for your follow up. My personal view: Yes, I think RINF is reliable. It measures what I conceptually want it to measure, that is inflation expectations. It does so with sufficient accuracy, giving me a daily gauge on where we are at in terms of (changes in) the market’s inflation expectations. Would I use it in the In & Out to trade equity (QQQ, SPY etc.)? Yes, I reckon this is the whole point regarding further developing the In & Out, isn’t it?
Note that RINF does not continuously create new lows, at least not in terms of how the algo uses it. Below is a picture of RINF’s values in the return sample. Currently, it is actually relatively high which I reckon reflects the market’s expectation of an economic rebound.
Peter Guenther
A call for additional in & out-type algos
We have several in & out algos in this thread and beyond, in multiple variants. I am sure that there will be additional exciting stuff coming, including the use of futures and sensitivity testing via QC’s Cloud Parameter Optimization / Sensitivity Testing.
However, reading the rationales, discussions, etc. you might say “nah, I would have done this completely differently, like so ...” I recall people mentioning chart-analytical techniques like breakouts in certain markets and other interesting ideas. If you have something that you are happy to share with the community, probably the best way is via a new QC thread, but do leave a post in this thread as well, just so that everybody who is interested in these kinds of tactics (including myself) is aware. It would be really cool to see some (fundamentally) different approaches to tackle the in & out problem. Keep on coding!
Peter Guenther
New Chart: The Annual Saw Tooth Returns - In & Out versus Always In

This time, no innovation on the algo itself but instead a new chart that might be useful. We have noted here and elsewhere that the In & Out is not directly meant to replace a stock selection strategy but can be an optional complement. In other words, the choice would be to run your stock selection strategy with an In & Out logic instead of running your stock selection strategy without this logic (= being always in). You would hope that the In & Out component acts like an insurance package against steep drawdowns in your strategy. However, we were wondering what the implicit costs of this insurance package are.
The chart takes an annual view on this question by comparing the returns of the In & Out execution of the stock selection strategy (light blue line) versus the always-in execution (black line) of the stock selection strategy (= QQQ in the attached algo). The returns are reset at the beginning of each year, hence the saw tooth pattern. The chart may be useful to understand the implicit costs of the In & Out in terms of underperformance during certain times. For instance, the below chart shows that the relative performance in 2013 is not great and there is a stretched out soft patch from 2017 to 2019.
PS: If someone knows an easier implementation to create the chart, I would be very interested. However, note that I have implemented the calculations for the chart so that the stock selection dictionary could consist of more than one stock (e.g. holding QQQ and SPY together) and then the chart should still be created correctly.
The attached algo also creates a chart to compare the three-month (60 days) returns of the In & Out versus always in.
The code for the calculations and charts are in lines 220-237. I initialize a few variables for the charts in lines 72-78.
Chak
Hey Peter,
You can consolidate the huge chunks of code if you incorporated a few if and elif statements. For example, X-month and Annual can fall under the same plot, if you're comfortable with clicking to highlight and unhighlight the additional labels. Alot of the code looks like copy paste. Try looking on stackoverflow for some hints.
Peter Guenther
Thanks, Chak, for your insights/guidance, much appreciated!

Saw Tooth Chart comparison: The Distilled Bear (top) and The In & Out (bottom)
Now that we have the Saw Tooth Chart, what we can do is to compare different in & outs regarding the distribution of their returns. Below is a pasted together view of the returns based on The Distilled Bear (top) and The In & Out (bottom) algos. The comparatively strong sections of The Distilled Bear seem to be in 2008, 2013 (substantial), 2016, and 2017. The In & Out performs comparatively better in 2011, 2012, and 2020 (substantial).
Implications
- can one in & out ‘learn’ from the other, resulting in a ‘super in & out’?
- should one mix the logics and simultaneously bet on both horses, e.g. 50-50?
- ...
Jack Pizza
Hey Peter Guenther how strong is the hypothesis with comparing industrials, gold, cash and their correlations? I'm sure a quick study can be done going back to almost 50-100 years on these broad markets?
My concern is if these correlations hold up or if they possibly break meaning the algo's in / out goes totally out the windows.
Also your thoughts on self stock selection vs ETF (stock selection chosen for you)
Peter Guenther
Elsid Aliaj, great questions! Regarding your first point, Menno did a test a while back, looking at a momentum strategy switching between equity, gold and bonds. The test was from 1875 to 2020 and the strategy seemed to generate systematic returns over this substantial time period. The strategy builds on a certain correlation between the involved assets. So we could argue that if the (negative) correlation was there the past 150 years, it is likely to be there in the future, although there might be phases when it's more muted versus stronger. For these situations where the correlation is muted, I definitely see the benefit of your earlier point that sometimes we might want to go into all cash.
Regarding the selection of individual stocks versus ETFs, ETFs can be efficient in terms of saving on commissions. So that is generally attractive. However, I reckon in the backtests we tend to select ETFs that we know had a good run in the past, creating a lookback bias. When we have a logic that systematically selects individual stocks from the universe of all stocks, e.g. based on fundamentals, the backtest results could be more valid. What are your thoughts?
Peter Guenther
Stream: Rich data modelists
Bringing it all together
As you know (see my earlier post), I was musing for some time now about whether it could be beneficial to combine the two in & out algos to create a ‘super’ in & out. The attached algo is a first attempt into this direction. There might be different ways how this could be done, e.g. see Derek Melchin’s suggestion to use correlations in the Amazing returns = superior stock selection strategy + superior in & out strategy thread.
The general idea of the attached implementation is that we track both algos and see how they are going return-wise and then increasingly switch to the overperforming one. A key variable is self.weight_inout_vs_dbear which is a number between 1 (fully on In & Out) and 0 (fully on Distilled Bear). It is determined (lines 263-272) via comparing the in & outs’ returns over a tight timeframe (10 days; see self.io_mom_lookback in line 112) so that we quickly capture comparative overperformance and switch.
The algo creates a plot for the weight towards the In & Out vs Distilled Bear (line 342) in the “In Out” chart.
The performances / total returns:
In & Out alone: 2,090%
Distilled Bear alone: 1,890%
New combo: 2,800%
The algo has a few features which are included because of other issues I want to explore but which have no relevance in the current setting, so feel free to ignore. For example, adding equities and alternative assets via ticker string (lines 45-59), including OnSecutiriesChanged (lines 149-164), and determining equities/alternative assets with the best momentum (lines 286-300). You also have a chart that tracks the cash level (line 331) and the Annual Saw Tooth chart (lines 334-337).
Jon Quant
I noticed that the recent algos now use "TIP" instead of "RINF" as a disambiguation signal. Is that better?
Liam Op
Hi Peter
Good job!
I just run a back-test for the same period
of v1.0 "Intersection of ROC comparison using the OUT_DAY approach" by Vladimir
It only has two variables, and more economical code (three times fewer lines), and it seem to get better results than yours.
What do you think?
Peter Guenther
Thanks for your post and positive note, Liam, appreciated!
Also thanks for sharing these results, I think that is fantastic.
With my ‘rich data modelist’ hat on (see the different camps/streams mentioned earlier), I would tend to think, let’s combine the three in & outs into one. It’s a fundamentally different mindset to the features you highlighted in your post (few parameters / lines of code) and that is OK. We can think about it this way: there is a continuum and you can allocate the following people on it in the following way:
[parameter minimalists] Menno <---- Vladimir ----> Peter [rich data modelists]
Now, just to clarify: the idea of combining the in & outs is not necessarily to boost performance, although this might happen in the process. It is rather about combining different logics, so that we do not have to pick one logic (‘a winner’) beforehand. Instead, we let the relative weights of the logics being determined empirically by the (future) data and how events unfold--this is the gist of what the post above tries to bring to the table. The weights are determined by the logics’ relative short-term performance. So, with this in mind, when we look at total return and number of parameters/lines of code of individual in & outs, I am not sure that we correctly capture what we might be able to develop here.
Vovik
Liam, Peter
I also see that v2.5 “Dual Momentum with Out Days” by Vladimir looks much better than InOut_DBear_v1.
Peter Guenther
Jon Quant, sorry almost missed your question there. Well spotted! It could be a bit better but I am not even sure. The rationale for replacing RINF is just that the ETF is only available from Jan 2012 onwards and I wanted to have something that I could build on from Jan 2008. So I am using the bonds - tip difference.
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!