# Imports needed to create and run pipeline
from quantopian.pipeline import Pipeline
from quantopian.research import run_pipeline
# Import any PIPELINE data we want to use
from quantopian.pipeline.data import Fundamentals
from quantopian.pipeline.data.sentdex import sentiment_free as sentiment
# Import any INTERACTIVE data we want to use
from quantopian.interactive.data.sentdex import sentiment_free as sentiment_interactive
# Import any built in factors we want to use
from quantopian.pipeline.factors.fundamentals import MarketCap
# Import any built in filters we want to use
from quantopian.pipeline.filters.morningstar import Q1500US
# maybe should be
# from quantopian.pipeline.filters import Q1500US
# Lets first check the data type of our pipline datasets
type(sentiment.sentiment_signal)
# Now lets check the data type of our interactive data
type(sentiment_interactive.sentiment_signal)
# Make a function with our pipline datasets to return our pipeline
def make_pipeline():
# Create the factors we want use
sentiment_factor = sentiment.sentiment_signal.latest
market_cap = Fundamentals.market_cap.latest
# Create a filter to select our 'universe'
# Our universe is made up of stocks that have a non-null sentiment signal that was updated in
# the last day, are not within 2 days of an earnings announcement, are not announced acquisition
# targets, and are in the Q1500US.
universe = (Q1500US()
& sentiment_factor.notnull()
& (market_cap > 50000000) )
# Create our pipeline columns and filter to just our universe
# Note that the 'longs' and 'shorts' columns are boolean filters creted from factors
pipe = Pipeline(
columns={
'sentiment': sentiment_factor,
'longs': (sentiment_factor >= 4),
'shorts': (sentiment_factor <= 2),
},
screen=universe
)
return pipe
# Now lets see our pipeline in action
# Run our pipeline and print the results
results = run_pipeline(make_pipeline(), '2017-1-7', '2017-1-7')
results
# Now lets see what our interactive data is
sentiment_interactive
# The above dataframe can be manipulated interactively (hence the name)
# For instance find securities with a sentiment signal > 5
sentiment_interactive[sentiment_interactive.sentiment_signal > 5]