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