Back to Community
List the Stocks obtained from Set Universe

Hi,

How would I go about listing all the stocks and stock IDs in a universe summoned by:

set_universe(universe.DollarVolumeUniverse(99.9, 100.0))

Any help is greatly appreciated!

Rich

4 responses

Hello Rich,

This works:

def initialize(context):  
    context.stocks = []  
    set_universe(universe.DollarVolumeUniverse(floor_percentile=20, ceiling_percentile=20.5))  
def handle_data(context, data):  
    context.stocks = [str(sid.symbol) for sid in data]  
    print  ', '.join(context.stocks)  

However, you might run into logging limits if your list gets too long.

Grant

Clone Algorithm
36
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 5320caf117be1206c8cc6054
This backtest was created using an older version of the backtester. Please re-run this backtest to see results using the latest backtester. Learn more about the recent changes.
There was a runtime error.

thanks grant! yeah, i'm seeing problems using set universe in my testing with dates prior to 2008, so this helps generate some specific sets to test with.

a follow up, it looks like the sec object changes once a security goes from "not yet existing" to "existing" i was reusing the old sec object so i was getting weird errors after the security becomes traded

I'm curious what the errors were.

And when using set_universe(), would like to find an effect like [sid for sid in data] except before leaving initialize() to populate context.stocks.