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
@.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?
Miguel Palanca
Hi Kamal, do you have a backtest or code for the above?
Kamal G
Thanks for replying Miguel. I know this algorithm is from another thread but the problem happens sporadically on algorithms in this thread too.
On one backtest the error was the below (I cant attach a backtest with an error).
BacktestingRealTimeHandler.Run(): There was an error in a scheduled event EveryDay: SPY: 100 min after MarketOpen. The error was ValueError : cannot convert float NaN to integer
And then on the following backtest with no changes made, the backtest ran successfully.
Peter Guenther
Great analysis, Strongs, thanks for sharing it with us! I was musing about whether one should follow 50:50 the Distilled Bear or ROC on the one hand and the In & Out on the other hand. To participate a bit in both worlds. The Distilled Bear and ROC outperformed leading up to 2020, while the In & Out outperformed in 2020. So, one could create a tiered in & out regime, which is 0% in (when both in & outs are 'out'), 50% in (when one of the in & out is in/out), or 100% in (when both in & outs are in).
Great point regarding inflation. I have used the ETF RINF before. It might be useful to provide a signal and funnel us into, say, gold instead of bonds. Alternatively, one could go into TIPS (Treasury Inflation-Protected Security). I will definitely give that a shot.
Peter Guenther
Thanks for sharing this issue, Kamal G!
I was wondering, since it specifically mentions the conversion to an integer, that the issue might be related to the calculation of the volatility (vola) and subsequent steps, ie the following code:
vola = self.history[[self.MKT]].pct_change().std() * np.sqrt(252)
wait_days = int(vola * BASE_RET)
period = int((1.0 - vola) * BASE_RET)
We try to convert to an integer to calculate wait_days and period.
Not sure why the error occurs, since it actually should work alright, but maybe it could help to use a dropna() again when calculating vola, ie along these lines:
vola = self.history[[self.MKT]].pct_change().dropna().std() * np.sqrt(252)
Jared Broad
Just a little update; we've been using this as a benchmark to optimize a real-world application of python on QC. We've made it run 100% faster over the last 2 months. Behind the scenes, we've migrated to a new framework (.net) and optimized the bridge to python 55%. 🏎
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.
Peter Guenther
Wow, this is amazing! Thanks a lot, Jared Broad and team, for all your relentless work. Fantastic innovations and improvements!
Peter Guenther
Kamal G, I think you are right about the line. We can try the same thing here, ie remove the nans.
See the attached backtest. Let me/us know whether this fixes the issue or whether it continues to occur.
Kamal G
@Peter Guenther, That didn't work either, but I worked it out with the help of @Alex Catarino
Basically the entire self.history(*) was returning nan values on some backtests. Alex advised that it was due to multiple time resolutions on the same equity. In this case “SPY”
I was initially using Hour for the Asset, Daily for the MKT and Minute for the SPY Put Option. Changing them all to Hour, only on SPY, resulted in no failed backtests!!! (It was driving me up the wall). The backtests return very similar returns and drawdowns after the change too, so that's pleasing.
spy = self.AddEquity("SPY", Resolution.Minute)
self.STK1 = self.AddEquity('SPY', Resolution.Hour).Symbol
self.MKT = self.AddEquity('SPY', Resolution.Daily).Symbol
Thanks all.
Peter Guenther
Thanks for sharing these details, Kamal G! This is really useful since others may be scratching their heads about the very same thing, so thanks again for reporting back to this thread.
Guy Fleury
On The Use of a Rebalancer, a Flipper, and a Flusher
I published a new article dealing with the IN & OUT trading strategy. In it, I try to provide a better understanding of the trade mechanics in order to better “control” the future outcome of this trading strategy.
Follow the link below:
https://alphapowertrading.com/index.php/2-uncategorised/408-on-the-use-of-a-rebalancer-a-flipper-and-a-flusher
Often, a different look at a problem can help us better understand our own methods.
Abbi McKann
Guy,
This looks rather vacuous. For what it's worth, in my opinion a little bit of math notation and a lot of superfluous words do not together (nor apart) amount to a meaningful furtherance of the discussion. In this post on ‘AlphaPowerTrading’ (by the way, what could that possibly mean??) exactly nothing is added to the conversation. To be fair, a very circuitous and nonsensical evaluation of the logic of this algorithm is rendered, but it seems to be far below the level of the conversation here.
Chak
Hey Abbi,
When you have the chance, explore the development of this trading strategy since its inception on quantconnect and quantopian, as well as an individual's intellectual and technical contributions to this trading strategy. I might be wrong here, but Guy's blog dates back to 2010 and he's produced other written works, some of which can be purchased as books. This could mean that he knows what he's talking about. Most people would think so.
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!