Max,
Pipeline API allows you to define a trading universe with filter terms, and express alpha factors with factor terms. A rule like "close price near 52 week high" is a true or false statement, and is best expressed as a filter. A factor is something that returns a number, like "fast moving average's distance from slow moving average."
The essence of using pipeline is creating factor terms that predict the future movement of asset prices, then filtering those results using filter terms. You then pass the numbers produced by the pipeline to Quantopian's Optimizer, which goes long on assets with high factor values, and short on assets with low factor values.
The pipeline below expresses the following idea: Go long on stocks whose fast moving average is above it's slow moving average. Only consider stocks that have had positive returns in the past year.
from quantopian.pipeline import Pipeline
from quantopian.research import run_pipeline
from quantopian.pipeline.factors import Returns, SimpleMovingAverage
from quantopian.pipeline.data import USEquityPricing
from quantopian.pipeline.filters import QTradableStocksUS
def make_pipeline():
fast_SMA = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=5)
slow_SMA = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=21)
# This is a factor that will produce numbers. Factors go in a pipeline column
fast_SMA_minus_slow_SMA = fast_SMA - slow_SMA
yearly_returns = Returns(window_length=252)
# This is a filter that will produce a true / false value. Filters go in a pipeline screen
has_positive_yearly_returns = yearly_returns > 0
return Pipeline(
columns = {'fast_SMA_minus_slow_SMA': fast_SMA_minus_slow_SMA},
screen=QTradableStocksUS() & fast_SMA_minus_slow_SMA.notnull() & has_positive_yearly_returns
)
run_pipeline(make_pipeline(), '2016-1-1', '2017-1-1')
For more information on how to create and combine factors, I recommend checking out the Combining Factors lesson on the Pipeline API tutorial.
Disclaimer
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.