Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Struggling to get basics to work

Hi,

I'm struggling to get the following to work... Orders simply won't execute.

# CODE  
# start 02/15/2013  
import talib as ta

def initialize(context):  
    set_benchmark(sid(8554))  
    context.gold = sid(26981)  
    context.silver = sid(28368)  
    set_commission(commission.PerShare(cost=0.005, min_trade_cost=1.00))

    schedule_function(rebalance_component5, date_rules.every_day(), time_rules.market_close(minutes = 15))  
    schedule_function(record_vars, date_rules.every_day(), time_rules.market_close())  

# Long component  
def rebalance_component5(context, data):  
    component5 = [context.gold, context.silver]  
    for stock in component5:  
        price = data.history(stock, "price", 251, '1d')  
        sma251 = ta.MA(price, timeperiod = 251, matype = 0)  
        if price[-1] > sma251[-1] and data.can_trade(stock) and context.portfolio.positions[stock].amount == 0:  
            order_target_percent(stock, (1/len(component5)))  
            print('long' + str(stock))  
        elif price[-1] < sma251[-1] and data.can_trade(stock) and context.portfolio.positions[stock].amount > 0:  
            print('False')  
            order_target_percent(stock, 0)

def record_vars(context, data):  
    record(leverage = context.account.leverage)  
2 responses

Try line 21 this way:

order_target_percent(stock, (1.0/len(component5)))  

This worked. Thank you!