Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
filtering universe of stocks by market cap and price in zipline

I'm trying to filter the universe of stocks I want to test my algorithm with using the code below. I'm trying to filter for an initial subset of all available stocks in the bundle on 2015-12-31, for stocks with average market capitalization over 50M for past 10 days, and with a price under $10. I only want to do this once initially to get the set of stocks. Then run the algorithm with the same stocks. I think the code below is refiltering the stocks each period. so I'm wondering how I can do it in the initialize function, instead of the handle_data function. I'm running this in zipline. any tips greatly appreciated.

def initialize(context):  
    context.set_commission(commission.PerShare(cost=0.0, min_trade_cost=0))

    date_time_str = '2015-12-31'

    date_time_obj = datetime. strptime(date_time_str, '%Y-%m-%d')  
    bundle = load('quandl', os.environ, date_time_obj)

    asset_symbols = list(set(str(asset.symbol)  
              for asset in bundle.asset_finder.retrieve_all(  

    assets = bundle.asset_finder.lookup_symbols(asset_symbols, as_of_date=None)  

    sids = [sid(asset.sid) for asset in assets]  

    context.assets =sids  

    context.window = 252  
    context.rebalance_period = 1  
    context.time = 0  

 def handle_data(context, data):  
     cleaned_weights = []  
     if context.time == 0 or (context.time % context.rebalance_period == 0):  
         # extract prices  
         prices_df1 = data.history(context.assets, fields='price',  
                               bar_count=context.window + 1, frequency='1d')  
         market_cap_df=data.history(context.assets, fields='volume',  
                               bar_count=context.window + 1, frequency='1d')\  
        *data.history(context.assets, fields='price',  
                       bar_count=context.window + 1, frequency='1d')  
 #         print(indx_df.shape)

         # filtering based on market capitalization  


         # filtering based on initial price less than capital base  
         # minimum of 20 stocks for diversification