# Import Pipeline
from quantopian.pipeline import Pipeline, CustomFactor
from quantopian.research import run_pipeline
# Import filter/screen
from quantopian.pipeline.filters import QTradableStocksUS, Q500US
# Import data
from quantopian.pipeline.data.morningstar import Fundamentals
# Import numpy and pandas
import numpy as np
import pandas as pd
# Create a pipeline function
# It's not neccessary to define a pipeline with a function but it is convenient.
# This way the function can be easily copied and pasted into an algo if needed.
def make_pipeline():
symbol_as_of_pipe = Fundamentals.symbol.latest
name_as_of_pipe = Fundamentals.short_name.latest
return Pipeline(
columns={
'symbol_as_of_pipe': symbol_as_of_pipe,
'name_as_of_pipe': name_as_of_pipe,
},
screen=Q500US(),
)
# Run the pipeline and display the money flow index values
pipe_df = run_pipeline(make_pipeline(), '1-1-2008', '1-10-2018')
pipe_df.head(10)
Now lets add a couple of columns which fetch the symbol using two other approaches.
# First, use the pandas 'map' method
pipe_df['symbol_current_via_map'] = pipe_df.index.get_level_values(level=1).map(lambda asset: asset.symbol)
# Next use python lists
assets = pipe_df.index.get_level_values(level=1)
pipe_df['symbol_current_via_list'] = [asset.symbol for asset in assets]
# Add the name just for convenience
pipe_df['name_current'] = pipe_df.index.get_level_values(level=1).map(lambda asset: asset.asset_name)
# See what we have
pipe_df.head(10)
The very first stock shows an asset which changed names as well as the symbol. Let's see how many of the Q500US stocks changed their symbol between 1-1-2008 and 1-10-2018.
different_symbols = pipe_df.query('symbol_as_of_pipe != symbol_current_via_map').drop_duplicates('symbol_current_via_map')
different_symbols
Looks like 66 symbol changes in about 10 years just for the Q500US stocks. However, some of these differences are simply because Morningstar represents a symbol differently than Quantopian. For example BRK.B vs BRK_B.