Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Bond Etf's vs Spy Dollar neutral strategy

This algo is dollar neutral buying underperforming bond etf's relative to their averages and selling outperforming bond etf's relative to their averages and hedging with spy. Exits when price meets average and each trade value is 2.5% of portfolio cash value. Please add suggestions.

import pytz  
from itertools import imap  
import numpy as np  
import math

def initialize(context):  
    context.sec=[ sid(34846),sid(23881),sid(34831),sid(33147),sid(35175),sid(35927),sid(34793),  
                 sid(33541),sid(34830),sid(32984),sid(33649)]  
    context.spy=sid(8554)  
    context.max_notional = 1000000.1  
    context.min_notional = -1000000.0  
    set_commission(commission.PerShare(cost=.0035))  


def handle_data(context, data): 


    sp=data[context.spy].close_price  
    hi=data[context.spy].high  
    lo=data[context.spy].low  
    vwaps=data[context.spy].vwap(2)  
    mavgs=data[context.spy].mavg(20)  
    current_share = context.portfolio.positions[context.spy].amount  
    cash=context.portfolio.positions_value  
    pam=(sp+hi+lo+vwaps+mavgs)/5  
    c=context.portfolio.cash

    for stock in context.sec:  
        price=data[stock].close_price  
        mavg=data[stock].mavg(20)  
        vwap=data[stock].vwap(5)  
        high=data[stock].high  
        low=data[stock].low  
        vwap=data[stock].vwap(2)  
        buy =(context.portfolio.cash * .025)/price  
        buys =(context.portfolio.cash * .025)/sp  
        pamo=(price+high+low+vwap+mavg)/5

        current_shares = context.portfolio.positions[stock].amount  
        do=cash+c

        if price>pamo :  
            order(stock,-buy)  
            order(sid(8554),buys)  


        if price<pamo :  
            order(stock,buy)  
            order(sid(8554),-buys)  
        if price==pamo:  
            order(stock,-1*current_shares)  
            order(sid(8554),-1*current_share)  
            if sp==pam:  
                order(stock,-1*current_shares)  
                order(sid(8554),-1*current_share)  
                if do <1000000:  
                     order(stock,-1*current_shares)  
                     order(sid(8554),-1*current_share)  
6 responses

Good trade. Technically speaking dollar neutral is very different from market neutral, but this strategy is actually a one-direction macro bet so it doesn't matter. (a free ride on Fed's QE). My (not-so-much-educated) hunch is it would do better if you neutralize your momentum exposure during 2010 and 2011.

A few revisions

@Roy Wei You are right as market neutral would have to factor the beta/volatility portion but I'm assuming low beta from the bond-equity nature of the trade and please post a revision.

The results are quite impressive, but the risk of the strategy seems really big. Looking at your backtest results, essentially you shorted a lot of bonds (-17.1M) to long a huge amount of SPY(33.1M) due to the momentum of your strategy. If SPY crashes all in a sudden right now in Nov. 2013, you would probably loose tens of millions. I would suggest to constrain the level of hedging you are using.

IVV - Core S&P 500 ETF VS Bond ETF Dollar neutral strategy

Pamo,

I constrained the max amount you can borrow to be -1M and re-ran your algo. Still the results are quite good: > 260% return. Can you elaborate a little more on how you think about your strategy? Basically you think that bond and s&p500 should go toward the opposite direction, so you long bond and short s&p500 when bond price is high, and vice versa, is that how it works? Thanks.

Another thing to think about is how to avoid the big dip in Feb. 2009. Your algo bet that bond price will be mean reverting. But once bond price shows momentum, your strategy tends to lose a lot of money.

-Huapu

@Huapu Pan

Thanks for the input. The strategy involves a diverse set of bond asset classes trading them around their mean average as set by the Pamo mean price. The bonds cover almost all bond asset classes and the equity position is there to create a dollar neutral position.