Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Quick and dirty momentum strategy

There are many proponents of momentum investing. A quick browse through Quantopedia suggests that momentum strategies have very good risk adjusted returns for such a simple strategy.

There are other strategies such as GEM as outlined by Antonacci, and sector rotation.

They are all pretty much the same thing. Each month, see which top x number of etfs did best over the past year. Buy the top x, liquidate everything else. Hold for a month. Repeat. Pretty simple. All of the momentum strategies outlined above differ only by the etfs that they rotate.

So I present to you my very humble code for those who are still learning the system. Again, I'm not a programmer or a professional investor, but I didn't see any momentum strategy template and I thought this might help some people out. In future code, I will use dataframes instead of lists of lists.

Also, my momentum strategy also employs a 200 day moving average trend following rule. Momentum is surprisingly uninspiring without it.

Please let me know if there are any bugs in my code or if you find it useful.

17 responses

That's a brilliant outperformance, despite the drawdowns. It means that you are rotating sectors faster than most investors. But you get in only after they have started to. You also get out before they thinkink of getting out. Lastly, it means outperformance lasts longer than a month. Is that a fair assessment of the idea behind the algo?

To be honest, the idea behind the algo is to test the outrageous claims of outperformance I have seen in the book "Dual momentum" by Antonacci. I have no idea why it works. In efficient market theory, you shouldn't be able to predict future performance based on past performance, nor time an exit. The author describes reasons, but they sound like excuses and don't seem provable.

I recently found this website. Some experience with python, but none with trading. I was thinking of starting off with a momentum strategy because they seem relatively simple, and this is very interesting and helpful. I'll keep an eye on this thread. Also may check out that book you mentioned. Thanks!
Also: why are some pieces of code commented out in your code?

Thanks for sharing!

Very helpful for people like me who are new to programming

Nice algorithm. I like the 200 ma part as well. This momentum strategy only involves rotating around single security from the list. Have you ever considered constructing a portfolio of long-short through sorting?

Alex:
I'm sloppy. The only part that needs to be changed is the list of etfs. I have sp500 sector momentum, gem, asset allocation momentum all in there and commented out the lists not used. Maybe I should clean up my code.

Banks: from what I read on alpha architect, shorting momentum stocks isn't a great idea. They seem very against most long short strategies. I read a recent article about ranking momentum based on skewness but I don't know how they calculate skewness.

Please let me know if you have a good short strategy. Nothing I have done so far outperforms the simple strategy of exiting the market when mavg of sp500 signals an out.

Haven't tested. I will develop something soon based off of this and let you know my results. The main motivation is from a paper developing risk factor portfolios that should 1) generate premia and 2) uncorrelated with the market. With #2 I think thats where the idea of shorting came from where stocks are longed for positive last year performance and shorted for negative last year performance.

Lastly, is there a specific reason why we target ETFS (I've noticed many algos aside from just momentum strats do this) rather than screen for stocks at each time period??

There is an issue with momentum in that it was a strategy that yielded returns of 5-10% above benchmarks in the past but since 2008 (?), no longer carries any premium

I saw a neat graph that showed this, but I can't find the source right now. I can only find this post that alludes to the study:
http://www.moneygeek.ca/weblog/2014/12/01/reasons-avoid-momentum-based-etfs-wxm/

Alpha architect loves momentum stock-picking, but I'm surprised at how much they love it since it carries a much higher standard deviation and larger drawdown.
http://blog.alphaarchitect.com/2015/03/26/the-best-way-to-combine-value-and-momentum-investing-strategies/

In other words, momentum has a lower sharpe ratio. Why not just borrow to increase returns and get lower risk with value stocks?

Cross-sectional momentum (ie. stock-picking momentum) is difficult with the Quantopian platform. You can't filter the universe by price change percentage. You can filter down to 200 stocks and find the best momentum stocks from that universe, but it's not very representative.

By the way, academic cross-sectional momentum looks at past 12 months BUT not including the last month. Does anyone know what this means, ignoring the last month? and why this is done?

That's sometimes called a "skip month" in the literature and its done because there are known effects at the one month level that are thought to negatively impact the momentum effect. So initiating the trade immediately after measuring the top past winners and losers may include some confounding effect if traded immediately, so you wait a month. Industries in particular have well known reverse momentum effects at one month formation level so last months winners reverse and vice versa for one month losers (this is counter to the momentum effect, see Moskowitz paper on industry momentum). The skip month lets those other effects play out before entering the trade.

Thanks Francis! I have much to learn.

In the finance literature, this is one of the more important papers - its the Moskowitz Industry Momentum discovery:
http://onlinelibrary.wiley.com/doi/10.1111/0022-1082.00146/abstract

If you Google "Jagadeesh and Titman" you will find papers dating from 1993. These guys are the fathers of momentum. Its my understanding that Moskowitz et al inspired the skip month with their findings. I've been trading momentum since about 2003 myself though I'm primarily an options trader. It does work as an options trade (trade the call options on the long side only for momentum winners]. Cliff Asness founded a hedge fund that is now one of the world's largest and his central strategy is momentum but with other variables. If you go to the AQR website, he has several papers that he co-authored and great data that's free to download. Momentum is a global and cross-asset class phenomenon.

Recreated Fred Piards 'triple switch' system using your script.

I noticed that switching to minute mode had a big impact on back-test results. I thought this was a bit surprising because the algo only trades once a month and all three ETFs have decent liquidity.

On line 34: if pct_change ==pct_change:
Is this to filter out NaN?

See if this helps the daily vs minute puzzle ...

Yeah it is to remove nan - saw it in another edit of your script

I had another look at the 'triple switch' strategy today as it seemed too good to be true....
Changing benchmark to MDY revealed that Freddy has been guilty of some overfitting!

Really interesting!

Could someone explain the following fact to me?

If I clone this algorithm I'm getting a different result than that case I create a new file and copy it. In the second case the model does not outperform the benchmark.

What would be the reason? Different server time? Bug?