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
Jonathon Tzu
On quantopian, I actually found that using the Vix as an indicator when used as an insensitive indicator is quite decent. Basically, I looked at spikes (percent changes) rather than a specific level.
Derek Melchin
Hi Peter,
To ensure the sell orders are filled before the buy orders are sent, we can use
def trade(self): buys = [] for sec, weight in self.wt.items(): cond1 = weight == 0 and self.Portfolio[sec].IsLong cond2 = weight > 0 and not self.Portfolio[sec].Invested if cond1 or cond2: quantity = self.CalculateOrderQuantity(sec, weight) if quantity > 0: buys.append((sec, quantity)) elif quantity < 0: self.Order(sec, quantity) for sec, quantity in buys: self.Order(sec, quantity)
The `Order` method submits market orders, which are processed synchronously by default. See the attached backtest for reference.
Continuing with the development of this algorithm, I recommend following Mark's suggestion of replacing the daily History call with a RollingWindow, manual warm up, and consolidator.
Best,
Derek Melchin
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.
Nathan Swenson
So I wanted to test some out of sample data on the algo. Unfortunately there isn't much data available for the funds used for signals and trading. The best I could do was to run from March 1, 2007 through Jan 1, 2010 to at least add some of 2007 and to see how it handled the crash of 2007-2008 and recovery into 2009. I went with QQQ and TLT for trading funds, but I would expect SPY and TLT/IEF to be comparable. The algo handled it very well with just over a 6% draw down. Very impressive, although I do see more significant draw down in 2013 for the extended runs. It would be more re-assuring if there were more out of sample data to try.
Regarding the code, please be careful if you clone this one. I have some irrelevant edits in there. Use the version from Derek just above.
Peter Guenther
Derek Melchin: Very cool code to ensure that sell orders are filled before the buy orders are placed, much appreciated! Great move to pull it into a separate function to avoid repetition. Regarding the rolling window and consolidator, I think that Mark Reeve is looking into this. This will make the code execution much more efficient. I reckon that one challenge might be how to integrate the shifted and averaged prices (see hist_shift) into the rolling window/consolidator framework (i.e. to create a shift of 60 days and calculate an averaged price using the prices in the +/- 5 day window surrounding the shift (inclusive)). But, we are getting there :)
Nathan Swenson: Nice work there regarding testing the algo out of sample. It's true, the only additional way is probably to 'freeze' a version of the algo and see how it will work in the next few weeks. I reckon the algo in its current form is mainly the version posted about 2 weeks ago ... not a long period yet but it will get longer :) We could also argue that the algo is not too different from the first version posted at the beginning of Oct 2020, so one could also test from there.
A third, more difficult, way would be to reconstruct the ETFs (signals) ...
Joseph Kravets
How does this indicator compare to using vix1/vix2?
There are better etfs than spy and qqq, try MTUM, ARKK. it would be cheaper to trade the mtum etf than buy/sell 30 stocks at once for a stock selection strategy. you can try selecting between quality, value, momentum etfs.
Tristan F
Thank you all for sharing, this is a really good algo. Open collaboration like this is encouraging to see. Successful trading strategies tend to be shrouded in secrecy, which unfortunately limits innovations. Thanks again for your openness.
I took the latest from Derek, rewrote the algo in a way that was easier for me to understand, put all parameters in the initalization, and used some of the shortcuts available in QC. Hope it helps.
Mateusz Pulka
Currently, I am working on using futures instead of ETF prices, however, I need your help here. So far I have found the following futures to use instead of ETF:
self.AddFuture(Futures.Metals.Gold, Resolution.Minute) #replace gld
self.AddFuture(Futures.Metals.Silver, Resolution.Minute) #replace slv
self.AddFuture(Futures.Metals.Copper, Resolution.Minute) #replace dbb
self.AddFuture(Futures.Currencies.CHF, Resolution.Minute) #replace fxf
self.AddFuture(Futures.Currencies.AUD, Resolution.Minute) #replace fxa
self.AddFuture(Futures.Currencies.USD, Resolution.Minute) #replace uup
self.AddFuture(Futures.Energies.CrudeOilWTI, Resolution.Minute) #replace ige
self.AddFuture(Futures.Financials.Y5TreasuryNote, Resolution.Minute) #replace shy
self.AddFuture(Futures.Indices.Dow30EMini, Resolution.Minute) #replace xli ? not sure about this
Only one thing is missing:
self.UTIL = self.AddEquity('XLU', res).Symbol # utilities
And I have not found a good replacement for this in futures list. Can someone verify my replacments above and also maybe someone has idea for a good replamcnet for XLU etf.
Diary Of A Finance Kid
Mateusz Pulka The XLU ETF corresponds to the performance of the Utilities Select Sector Index which takes pricing from multiple Util companies in the S&P500, Ideally you want to recreate the basket of stocks in that index using futures pricing because one single futures contract is unlikely to be a good representative.
Tristan F
Mateusz Pulka Why are you looking to have the indicators come from futures? Futures logic is a lot more complex in QC (and anywhere really).
If you want to trade futures, you can continue to use ETF indicators and you only need the futures information for the traded assets; for example:
Guy Fleury
One could do so much more with this trading strategy simply by gaming it, albeit at a higher trading cost but that should be expected.
You want to do more, you will have to take on some incremental risk while bringing in more capital. I accept limited leveraging as a means to this end as long as the trading strategy can more than pay for the added costs and added risk. It is the classic risk/reward consideration everyone is facing.
Let's do an example. Split a $1M trading strategy C into two portfolios: C = A + B. Put $900,000 in strategy A and $100,000 in strategy B. Portfolio B is the 10% of portfolio C put at some anticipated higher risk.
You make strategy A track a market index (Ex. QQQ, SPY, or DIA) or whatever with an expected 10% outcome over those 13.24 years. Thereby, portfolio A's expectation: E[A] = $900,000 ∙ (1 + 0.10)^13.24 = $3,178,935. If you could add 0.10 as alpha to that scenario it would push portfolio A higher: E[A] = $900,000 ∙ (1+ 0.10 + 0.10)^13.24 = $10,060,098. This 10% alpha is something you will have to bring to the table, either by luck or using your trading skills.
Portfolio B puts into action the In & Out strategy presented here, but with some modifications. Instead of using QQQ, let it use TQQQ as the ETF for the upside. TQQQ trades some 75,000,000 shares per day, so you can get “in or out” of TQQQ with relative ease even if you played thousands of shares at a time. Nonetheless, it is a 3x leveraged ETF.
I would not be holding TQQQ for the long term since it might not outperform market averages. For instance, over the last 10 years, TQQQ had a 6.9% alpha with a beta of 3.38 resulting in a mean annual return of 4.17%. However,... this is a trading portfolio and things get to be different. You will be getting in and out all the time, almost swing trading the thing.
The reason holding TQQQ for the long term is technically suboptimal is due to its very structure. It cannot escape the consequences of multiple 20% swings: (1+ 0.20) ∙ (1- 0.20) = 0.96 and not 1.00. You repeat this 20 times and you get: [(1+ 0.20) ∙ (1- 0.20)]^20 = 0.44. This means that with a 50% hit rate, and a long-term time horizon, it will make you lose more than half of portfolio B potential just because TQQQ rebalances its 3x every day. Also, drawdowns, due to the leveraging, are amplified by this factor of 3. Therefore, there is real caution to be had here.
For portfolio B's protection (more your protection) you continue to force it to switch to bonds on any hint of market turmoil. We could design better ways, but for the moment, let's stick to the program's code. Strategy B's switching mechanism appears sufficiently sensitive to act as a running global stop-loss. It is probably the main reason why none is explicitly given in the strategy since it was not needed or necessary.
But, how far can you push portfolio B?
In my tests, I reached in succession: 32,560%, 45,641%, and 52,428% by adding stuff in my exploration for the strategy's limits. That is a CAGR equivalent of 56.67%, 60.93%, and 62.67% respectively. Therefore, you could get for outcome: E[A] + E[B] = 0.9 ∙ C ∙ (1+0.10)^13.24 + 0.1 ∙ C ∙ (1+0.6237)^13.24 = $65,942,970. Strategy B producing 95% of total returns making portfolio C operate at a 37.21% long-term CAGR equivalent.
What was at risk?
To give portfolio B some initial leeway, you activated a one-year trailing stop-loss at $100,000 after its first year of operation. This way, if strategy B failed thereafter, you would get your money back plus the results from portfolio A. So, the same formula as above would give: E[A] + E[B] = 0.9 ∙ C ∙ (1+0.10)^13.24 + 0.1 ∙ C ∙ (1+0.00)^13.24 = $3,278,935 for the no alpha scenario, and E[A] + E[B] = 0.9 ∙ C ∙ (1+0.10 + 0,10)^13.24 + 0.1 ∙ C ∙ (1+0.00)^13.24 = $10,160,098 with the 0.10 alpha accounted for. The loss on portfolio B would be the lost return (a kind of missed revenue opportunity) of $253,215 since you got back your initial capital but with no profits.
Over the long term, with portfolio B remaining active for the duration, the expected gain for portfolio C would have been: E[C] = E[A] + E[B] = $65 million as expressed above.
The consideration becomes will you implement strategy B in concert with strategy A or some variation thereof?
Portfolio A offers a consolation prize in the range of $3,278,935 to $10,160,098 or more with more alpha. Portfolio C on the other hand (including an active strategy B) offers $65,942,970. Based on my further tests, It could do even better. It is really a matter of choice.
Regardless, you can still do even more.
For instance, using an 80/20 initial fund separation would give: 0.8 ∙ C ∙ (1+0.10)^13.24 + 0.2 ∙ C ∙ (1+0.6237)^13.24 = $128,353,791. Notice that the 80/20 split is an administrative decision, in a way saying it is your decision and not the program's decision.
Why use portfolio A with such results?
Simple: in the beginning, portfolio A (due to its size) acts as a damper; a volatility and drawdown buffer. While at the end, portfolio A is just a small fraction of the whole (less than 3% of portfolio C).
Happy trading to all.
Vladimir
Guy Fleury ,
Welcome to the club!
Mateusz Pulka
Diary Of A Finance Kid Correct me I am wrong. For example XLI etf is an industry sector. So for replacement of XLI I can use Dow Jones Industrial Average which behaves very similar to XLI when I compared charts. For XLU I have found Dow Jones Utility Average however I haven't seen any corresponding futures for these indices.
Nathan Swenson
Mateusz, I don't see any advantage to using futures for signals other than maybe HG (copper) which responds a bit quicker than DBB. I am going to port the strategy over to my NinjaTrader and I'll use TD AmeriTrade data feeds to get the ETF signals and just trade the futures instruments such as /NQ (or /MNQ micro) and /ZB and /XT (30year and 10year treasuries) for bonds. This way you get the signal in the same way but get the leverage of futures without the decay of 3x ETFs. Additionally if you go with futures for signals, it gets complicated due to roll over. I don't want to deal with that.
Goldie Yalamanchi
I deployed a live server for Alpaca (paper) and then will try Interactive Brokers (paper) next. If they go good after a few days I am going to try the real account after that.
Algorithm sounds great and everything else does too but I have to concede that I doubt if the performance of tech stocks going forward is going to be like what they were for AAPL and AMZN going up by 1000%'s. US GDP is maybe max 2-3% and the buffet indicator of total market cap to GDP is at 175% well over the amount for a good crash to happen. A good crash would definitely make QQQ or SPY or any other basket of stocks ticker seem appealing again. Otherwise we would have to assume outsized gain for the US which is highly unlikely we would have to be valued at like 90% of the worlds GDP to get the performance of the last 12 years. I just don't think it is going to happen.
Mateusz Pulka
Thanks for explanation Nathan Swenson I just wanted to test it and see the result only because of curiosity. How diffrent signal we can get.
I think you have to be careful Goldie Yalamanchi because I have checked alpaca. The data which they provide are slightly different than quantconnect, so it is mean that strategy will be generate different signal.
Goldie Yalamanchi
Mateusz Pulka Ok so for equities the only real broker to reliably use then is Interactive Brokers? Which has all sorts of issues unless you setup sub-accounts and multiple logins because I think if you log into their site while your algorithm is running that will kick your algo out, if I am not mistaken. Hmmmm, I wish the TD Ameritrade API or integration was done.
Nathan Swenson
Goldie Yalamanchi, the trade frequency is so low and entries so imprecise that there is very little benefit to an autotrader. You really only need the signal. You could even do the trade later by simply checking what the entry was in the paper traded algo and simply set a limit order at or below the price that was recorded in algo. Most likely you can do better than the algo did with a quick chart read and proper order placement. I am trading my TDAmeritrade account and I got into TMF at a much better price than algo did. I got 34.10 and algo entered 37.06.
Mateusz Pulka
To be honest goldenzipper12 I don't know cause I have compared different data sources and every source has different close prices, which impacts the final result 1700% vs 400% for example. Generally it is a simple to check: run backtest on QT print log of the result of self.History and do the same on another data source and compare the prices.
Nathan Swenson Yes you are right however in my case as I would like to start with a small amount of money is a problem cause the cost of QC live is about 300$/year. So even for start capital 5000$ it is a 6% cost which is quite huge for me.
Goldie Yalamanchi
Nathan Swenson I got you Nathan and I have NinjaTrader as well but also a day job totally unrelated to quant stuff so it's like I want to just setup this in our Roth IRAs and just run it without worrying about it. I want it to run passively rather than me have to trade it.. But I hear you the trade frequency is so low -- so why have a cloud server and all that can go wrong? Idk just because I want to be lazy and have the system run rather than me think about it everyday and trade it everyday or whenever that once a month cycle comes. Also if it works good I want to run it across multiple IRA's and multiple other types of accounts. Still though based on QuantConnect server model now I think I would have to have 5 server hosts to run 5 copies of the algo against 5 accounts.
Guy Fleury
Ran my latest version of In & Out with $10k to test its downside scalability. No further change was made to the program except for the initial capital (still used TQQQ for the long side).
Portfolio metrics gave a total net profit of 120,148.019%. The equivalent of a 73.358% CAGR over those 13.24 years. The win rate was a modest 65% (which technically is still interesting).
The beta remained relatively low at 0.369 while the Sharpe ratio sat at 1.855.
So, there is something that can be done with this strategy even with a small-sized trading account.
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!