Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Setting Universe in Initialize

Hello,

I apologize if this is a simple question but I looked through the help docs and searched the forums and couldn't really find anything analogous, so if anyone could point me in a direction for more information or answer here that would be helpful.

Is there a way to set the universe of stocks in initialize with fundamental data? I tried using the get_fundamentals with a SQLAlchemy query but it gave me an error that that function can only be used in before_trading_starts. I am pretty new to this coding environment so I am trying to set my universe in initialize rather than before_trading_starts because that is called every bar and will change what stocks are in my universe (after updating to incorporate the stocks returned from the get_fundamentals call) while I really just want to monitor a certain collection of stocks with properties filtered from a SQL like query. Basically, I believe certain qualities will fit my algorithm but I want to monitor only a certain number of stocks without that changing, but I don't know them before (aka can't hardcode them in in initialize).

Thanks for any help/direction

1 response

Hello Robert,

You can restrict the call to update_universe within before_trading_start with a flag that only allows the universe to be updated once.

Grant

def initialize(context):  
    context.universe_updated = False

def before_trading_start(context, data):  
    if not context.universe_updated:  
        # update universe code goes here  
        print 'universe was updated'  
    context.universe_updated = True

def handle_data(context, data):  
    order(sid(24), 50)