from zipline.pipeline import Pipeline from zipline.pipeline.data import USEquityPricing from zipline.pipeline.factors import SimpleMovingAverage, RSI from zipline.pipeline.filters import StaticAssets universe = StaticAssets(symbols(['AAPL', 'MSFT', 'IBM'])) sma_50 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=50) rsi_14 = RSI(inputs=[USEquityPricing.close], window_length=14) pipe = Pipeline(columns={'sma_50':sma_50, 'rsi_14':rsi_14}, screen=universe)
from zipline.pipeline import Pipeline from zipline.pipeline.data import USEquityPricing, morningstar from zipline.pipeline.factors import Returns, SimpleMovingAverage, CustomFactor from zipline.pipeline.filters import StaticAssets class PriceToEarnings(CustomFactor): inputs = [morningstar.valuation_ratios.pe_ratio] window_length = 1 def compute(self, today, assets, out, pe_ratio): out[:] = 1/pe_ratio universe = StaticAssets(symbols(['AAPL', 'MSFT', 'IBM', 'GOOG', 'AMZN'])) pe_ratio = PriceToEarnings() pipe = Pipeline(columns={'pe_ratio':pe_ratio}, screen=universe) top_5_pe = pipe.top(5, mask=(pe_ratio > 0))This code uses Price-to-Earnings ratio as a Custom Factor to calculate the inverse of PE ratio, and then finds the top 5 stocks with highest inverse PE ratio. In summary, Pipeline is a powerful tool within the Python Zipline package library that can perform various computations over datasets and assist with backtesting trading strategies.