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

Here's a quickie algo that does a daily re-allocation of S&P 500 sector ETF's (http://www.sectorspdr.com/sectorspdr/) based on a z-score comparison.

A couple notes:

# for development, turn off commissions & slippage  
    set_commission(commission.PerTrade(cost=0.0))  
    set_slippage(slippage.FixedSlippage(spread=0.00))  

Also, note that orders are submitted at the close (and if the market closes early, no orders are submitted). This is inconsistent with the current Quantopian/IB practice of cancelling all orders at the end of the day. So, for live trading, the algo would need to be re-jiggered.

Grant

10 responses

Hi Anony,

Sorry for any confusion. When I get the chance, I'll have a look at your feedback and reply. I agree that "recycling" variable names may be a bad practice.

Grant

Anony,

Here's a version that hopefully is easier to interpret. Note that I switched to:

allocation = allocation.clip(lower=0)  

Here's a discussion of various techniques to do the same thing (including the one I used in the original post):

http://stackoverflow.com/questions/3391843/how-to-transform-negative-elements-to-zero-without-a-loop

Grant

i personally dislike the dataframey/matrixy code prevelent in most of the quantopian stuff I see around, but yeah, I'm a c# / typescript guy so....

Anyway, as for the actual strategy here: is this much different from the results from the quantopian 9 sector rebalance? maybe you can explain/contrast a bit grant?

Here's a longer run. Earlier today, I ran (and thought I'd posted) a run with equal weights, re-balanced daily. It basically followed the SPY returns.

I think that this is basically a kind of momentum algo, since it weights by the degree to which an S&P 500 sector ETF is outpacing SPY.

Not very exciting yet, and unrealistic due to the zero commission setting, but it is a start.

Hi, Grant,
I am new in Quantopian and learning Python now.
Daily balance in SPDR sector ETF allocation is too many trades for me. I am working on a monthly balance among top 3 sectors ETF by 1 month performance. Rebalance on each month first available trading day. Do you have similar algorithm can share with me as example to make my start easier? Thanks.
Jerry Liou

Hi,
Maybe the Quantopians could add a timestamp indicating market closure, or import csv?
J,

There is a "tradingcalendar" class in zipline that you can import, then use that information to determine whether you are in the first/last trading day of the month.

First do this at the top of your code:

import zipline.utils as zlu  

To trade on the last day of the month, you need to know what the next trading day is:

    tomorrow = zlu.tradingcalendar.trading_days[\  
        zlu.tradingcalendar.trading_days.get_loc(get_datetime().date()) + 1\  
    ].date()  

To trade on the first trading day, just do the opposite:

    yesterday = zlu.tradingcalendar.trading_days[\  
        zlu.tradingcalendar.trading_days.get_loc(get_datetime().date()) - 1\  
    ].date()

Then just compare that to the current date provided by get_datetime().date().

I'm attaching a backtest that I did using the "trade once per month" code in my above post.

In general, I don't like periodic re-balancing. I think a rebalance should be triggered based on some other metric, and rate-limited if necessary. If you don't want to trade more than monthly, I think that is OK, but you shouldn't force yourself to trade monthly either. Think about it - Quantopian lets us trade minutely, but few of us would feel the need to trade every single minute. The same logic applies monthly IMHO.

Maybe I'll play with this algo some more and try to come up with an automatic trade trigger.

Hello Jerry Liou,

I took a week-long summer hiatus. Did you get sufficient guidance from the posts above? Or are you still trying to sort out an algorithm?

Grant

Thanks to Ray suggestion. I am able to start now.