from zipline.pipeline import Pipeline
from zipline.pipeline.factors import AverageDollarVolume, SimpleMovingAverage, ExponentialWeightedMovingStdDev
from zipline.pipeline.data.equity_pricing import EquityPricing
from zipline.pipeline.data.master import SecuritiesMaster
def make_pipeline():
"""
Create a pipeline with the following rules:
screen
- common stocks only
- must be liquid (top 10% by dollar volume)
- must be above 20-day moving average
- must not be too cheap or too expensive
columns
- 20-day moving average
- prior low
- standard deviation of closing prices
"""
mavg = SimpleMovingAverage(
window_length = 20, inputs = [EquityPricing.close])
are_common_stocks = SecuritiesMaster.usstock_SecurityType2.latest.eq(
"Common Stock")
are_liquid = AverageDollarVolume(window_length=30).percentile_between(90, 100)
are_above_mavg = EquityPricing.close.latest > mavg
are_not_too_cheap = EquityPricing.close.latest > 10
are_not_too_expensive = EquityPricing.close.latest < 2000
pipeline = Pipeline(
columns = {
"mavg": mavg,
"prior_low": EquityPricing.low.latest,
"std": ExponentialWeightedMovingStdDev(
inputs=[EquityPricing.close],
window_length=63,
decay_rate=0.99)
},
screen=(
are_common_stocks
& are_liquid
& are_above_mavg
& are_not_too_cheap
& are_not_too_expensive
)
)
return pipeline