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)
high_dollar_volume = dollar_volume.top(5)
pipe.set_screen(high_dollar_volume)
results = pipeline_output('my_pipeline')
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)

# 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.

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

# Access results using the name passed to attach_pipeline.
results = pipeline_output('my_pipeline')

# 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.

50
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)
high_dollar_volume = dollar_volume.top(5)
pipe.set_screen(high_dollar_volume)