Back to Community
How to create the NASDAQ100 and the S&P500 universe etc?

My codes look as follow. But I am not sure if they are correct.

And I want to know how to set the Russel1000 and Russel2000?

The first is the NASDAQ100 universe.

Clone Algorithm
7
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
"""
This is a template algorithm on Quantopian for you to adapt and fill in.
From: https://www.quantopian.com/tutorials/pipeline#lesson8
"""
import quantopian.algorithm as algo
from quantopian.pipeline import Pipeline
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.filters import QTradableStocksUS
from quantopian.pipeline.data import Fundamentals
from quantopian.pipeline.classifiers.fundamentals import Sector
from quantopian.pipeline.factors import AverageDollarVolume

def initialize(context):
    """
    Called once at the start of the algorithm.
    """
    # Rebalance every day, 1 hour after market open.
    algo.schedule_function(
        rebalance,
        algo.date_rules.every_day(),
        algo.time_rules.market_open(hours=1),
    )

    # Record tracking variables at the end of each day.
    algo.schedule_function(
        record_vars,
        algo.date_rules.every_day(),
        algo.time_rules.market_close(),
    )

    # Create our dynamic stock selector.
    algo.attach_pipeline(make_pipeline(), 'pipeline')

def make_pipeline():
    """
    A function to create our dynamic stock selector (pipeline). Documentation
    on pipeline can be found here:
    https://www.quantopian.com/help#pipeline-title
    """
    
    exchange = Fundamentals.exchange_id.latest
    nyse_filter = exchange.eq('NAS')

    morningstar_sector = Sector()

    dollar_volume = AverageDollarVolume(window_length=5)
    
    return Pipeline(
        columns={
            'exchange': exchange,
            'sector_code': morningstar_sector,
#            'dollar_volume_decile': dollar_volume_decile,
            'dollar_volume': dollar_volume,
        },
        screen=(nyse_filter)
    )    

def before_trading_start(context, data):
    """
    Called every day before market open.
    """
    context.output = algo.pipeline_output('pipeline').sort_values('dollar_volume', ascending=False).dropna().head(100)
#    context.output = algo.pipeline_output('pipeline').dropna()
    
    log.info("Output:%d\n%s:" %(len(context.output), context.output))

    # These are the securities that we are interested in trading each day.
    context.security_list = context.output.index


def rebalance(context, data):
    """
    Execute orders according to our schedule_function() timing.
    """
    pass


def record_vars(context, data):
    """
    Plot variables at the end of each day.
    """
    pass


def handle_data(context, data):
    """
    Called every minute.
    """
    pass
There was a runtime error.
3 responses

And here is the universe for S&P500.

Clone Algorithm
7
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
"""
This is a template algorithm on Quantopian for you to adapt and fill in.
From: https://www.quantopian.com/tutorials/pipeline#lesson8
"""
import quantopian.algorithm as algo
from quantopian.pipeline import Pipeline
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.filters import QTradableStocksUS
from quantopian.pipeline.data import Fundamentals
from quantopian.pipeline.classifiers.fundamentals import Sector
from quantopian.pipeline.factors import AverageDollarVolume

def initialize(context):
    """
    Called once at the start of the algorithm.
    """
    # Rebalance every day, 1 hour after market open.
    algo.schedule_function(
        rebalance,
        algo.date_rules.every_day(),
        algo.time_rules.market_open(hours=1),
    )

    # Record tracking variables at the end of each day.
    algo.schedule_function(
        record_vars,
        algo.date_rules.every_day(),
        algo.time_rules.market_close(),
    )

    # Create our dynamic stock selector.
    algo.attach_pipeline(make_pipeline(), 'pipeline')

def make_pipeline():
    """
    A function to create our dynamic stock selector (pipeline). Documentation
    on pipeline can be found here:
    https://www.quantopian.com/help#pipeline-title
    """
    
    exchange = Fundamentals.exchange_id.latest
    nyse_filter = exchange.eq('NYS')

    morningstar_sector = Sector()

    dollar_volume = AverageDollarVolume(window_length=5)
    
    return Pipeline(
        columns={
            'exchange': exchange,
            'sector_code': morningstar_sector,
#            'dollar_volume_decile': dollar_volume_decile,
            'dollar_volume': dollar_volume,
        },
        screen=(nyse_filter)
    )    

def before_trading_start(context, data):
    """
    Called every day before market open.
    """
    context.output = algo.pipeline_output('pipeline').sort_values('dollar_volume', ascending=False).dropna().head(500)
#    context.output = algo.pipeline_output('pipeline').dropna()
    
    log.info("Output:%d\n%s:" %(len(context.output), context.output))

    # These are the securities that we are interested in trading each day.
    context.security_list = context.output.index


def rebalance(context, data):
    """
    Execute orders according to our schedule_function() timing.
    """
    pass


def record_vars(context, data):
    """
    Plot variables at the end of each day.
    """
    pass


def handle_data(context, data):
    """
    Called every minute.
    """
    pass
There was a runtime error.

Hi Thomas,

I recommend combining a QTradableStocksUS() filter with a market cap filter like MarketCap().top(500) to create trading universes that suit your needs, as opposed to replicating indexes like the S&P500.

Creating a trading universe on the Quantopian platform requires creating and combining pipeline filters. It looks like you understand how to create filters. To learn more about combining filters, check out this lesson of the Pipeline API Tutorial.

Recreating indexes can be difficult. For example, the S&P500's profile page offers a PDF detailing the criteria an asset must pass in order to be included in the index. As of today, that PDF is 41 pages long. Your pipelines look OK technically, and run successfully when I clone them, but you would have to write many more filters (and possibly provide some of your own data) to truly recreate all of the rules that govern a fund like the S&P500.

My recommendation is to augment the QTradableStocksUS filter to suit your needs. For example, I have attached a notebook with a pipeline that outputs the close price of the 500 equities with the highest market cap values which pass the QTradableStocksUS filter. The stocks that pass these two filters (large market cap, passes QTU) are mostly the same stocks that make up the S&P500.

Lastly, to enter the Quantopian Contest, or be considered for funding, an algorithm must use the QTradableStocksUS filter.

I hope this helps you and others who are trying to create trading universes. Please let us know if you have any further questions.

Loading notebook preview...
Notebook previews are currently unavailable.
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 Cal,

Many thanks!

How about my NAS100 universe? Is it OK? Or should I use the QTradableStocksUS() as filter as well?