This is the original name of the algorithm that I created as a result of a successful collaboration
on the Quantopian forum thread "New Strategy - In & Out" in October 2020.
Unfortunately, the collaboration did not continue on the QuantConnect forum.
At least I am very uncomfortable with the strange names used by Peter Gunther in the algorithms,
such as "Distilled Bear", variable names and decision making logic.
This algorithm unlike those from Peter Gunther has three pairs as a source, two parameters and
concensus of all three for exit signal.
I did not optimized parameters, so you can get better results.
I want to thank Jared Broad and his team for giving me the opportunity to recover one of
my favorite algorithms.
Happy New Year to all
Guy Fleury
@Vladimir, yes.
Some might think they can reach the same objective using: BONDS = ['TMF', 'TLT']. TMF would still not trade prior to February 2010. However, after TMF would become available, trades would be split between TMF and TLT. Based on the strategy presented, this resulted in reducing total equity by 180 million. An expensive administrative decision. So, I would agree with your proposition.
I had started making those 2 modifications on version 1.2 when you came out with version 1.3. Since I only had two easy modifications to make, I opted to switch. Your version 1.3 appears as much better than version 1.2 Sorry, but I have not read the code beyond line 30, it is my next step to see where more pressure can be applied. And even game the thing some more.
Nice work. Thanks for sharing your insight.
Making another administrative decision, putting leverage at 1.5x for version 1.3 would result in something like this:
This had a 72% win rate, with an average win of 2.53% while the average loss was -1.20%. Of note, the beta was at -0.221 which could be exploited by combining this strategy with something else with a higher beta.
Vladimir
It is very interesting that both Ernst Chan and Menno Dreischor see rebalancing as process
of selling relative losers and buying relative winners.
William Sharpe consider a contrarian, under assumption that no position in portfolio are negative:
Rebalancing a portfolio to a previously-set asset allocation policy involves
selling relative winners and buying relative losers.
Vladimir
Here is my attempt to realize in this strategy Dr Ernst Chan's recommendation:
"No matter how the optimal leverage is determined, the one central theme is that the leverage should be kept constant".
v1.5
The fee saving part of the code was excluded, and daily rebalancing was
implemented with target leverage = 1.50.
The actual leverage ranges from 1.47 to 1.57.
But I still prefer v1.4 with target leverage = 1.50.
The actual leverage ranges from 1.39 to 1.59.
Guy Fleury
@Vladimir, taking version 1.5 and replacing TLT with TMF produced the following:
It increased total equity about 4 times. Sure, it puts TMF at 4.5x leverage in periods of market downturns and thereby increases drawdowns and volatility. Leveraging fees however remain at 1.5x. TMF is not something that will go bankrupt. It could decline in value but that would be spread out over years and you could always switch back to TLT even on the flip of a coin (meaning for any reason).
Increasing the 100k initial capital to 1 M generated the following:
It should have been expected to increase 10-fold since the strategy is scalable. It is about what it did and a little bit more.
I liked the 11,000+ trades. It is more than enough to have the strategy's statistical averages to be significant and not dominated by outliers. I also like the win rate which was maintained at 71-72% indicating that there is a statistical edge at work here.
The 2 trading decisions are administrative in nature. We usually make them before we even run a simulation. However, their impact can be considerable as can be seen in those 2 simulation headers. It always goes back to: it is a matter of choice. Do you push or not?
Guy Fleury
@Vladimir, in case what was presented was not enough, you could always push for more:
Frank Schikarski
Building on the comment of Menno Dreischor: this algo (and the other in-out-algos) currently consists of an excellent regime filter, but there are some more features needed to make sure someone can sleep well while trading live. Given the low number of regime switches, the algo has prevented very well from situations where three pairs have simultaneously indicated risk in the past.
Vladimir is this something which you would like to discuss in this thread? The code would become a bit more lengthy amd the backtest take a bit longer, however the live trading more relaxed ;). I can provide an algo with the stop loss part (until next rebalance) and the portfolio part (based on Emilios nice contribution) including feature switches so performance can easily be compared moving further.
Guy Fleury
@Menno, we have slightly different views on this thing and here is mine.
My question is: now that you have this strategy, how do you game it? You know what it can do, you know its weaknesses and explored its potential. You know you can turn bets on and bets off at any time of your choosing. So what will it be? You want to reduce volatility, then hedge the thing with options or using it with other trading strategies. Put less capital at work in what you might consider a more risky proposition, it would tend to reduce the impact of drawdowns. You have many solutions at hand.
The initial questions are answered by the simulations. Does it survive? Can it support leveraging and by how much? Is it tradable? Is it executable? Is it worth it?
A long-term investment strategy is not the same as a long-term trading strategy. Like I would not even suggest a long-term Buy & Hold (even Buffett style) using leverage. On Berkshire Hathaway's record, we have 4 drawdowns that exceeded -50%, and at 2x leverage, it would have been game over the very first time it occurred. Mr. Buffett knew that from the start, most probably why he does not generally use leverage. Note that during the financial crisis, the drawdown exceeded 100B in losses, and yet, he took the opportunity to buy stuff and stay the course. Also note that Mr. Buffett's investment strategy is not what we would consider a trading strategy even if he does trade from time to time.
But, and this is a serious but, in a trading environment, the problem is very different. For instance, this bond switcher changes direction all the time. Liquidating all its stocks or bonds depending on its trend-following decision making proxy. It switches direction a lot more times than needed or necessary. Over the trading interval, the switch occurs something like 70+ times of which 60 might not have been required but still served as anticipated protection. It is the just in case part of the strategy, preemptively putting aside the need to set stop-loss measures either on the individual stock basis or from the global side of the equation.
Nonetheless, the portfolio, whatever its composition is fully invested at all times either in bonds or in stocks. The bond part is easy. If it was in only one bond, I would prefer switching to cash or somehow go on the sideline. However, we are dealing with an ETF, an average of many bonds rendering the proposition more secure (meaning less subject to huge variations). And since we do have market halts in too volatile times, the risk is reduced there too. Note that this would not stop something like the Flash Crash of 2010 where some ETFs exchanged hands at one cent per share (those trades were later reversed). Remedies (new rules) have been put in place not to repeat those circumstances. Yet, they might still happen going forward...
All TMF does is accentuate TLT's volatility and indirectly risk. We all understand that. But, we are in the game to make money and take measurable risks. We need to manage this risk. The mission remains the same: maximize overall return in this compounded return game in which time is a major component. I do not design strategies to spare feelings, I have them follow the math. Someone wishing to do less can always scale down a strategy to the level they feel comfortable with. It is their choice. But there is also an opportunity cost to it as illustrated in the charts provided.
All the profits generated can only come from your trading procedures. Your portfolio with obey the following equation that it wants it or not: F(t) = F_0 ∙ (1 + g – exp)^t where return g = r_m + α_t, with exp the trading expenses, r_m the average market return, and α_t the alpha you bring to the game. To improve on your long-term outcome, you do not have that many choices, all the variables are there. You can increase or decrease the initial capital F_0. Increase your portfolio return g, but to do so you will need to generate some positive alpha since r_m is not under your control. It should be implicit that to reach t (the end game) the strategy needs to survive for the duration.
You can always try to reduce trading expenses, but I have always found that trivial. You cannot add more capital as you go along. All the money needs to be generated from within the trading strategy. You improve market timing, stock selection, trading procedures, betting system, and strategy gaming. All aimed at giving you a higher alpha. And since it is all compounding over the entire trading interval, anything that will improve the strategy should bring higher returns.
This strategy is scalable, trades some 150 stocks, and will try to maintain its stock weights constant with a constant leverage: self.wt[sec] = LEV/len(stocks). When adding leverage, all you do is increase the bet size for every trade. It does not change the nature of the strategy or it dynamics. The bet size will increase with rising equity and decrease in periods of drawdowns. In this case, it is not the price movement of stocks that determines the trend, it is a surrogate that it be right or wrong. And it is wrong a lot of times. However, there is an advantage to this even if it increases trading expenses. It makes money.
In the 1.5x leverage scenario, there were 11,937 trades, some due to rebalancing and a lot of them due to the bond switcher logic. We do not have data on which is producing what. Nonetheless, 11,937 trades is sufficient to speak in terms of averages. We really do not know what triggered each of those trades, but we do have statistics on them. The hit rate was 72% which in any trading strategy should be considered as remarkable and also as the expression of an edge due to the trading procedures (view it as a non-random outcome). The average win was 0.29% while the average loss per trade was -0.22%. Again, expressing a slight edge in the trading procedures even though I find it relatively small, but with a 72% win rate, it explains the overall outcome. Also, the average loss per trade of -0.22% shows how sensitive this strategy is to downturns. It does not tolerate much.
So, what we see in the charts presented is: F(t) = LEV ∙ F_0 ∙ (1 + g – exp) )^t where the added leveraging fees are not accounted for. You change LEV and it will have an impact on the total return. The question becomes as said before: how far do you want to push this thing? BTW, you can push it to even higher levels, if you wanted to.
Jack Pizza
Frank Schikarski also I think we would need some interest rate logic for the bond selection or maybe switch to 1-3 year short duration bond funds.
Because the out only works well now because of low interest rates. If you have a regime where % rates start rising rapidly and the markets crash the bond funds would perform poorly at the far end of the duration.
So there would need to be an out of say very short term funds or just straight money market cash.
Chak
Yes, sampling theory posits prediction accuracy in population/sample parameters similarity, which I completely agree with, though you can't discredit value in an simple model that's not overfitted year over year. To dummyify things, I suppose one could just look at the F-statistics, R-Squared, R-Square Change and beta coefficient tables vs the distance/variance of the actual-predicted errors to see merit in the model's performance on an index that generally grows ~8%/year across time.
If we're focused on randomly selecting stocks from an index based on certain attributes, then, yes, the model should be completely reconsidered. But the model looks at, sorts, and selects stocks with the largest DollarVolume from an index per rebalance, so we're essentially selecting the equities that contributes the most to stock market growth.
Gv
Vladimir v1.3 selects top 100 based on DollarVolume on that particular day. Do you see any value to change this to AverageDollarVolume from say last 126days?
Vladimir
Gv,
Do you see any value to change this value to AverageDollarVolume for, say, the last 126 days?
Definitely!
In one of my successful out of sample Quantopian algorithms, created in 2019, I used the exact same period to average the dollar volume.
ADV = 126;
Dollar_volume = Factors.AverageDollarVolume (mask = m, window_length = ADV)
Vladimir
Menno Dreischor,
Just a friendly reminder.
The topic of this thread is not validation of overfitting or sensitivity tests so you have your own
thread for this.
I have seen several times "overfited" for this actually one parameter strategy.
Nobody now asking question why J. Welles Wilder chose default period for RSI 14
or Gerald Appel choose default parameters for MACD 12-26-9
or Goichi Hosoda choose default parameters for Ichimoku Clouds 9-26-52.
We have been using these magic numbers for many many years.
And they work if applied correctly without sensitivity tests, walk forward optimization and PCA.
I do not realy understand why you choose specificaly this strategy for your nanotechnology testing.
You are not holding back your words.
In any case, I will stop polluting Vladimir's thread.
For those who always call "the glass half full", there are thousands of ways to improve this strategy.
Fill it with the juice of various factors (technical or fundamental), use the stop loss technique
and enjoy a different flavor.
A famous hedge fund manager said:
"The best indicator of future performence is past performence".
Thunder Chicken
Vladimir
Your stance is fine. However, having someone like Menno Dreischor critique these approaches is entirely reasonable. I appreciate the input both of you provide. I will not provide any critiques on the approach or the investment philosophy as I don't see the value it would provide. However, this thread has devolved into a massive curve fitting and parameter optimization exercise.
It seems we are down the rabbit hole of just changing variables to increase Sharpe and returns, or to decrease drawdowns or volatility. This variable optimization approach is the literal definition of curve fitting and is guaranteed to fail. I'm certain Jared Broad can confirm the same.
In that case, I don't see the point of continuing it. I'd propose we just start a new thread with a focus on parameter optimization, and make it clear, and close this thread.
I don't think it makes sense to pollute @Vladimir work. We should just agree to disagree on curve fitting and parameter optimization.
Thank you.
Frank Schikarski
Hi there,
building on v1.5, please find some feature additions to play with:
- Trailing Stop Loss. To use it, please set TSL to a value > 0. It is basically the original QC code plugged into this algo in a helpers.py to get out of the way.
- Portfolio Optimization. To use it, please set PFO = 1. Thanks to Emilio Freire for his original contribution! Also located in the helpers.py. This feature is slow, but with the 'riskParity' logic it builds a great low volatility portfolio especially for a bundle of high momentum securities with a lower correlation and for live trading.
- Weighted Fundamentals. To use it, please change the weights for the currently three fundamentals. In case you want to change the fundamentals: helpful could be a combination of high actual return, high historic growth & currently undervalued price etc. Please change the sort order in line with a 'lowest' or 'highest' target. As some stocks don't have all fundamentals filled, please make sure that you add filters in the 'filtered_fundamental' section to prevent errors. Due to these filters, the results of this version does not match exactly to v1.5 even if the features are unused.
- Some quick inspiration on further states from the pairs logic: bear, down2x, up2x, up3x which might help for an easy case logic to switch leverage.
As mentioned before, the idea is to take the next steps in risk reduction for this beautiful algo idea! The timeframe in the attached backtest is only 2021, so untuned. Let's see where this goes - enjoy!What happens is that the TSL is checked every hour and liquidates an individual security with a market order, should the price drop. In case of daily rebalancing, it can happen that the same security is bought again next day.
Damiano Bolzoni
Frank Schikarski the version you posted buys and sells stocks every day, and updates the universe every RebalanceFreq days. Is that the intended behavior?
Frank Schikarski
Hi Damiano Bolzoni, a quick answer in my lunch break: the purpose was to add some fresh ideas / features for everybody to play with, on the way to make this algo even more live tradeable. As this may reduce PSR and return, I didn't paste a backtest startin in 2008. So please feel free to change parameters, switch logic on/off, share your insights or bring in further code snipets ;). Rebalancing and trading follows the v1.5.
Damiano Bolzoni
Frank Schikarski apologies, I had missed that the daily rebalancing had been introduced PRIOR to your additions. I was very curious to test those hence the cloning :-)
I'll code a monthly rebalance and then test. With a momentum-based strategy even weekly rebalancing could be too often...
Thanks!
Guy Fleury
Is there something in @Vladimir's In & Out strategy (version 1.5)? What I see is that there is money in there. But, you have to determine that for yourself. What follows is not intended to convince you, you have to do your own homework.
Is there an edge that could persist going forward? Is it of any consequence what this strategy did over its simulated past? Is this strategy overfitted or not? In all simplicity: is it worth it? There is so much that could be said about this strategy.
We can gather opinions, anybody can have those, even people that did not even look at the code to see what it really does. But what I see is that little is given in substantiating those opinions. I find it understandable since I do not know what the future will bring either. For instance, just consider: during 2019, who programmed their strategies beforehand to handle the impact of Covid-19 in 2020? It should be remembered as a testament to our own predictive abilities which might be considered as rather limited when faced with uncertainty.
Notwithstanding, is there really a positive probabilistic edge? To answer that question we might have to answer another one. Is the signal used predictive in some way of the average market trend? Or expressed differently, is its trend-following declaration good enough to extract profits from stock price gyrations going forward?
An even more elementary question might be: how is this trading strategy making its money? If we know how and why then we could evaluate if the trade mechanics could prevail going forward. And thereby see that the strategy might in fact continue to extract profits going forward.
We have theories for everything. Even for stock trading and investing. One of the most limiting theories is the MPT (Modern Portfolio Theory) which already dates back to the '60s. It can be summarized in that the expected optimal long-term market portfolio resides on the Markowitz efficient frontier. This states that over the long term, for a fully invested portfolio, your expected future outcome will “tend” to the market average. It does not say that the ride will be a straight line. Even for such a portfolio, you should expect a lot of ups and downs.
Yet, most modern portfolio managers have a hard time hitting that mark. Anyone exceeding that mark using their private IP is immediately put in the overfitted trading strategy bin. This without even a demonstration or proof that the trading strategy is or not overfitted.
The other guy does better than them, therefore, their strategy is overfitted while theirs is still better and not overfitted for some nonsensical reason. Period. No proof or corroborating evidence, just an opinion and apparently it should be sufficient. Well, for me, it is not. I will do my homework and determine what holds and what does not. I will test the thing and see what is under the hood.
We need to know how a trading strategy is making or losing its money. That sounds simple, we could tentatively say that the trading rules were the main reason for the generated profits. Whatever, the question remains: is it really so?
We do not need drawdown protection when the market is going up, it is when it is going down that such protection shows its value.
For instance, @Frank was kind enough to put out a version of @Vladimir's 1.5 that has stop-loss procedures in place. For me, it contradicts something I have said in a prior post: “...the strategy does not need or require a stop-loss”. A simple test to see if this holds is to enable the stop-loss and see what happens. Here is what I found: the strategy will slowly degrade til there is not enough money left in the account to even execute a single trade. You simply lose, and the reason is also quite simple.
For those finding weaknesses in this trading strategy; know that there are some. However, once you have identified those areas of vulnerability, it is your responsibility to compensate for them. You think that bond prices might fall going forward, then you should add procedures in your version of the program that would handle the situation. You identify some weaknesses in the program structure, its trading mechanics, then find ways to correct them. You have a template that is offered on a silver platter, it's debugged, it provides right out of the box a higher return than market averages. What you need to do is prove to yourself that the strategy has merit, that it can withstand time. Push it to its limits, see it blow up to identify added structural deficiencies, then correct those potential problems if you can. That is the job.
This strategy has been in a walk-forward mode (out-of-sample) since at least last October and it is still going strong. There is something in the trading methods used. It is up to you to determine if it is enough for you or not. All I know is that you can push this strategy to quite a great extent even with simple administrative procedures and it can translate in quite a pretty penny. At least, I think I have demonstrated that it can be done over past market data (see my prior posts). It is not just an opinion, it is corroborating evidence of what the strategy did over past market data.
I know what makes this strategy tick. Where do the profits come from, and how they are made. There is math underneath that governs this trading strategy and you can control the math. So, my suggestion is, dismantle the strategy and then reconstruct the parts you want, see why it is profitable, see how you could improve on its design. I would add, if you do not see how this strategy is making its money, how can you control it? How could you even trust it?
Once you will know what this strategy really does and how it operates, you will be able to control it, and then add the protection you think it might need or whatever. It should be a way for you to gain the confidence needed by first showing to yourself under your own set of rules and constraints the level at which you might find an executable compromise. It is always up to you.
Vladimir
The strategy you see in my second post was developed in October 2020 on Quantopian forum.
In that version it trades two symbols QQQ and TLT and has only two parameters one not significant VOLA = 126 (the period on wich we calculate anualised market volatility) and the other more or less significant BASE_RET = 85
They needed to calculate wait_days and momentum period adapted to market volatility
self.history = self.History(symbols, VOLA + 1, Resolution.Daily)
vola = self.history[[self.MKT]].pct_change().std() * np.sqrt(252)
wait_days = int(vola * BASE_RET)
period = int((1.0 - vola) * BASE_RET)
Since then, several versions have been published with the same parameters:
v1.1, in which diversified static lists are traded.
v1.2 with dynamic stocks selector by momentum(thanks to Mikko M)
v1.3 with dynamic stocks selector by fundamental factors and momentum(Leandro Maia setup)
v1.5 with dynamic stocks selector by fundamental factors and momentum
eliminated fee saving part of the code plus daily rebalence specificaly for leverage testing.
Guy Fleury,
Could v1.2-v1.5 be considered out-of-sample since they trade completely different symbols dynamically selected without any lookahead bios?
Thanks for your support.
Thunder Chicken
However, this thread has devolved into a massive curve fitting and parameter optimization exercise.
Where did you find a massive curve fitting? This strategy has only one meaninfull parameter.
and the worst BASE_RET from 50 to 150 will outperform SPY.
Frank Schikarski, Damiano Bolzoni
I do not recommend in future research use v1.5 because it was designed specifically
for testing Dr Ernst Chan's recommendation.
Use v1.3
Frank Schikarski,
Thanks for sharing your version with stop loss.
On Quantopian I used the addition of stop loss on similar strategy but that version increased return.
The opposite happens here with a little lower drow down but takes 10x time to complete.
Try it on version 1.3.
It is completely up to you to use it or not.
Regarding your post.
You probably gave the best recommendation in that thread, but do not fall into the illusion that the higher sharpe ratio, the better. I first look at the return and then share ratio for the reason explained here.
Radu Spineanu
Vladimir I took v1.5 and removed the daily rebalance so it's not doing anything if the stock picks have not changed and it perfomed worse. The original idea of rebalancing by buying losers and selling winners had an edge.
I wish I could share the code but I have no idea how to recover old backtests.
v1.3 has cleaner code, but otherwise why do you prefer v1.3 over v1.5?
Vladimir
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!