Here is Trend0. It is a trend following algo that goes long when the stock it's trading is trending up, and short when trending down. It is neutral and balanced in this respect so the gains are not simply dependent on the gains of the stock it is trading.
.ekz.
Thanks for sharing, It's posts like this that encourage conversation and the exchange of ideas!
With that said, it's usually helpful to give some meaningful detail to drive the conversation.
Where you say “It goes long when the stock it's trading is trending up” some meaningful detail would be some description of how you detect this. How do you determine the stock is trending up?
Right now, your description doesn't say much --it's just as helpful saying “the algo buys when it's time to buy” :-)
Warren Harding
Hi .eke.
Thanks for the feedback. I use my 'Trend' indicator. It's completely defined in the code towards the bottom of the script. Not sure if it's been invented independently or if I'm the first to do define the 'Trend' indicator. I'll provide a verbal description here as well.
The algo first obtains an array of the closing prices for the previous 10 hours. It then calculates an array of the changes in price between each adjacent pair of closes in the first array. If that confuses you try reading the ChangesPercent function. I then pass the array of changes to a basic exponential average function. So basically it calculates the average change in price, but gives more relevance to change in price that is more recent in time.
Warren Harding
It then goes long if the calculated trend is over 0, and short if it is under 0.
.ekz.
Aha. Thats great.
I had thought so, but wasn't sure, that your measurement is based on momentum, with a bias for recent momentum.
I'd imagine this would do quite well in a short-term momentum portfolio rebalancing strategy: eg: Everyday, rank the S&P 500 (or Nasdaq 100) stocks based on this momentum metric, pick the top X stocks with sustained / spiked volume, and hold them for the day (or until a stop loss / take profit is reached). Could be worth a try.
I'm going to bookmark this one and tinker with it…. one day… some day… when i have the time :)
Thanks again for sharing!
Gv
Thanks for sharing Warren. This algo only traded AMZN. Is it by design? Doesn't this lead to look back bias?
Warren Harding
Yes it just trades one stock at a time by design. I have multi-stock versions but thought I'ld post a simple example. Yes there is risk of look back bias when you select a stock for it to trade. As soon as you select one backtest over the other in any way you encounter this problem., so it's hard to avoid I try to keep an eye on the number of trades put through, a larger sample set is better. The algo made 1518 trades in the backtest above so there is some hope for it. Whether this approach is rigorous enough to overcome the look back problem I leave to be debated.
Vladimir
Warren Harding,
Thanks for sharing Trend0.
About terminology:
From the code I can see that your Trend0 filter has nothing to do with Exponential
Moving Average or Exponential Smoothing.
These two from Digital Signal Processing view are Infinite Impulse Response Filters.
Your Trend0 filter is Finite Impulse Response Filter.
If you will set
decimal exponent = 0.0m;
You will get Simple Moving Average.
If you will set
decimal exponent = 1.0m;
You will get Weighted Moving Average.
So I would call it Harding Power Weighted Moving Average
and changed decimal exponent to decimal power.
I cannot professionally comment on your code because C# is not my native language.
Correct me if I am wrong.
It would be nice if somebody from QC would port Trend 0 to python.
About performance:
It is pretty good for AMZN
And not so for
GOOGL
AAPL
MSFT
Warren Harding
Ya, it's definitely highly dependent on which stock it is trading. Works ok for TSLA too, I haven't tried it much with lower cap stocks yet.
Warren Harding
Hi Vladimir,
When I hacked up my ExponentialMovingAverage function I built what I had in mind and did not attempt to use the standard equations. I might have named it quite poorly as I do not want to create confusion between my function and the standard exponential moving average if you are getting conflicting results.
Vladimir
Hi Warren,
You have developed a good indicator.
I would even recommend QC to create a build in version of Power Weighted Moving Average as it can be a Simple Moving Average, a Weighted Moving Average and thousands in between and beyond, which can be very helpful.
But by the method of calculation, it is not an Exponential Moving Average, which for each bar takes some part of the value of the previous bar.
Warren Harding
Hi Vladimir,
Thanks for clarifying that. My ExponentialMovingAverage function should be renamed, to alleviate any further confusion. PowerWeightedMovingAverage sounds good.
Derek Melchin
Hi everyone,
We've implemented the Trend0 logic inside a custom python indicator. See the attached backtest for reference.
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.
Warren Harding
Just so you know…
Trend values are balanced about zero, a positive trend suggests that the instrument has been rising, negative is falling.
Trend values of different instruments are directly comparable despite any difference in price between the instruments. So you can easily sort by trend to find the instrument with the highest or lowest trend factor.
Vladimir
Derek Melchin,
Thank you for the "11613 trend0 custom python indicator" that can serve as a basis
for creating on QC other Finite Impulse Response Digital Filters.
In the attached version, I just completely cleaned the code from the "exponent" residues.
Warren Harding
I wrote an algo that builds upon what I've learnt from Trend0. The algo uses universe selection to select a basket of high dollar volume stocks. From that basket the algo selects a smaller basket of stocksToTrade that should perform well given a Trend0 like approach. It trades the basket of stocksToTrade with an approach similar to Trend0. The basket of stocksToTrade is rebuilt at regular intervals throughout the backtest.
This approach eliminates one source of look back bias as universe selection is used instead of handpicking a stock to trade.
Here are some screenshots.
.ekz.
Thanks for sharing.
“The algo selects a smaller basket of stocksToTrade that should perform well given a Trend0 like approach”.
Can you please elaborate a bit here? What type of stocks would perform well with a Trend0 approach?
Warren Harding
Sorry, but I likely won't be releasing full details of my universe selection version of Trend0. I might trade it myself or try to monetize it somehow.
Selecting a basket of stocksToTrade is an interesting problem. I'ld be curious to see other people's approaches to this problem.
.ekz.
That's understandable. The results do look good.
Selecting stocks to trade is indeed an interesting problem with many interesting solutions. I've dabbled in OLPS for short term trades using various metrics like Momentum, Sharpe Ratio, Return/DrawDown, etc. I havent found any consistently profitable results yet, but I'm looking at incorporating volume in a composite performance metric, which should give some positive results.
There's one interesting approach I saw recently, that uses walk forward optimization for portfolio selection (universe selection). In this article he speaks about selecting just 1 stock a day, but I chatted with the author about portfolio selection. It turns out that his algos have multiple portfolio selection methods built into them (ie: selection rules, criteria, metrics etc). Even though the algo only uses one selection method at a time, it constantly assesses all of them, and determines which selection method would have performed best, on average, over recent history. It then switches up and uses that selection method instead for a while.
Definitely an interesting space with lots to explore.
Warren Harding
Thanks .ekz.
I did a lot of stuff with basic mean reversion. There you want your basket of stocksToTrade to vibrate consistently so that the mean reversion will profit. With Trend0 you are looking for stocks that trend, which is a different problem altogether.
.ekz.
I see. For finding trending stocks, I suppose we could select stocks whose price action (ie the time series of a buy+hold) has a high Sortino ratio. ( The metric used for portfolio performance could easily be used here ). That, combined with high relative volume (ie:the stock is trading with more cumulative volume than it usually does) so we know it's a real move.
Thoughts?
Warren Harding
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!