Vladimir has shared some great filter implementations (eg: thread 1, thread 2) that inspired me to tinker with them a bit. As can be expected, it seems that Kalman, Laguerre and EMAs show favorable results when combined for trend entry/exit signals. I’ve attached a simple algorithm that trades Crypto (or any ‘trendy’ asset), using crossovers between the three. It borrows from Vladimir’s original code but it has been refactored so the logic can more readily evolve.
In the algorithm you’ll note I’m using something called a SmartRollingWindow —this is a simple utility I whipped up to more easily check for indicator crossovers and falling/rising/flat values. I hope people find it useful.
As for the strategy itself, I’d love to hear suggestions from the community on how to make it more tradable for liquid Crypto (BTC, ETH, etc). Specifically:
- How might you adjust the filters? What would you change?
- What regime filter would you use? Long-term MA? ADX?
- How would you manage money? Volatility-based sizing?
- How might you improve the exits? Trailing stops?
Thanks in advance.
PS:
- Vladimir : I took your Kalman filter logic and turned it into an indicator. It seems to work fine, but please take a look to make sure it still performs as expected.
- Tagging you, Simone Pantaleoni, as I believe you're exploring similar use of for crypto trends
Vladimir
ekz,
Great codding, congratulation.
Yesterday we published "Trading Kalman Filter" - the very simple version with only one indicator. and only one parameter which was not optimized yet.
Compounding Annual Return - 237.024%Drawdown - 38.500%The ratio of the annual return to the maximum drawdown is 6.16.
Compare to yours, with 3 indicator and 3 parameterCompounding Annual Return - 281.711%Drawdown - 50.300%The ratio of the annual return to the maximum drawdown is 5.60 and less than in our simple version.
In our practice, to compare algorithms, we use the ratio of the annual return to the maximum drawdown as the first metric, because we can normalize the return to the accepted maximum drawdown by simply reducing the exposure.
We usually start creating trading algorithm with the simplest version and then adding indicators and parameters one at the time only if they improve risk metrics of our choice.
Best Regards,
Vladimir
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.
Mak K INVESTOR
Hi ekz,
Very cool algo, good job. The returns are amazing but the downside risk very heavy, why don't you sacrafice some of that return to hedge your position?
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.
.ekz. INVESTOR
Hi Vladimir! Thanks for the reply and the kind words. CAR/DD ratio is a good metric to use for sure. I factor it into my algo performance criteria as well and I agree this is not tradable as is. As you rightly pointed out, for some assets using Laguerre and EMA crossing Kalman is less performant than your original version that just looks at price crossing Kalman. The fun is only just beginning, to research even better performance across the board.
On a related note: do you know how to modify the gain of the Kalman filter in Pykalman? I'd like to attempt Kalman-vs-Kalman crossovers, with more responsive vs less responsive, but to do so I need to modify the gain and I dont know how to do so with this library.
Mak K : Thanks a lot and, agreed! What are the different ways you would consider hedging positions in such a system? Would love to hear thoughts.
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.
.ekz. INVESTOR
Here’s my updated version, with Ret/DD at 9.69
Changes:
Note: I am already doing this but noticed others might not be: setting an explicit brokerage model (instead of the default) also impacts performance. I'm using Bitfinex, where I plan to go live, and perf is significantly better.
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.
Vladimir
.ekz.
--> I'd like to attempt Kalman-vs-Kalman crossovers
There are thousands of ways to apply the Kalman filter to trading algorithms, from estimating the next bar's prices and the standard deviation of prices, to generating signals based on forecast errors and dynamically calculating hedge ratios.
A couple of years ago, we created over a hundred Kalman Filters applications on Quantopian that have not yet been ported to QuantConnect.
We'll see if the one you are looking for is there.
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.
Vladimir
Here is slightly modified “Simone Pantaleoni btcusd kalman-mom strategy”
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.
Spacetime
Interesting share indeed. I have ran this with ETHUSD and the results are attached. Looks like it is giving better returns. (.ekz latest version from above)
Would be nice if we can test it out with leveraged crypto futures or crypto options (maybe use it as hedging of some sort).
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.
Vladimir
ekz.
-> Vladimir :
for the Kalman filter, how come you have such a long warmup period?
IE: Why 5 * Period? What is the significance of the constant '5'?
From digital signal processing point, Kalman filter is adaptive infinite response filter (IIR).
Current value of any IIR substantially depends of starting value of time series.
In our practice we usually use 5 times more data points to warn up any IIR to mature.
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.
.ekz. INVESTOR
Great to see the discussion picking up!
spacetime: Awesome that you are trying this on other cryptos. That's the idea. I’ve also tried it on ETH and SOL, and I feel the drawdown can be mitigated more.
Vladimir : Good insights. It makes sense then, that the period will influence the response filter performance. Also, thanks for the version that factors in momentum. I will play around with it, but I admit i am wary of the introduction of multiple parameters that are related to time periods (eg:lookbacks). From what I’ve learned about Kalman, a core part of its value is the fact that it adapts effectively, with much less dependency on lookback compared to, say, moving averages.On a related note, Vladimir, your current entry condition (price is above kalman) unfortunately leads to overtrading, because even though you are already invested it will sell and re-buy. This leads to about 443 trades. Adding the condition if (not self.Portfolio.Invested ) reduces this to 81 trades with slightly better performance, and a savings on transaction fees.
Trading a Crypto portfolio:
For anyone else looking to tinker with this: something to keep in mind as a ‘guiding principle’: Ideally we should able to trade this system with a portfolio of cryptos —imagine daily scanning for whichever security meets the entry criteria, and when we have several, holding them with equal weight (or some factor-based weight). In that case, we’d want a generalized system that is NOT fit to one particular security. This is why I’m hesitant to add new time based parameters because they are likely to have different requirements (e.g. the favorable momentum period of SOL will be different from that of ETH, ADA, or BTC).
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.
.ekz. INVESTOR
Updated Exits With Dual Kalmans Crossing: To reduce drawdown, I investigated other exits (other than price crossing below the Kalman --this happens quite late), and was able to test a responsive Kalman crossing below a less-responsive Kalman. I was able to increase the responsiveness by updating the transition_covariance property (higher value makes it more responsive and follow price more closely). It seems to work alright, squeezing out a better CAR/DD for BTC (298% / 29% = 10.3 ), but not so well for ETH and SOL --also, the optimized values are quite different for different securities which, as mentioned, is not ideal…
Also, because this exit signal tends to fire early sometimes, I updated the entry signal to match vladimirs, so we jump back in the trades whenever price is above the original Kalman (previously i was only entering when price *crosses* over the Kalman.
All that said, it's still worth sharing. See the attached backtest.
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.
Guy Fleury
A small observation.
The entry and exit method 3 is the only one being executed. Methods 1, 2, and 4 do not happen.
Method 1 and 2 happen after method 3 has triggered a trade. While method 4 is never reached nor executed.
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.
.ekz. INVESTOR
Hi Guy Fleury , great to see you chiming in!
You are correct. I parameterized entries and exits to easily test different mechanisms. Only one entry (and only one exit) method can be used at a time.
As of my last version, here are what the methods correspond to:
Entries:
Exit
Right now, My preferred Entry/Exit pair is Entry #3 + Exit #3, as they seem to be the most consistent across securities, and require fewest total # of parameters.
Hope this helps.
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.
Guy Fleury
.ekz.
On your latest version above, all entries are done on method 4 while all exits are done on method 5.
Is there a reason why most of your trades occur at 20:00:00 and some at 19:00:00?
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.
.ekz. INVESTOR
Guy Fleury Again you are correct.
In my last attached backtest, I was testing entry method 4 and exit method 5. I described this in the body of the message/post. Ultimately i still believe entry #3 and exit #3 are more suited for trading multiple securities.
Not sure why some of the trades occur at 20:00 and some at 19:00. It might have to do with the way Lean consolidates the data, perhaps. Good catch, though. It might be worth investigating.
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.
.ekz. INVESTOR
In case it's not obvious to others: i am using externalized parameters, and you can see them in the left pane of the IDE.
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.
Guy Fleury
.ekz.
In your strategy, you have 5 trade triggers for entries and 5 for exits. The order in which they are placed can determine which will be executed first since only the first one reached of each group can be executed at any one time, as you said. You can also force which one will be executed which is what is being done here.
Theoretically, over the same lookback period, the Kalman filter is more responsive to price movements than the EMA signal which is more responsive than the Laguerre signal which in turn is more responsive than the SMA signal. If you test for the price to cross the fast Kalman filter first, the other cross-overs will not even be tested, and, therefore totally redundant. Price precedes them all.
In Vladimir's version, method 2 (Laguerre crosses Kalman) is the more productive method. The same goes for your version. However, your method 5 does outperform them all.
In this strategy using the Kalman filter as a cross-over system you simply have a variation of the old EMA cross-over trading strategy. It becomes something like finding the best lookback period for an EMA cross-over on some past market data. This might also raise a curve-fitting flag. That lookback period might not hold or be the best going forward.
Bitcoin is, let's say, volatile. It can swing 20% in a day (it has done so a few times). Any fixed lookback period could be costly. Maybe something even more adaptive might be needed, but then again, you would be back with the EMA cross-over thing. Almost suggesting that maybe minute data should be investigated next.
The strategy has no fundamental data support. The reason is simple: none is available. There is no formula that can say this is how much Bitcoin is worth. We cannot say either that Bitcoin is over-priced or under-priced at any given time. Nor can we forecast a future value within any confidence interval. You can only rely on lagging technical indicators for trading decisions as was done in your trading script. On the other hand, we could just say it is going up until it doesn't which would put you back at the right edge of the chart.
The big question is: does the price preannounce where it is going or do we just trail behind to wherever it wants to go?
I found the CAGR to be impressive. Good work.
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.
Fred Painchaud
Hi .ekz.,
You could also investigate variable lookback periods based on volatility (such as ATR). When volatility is higher, lookback is lower. And vice versa. It gives you faster response when needed (in big swings which normally come with greater volatility) and better statistical significance when needed (sideways market - flatter volatility).
Such as done within Dynamic Momentum Index, for instance.
Fred
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.
.ekz. INVESTOR
Guy and Fred, thanks for the feedback!
Guy Fleury (on evaluating the system):
Love the breakdown, and appreciate the detail you put in your response. You raise a very good point, too, and I landed at the same conclusion (that Vladimir highlighted to his credit): Price is the most responsive to price (ha), and so using this as a crossover signal will beat them all.
Also, you said it correctly about the Kalman crossovers, we definitely run into the problem again, with subjective parameters. because not all securities created equal... if only there was some way to normalize the lookback/parameterization... :)
Fred Painchaud (on dynamic lookbacks):
... which brings me to your point, Fred. This sounds like a good idea! Adjusting the responsiveness based on volatility, similar to how the Kaufman adaptive moving average adapts based on market efficiency (different from volatility, but comparable). With some adjustments, we may be able to get the appropriate responsiveness for the corresponding market voaltility (or market noise), regardless of the security. If trading on the daily, perhaps we will want to look at the volatility/noise on a lower time frame --perhaps the 4 hour. I will think about this and see how best to apply the logic. If you have any examples of this in practice, or any additional guidance, please share!
Everyone (on 'Strategy' vs Testbed):
Everyone, one thing to note: the backtest i've shared isnt exactly a 'strategy' as is. It's more of a strategy testbed. This is why you see multiple interchangeable entry and exit methods in there --it's easier to test and compare them against each other (using the optimizer). Typically, once I've tested and determined the ideal entry/exit method, i remove the others. I will probably remove the non-preferred ones soon, but I may want to try one or two more things first.
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.
Guy Fleury
.ekz.
Here are the test results of both versions (yours and Vladimir's) when forcing the program to only execute each method in turn.
The first 4 methods gave identical results. The second method was the most productive (Laguerre crossed above/below Kalman). This is a slightly delayed crossover when compared to using price. It was like shifting a position slightly forward in time.
As can be observed, your method 5 did even better.
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.
Fred Painchaud
Hey .ekz.,
Glad you liked the idea. It's really as old as mathematics.
An easy way to dynamically compute periods is to take one measure of volatility and divide a chosen "mid-period", adequate for the metric at hands to “dynamize”, by it. When your measure of volatility is greater than 1 (rising), your “mid-period” decreases. And vice-versa. Like pretty much every metric on y-half-bounded series like price action and volume, your measure of volatility will in theory be able to go as down as 0 and, however, won't have, in theory again, an upper limit. So you'll need to clip the resulting ratio ("mid-period"/volatility), or else, you'll end up with potentially a division by zero (and very very high resulting ratios when volatility is close to 0) and then also potentially very very low resulting ratios when volatility gets very high (the limit calculus being 0).
So, in a nutshell:
Volatility:
Mid-period rebase:
That's it. Well, that's one easy easy way.
Many normalize (read, rescale between 0 and 1 here - as normalization means quite a few things) volatility, usually with artificial stochastics based on running minimums and maximums (stochastics don't apply to samples. but anyway), and then multiply by the mid-period…
Electrical engineers would yet tackle the problem differently. Etc.
Enjoy!
Fred
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
I'm thinking to set up the live trading, but to use it with paper trading just to get the signals in real time. I guess it would be better if it alerts ahead of time, like at open that there will be a trade at 2 hours after open. Because this strategy has so few trades, it's easy enough to just do the trades manually. I'm thinking I'll use a small amount in futures to trade NQ and ZB pair and my TD Ameritrade self managed 401k with a more conservative setup.
Regarding the Alpha Competition, this version qualifies with 84% PSR (Alpha score). Peter, you should think about entering it. You did all the work. I'm just playing around with for consideration of using for my own accounts.
Regarding the high results above, that was without any Margin. So if you wanted to go crazy, you could add some margin to it, or simply trade non-Leverage funds but lean more on margin.
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
Screen shot of the end result.
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
Sorry for all the posts. Having trouble posting screen shots when working from an IPAD.
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.
Mateusz Pulka
Tien Duy Vo Peter Guenther
I have spotted this bug also, the problem is here (the loop is run outside the if statement):
if not self.be_in:
# Close 'In' holdings
#for asset, weight in self.HLD_IN.items():
# self.SetHoldings(asset, 0)
#for asset, weight in self.HLD_OUT.items():
# self.SetHoldings(asset, weight)
wt[self.STKS] = 0
wt[self.TLT] = .5
wt[self.IEF] = .5
# Thomas's reducing unnecessary trades
for sec, weight in wt.items():
cond1 = (self.Portfolio[sec].Quantity > 0) and (weight == 0)
cond2 = (self.Portfolio[sec].Quantity == 0) and (weight > 0)
if cond1 or cond2:
self.SetHoldings(sec, weight)
The for loop will be fire every time even if we set self.be_in to false. The correct version is:
if not self.be_in:
# Close 'In' holdings
#for asset, weight in self.HLD_IN.items():
# self.SetHoldings(asset, 0)
#for asset, weight in self.HLD_OUT.items():
# self.SetHoldings(asset, weight)
wt[self.STKS] = 0
wt[self.TLT] = .5
wt[self.IEF] = .5
# Thomas's reducing unnecessary trades
for sec, weight in wt.items():
cond1 = (self.Portfolio[sec].Quantity > 0) and (weight == 0)
cond2 = (self.Portfolio[sec].Quantity == 0) and (weight > 0)
if cond1 or cond2:
self.SetHoldings(sec, weight)
The same should be applied in second if statement.
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.
Mateusz Pulka
Sorry some kind of formatting problem. Generally the for loop should be move inside the if statement as Tien Duy Vo pointed out when algo starts and be_in paramter is true we will buy long and short at the same time.
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
I'm sorry, I don't know Python. I don't see any difference between your code blocks. Is this mistake resulting in inflated results due to margin usage?
Edit: Didn't see your last response. I will wait for a Python coder to update it. I only know C#.
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.
Mateusz Pulka
Sorry for formating problem. The code should look like this:
https://pastebin.com/cnKm9BVYinstead of this:
https://pastebin.com/pPcGFKR0The 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 the issue pointed out by Gpw radar and Tien Duy Vo only occurs if "be_in = true" at the time of start. I don't see any issues with my order when ran from 2008 to 2020. It would seem that the results for mine are correct. I was worried that the results were invalid.
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.
Tien Duy Vo
gpw radar : Thanks for pointing out. Great job. Unfortunately, the backtesting framework on Quantconnect seems to be much slower than on Quantopian. It is quite difficult to debug that way.
There is another issue related to this part of the code:
self.Schedule.On(
self.DateRules.EveryDay(),
self.TimeRules.AfterMarketOpen('SPY', 120),
self.rebalance_when_out_of_the_market
)
self.Schedule.On(
self.DateRules.WeekEnd(),
self.TimeRules.AfterMarketOpen('SPY', 120),
self.rebalance_when_in_the_market
)
Here, the algo is firing the two functions rebalance_when_out_of_the_market and self.rebalance_when_in_the_market, which is responsible to get "in" and "out". However, you can see that one is started every day and the other one at weekend. I wonder, whether this is done on purpose.
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.
Mateusz Pulka
You are right Nathan Swenson it only occures once at the begining when you start algo and when flag in is set to true. For example check out your orders when you set the star date at 1st January 2018. It is not a big deal but worh to fix it.
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.
Mark hatlan
Great work! This market timer does a really nice job.
However TQQQ does do way better than SPXL. SPXL starts on 11/03/2008. UPRO starts on 6/28/2009. TQQQ starts on 2/7/2010. When switching the start date to 2/7/2010 here are the returns:
SPXL +7,552%
UPRO +7,180%
TQQQ +19,859%
I think SPXL only looked better because of the earlier first trade date.
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
Thanks Mark! Good point!
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.
Aalap Sharma
Relativily new to all this and I would really like to know are these returns realistic in the real markets? Has anyone deployed such algos to the market and seen healthy returns. Just curious...
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
Aalap, I just turned the algo on and entered TMF. We shall see soon enough. I'm going very aggressive with leveraged products. It should be very realistic as it only trades about 10 times a year with trades just 2 hours after RTH market open. Algo is entirely in treasury bonds at the moment.
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.
Aalap Sharma
Cool! I was thinking the same :)
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.
Mateusz Pulka
Hi Guys,
I have added some improvments to the code and please noticed the result. So first algo has improvment in terms of open the trade at the begning of the cycle when we start the algo. The return for 2010-2020 is: 16 772%
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.
Mateusz Pulka
The second run contains the following improvement. I just want to generate a signal about in/out just right after the open market. But open position just in the same way as the original algo. What is surprising the result is 21 011%. I am a little bit surprised about this, to be honest. Any idea where this difference comes from?
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
Gpw Radar, I see it takes more trades when you check signal at open. Perhaps it's due to increased volatility at open which is more likely to trigger needed deviation. The 2 hours after close is actually European market close for which there is often a ramp up in index price. I had thought about messing around with that as even 5 minutes makes a difference in that period. I know 2hrs and 5 min is significant. The timing of entry vs exit and asset type all matter. Bonds seem affected more at open, mid day, and close, while 2hr after open seems more significant for equities. Lots of opportunity for improvement I believe with timing alone.
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.
Mateusz Pulka
please notice that I only generate a signal in/out 1min after the market is open and then after 120min I open the position base on the signal. I have noticed that as I used IB with the cash parameter some of the orders were not filled (lack of money) and the order was later sometimes even 5 days later.
Peter Guenther Have you tried to use futures instead of etf to generate the in/out signal?
https://finance.yahoo.com/commodities?.tsrc=fin-srch
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
Gpw, perhaps the delayed order in due to transaction order. I often see that the buy order is executed before the sell order for prior holdings. This probably needs to be fixed.
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!