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
To play devils advocate out of sample is just as useless, we can sit around and debate in sample / out of sample and discard strategies willy nilly.
The best gauge will always be running it live. I started trading this live in my fidelity account we'll see how it performs.
https://econometrical.com/2014/06/09/is-out-of-sample-testing-of-forecasting-models-a-myth/
Vladimir
Elsid Aliaj,
It's not ready to be shared.
Vladimir
Leif Trulsson,
Here are the results of another trading strategy.
It trades QQQ and TLT without In Out, has one source,
uses a simple indicator with only one widely used parameter.
So your assumption:
The strength of the algorithm per se does not lay in the data itself, but in how the data
is handled, and in particular this part:...
is not quite right.
Leif Trulsson
Vladimir when I said "The strength of the algorithm per se does not lay in the data itself, but in how the data
is handled", I meant it literally. But I do agree with Elsid Aliaj when he says: "To play devils advocate out of sample is just as useless, we can sit around and debate in sample / out of sample and discard strategies willy nilly.".
So e.g. when working with neural networks, the data universe is very important, because you don't want to have a to high degree of correlation, because you will then have overfitting. The same goes with this strategy, but in the end it's still how we select and handle the data that's important.
Guy Fleury
@Menno, yes. The Numpy random-normal distribution function was used for base: N(0,1). Price series were normalized and injected with random outliers to mimic fat tails. The price series were therefore stochastic functions with random jumps. Also, historical market values for the drift and volatility were used to make the price series more realistic. It made the price series short-term quasi-unpredictable as should be expected. Nonetheless, the series were fabricated and not real-life, but still a reasonable tool to analyze general portfolio behaviors. Since no seed was used, from test to test, you would generate 20,000 portfolios having no relation to previous ones or to those that would follow. All the portfolios, as well as all the price series, always unique.
Frank Schikarski
Hi guys, great co-creation being done here!!! Some ideas on further In-Out triggers, in case someone has some spare coding time over the long week-end:
Vladimir
Leif Trulsson,
...when working with neural networks, the data universe is very important, because you don't want
to have a to high degree of correlation, because you will then have overfitting.
This algorithm is not for curve-fitting, but for trading.
S.T.E
@Frank Schikarski Great points but I do know if it can help the algo since they move around a lot, tends to increase false signals in the algo and decrease the potential profit. Other thing worth exploring is also the volatility of bonds and FX to the VIX.
Goldie Yalamanchi
I tried another approach to using the In/Out signals. I call this sort of the Value Momentum strategy. The value momentum concept is from another strategy in the forum in terms of picking stocks that have certain EV/Ebitda but also strong dollar volume (momentum) because as you know value stocks alone don't necessarily move or that profitable.
I made the "in" signal when the portfolio gets rebalanced. Then when it is out it dumps everything but TLT.
What I like about the stock selection is that I believe QQQ is very overpriced due to tech stocks. Maybe this will provide a long term play on value momentum which is still very great returns though not QQQ/TQQQ (Amazon, Apple, Facebook, Goog) returns. About 911% from 2010 to 2020.
The code looks a bit like Frankenstein so needs to be cleaned up a bit. Just not super strong python developer yet.
Here it is 2010 - 2020 (11/27/2020) - 911%:
Goldie Yalamanchi
And actually if I run it from 2008 - it has 1130% total profit running the high value momentum stocks. Also, it seems there were many discussions in this thread if the silver/gold, dollar/risk currency and other pairs used to determine IN/OUT state were overfit, wouldn't this separate universe of value+momentum stocks prove it works with out-of-sample data in yet another way?
Yumeng
@Nathan Swenson I am wondering hows ur live result so far? I implemented the algo live but find out for some reason it won't sync with the back test result, even adding the warm up period (althouth this incurrs another problem as the memory will max out when you put into 65 days as warm up). but in short, say today the live alo will indicate "in" but if you back test after EOD today the algo actully indicate "out", which I am still scratching my head and trying to figure out.
Vladimir
Goldie Yalamanchi
I have backtested your algorithm.
On B4-12 node e0e53b43 in 3781.30 seconds at 126k data points per second aproximatly
10 times slower than in free Quantopian.
In my opinion, you chose not right code structure.
And therefore it does not control the leverage properly.
I got many of warnings like this:
2013-08-26 11:30:00 : GOOG: The security does not have an accurate price as it has not
yet received a bar of data.
Your "Value Momentum strategy" is very similar to the “Quality Companies in an Uptrend” originated
by Chris Cain and widely discussed at the Quantopian Forum.
Peter Guenther Oct 13, 2020 posted there his "Amazing returns = superior stock selection strategy +
superior in & out strategy", there was also my version with Chris Cain's original quality
definition and luc prieur Oct 16, 2020 version with a different momentum logic (the best one).
BTW Peter Guenther has already opened the thread
"Amazing returns = superior stock selection strategy + superior in & out strategy"
on QuantConnect forum.
So it makes sense to move the discussion there.
Goldie Yalamanchi
Sure not a problem to move it. But it seems the defintions and code have all been stripped out of that quantopian linking regarding Luec's defintion.
Anyways, if somebody has done the same which is to combine quality stocks and the in/out strat and has posted it, please link it. I have made a very rudimentary attempt to do so without knowing if someone has already done so. Yes, I did take pretty much the guts from Chris's strategy. Those symbol errors are in the beginning of the backtest but don't occur later in time. It needs to be fixed (the code). I am just trying to find different criteria to find stocks rather than QQQ.
Jon Quant
Here is a C# version of Tristan F python code v1.1. I just realized how powerful python is as a financial programming language. A single line in python is equivalent to a few lines of code in C#. This is 4 times the amount of code but executes 3 times faster than python (in backtests). Porting could have possibly been easier and involved less code if a C# version of pandas and numpy libraries were available in QuantConnect. It's good that Math.NET was available.
Vladimir
Goldie Yalamanchi,
The main difference in the luc prieur code is that he applies the momentum filter first and the fundamental filter second.
To solve the leverage problem you should calculate stocks and bond weights once per bar in the same place.
But let's talk about that in the "Amazing returns = superior stock selection strategy + superior in & out strategy".
Peter Guenther
Very much enjoying the discussion here; we are now officially at 200+ posts (plus the 100+ from Quantopian). A big thank you again to QuantConnect for hosting this discussion and allowing us to backtest strategies/ideas, plus the helpful comments and suggestions from the team (Jared Broad, Derek Melchin, Shile Wen)! To the thread's discussion participants: amazing work, keep it up! Very nice work on multiple in & out algos and looking beyond (stock selection, alternative assets selection, out-of-sample testing - possibly all warranting individual threads at some stage). Very interesting ideas, all!
Thunder Chicken
Hello,
Would anyone be able to in some detail tell me what these 5 snippets are trying to accomplish?
Any help would be great!
1. def rebalance_when_out_of_the_market(self):
# Returns sample to detect extreme observations
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)) / 11)
returns_sample = (hist / hist_shift - 1)
2. # Reverse code USDX: sort largest changes to bottom
returns_sample[self.USDX] = returns_sample[self.USDX] * (-1)
3. # For pairs, take returns differential, reverse coded
returns_sample['G_S'] = -(returns_sample[self.GOLD] - returns_sample[self.SLVA])
returns_sample['U_I'] = -(returns_sample[self.UTIL] - returns_sample[self.INDU])
returns_sample['C_A'] = -(returns_sample[self.SHCU] - returns_sample[self.RICU])
self.pairlist = ['G_S', 'U_I', 'C_A']
4. # Extreme observations; statist. significance = 1%
pctl_b = np.nanpercentile(returns_sample, 1, axis=0)
extreme_b = returns_sample.iloc[-1] < pctl_b
5. # Determine waitdays empirically via safe haven excess returns, 50% decay
self.WDadjvar = int(
max(0.50 * self.WDadjvar,
self.INI_WAIT_DAYS * max(1,
np.where((returns_sample[self.GOLD].iloc[-1]>0) & (returns_sample[self.SLVA].iloc[-1]<0) & (returns_sample[self.SLVA].iloc[-2]>0), self.INI_WAIT_DAYS, 1),
np.where((returns_sample[self.UTIL].iloc[-1]>0) & (returns_sample[self.INDU].iloc[-1]<0) & (returns_sample[self.INDU].iloc[-2]>0), self.INI_WAIT_DAYS, 1),
np.where((returns_sample[self.SHCU].iloc[-1]>0) & (returns_sample[self.RICU].iloc[-1]<0) & (returns_sample[self.RICU].iloc[-2]>0), self.INI_WAIT_DAYS, 1)
))
)
adjwaitdays = min(60, self.WDadjvar)
Vladimir
Peter Guenther,
I performed out sample testing of 4 "In-Out" algorithms.
All of them using 12 input sources, complex methods to calculate momentum and outdays.
Exit signal generated as union of 8 individual signals.
if (extreme_b[self.SIGNALS + self.pairlist]).any():
In_out Tentor Testivis October 2020,
In_out_flex_v4 2020-11
In & Out ‒ parsimonious trades edition 2020-11
In & Out ‒ parsimonious trades edition Update (2nd) 2020-11
Was everything as intended in November?
What do you think?
Peter Guenther
Vladimir: Thanks for running these tests and sharing the results!
"Was everything as intended?" -- It may sound funny but I would almost say "partly yes". I think the algo does capture "jitter" in the market, see some of the recent business headlines (surging COVID cases, new lockdown measures in multiple countries, concerning US employment numbers, questions re whether the S&P500 surged too high too quickly, questions re tech overpricing etc.). However, did the algo outperform the market? There the answer is clearly "no"; SPY and QQQ made about 10% in Nov. The algo was completely in bonds for most of the month, with an initial uptick at the beginning but, overall, a zero return (see Nathan Swenson's recent insights in Amazing returns = superior stock selection strategy + superior in & out strategy).
Goldie Yalamanchi was crunching the signals (see recent post in Amazing returns = superior stock selection strategy + superior in & out strategy) and found that the SHY signal does not add much in terms of total returns over the backtesting period from 1/1/2008. Yet, the SHY signal was the main reason for that the algo was out in Nov. Based on Goldie's finding, attached is the algo without the SHY signal and with a daily (instead of weekly) 'in' reschuffling (some people may be running the algo with this setting, i.e. removing the 'lazy equity trader approach). I still have to test this setting over the full backtesting period. For Nov, the attached backtest suggests that this setting does a better job (i.e. setting = remove SHY signal in lines 41 and 51, schedule 'in' reschuffling to daily instead of weekly in lines 71-72).
Vladimir
Peter Guenther,
Here is my opinion sent to you 6 weeks ago.
And now you're using 8 factors.
Think about 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!