Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Swing trade between two ETFs

I am very new to Quantopian. I just wanted to ask is there a way of swing trading between the two ETFs 32270 and 8554 by using the weights that are attached to them? Basically changing weights depending on the direction of the market.

# Put any initialization logic here.  The context object will be passed to  
# the other methods in your algorithm.  
def rename_col(df):  
    df = df.rename(columns={'Adj Close': 'price'})  
    df = df.fillna(method='ffill')  
    df = df[['price', 'sid']]  
    return df

def initialize(context):  
    yahoo_vix_url = "http://ichart.finance.yahoo.com/table.csv?s=%5EVIX&d=0&e=22&f=2014&g=d&a=0&b=2&c=1990&ignore=.csv"  
    context.target_notional = context.portfolio.cash  
    context.last_dt = None  
    fetch_csv(yahoo_vix_url, date_column='Date', date_format='%Y-%m-%d', symbol='vix', usecols=['Adj Close'], post_func=rename_col)  
    context.a_sid = sid(32270)  
    context.b_sid = sid(8554)  
    context.b_sid = sid(42950)  
    context.a_weight = 3.0  
    context.b_weight = 15.0  
    set_commission(commission.PerShare(cost=0.0015))  
    set_slippage(slippage.FixedSlippage(spread=0.01))  
def handle_data(context, data):  
    vix_data = data['vix']  
    record(vix=vix_data['price'])  
    dt = vix_data.datetime  
    if vix_data.mavg(25) > 30:  
        order_target_percent(context.a_sid, 0.0)  
        order_target_percent(context.b_sid, 0.0)  
    elif context.last_dt is None or context.last_dt.month != dt.month:  
        order_target_percent(context.a_sid, context.a_weight)  
        order_target_percent(context.b_sid, context.b_weight)  
    context.last_dt = dt  

1 response

Hey Paul,

I made a few changes to your code and attached. To manipulate the data from fetch_csv, use the pandas function rolling_mean in the pre-function.

Ryan