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
Guy Fleury
Guy Fleury
My latest article: The Makings of a Stock Trading Strategy – PART I deals with the mathematical structure of stock trading strategies. It is related to the IN & OUT strategy, especially the upcoming PART II.
The article is looking at stock portfolios from an equation point of view. From the expected endpoints, it tries to re-fabricate what could have been required to get there to then include these findings in the trade triggering mechanics to ensure a profitable outcome.
The objective is to gain control over the evolution of a trading strategy as well as increase performance.
The article is also in HTML format.
Hope some will find it interesting.
Guy Fleury
Here is PART II of The Makings of a Stock Trading Strategy
This HTML file can be very helpful to anyone designing automated short-term stock trading strategies. It has deep implications. It deals with correcting for long-term portfolio return degradation, on how to fix it, and even how to reverse it.
It builds on mathematical equations used in describing the outcome of our trading portfolios and shows how easy it is to improve on these designs with simple trade triggering techniques. As if saying that your trading procedures can be greatly improved just by requesting more and letting long-term compounding do its job.
The main idea is to stop long-term return degradation which tends to make trading strategies break down or fail going forward. Not only can we stop return degradation, but we can also easily reverse it to then make our strategies prosper and thrive instead of their overall return degrade and die. As always, it is only if you want to do it. But regardless, you should be aware of how to compensate for return decay, overcome it, and reverse its detrimental effects.
Take the time to play with the equations provided, adapt them to your own trading strategies. Study the limits of what you could do or can do. It will open portfolio building and portfolio reengineering to anyone wanting to make it big for whatever purpose. Evidently, there will be work to do, but it will be based on a solid foundation from which one can soar. And yet, you should find it so simple. Make the following trade imbalance hold: (1 + fW) ∙ (1 – fL) > 0.
Click tor the HTML version.
The article is a little long, I agree. It is there to protect your portfolio, not mine. So, you be the judge.
Related Articles:
The Makings of a Stock Trading Strategy – PART I
Your Automated Stock Trading Portfolio
On The Use of a Rebalancer, a Flipper, and a Flusher
.ekz.
Guy Fleury : thank you for sharing this. Seems like it will make for a great read.
Given the content / relevant of the article, I suggest sharing this in its own forum post. It will surely not get the best exposure, buried in this In&Out thread.
This will likely reach, and benefit, more people in its own post. Further, we can have focused discussions around it on a dedicated thread.
Guy Fleury
My reflections on the IN & OUT strategy.
I consider the IN & OUT strategy as operating on market noise, especially when rebalancing on a daily basis. We all know we cannot win a heads or tails process except, of course, by luck. So, why put so much emphasis on this strategy which under other trading procedures might be classified as rather ordinary.
It operates as if a positive outlier or an unexpected sore thumb depending on the point of view. Who would risk so much on market noise in the first place?
The answer is: it is not totally market noise. As in any stochastic process equation, there is a drift part and a random-like component, which by itself does, in fact, have a zero expectancy. So, it is not from the stochastic part of the equation that will come the expected benefits, but from the drift component which is easy to get. The drift component of the equation is often defined as the underlying trend.
Any trend-following strategy should capture this underlying trend. Not because it is prescient, but just because it participated in the game. Not surprisingly, the average long-term portfolio return tends to the market's long-term expected average: E[r_m].
That seems like taking the merit of designing such a trading system out of the equation. As if saying, you won, but it was not entirely your fault, you were buying in an upward market and were simply in it for the ride. Nothing more. You would still get the merits of participating and would collect the benefits of such a ride. That is perfectly OK. It was what you wanted in the first place: the profits, pretty please, the same here. I do not mind so much how or why I profit as long as (over the long term) my strategy can profit substantially from the game. And I suspect you would conclude to the same.
It remains that trading over market noise appears as an opportunity bet in the sense that you know why you got in a trade based on some reason, but that your gain is still connected to a partially random-like exit strategy. This is where you should take advantage of price variance.
You know prices will swing all over the place, that you like it or not. It is what it is. So, your advantage would be to exit a trade while variance is also to your advantage (∆σdW > 0) even if this variance is the result of market noise. That is what is exploited in the IN & OUT strategy (all its versions, btw).
Is this wrong? I do not think so. Why should it be? You entered the game to make some money. You determined that this type of daily rebalancing could provide an advantage, an edge, even if small since mostly dealing with the delta of the drift (∆rdt).
The variation of the underlying trend from day to day is in the order of 0.04% over the long term. It is not much. It is 4 cents on a $100 dollar stock or 1 cent on a $25 dollar stock. But it is still there, it adds up day by day, and is given free of charge. All you have to do is participate while you identify the trend as going up. This trend has been mostly up since March 2009, or, with a longer perspective, for over 240 years.
Almost any trend-following strategy following this underlying trend could have profited during those past 12 years (in fact, all the IN & OUT variants did).
In a recent simulation of mine, I had the average win rate at 0.06% thereby catching part of the long-term drift as well as a little bit extra, the result of the trade mechanics associated with the rebalancing process itself. Note that the average trade duration was more than one day. It is like taking advantage of this stochastically moving statistical blob of fluctuating variance penny by penny.
Yet, I consider the trading strategy's trend declaration to be wrong most of the time (~85%). I also noted that the strategy by its structure had an overall safeguard measure, thereby becoming a desirable protective “feature”.
This all-in at all times strategy was winning due to its repeated flushing which served as its universal stop-loss. The strategy would not accumulate falling knives even though it was intrinsically designed to do so (buying the dips and the dips and the dips). The flip-flopping would force the execution of the stop-losses before they could be considered troublesome or too detrimental to one's portfolio. And since the strategy dealt with 100 stocks, any one stock's downfall would be considered to have only a small impact on the overall portfolio.
You want to win? Understandably. Then know why you win. Know what your trading strategy is really doing. A simulation is just there to confirm your trade mechanics. If it didn't work on past data, you have your answer for going forward even if you do not know what the future might bring. However, if your trade mechanics give you an edge, exploit it.
Should the IN & OUT strategy warrant more investigation as to why it can be made even more profitable? I think so. How about you?
Jake Rocket
Thank you to all of the contributors who have made this forum post (and a couple of other related ones) an extremely educational read. I have played with a few variations of the In & Out algo, and it seems that there have been no trades since about April-May timeframe. Is that correct that the algo has been “out of the market” for the last 3-4 months?
Is there another thread that focuses on live trading that I may have overlooked? It would be great to hear about some live trading experience and results given that we've had about 10 months of real-world trading since the algo was first proposed by Mr. Guenther in October 2020.
Again, thank you all for the great work and especially the knowledge sharing in this forum.
Narendra Kulkarni
Guys I read the whole discussion. Super interesting. My concern is that while the back tests look incredible, I suspect they are incredible because its overfit. 2008 to 2021 has been an incredible bull run. So if we just dodge couple of drawdowns, we can “beat” the market quite handily. However i suspect this might just be overfit rather than a signal. My main criticism is that as name suggests its an “in or out” strategy. So are inferring a lot just based on the signal. Its not a situation where we are probabilistically changing our weights given expectations of next day returns, but instead we completly rotate out. So this makes me think thats its very easy to overfit because its a very binary signal. If you get some of the big moves wrong you could lose a lot of money. So its not a question of slightly underperforming the markets, because the system is slightly overfit. Anyways I was curious to hear your reasons for why you believe that the system is not overfit.
Guy Fleury
Is “IN & OUT” overfitted or not?
We all have “opinions” on that since we cannot “prove” if it is or not. We have no tools to effectively demonstrate either yes or no.
The first question that should come to mind is: overfitted, compared to what?
Maybe, here is a better question: is “IN & OUT” outperforming long-term market averages? Because, if it is not, who would want to play with it? It would have lost all its potential value since it would then be underperforming market averages.
I am of the “opinion” that “IN & OUT” is operating, for the most part, on the fringe, the fumes, of market noise. And that cannot be overfitted due to its random-like nature. You cannot consistently overfit randomness. Every day, the price variations are, structurally, surprises that are highly unpredictable. You could, nonetheless, take a bet on the long side based on the underlying long-term market uptrend.
Any trading strategy should be biased toward its goal. Meaning that in an upward market, most trades should be biased to the long side. Should the upside last for 10 years or more, you still should favor, for the duration, the long side for your “average” trade. Moreover, that average trade should generate a profit, not because you anticipated a market rise (even if you should have) but just because you participated in some market upswing while it was going up.
The only prediction we can make going forward is that market noise will be there, that we like it or not. And it stops there. As for predicting tomorrow's prices, your guess is as good as mine, although I will continue to favor the upside. This is what the “IN & OUT” was designed to do over the last 12 years.
Regardless, the strategy still needs some added protection and will need to change its upward stance for when the market really turns down again. However, it is already quite sensitive to market fluctuations, to the point that its average universal “stop-loss” is relatively low (my average loss rate is below -0.1%). The trading signal goes down a fraction of a percent and its flusher kicks in which could explain why it is wrong so often. At least, it will catch all downtrends of significance. It is a rare strategy where being wrong often (~85%) becomes a positive protective measure...
“IN & OUT” wins the game because it was wrong most of the time. That, in itself, is interesting. Note that its rebalancing procedures (flipper, switcher, and flusher) had a major structural role in making this strategy win over its simulation period. It won mostly due to its trade mechanics. And that is also quite interesting.
Narendra Kulkarni
one way to check if this startegy is overfit would be run the backtest from 2000-2008 time period. We could simplify the code so that we can run the code from 2000-2008 and see how the backtest performs. What do you guys think?
Peter Guenther
Thanks for sharing your observation, Jake Rocket. I agree that that is quite a long time for being out of the market, although it's also indeed true that the in & outs are sometimes out of the market for several months. Anyway, I have looked into an In & Out version that exists the market less frequently. My approach was to try and find a better balance between the size of the returns sample and the critical cut-off percentile and at the same time, ideally, reduce the number of required signals.
So, attached is the latest version of these efforts (v7) – work in progress. Some additional notes regarding this algo:
(1) It saves important state information (dcount and outday) to the ObjectStore which ensures recovery of its state in live trading when the algo is interrupted.
(2) It creates a chart called ‘Out return’ that helps to track the return delta generated by the decision to exit the market instead of stay in the market. This chart can be quite instructive in terms of whether the out decision indeed was a good one … or a lousy one.
As always: Looking forward to any comments and improved versions.
Peter Guenther
As a follow up to my prior post;
Since it's difficult to see in the backtest charts above, below is a picture of the Out return chart which some may find useful. The chart value is zero when we are in. As soon as we are out, the chart value starts to fluctuate according to the following difference: out portfolio return minus return if we had stayed in. In science they would call the latter component the counterfactual, which here is the return in the parallel-world scenario where we would have remained invested in the market instead of going into bonds. When we go back in, the Out return chart value jumps back to zero, giving us a clear cliff's edge (sometimes we may have to zoom in a little) indicating the eventual return differential (out return - counterfactual in return) of the out decision. If positive, the out decision was a good one. If negative, we would have been better off remaining invested in the market.
Peter Guenther
Thread anniversary
Happy first birthday to this thread, it’s been exactly one year since the post below. Much work has been done on the In & Out strategy and related in & out-type strategies.
Stock take (continued)
In terms of a stock take of the work, I had posted an overview and summaries earlier, so just a list of the previously discussed threads here:
Listed earlier
1. This thread
2. Amazing returns = superior stock selection strategy + superior in & out strategy (link).
3. Dual Momentum with Out Days (link).
4. Intersection of ROC comparison using OUT_DAY approach (link)
5. A very profitable version of IN and OUT, and why it is likely to fail in real life trading like its siblings (link).
6. ROC comparison Utilities and Industrials (link).
Additional new threads related to the discussion of timing market entries/exits
7. KEI based Strategy (link). Focuses on monthly and quarterly leading economic indicators for OECD and non-OECD countries (e.g., including consumer sentiment, prices, national accounts, etc.) to select/rotate into ETFs and asset classes as well as possibly timing market entry/exit. Fine-grained regimes are discussed that inform the investment allocation.
8. Simple stock bond strategy hedged using VIX options (link). Focuses on developing a strategy that buys VIX options according to the current VIX level, meaning that the latter provides an in/out signal for the investment into the former. Discusses the difficulty of predicting optimal entries/exits accurately.
In & Out: An evolution
1. Re-entry: The good old outday and waitdays parameters
Starting with the very first version I had posted, the In & Out (and the other in & out-type strategies for that matter) use the original concept of an exit day (outday) and a waiting period before re-entry (waitdays). Just eight days after posting the original In & Out, the above post was musing about alternative ways of implementing the re-entry process. However, the fundamental outday-waitdays mechanics have never been altered since.
2. Signal clutter
In addition, it has always been interesting to see how the signals cluster together when things really begin to go pear shaped. For instance, see the 2008/2009 downturn, or 2015/2016, or March 2020. So, there might be some informational value in the signal build-up. However, what never worked well for the In & Out is to ignore the first signal and wait for two or more signals (confirmation) before going out. One explanation is that the signals themselves are rare extreme signals (see the percentile approach) and therefore, in themselves, provide sufficient evidence for that something negative is going on in the market. See similar comments here and here. Therefore, the signal build-up has to be used differently, that is, not according to the concept of confirmation.
In & Out v8
The attached, new version of the In & Out tries to integrate the above ideas and observations. What the algo does can be illustrated using the above picture: The blue line tracks the in (1) and out (0) decisions, both of which are based on the black line. The black line is a smoothened average (using the 50 exponential moving average) of the eight signals in the previous picture. We go out when the black line is starting to increase, that is, when a new out signal comes in, thereby staying true to the original operationalization. The new part is that we go back in when the black line (consistently) declines, that is, approximately although not exactly at the inflection point. Due to this logic, the new algo version does not require the outday and waitdays parameters, which is a first for any of the in & out algos.
Peter Guenther
Benchmarking the In & Out v8
At least for trading the QQQ ETF from Jan 1 2008 to now, it seems like the v8 is able to realize incremental returns compared to alternative algos.
A. Results using the v8
B. Results using the v7
C. Results using the flex_v5_disambiguate_v2
D. Results using the DistilledBear
GEightyFour
Peter Guenther Can you explain what are we trying to achieve here? Looks like some odd delayed simple moving average calculation to me.
Peter Guenther
Thanks for joining the discussion, GEightyFour. The discussion history of the In & Out might be useful to understand the this component and its evolution more fully; it has been a key component of the very first algo version, although coded slightly differently at the beginning.
Let me try and summarize the gist of the discussion and hopefully this will be useful: By the end of the day, the algo wants to identify extreme returns and use these as out signals. Using daily returns for this check is likely to be highly fickle since daily returns are fickle. So, we need a somewhat longer period for the returns calculation. Here I had to make a judgement call and I said: about three months back (20 trading days/month x 3 months = 60 trading days). However, using the price exactly 60 trading days back means that the denominator for the return calculation rests on a single price point. This could again introduce a certain fickleness and can be addressed by using an average price for the denominator i.e. using the price information surrounding the 60-days back price. I used +/- 5 trading days, i.e. 11 days including the 60-days back price. For these reasons, the formula you cite above uses .rolling(11, center=True).mean() (i.e. the 11 day average price) and .shift(60) (i.e. surrounding and including the 60-days back price) when you fill in the corresponding parameter specified in Initialize.
In the historic discussion, we had the question, why not use an average for the numerator as well. Answer: Because the most recent price contains the newest information and we want to react to new instead of lagged information.
Of course, when I say that I needed to make a ‘judgement call’, this indicates a parameter whose value has been optimally chosen to a certain degree (e.g., based on observation, trial and error etc.). We had some interesting conversations that you can discover in this thread's history regarding parameter optimization, out-of-sample robustness etc. A great contribution regarding testing the sensitivity of the 60 and 11 parameter settings can be found here. They seem to be close to optimal.
Jack Pizza
Hi Peter, would v8 suffer from the same “fail at live testing” fate as you wrote for the other algorithms?
Jack Pizza
sorry as that other author wrote for one of the versions of in / out. Think the biggest failing point will be the out being bonds. There will be a time when both bonds stocks will be correlated and it will probably totally blow up the strategy.
For better research maybe a long / short one asset class or long / cash.
GEightyFour
Peter Guenther I appreciate your explanation Peter. Having the center=True parameter made that entire line a less intuitive read for me. So I experimented by setting it to false and by adjusting the shift to 55 (I left it as 60-5 in the attached backtest-still the same regardless) and I got the algorithm to match your v8 baseline.
Peter Guenther
Good questions, Elsid Aliaj! Regarding the first one, I reckon you mean to what extent the code can recover from a Live Trading interruption. It is programmed so that it should be able to recover since the key parameter (visualized in the black line in the picture above) is continuously saved and loaded from the ObjectStore when the code is employed to Live Trading, if that makes sense. Specifically, see the handling of the self.signal_dens parameter. Not sure whether this answers your question or whether you were after something else.
Regarding the idea of going into cash instead of bonds (or a different asset combo), what we need then is a logic that helps us decide when to chose bonds vs cash when being out. Did you have something particular in mind here regarding how you would make that decision? For example, looking at the current environment, it might be an idea to base this decision on the inflation expectation level. If too high (e.g. the RINF ETF is above a certain median level for a certain timeframe) then go only partially in bonds (or not at all). Etc. So, to structure this a bit, what is needed in my view is at least two things:
1. an economic intuition. What factor should we be looking at to determine our allocation when being out and why (i.e., what is the underlying economic rationale)?
2. a measure. What measure (e.g. an ETF and how? returns? median? level or change?) should we be using to assess the factor?
To get things started, we could begin with the RINF measure and the median level logic (I have used that before for one of the earlier algo versions) and have a look whether something interesting pops up.
We can absolutely do this, GEightyFour, this should be mathematically equivalent. The only thing I would recommend is to keep in mind where the parameters came from, since we might forget over time that the 55/11 combo actually comes from the logic ‘three-months back (60 days) and a window of +/-5 days surrounding (and including) this shift’.
I do see a certain backdrop for the center=True logic since you often see this in Finance research, especially concerning event studies for which abnormal returns are usually calculated for a window that is centered around an event and expressed like +/-3 days, or +/- 5 days, often written as [-3, +3] or [-5, +5]. On the other hand, I would say: whatever helps us personally to see the code more clearly should be our personal preference. So, I fully agree with you here.
Frank Abraham
Hi Peter,
I've been following the development of this algo with very high interest!
The use of moving average center=True vs False highly impacts the results/performance of the algo. When the parameter center is equal to True, a centered (two-sided) moving average is used. This type of moving average relies on future data (non-causal) and cannot/should not be used for real-time decision making. When center is set to False, a one-sided moving average is calculated. The latter relies on past and current data for the calculation (causal system).
Thank you!
Frank
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!