Back to Community
Help getting a list of the five highest volume securities over the past 5 days
from quantopian.algorithm import attach_pipeline, pipeline_output  
from quantopian.pipeline import Pipeline  
from quantopian.pipeline.factors import AverageDollarVolume



def initialize(context):  
    pipe = Pipeline()  
    pipe = attach_pipeline(pipe, name='my_pipeline')  
    dollar_volume = AverageDollarVolume(window_length=5)  
    pipe.add(dollar_volume, 'dollar_volume')  
    high_dollar_volume = dollar_volume.top(5)  
    pipe.set_screen(high_dollar_volume)  
def before_trading_start(context, data):  
    results = pipeline_output('my_pipeline')  
    print results.head(5)  
    context.pipeline_results = results  

So I'm not really sure what I am missing. I am trying to create a list of the five highest volume stocks over the past five days so that my algorithm can trade those. For some reason I can't get the algorithm to print the results of my screen on the pipeline. Any suggestions? I came up with this piece of code by combining these two pieces of code from the help page:

from quantopian.algorithm import attach_pipeline, pipeline_output  
from quantopian.pipeline import Pipeline  
from quantopian.pipeline.factors import AverageDollarVolume  
...
def initialize(context):  
    pipe = Pipeline()  
    attach_pipeline(pipe, name='my_pipeline')

    # Construct an average dollar volume factor and add it to the pipeline.  
    dollar_volume = AverageDollarVolume(window_length=30)  
    pipe.add(dollar_volume, 'dollar_volume')

    # Define high dollar-volume filter to be the top 10% of securities by dollar volume.  
    high_dollar_volume = dollar_volume.percentile_between(90, 100)

    # Filter to only the top dollar volume securities.  
    pipe.set_screen(high_dollar_volume)  
from quantopian.algorithm import attach_pipeline, pipeline_output  
from quantopian.pipeline import Pipeline  
from quantopian.pipeline.data.builtin import USEquityPricing  
from quantopian.pipeline.factors import SimpleMovingAverage

def initialize(context):

    # Create and attach an empty Pipeline.  
    pipe = Pipeline()  
    pipe = attach_pipeline(pipe, name='my_pipeline')

    # Construct Factors.  
    sma_10 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=10)  
    sma_30 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=30)

    # Construct a Filter.  
    prices_under_5 = (sma_10 < 5)

    # Register outputs.  
    pipe.add(sma_10, 'sma_10')  
    pipe.add(sma_30, 'sma_30')

    # Remove rows for which the Filter returns False.  
    pipe.set_screen(prices_under_5)

def before_trading_start(context, data):  
    # Access results using the name passed to `attach_pipeline`.  
    results = pipeline_output('my_pipeline')  
    print results.head(5)

    # Store pipeline results for use by the rest of the algorithm.  
    context.pipeline_results = results  
2 responses

Hmm, not sure why your code isn't printing for you. I pasted it into an algorithm and the logs do indeed show 5 stocks each day. See attached algorithm and check the logs.

Clone Algorithm
50
Loading...
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
from quantopian.algorithm import attach_pipeline, pipeline_output  
from quantopian.pipeline import Pipeline  
from quantopian.pipeline.factors import AverageDollarVolume



def initialize(context):  
    pipe = Pipeline()  
    pipe = attach_pipeline(pipe, name='my_pipeline')  
    dollar_volume = AverageDollarVolume(window_length=5)  
    pipe.add(dollar_volume, 'dollar_volume')  
    high_dollar_volume = dollar_volume.top(5)  
    pipe.set_screen(high_dollar_volume)  
    
def before_trading_start(context, data):  
    results = pipeline_output('my_pipeline')  
    print results.head(5)  
    context.pipeline_results = results 
 

         
There was a runtime error.

For does like me who are starting on Quantopian and trying the code on research notebook.
The problem is just that the first import on the notebook needs to be a little different.

from zipline.api import attach_pipeline, pipeline_output

Reference.
https://www.quantopian.com/posts/research-notebook-importerror-no-module-named-algorithm