Back to Community
Getting started Mean-reversion tutorial, but with dynamic set of securities

Hi everybody,

I am quite newby here. I am trying to make a variant of the Mean-reversion algorithm as posted in the Getting started Lesson 11 .

Does anybody already published a variant of this algorithm with dynamic set of securities, by using pipeline over let say Q1500US.

All I need is to have in intialize(context) something like:



4 responses

Hi Kalin,

This is actually covered in the Pipeline Tutorial. It shows you how to dynamically select a trading universe every day by combining factors, filters and classifiers. By the end, you will construct a Mean Reversion strategy with a dynamic universe.

I hope this helps.


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 Ernesto,
Thank you for your help.
May be I have to be more specific.
Now I have this static list of 5 securities.

 context.security_list = [sid(5061), sid(7792), sid(1941), sid(24556), sid(1746)]  

My algo is doing well with these 5 (rather arbitrary) securities, but I need to work with larger set of securities.

So, I would like to apply the Q1500US filter and to have the dynamic list of these 1500 securities which passed trough.

So which is the function (I suppose some very simple, built-in one) which I have to plug in to the pipeline (I suppose), which will provide me with this list.

Thanks a lot!

Yeah, just use Q1500US as the screen for pipeline. Check out the pipeline tutorial. Actually, the default template for new algorithms already has precisely this functionality. Each day the updated list of securities is placed into context.security_list

This is a template algorithm on Quantopian for you to adapt and fill in.  
from quantopian.algorithm import attach_pipeline, pipeline_output  
from quantopian.pipeline import Pipeline  
from import USEquityPricing  
from quantopian.pipeline.factors import AverageDollarVolume  
from quantopian.pipeline.filters.morningstar import Q1500US  
def initialize(context):  
    Called once at the start of the algorithm.  
    # Rebalance every day, 1 hour after market open.  
    schedule_function(my_rebalance, date_rules.every_day(), time_rules.market_open(hours=1))  
    # Record tracking variables at the end of each day.  
    schedule_function(my_record_vars, date_rules.every_day(), time_rules.market_close())  
    # Create our dynamic stock selector.  
    attach_pipeline(make_pipeline(), 'my_pipeline')  
def make_pipeline():  
    A function to create our dynamic stock selector (pipeline). Documentation on  
    pipeline can be found here:  
    # Base universe set to the Q500US  
    base_universe = Q1500US()

    # Factor of yesterday's close price.  
    yesterday_close = USEquityPricing.close.latest  
    pipe = Pipeline(  
        screen = base_universe,  
        columns = {  
            'close': yesterday_close,  
    return pipe  
def before_trading_start(context, data):  
    Called every day before market open.  
    context.output = pipeline_output('my_pipeline')  
    # These are the securities that we are interested in trading each day.  
    context.security_list = context.output.index  
def my_assign_weights(context, data):  
    Assign weights to securities that we want to order.  
def my_rebalance(context,data):  
    Execute orders according to our schedule_function() timing.  
def my_record_vars(context, data):  
    Plot variables at the end of each day.  
def handle_data(context,data):  
    Called every minute.  

Thanks guys!
My precious algo works now! .. And we are at the next level - it performs worse.

Actually when I used these 5 securities:

 [sid(5061), sid(7792), sid(1941), sid(24556), sid(1746)]  

for the last two years it was providing positive returns well above the market (Beta 0.01, Sharpe 0.71, Returns +30.5%)

Now, with both Q1500US and Q500US (again over the last two years) it is far below the market with negative returns. (-21% returns, Beta -0.01).

I use
data.history(context.security_list, 'price', context.num_days, '1d')

Any ideas why? Too many securities?