Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Looping through stocks universe inside initialize function

I want to loop through the stocks universe inside the initialize function, I am already looping inside this function using context.stocks = [sid()...]
but I want to benefit from the set_universe function, I have to do this inside initialize because I am instantiating class objects

def initialize(context):

    set_universe(universe.DollarVolumeUniverse(floor_percentile=90, ceiling_percentile=90.2)

    context.stocks = [sid(24),sid(114)]   # it is bigger list than this # I want to replace this with the set_universe  
    context.algos = [class(stock) for stock in context.stocks]

Data variable is not available in initialize scope
schedule function is not an option because each instance is holding class variables, so it must be instantiating once only
Thank you

7 responses

Adham, could you explain more about you're trying to do? Why do you want to create a static list of sids from set_universe?

The list of stocks in set_universe will change each quarter, as stocks fall in/out of this percentile. However, your list of sids will be static after it's initialized.

If you want to see all the stocks that are in set_universe at a given time, you can create a for stock in data loop within handle_data().

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Hi Alisa,

Actually I am using the prototype from this page:https://www.quantopian.com/posts/abstracting-an-algorithm-from-one-stock-to-many
which solves a lot of problems I am having, but using such OOP approach would not allow me to benefit from the set_universe function because I have to declare all class instances at the initialize function level because each class got its own instance variables (self variables),

I just wanted to benefit from set_universe with this approach that's all. and you are right the list of stocks will be static but that is not a big problem for me.

Maybe I'll just print them as you suggested, and hard code them instead.

It would be great though if you add a 'once (one time only )' in the Date Rules parameter of the schedule function.
it will allow to experiment with different sets of the set_universe and choose the best set.

Idea Failed
Can't print the stocks,
2013-01-04null:nullWARNLogging limit exceeded; some messages discarded

Hi Adham,

Can you further elaborate on what it is that you are trying to do with set_universe? I see what David did in the post that you linked to but I'm not sure exactly what you are trying to do that is different!

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

I just can't use set_universe with that linked approach! in which I always use David's approach in my algos to solve alot of my problems