Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Set_Universe, Calculate number of sids, Simple Question

Hi, How can i calculate the # of sids from set_universe, in order to define the position weight,
Thanks

4 responses

Try

n=len(get_universe())  

and if get_universe doesn't exist, complain, because there should be a getter for every setter.

For now, you can use data.keys() from within handle_data, but that will only give you those securities in your universe that have traded in the last minute.

Correct me if I am wrong, but can't you use this:

n = 0  
for stock in data:  
    n += 1

Yes, you can. But what good will it do?

You could write and run outside Quantopian:

print """def initialize(context):  
    context.stocks = [  
"""
for n in xrange(1, 8000):  
    print " "*8 + "sid({}),".format(n)  
print """    ]

def handle_data(context, data):  
    pass  
"""

Then paste the output into the Quantopian IDE as a new algorithm and run it. It will throw an error on the first invalid SID. Comment it out and run the code again. Repeat until you have no invalid SIDs.

@Tristan - Sorry, I was answering a different question.

What you wrote could be achieved more simply by

n=len(data.keys())  

but it would omit the securities from your universe that did not trade in the last bar.