Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Weekly Rotation S&P 500

Simple momentum algorithm. Got the rules from a book by Laurens Bensdorp.

Objective:
- Trade long only, a big index, mainly blue-chip stocks that rise in value.
- Execute trades once a week. Catch big trending stocks, expecting they will continue to rise.
- Outperform the S&P 500 with lower drawdowns in bear markets.

Trading universe:
- S&P 500. As Quantopian does not provide it directly, found something similar in the forums.

Filters:
- Minimum Average Volume of the last 20 days is above 1.000.000 shares (ensure liquidity).
- Minimum price is 1 USD.

Position Sizing:
- Maximum 10 positions, dividing the equity by 10 to get the position size.

Buys:
1. Trade the first day of the week.
2. Last close of the SPY is above the 200 daily SMA band (with a -2% buffer). In case it goes below, we go back to cash.
3. Filter by the 3 day RSI of the stock. If it's below 50 it's a candidate (ensure not overbought).
4. Select the 10 stocks with the highest ROC-percentage increase over the last two hundred (momentum).

Sells:
1. Trade the first day of the week.
2. We rotate when one of the stocks of our portfolio is not in the top 10 ROC.
3. We exit all positions when the SPY is below the 200 daily SMA band (with a -2% buffer).

Notes:
- The algorithm tracks the available cash trying to avoid spending more than we have.
- The rebalance is split in 2 functions with an hour gap. This is just to "ensure" sell orders are filled before we start buying.
- The results are not as good as the backtests shown in the book, I might be missing something. However it does beat the S&P 500.

Other algorithms:
- Mean-Reversion Long - For Bold Contrarians
- Mean-Reversion Short - Money in bad markets

7 responses

Hi Mark,

Thanks for sharing. It is a good simple algorithm.
I have noted that one mistake that spy close is one day delayed.
The is_positive_trend function is delayed.
Better put the global variable spy close in the initialize.
It will have better performance.

Hi Vidal,

Thanks for you feedback. I've scheduled the computation of the trend a minute before start trading and got around 10% performance improvement.

Performs better in recent history than I would have expected from a strategy like this. Nearly manages to keep pace with benchmark.

Mark, I saw you posted another new algo to the public. Really amazed that you could get some solid trade ideas from that book that could have quite good performance, I think I should check that book too.
My small change to your algo: sell all positions when spy close below ma100 in daily basis

and another one:

Hi Marc,

Many thanks for sharing the algorithm.
My understanding of the strategy as described in the book is different from your implementation:
1) The strategy only buys stocks that are in the Top 10 of ROC(200) of all stocks in the S&P 500. Your implementation buys stocks that are not in the top 10 because it first filters on RSI(3) < 50 and then creates a top list of the remaining stocks.
2) The strategy does not sell all positions if SPY is below the SMA 200 band. It only sells stocks if they are no longer in the top 10 of strongest stocks by ROC. For the exit, it does not take RSI(3) into account.
Maybe this explains why the results do not match the backtest in the book. I will try to implement these changes.

Hi @Marc/ @Vidal: It would be great if you guys or someone from the community may add some commentary on the Source code, so noobs like me can understand what is going on in code.

Thanks in advance