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
S.T.E: just forgot: good thinking regarding VIX, as a 'fear gauge'. I gave it a try in an old version but didn't really get it to work back then. I think I tried different levels, e.g. above 15, 20, 25, 30. However, it might just be a question of implementing it right, e.g. not via static levels but via sudden jumps or similar. So, definitely an interesting indicator to play around with and see whether it could add value.
Alex Rehf
Peter Guenther thanks for your reply. Is there a reason, that you use a average of the 11 days 60 days +-5 and no average for the most recent value? In this way, you combine an averaged and non average value. I might try to modify the code tomorrow and see the results.
Peter Guenther
Alex Rehf: absolutely, definitely worth a try I would say. My thinking was that, for the 'out' decision, we want to be fast, i.e. no 'averaged, partly historic information' including historic prices, but just the very recent information, since when things go pear shaped we may need to react quickly to new information. However, to evaluate the current price, we need a (somewhat universal) benchmark, hence the return operationalization. Yet, a benchmark resting on just one data point alone can be fickle (see the 'magic 58' discussion), so we may want to base the benchmark on multiple price points to reduce fickleness, hence the idea of using an average of multiple historic price points. So, long story short: by the end of the day, I reckon we want to assess current price drops in important economic and sentiment indicators and for this assessment we need a somewhat stable benchmark.
Joshua Tsai
I'm a bit confused why you aren't using a triple leveraged SPY ETF like SXPL since it seems to complement this strategy very well as volatility slippage will be ameliorated by the predictive power of the in-out strategy.
Also, I was wondering whether you'd tried a global ETF rotation strategy with this since many might argue that the returns might decrease in the future as we are coming off one of the longest bull runs in SPY history. While I've found that global ETFs work on their own, when combining them with this strategy (assuming that most of them would be highly correlated with the SP500), the strategy performs only moderately well with greater drawdowns.
Also, I believe that Quantopian and QC present data differently as the quality in an uptrend model I tried had pretty different results.
https://www.quantconnect.com/forum/discussion/1637/quantconnect-data-vs-quantopian-data/p1Nathan Swenson
Joshua, there is another thread where leveraged ETFs are tested in this algo::
https://www.quantconnect.com/forum/discussion/9632/amazing-returns-superior-stock-selection-strategy-superior-in-amp-out-strategy/p1/comment-26737Matthew Wormington
Peter Guenther and collaberators, I am new to quantitative finance and have been folloing this thread with interest both on Quantopian and now here on QuantConnect. I was wondering if anyone has done anyone else has run backtests with cash rather than bonds as the safe haven assest and seen the performance drop massively. I was wondering how this may impact the performance of this algorithm going forward with the fed basically causing treasury bond returns to drop dramatically for the forseeable future. I guess there are other safe haven assets such as gold as alternatives but I havent tested and furthermore, I was looking at this approach for in-out signals that might be applied to a 401K plan with limited asset types, mainly to reduce drawdown. I know that the discussion wanted to stay clear of asset selection but the approach does seem to be really impacted by the performance of the out of market assets which may well behave very differently in coming months.
As I say, I'm a new and so may be missing something here and I'd be really interested to see what folks have to say...
Nathan Swenson
Hello Matthew,
If I replace TLT and IEF with GLD and UUP (Gold and US Dollar) on the QQQ version, returned drop from 1759% to 829.6%.. The Treasurey Bonds make a huge difference in performance.
Peter Guenther
Joshua Tsai: Welcome on board regarding this discussion! Definitely interesting points there and I would say plenty of opportunities to explore! The algo is still relatively 'young' (posted on Quantopian on 4 Oct 2020), so I would assume that the opportunities for scrutiny and improvement are still endless. As Nathan Swenson noted, we have just begun to discuss the stock selection strategy (and leverage) in this thread here. We have not yet tried an ETF rotation strategy of the kind you have described, just a very simple tech selection strategy, so your findings are definitely very interesting. What would also be interesting is to try and run your ETF rotation strategy with another in & out-type strategy, such as Vladimir's ROC.
Matthew Wormington: Also to you, welcome to the team! Absolutely, great tests regarding cash vs holding the bonds and great thinking. Consistent with your tests, we also found that the contribution of bonds is substantial; for some of the findings, see in the Quantopian archive using the search terms: "Just musing, but it almost seems the signals are really just finding when to get into bonds?", "Earlier theme: Contribution of bonds", "Conclusion: 'out' = signal for return opportunities in alternative assets and possibly investors' crystal ball for market downturns, although these may not actually materialize".
In terms of holding alternative assets other than bonds, I think that, on Quantopian, Frank Sch was reporting an improvement by adding 10% gold to the basket plus 45% of each of the bonds. However, he was using a prior version of the In & Out together with a certain stock selection strategy ("Quality Companies in an Uptrend"). Regarding your question concerning whether we can assume that bond returns will equally contribute in the future, here is my take, others may see it differently:
<< ... I reckon for backtests that is the question: are we sufficiently confident that the future will look like the past? There can only be opinions about this since no one can predict the future, so here is my opinion: the past decade was characterized by cheap debt capital, which fuelled company growth and margin-based trading (trading with borrowed money). Due to the Covid crisis my assumption would be that the next, at least, half a decade will look very similar. So, it could be valid to say that the (near) future is likely to look like the past. Now, the bonds basket generated excess returns in the past despite it being the environment that is was (e.g., low interest). So, I would assume that this basket can also generate excess returns in the (near) future. >>
Matthew Wormington
Nathan Swenson OK, glad that it's not just me. Given the impact of the fed for the forseeable forseeable future on Treasury Bonds and them having such low returns then it will be interesting to see how this algorithm paper trades in coming months. It might be critical to consider alternative risk-off assests than in the past so as not to be very mislet by backtests. For example, using GLD and UUP you take a 2x hit but its nothing like the hit you take going to cash or possible treasuries for months/years. So alternate risk-off assets might be critical to have this algorithm perform going forward. At least from the perspective of a newbie...
Alex Rehf
As this strategy doesnt need to be traded automated, i set up a telegram bot notifying me, when the status is changing.
has anyone played around with alternatives for IEF and TLT?
Nathan Swenson
Alex, this is exactly what I want to do. I plan to trade the signals with my NinjaTrader futures platform and my TD Ameritrade account. Regarding playing around, please check the other thread where I ran it with SPXL and TMF to reach nearly 40,000% return from 2008 to 2020.
Can you show me the code you used for the SMS?
Alex Rehf
Nathan Swenson I used the code (more or less as it is) and used yahoo finance to get the stock data. i just started the server in my amazon cloud.
Nathan Swenson
Interesting. I didn't realize you could run this code outside QuantConnect. I was going to just pay the $28 for the live trading node and paper trade it. Was hoping for some alert system to notify me of pending trades.
Jack Pizza
Read an article awhile back too, performance for bonds still perform fairly well even during rising interest rates, especially the 7-10 year duration, they don't get hit nearly as hard as others. The only time it gets hit hard is rates sky rocket to like say 20% or something. 10% GLD should be more then enough to cover as insurance for bond losses.
I think GLD should have it's own logic, a 2nd out so to speak, if not in stocks & not in bonds then x% in GLD. would be interesting to test.
Jack Pizza
Here is backtest with GLD added in as a 3rd option, split between bonds 33/33/33. returns drop slightly DD goes up by .50%. Problem is even with gold we're assuming a continous uptrend or some sort of correlation with bonds for the out signal. Think like i mentioned above gold should be split into it's own signal, so it's weighted against the performance of stocks and bonds.
Because the whole hypothesis is what happens if bonds don't perform, if bonds don't perform it will throw the signal to IN and you wouldn't be holding GLD anyway.
Jack Pizza
Alex Rehf You'd still need to run it with live data to get the signals at 11:30. Or are you waiting and trading it next day, as yahoo data doesn't provide live realtime data I don't believe.
Jack Pizza
Actually what's needed is not us playing around with the GLD's of the world, trying to predict will bonds perform in the future or not. What we need is a tail risk system like Taleb's at Universa. Then you can switch this strategy to say something like long only with 3X ETFs while having the out of money options to protect downside.
But from what I've read about options protecting your downside usually ends up costing too much, so I wonder if anybody knows or has experience on how Universa implemented it to generate 4000% returns in March. They were able to hedge a hypothetical S&P Portfolio with only around 3% of invested in the tail fund which covered about 12% drawdowns.
You could theoretically run say a 40% DD algorithm assuming it's producing outsized returns of say 40-50%, buy about 10-12% of tail risk protection, and capture around 30-40% almost risk free, assuming a sound trading strategy that produces gains.
https://www.institutionalinvestor.com/article/b1nhg4w9k5hjp0/Nassim-Taleb-and-Universa-Versus-the-WorldAlex Rehf
Elsid Aliaj Yes, thats right. My plans are use an other data provider to get the current price of that day.
Mark Reeve
Hi All,
I am looking at this algorithm and trying to wrap my head around a few methods used within.
For starters, doing the history request DAILY is super inefficent and massively slowing down your backtests. You are trying to compare price to where it was 58-days ago... or in more recent versions the MEAN of 55-65 days ago...
If this was all you were doing then simply using the MomentumPercent indicator would do the trick with no need for repeated history requests (See Basic Indicator Usage), and no need for any hist.shift()
# Creating a dict{} of MomentumPercent indicators, indexed by symbol.... return_sample = {} for ticker in self.SIGNALS: return_sample[ticker] = self.MOMP(ticker, 60, Resolution.Daily) ## Avoids the need to do this... hist = self.History( self.SIGNALS + [self.MRKT] + self.FORPAIRS, 252, Resolution.Daily)['close'].unstack(level=0).dropna() hist_shift = hist.apply(lambda x: (x.shift(65) + x.shift(64) + x.shift(63) + x.shift(62) + x.shift( 61) + x.shift(60) + x.shift(59) + x.shift(58) + x.shift(57) + x.shift(56) + x.shift(55)) / returns_sample = (hist / hist_shift - 1) ## And should be much more efficient in backtests.
However, it seems like you are ALSO using the history request to calculate the historical significance of the most recent return_sample:
# Extreme observations; statist. significance = 1% pctl_b = np.nanpercentile(returns_sample, 1, axis=0) extreme_b = returns_sample.iloc[-1] < pctl_b
In this instance you are probably best served by creating a Rolling Window of the close prices, 65days long, and then using this for all your calculations above.
We recommend using RollingWindows to hold periods of data instead of making multiple Historical Data Requests. It is much more efficient as we just need to update the RollingWindow with the latest data point, while a History call fetches the whole requested period and synchronizes the data.
I'll try and code all of the above and post here when done...
Meanwhile, picking the MEAN of 55-65day lookbacks avoids the need to specify exactly which day, it does still however comes down to ONE single day. Why not chose them all? Create an ensemble, 11 different signals, and trade them all, effectively creating a dimmer switch for the IN_OUT functionality of this algo - instead of the wham bam 100% IN today, 100% OUT tomorrow signal?
Mark Reeve
I also meant to point out - it appears you are requesting 252 trading days of history. Using 65 to measure returns, but this then drops 65days of data for your "extreme observations".
You arent doing them on 65-days and you arent doing them on 252-days, you are doing your calculations on 252-65 = 187-days. As in returns_sample is 187-days long. Was this your intention?
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!