Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Simple Strategy on Consumer Staples SPDR ETF (XLP)

I am quite new on Quantopian and I wanted to implement a simple strategy on the Consumer Staples SPDR ETF (XLP). The idea comes from this website. I tried to implement this strategy on Quantopian, but on the backtester I only get very poor cumulative performance. Maybe I am doing something wrong and someone can help me. Have a look at the attached Algorithm. Thanks.

2 responses

As a general tip:
Use the record function for variables that are important to track, in this case it would be your trading indicators.
The algo now records the indicators as the backtest processes. It shows open to close (O2C) and close to close (C2C) in bps. It is great for tracking long/short positions, leverage, trading indicators, etc...

I also added a few print statements that show your variables in the log.
When I'm building an algo and I want to follow it as it steps through, I do the build over a short time span, a couple of weeks, and print the variables to the log, to make sure it is doing what I thought. It's a simple way to watch what is happening, if you don't know how to use the debugging functionality.

As for the strategy:
There are very few days when from close to open the price falls more than 10bps, so you're algo only traded a couple of times over the 13 years.
If you adjust to any day it is down from close to open, it trades a bit more frequently, but still not much. Even the article shows that it trades on about 8% of trading days. Then holding a long position for one day and exiting at close doesn't result in much activity/return.

It is a simple counter trend strategy, good for learning how to build an algo, not so good for actual trading (in my opinion).
Hope these couple of little tips help you make progress.

Best

Hi Cory, thanks a lot for your help. I have also simulated the strategy in the attached Notebook. There, the results looks much better, but it ignores trading costs and assumes buy/sell at open/close price. Look at the close/open prices in the Notebook. There seem to bee quite a lot of trading opportunities. Do you know how to run the backtester in Notebook? There is a function in zipline called run_algorithm(...) but I do not know exactly how to insert it in my Notebook.