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
Jack Pizza
Thomas Chang where do you add those in?
Thomas Chang
Hi Tentor,
I would say the equity chart looks somewhat "ugly". Could you make it like this? This is much "beautiful". :-)
T
Thomas Chang
I wonder how to plot the leverage.
Diary Of A Finance Kid
Has anyone else tried different time rules other than 75 minutes and observed the effects on returns? I have been looking in to this becuase a lot of my manual trading is effected by the time of day i enter trades.
I believe returns are increased slightly by changing both instances of the following line of code to 120.
time_rules.market_open(minutes = 75)
)
Thoughts on this?
Adam W
Tentor Testivis
You'd have to stitch the futures together manually unfortunately, but there's a helpful `FutureChain` class that can track futures contract on the same underlying asset. With a single asset (in this case, SPY), it shouldn't be too tedious but would be a bit of work compared to ETFs. Trading futures is limited to IB Brokerage currently as well.
To do this, we could create an `Indicator` class, loop over `FutureChains` and update the indicator to represent the time series as the value of $1 invested in the stitched contracts. For instance,
Thomas Chang
I think that picture is the old LEAN equity curves. You could get a local LEAN running and customize it however you want though.
For plotting leverage:
def Initialize(self): self.Schedule.On(self.DateRules.EveryDay('SPY'), self.TimeRules.BeforeMarketClose('SPY',0), # Frequency of plotting self.PlotLeverage) def PlotLeverage(self): leverage = self.Portfolio.TotalAbsoluteHoldingsCost / self.Portfolio.TotalPortfolioValue self.Plot('Leverage', leverage)
Peter Guenther
Fantastic, thanks for sharing these results, Diary of a Finance Kid! Indeed, we didn't play around with these numbers so far, so that's great. I have directly used this in my update below :)
Updated: In & Out with percentiles, return differentials, and wait day multipliers based on negative return flips of the alternative assets in asset pairs
Today, I have tried to replace the ratios with the idea described earlier, i.e. to capture when the alternative asset in the pairs (e.g. silver in the gold/silver pair) goes from having a positive return on the previous day to having a negative return on the current day, while the other asset still has a positive return (see lines 104-106). If this occurs, I am feeding in 15 (the initial wait days) as a multiplier; it doesn't matter that much what exactly the multiplier is, see my earlier comment (in the archive) that large multipliers are digested relatively quickly over time due to the exponential decay via the multiplication with 0.5 (see line 99). So this modification should take the problematic direct use of ratios out of the game. Happy to discuss the sense and nonsense of this new modification.
I have also changed the trading time to 120 mins after trading start, as per Diary of a Finance Kid's suggestion above.
Positive outcome: we are back north of the 1,000%. The backtest gives me 1,100% using the updated algo ... (hopefully no errors in the coding etc.). I still have to integrate Thomas's suggestion regarding reducing the number of trades. This is still on the list, thanks Thomas!
Frank Giardina
Gentlemen,
I too was following this thread on Quantopian. Many thanks for converting this over. Very impressive on how quiclky plus the graphs well done!. I do have question was it Vlad's version or another that greatly reduced the number of trades? Sorry i can't contribute much to the technical coding you guys are way out of my league. Nice job
Thanks
Frank
Jack Pizza
Diary Of A Finance Kid I played around with the timing, did it near end of day ect. returns went slightly down, I think at that point where you're playing with time of day you're likely to start overfitting, but worth a shot i guess.
Peter Guenther I tried turning off the rebalancing but kept complaing about something in the method, would be interesting to see how much of a performance boost rebalancing adds, I'm assuming not much, if so would probably make sense to turn it off for simplicity, and comission savings if running automated on say IB.
Jack Pizza
Adding QQQ instead of SPY keeps DD fairly similiar but pumps results massively.
Jack Pizza
Also for live trading maybe some logic needs to be added to wait to start rebalancing until an actual order signal not rebalancing is given, not sure if this does it now.
Peter Guenther
@Frank: Great to have you back on board, and an important question! The reduction of trades can be achieved via Thomas Chang's code (in the archive and Thomas's new code posted above), which I reckon theoretically can be integrated into any of the 'in & out' tactics discussed here. It's still on my list to try that out for the In & Out algo. When I got it, I will post it here.
@Elsid: Great thinking, I have also played around with the 'stock selection component' via swapping in the QQQ to see what a tech stock selection would yield. I have created a spin-off thread to discuss these and other possible combinations between the latest 'in & out' tactics discussed here and superior stock selection strategies discussed elsewhere. I assume that there is a lot to discover in this space combining the two components, particularly in regard to which components work well together (synergies) and which do not (dissonance). For instance, in an old test (archive), we discovered that Vladimir's "ROC" in & out tactic combines nicely with the "Quality Companies in an Uptrend" algo originally created and posted by Chris Cain on Quantopian on 22 Nov 2019. The In & out algo did not achieve comparable results, so SEL["Qual up"] + I/O["ROC"] seems to form a better pair.
Let's see what additional strategies and combinations the community comes up with ... :)
Frank Giardina
Peter Guenther
Thanks Peter, using my limited coding skills i changed the out of market to weekly and in market schedule to monthly. I also used my favorite ETF allocation self.HLD_IN = {self.MTUM: .4, self.XMMO: .4,self.FVAL: .2} and the results were 1.67 Sharp and reduced trades to 136 over roughly same time period. I know we should not get into WHAT to invest but could not help myself really like this algo
Nathan Swenson
Great stuff guys! I'm new here. Has anyone converted this over to futures with rolling contracts? I would be interested to see the results with the high leverage and low tax consequences for futures. I may try myself, but I'm still very green in this new environment. My experience is in NinjaTrader.
Alex Rehf
Peter Guenther nice modifications, thank you! Just a quick question: The timeperiod for calculations of the differentials is fixed ( ~60days?)
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)) / 11)
Peter Guenther
Alex Rehf Well spotted, indeed it is about 3 months back i.e. 60 = 5 trading days/week x ~4 weeks/month x 3 months. A bit of history: when we you go back in the Quantopian archive, in the first algo version this was set to 58 days, which we then began to referred to as the 'magic 58' since slight changes in this number had a substantial impact. To be less dependent on one particular price point (i.e., exactly 58 days ago), the current algo version(s) use a shifted average; shifted by 60 days and calculated as an average using the prices in the +/- 5 day window surrounding the shift.
Other in & out algos are even less static in this regard and determine the shift period via the market volatility, see for instance Vladimir's ROC algo. I do not think that this flexible calculation works particularly well for the In & Out, but it might just be a matter of implementing it in the right way for this particular algo. So still plenty of improvement opportunities there ;)
Peter Guenther
Frank Giardina Thanks for sharing these results, much appreciated! The stock selection set looks like a nice balance between growth and value. Also, very interesting info that the results look pretty solid in the weekly/monthly trading frequency context. I have never changed the daily/weekly pattern, so that is definitely (encouraging) news for me, and the number of trades is reduced dramatically saving much money on commissions. Great!
Peter Guenther
Updated: In & Out ‒ parsimonious trades edition
The attached version integrates the code that Thomas Chang has kindly shared above; sorry for my 'wordy' implementation (see lines 121-138 and 147-163). From my understanding, the intention is that trades only occur when we switch from being 'in' to being 'out' or vice versa, but not in-between (e.g., reinvestment of dividends?). The total number of trades is reduced to 224 from 3,067 (see backtest above) over the approx. 13 year backtesting period. This is equivalent to about 1.5 trades per month in the updated In & Out version, so much more parsimonious trading here. The total return of 1,116% is pretty much the same, even slightly better.
This update might be particularly interesting for Elsid Aliaj and Frank Giardina, although Frank could reduce the number of trades even more strongly (136) via changing the scheduling function frequencies from daily/weekly to weekly/monthly (see his post above).
S.T.E
Hi - Peter Guenther great work and thank you for sharing. Have you tried to add Quality Comps in Uptrend with ROIC + Value factor?
Do you think adding UX1 and UX2 futures (vix futures) imporve market timing?
Jack Pizza
FYI for live trading:
1. It will enter the existing trade, so maybe some code with an Bolean True / False, if you want it to join existing orders or wait for the next signal.
2. With Interactive brokers, it sent orders, said it was filled, but nothing appeared on IB's side....this was with QC data selected, I will try selecting IB's Data feed, to see if it picks up orders. Still very weird behavior.
Peter Guenther
S.T.E: I did not yet get a chance to implement Chris Cain's "Quality Companies in an Uptrend" algo, but I know that there are several people in the forum trying to implement it. The algo has been very popular on Quantopian, so I am sure that we will see an implementation rather sooner than later. When we get there, then one relevant finding we had is that Vladimir's ROC in & out tactic combines very nicely with the "Qual Up" stock selection, better than the In & Out, at least an old version of it.
Elsid Aliaj: Thanks a lot for sharing and, if you are happy to, keep us posted. It seems like the Live Trading implementation is again a whole different kind of beast. Good on you for tackling it!
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!