Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How can I create a custom universe?

For example, how can I create a universe that includes securities priced less that $2 and trading 1M+ in volume?

I only see the DollarVolumeUniverse

3 responses

@Elliot, use the the update_universe within the before_trading_starts() method (search the forum and the help.)

You'll be able to fetch a galaxy from the universe of securities that meet your criteria. I'm not familiar with how to get current price out of fundamentals. But you can combine fundies with data[x].close_price to filter your galaxy down.

Like this:

def initialize(context):  
    context.myOwnPrivateIdahoUniverse = []

def handle_data(context, data):  
    context.myOwnPrivateIdahoUniverse = [stock for stock in data if data[stock].close_price <= 2.0]  
    record(SecurityCount = len(context.myOwnPrivateIdahoUniverse))

    if (len(context.myOwnPrivateIdahoUniverse) > 0):  
        if (context.myOwnPrivateIdahoUniverse[-1] in data):  
            record(Price = data[context.myOwnPrivateIdahoUniverse[-1]].close_price)

def before_trading_start(context):  
    f = fundamentals  
    marketCapFundy = get_fundamentals(  
        query(  
            f.valuation.market_cap  
        )  
        .filter(fundamentals.valuation.market_cap > 1000000)  
        .order_by(fundamentals.valuation.market_cap.asc())  
        .limit(200)  
    )  
    #: Update our universe with the list of symbols  
    update_universe(marketCapFundy.columns.values)  

Thanks for the help Market Tech.

Any idea how to get average volume (say, 3 month) and last trading day volume? I don't need market cap right now.

@Elliiot, I believe you're going to be reduced to calculating avg volume on your own. Volume does not appear to be a fundamental metric available through the API noted above.

But it should be easy to do using history.

[Updated] Test code only (prints out first stock's avg vol only).

    volumeDeck = history(64, "1d", "volume").dropna(axis=1)  
    valid = [stock for stock in volumeDeck if stock in data]  
    volumeDeck = volumeDeck[valid]  
    avgVolDeck = volumeDeck.apply(talib.MA, timeperiod=63).dropna()  
    for stock in avgVolDeck:  
        avgVol = avgVolDeck[stock][-1]  
        record(AverageVolume = avgVol)  
        break