Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Trading all stocks on Quantopian

Hi there!

I am working with an algorithm that hand selects stocks by their symbol (as showcased in the tutorials) and then passes them through a filter before trading. How can expand my code so that it considers all stocks available on Quantopian and then filters them?

Many Thanks!

def initialize(context):  
    set_symbol_lookup_date('2015-01-01')  
    context.tradeable_stocks = [  
        symbol('AAPL'), symbol('AMZN'),  
        symbol('GOOG_L'), symbol('IBM'),  
        symbol('MSFT'), symbol('NFLX'),  
        symbol('TSLA'), symbol('YHOO'),]  
    attach_pipeline(make_pipeline(), 'tradeable_stocks')  
3 responses

By default the pipeline will look at all stocks. Unless one specifically adds a filter you will get all available stocks returned in the pipeline output. Not sure what your 'make_pipeline' is expecting, but maybe ensure that isn't using 'context.tradeable_stocks' somehow to set a filter.

As a side note, it's good practice to limit the universe of securities in some fashion. There are both stocks and ETFs in the Quantopian data and generally ones algorithms would focus on one or the other. If stocks are the universe you want, then consider using one of the built in filters Q500US or Q1500US (https://www.quantopian.com/help#built-in-filters) at least as a first pass when developing ones code.

Hi Dan,

Thanks for your insight. So if I run the pipeline first and then setup initialize as follows:

def initialize(context):  
    attach_pipeline(make_pipeline(), 'assets_to_trade')  

I can call 'assets_to_trade' with,
context.assets_to_trade Also, am I correct in my understanding that I can select certain columns in the pipeline (e.g. 'long', 'short', 'sma_200') in a similar way?

I'm not sure what your 'make_pipeline' function does, however, all you really need is:

  1. Define a pipeline by specifying the columns in the output dataframe and, optionally, any filter you want to apply to the output.
  2. Attach a pipeline to the algorithm using the 'attach_pipeline' method.
  3. Get the resulting output from the pipeline by calling the 'pipeline_output'. Typically done every day in the built in 'before_trading_start' function.

1 and 2 would go in the 'initialize' function because they happen only once. Note that defining the pipeline is often placed into a separate function just to make it easier to read, and then called inside of the 'initialize' function. That's what your user defined function 'make_pipeline' does, but this isn't required.

    # Create any factors you want to use (in this case macd). Just ensure they are imported first.  
    macd = MACD(fast_period = 2, slow_period = 400, signal_period = 3)  


    # Create any filters you want to use.  
    macd_low_filter = macd < 0

    # Now create a pipeline and add all the factors and filters you want. These become the columns in the  
    # dataframe that the pipeline output returns  
    pipe = Pipeline(  
            columns = {  
            'macd' : macd,},  
            screen = macd_low_filter  
            )  
    # Finally 'attach' the pipeline to the algorithm  
    attach_pipeline(pipe, name='my_pipe')

Then get the output of the pipe(typically within the 'before_trading_start' function)

    context.output = pipeline_output('my_pipe')

The 'context.output' object is a pandas dataframe. The rows are indexed with the security object. The columns are the factors (or potentially filters and classifiers) set up in the pipeline definition.

'context.assets_to_trade' isn't any sort of built in variable. One would need to assign that to something to use it. In the example above the variable 'context.output' is an arbitrary user defined variable that refers to the dataframe returned by the pipeline.