Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
handle_data with multiple securities - help

Anyone know what I'm doing wrong here. I'm gettin the error in the: "def run_this" section

def initialize(context):  
    context.securities = [sid(24), sid(5061)]  
    for i in range(15, 390, 1):    # start, stop, every n minutes, where n is 1 in this case  
        schedule_function(run_this, date_rules.every_day(), time_rules.market_open(minutes=i))  


def run_this(context, data):

    for s in context.securities:  
        hist = data.history(context.securities, 'volume', 11, '1m')  
        vol_10 = hist[0:9].sum()  
        vol_1 =  hist[10]*3.5  

It's looking at previous history bars and running calculations, but i get an error.

Thanks!

1 response

If you really want to loop through each security then don't use your context.securities list as an input to data.history. Simply use the security. That should fix your error.

    hist = data.history(s, 'volume', 11, '1m')  

A more 'pythonic' way to do this however is to nix the loop.

    hist = data.history(context.securities, 'volume', 11, '1m')  
    vol_10 = hist.ix[0:9].sum()  
    vol_1 =  hist.ix[10]*3.5 

In this case vol_10 and vol_1 are now series (rather than simple numbers as in the original example). The securities in context.securities are the indexes for each series.