Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Iterating over a list of securities, having trouble with sid(id)

I have a few lines which are variations on follow:

    for index in range(len(seclist)):  
        curr_price[index] = data.current(sid(seclist[index]),'price')  

Where seclist is a constant array of the id numbers of the securities I am looking at.

I am getting a build error "The sid(id) method takes one parameter."

What am I doing wrong?

1 response

You have to put a literal in sid (e.g. sid(24)); it won't take a variable (e.g. sid(n)) (a mystery why not, but it just ain't gonna take it).

Try something like this:

curr_price = {}  
for stock in context.stocks:  
    curr_price[stock] = data.current(stock,'price')  

See https://www.quantopian.com/help#api-data-current for another way. You can pass a list of stocks to data.current and get back everything in one shot! No loopty loop. Not sure if it is any more efficient, though, if there's just a loop under the hood, and no actual vectorization.