Notebook

Get a stock symbol from an asset

In [26]:
# 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
In [27]:
# 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(),
    )
In [28]:
# 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)

Pipeline Execution Time: 1 Minute, 5.82 Seconds
Out[28]:
name_as_of_pipe symbol_as_of_pipe
2008-01-02 00:00:00+00:00 Equity(2 [ARNC]) Alcoa AA
Equity(24 [AAPL]) Apple AAPL
Equity(62 [ABT]) Abbott Laboratories ABT
Equity(67 [ADSK]) Autodesk ADSK
Equity(76 [TAP]) Molson Coors Brewing TAP
Equity(88 [ACI]) Arch Coal ACI
Equity(114 [ADBE]) Adobe Systems ADBE
Equity(122 [ADI]) Analog Devices ADI
Equity(128 [ADM]) Archer-Daniels Midland ADM
Equity(161 [AEP]) American Electric Power AEP

Now lets add a couple of columns which fetch the symbol using two other approaches.

In [29]:
# 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)
Out[29]:
name_as_of_pipe symbol_as_of_pipe symbol_current_via_map symbol_current_via_list name_current
2008-01-02 00:00:00+00:00 Equity(2 [ARNC]) Alcoa AA ARNC ARNC ARCONIC INC
Equity(24 [AAPL]) Apple AAPL AAPL AAPL APPLE INC
Equity(62 [ABT]) Abbott Laboratories ABT ABT ABT ABBOTT LABORATORIES
Equity(67 [ADSK]) Autodesk ADSK ADSK ADSK AUTODESK INC
Equity(76 [TAP]) Molson Coors Brewing TAP TAP TAP MOLSON COORS BEVERAGE CO
Equity(88 [ACI]) Arch Coal ACI ACI ACI ARCH COAL INC.
Equity(114 [ADBE]) Adobe Systems ADBE ADBE ADBE ADOBE INC
Equity(122 [ADI]) Analog Devices ADI ADI ADI ANALOG DEVICES INC
Equity(128 [ADM]) Archer-Daniels Midland ADM ADM ADM ARCHER DANIELS MIDLAND CO
Equity(161 [AEP]) American Electric Power AEP AEP AEP AMERICAN ELEC PWR CO INC

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.

In [30]:
different_symbols = pipe_df.query('symbol_as_of_pipe != symbol_current_via_map').drop_duplicates('symbol_current_via_map')
different_symbols
Out[30]:
name_as_of_pipe symbol_as_of_pipe symbol_current_via_map symbol_current_via_list name_current
2008-01-02 00:00:00+00:00 Equity(2 [ARNC]) Alcoa AA ARNC ARNC ARCONIC INC
Equity(1637 [CMCS_A]) Comcast CMCSA CMCS_A CMCS_A COMCAST CORP
Equity(2621 [VAL]) Ensco ESV VAL VAL VALARIS PLC
Equity(3128 [TGNA]) Gannett GCI TGNA TGNA TEGNA INC
Equity(4010 [TT]) Ingersoll-Rand IR TT TT TRANE TECHNOLOGIES PLC
Equity(4120 [J]) Jacobs Engineering Group JEC J J JACOBS ENGR GROUP INC
Equity(4849 [SPGI]) McGraw Hill Financial MHFI SPGI SPGI S&P GLOBAL INC
Equity(7272 [NLOK]) Symantec SYMC NLOK NLOK NORTONLIFELOCK INC
Equity(7612 [ANDV]) Tesoro TSO ANDV ANDV ANDEAVOR
Equity(7679 [JCI]) Tyco International TYC JCI JCI JOHNSON CONTROLS INTERNATIONAL PLC
Equity(8089 [WBA]) Walgreen WAG WBA WBA WALGREENS BOOTS ALLIANCE INC
Equity(8468 [SITC]) DDR DDR SITC SITC SITE CENTERS CORP
Equity(8580 [CB]) ACE ACE CB CB CHUBB LIMITED
Equity(11100 [BRK_B]) Berkshire Hathaway BRK.B BRK_B BRK_B BERKSHIRE HATHAWAY INC CL-B
Equity(12213 [TFCF_A]) Twenty-First Century Fox FOXA TFCF_A TFCF_A TWENTY-FIRST CENTURY FOX INC
Equity(14848 [AABA]) Yahoo! YHOO AABA AABA ALTABA INC
Equity(16850 [TFC]) BB&T BBT TFC TFC TRUIST FINL CORP
Equity(19917 [BKNG]) Priceline Group PCLN BKNG BKNG BOOKING HOLDINGS INC
Equity(20387 [VIAV]) JDS Uniphase JDSU VIAV VIAV VIAVI SOLUTIONS INC
Equity(21429 [ON]) ON Semiconductor ONNN ON ON ON SEMICONDUCTOR CORP
Equity(21612 [DNDN_Q]) Dendreon DNDN DNDN_Q DNDN_Q DENDREON CORP
Equity(22099 [TPR]) Coach COH TPR TPR TAPESTRY INC
Equity(23047 [ZBH]) Zimmer Holdings ZMH ZBH ZBH ZIMMER BIOMET HOLDINGS INC
Equity(23103 [ANTM]) WellPoint WLP ANTM ANTM ANTHEM INC
Equity(26367 [CBRE]) CBRE CBG CBRE CBRE CBRE GROUP INC
Equity(26470 [IAC]) IAC/InterActive IACI IAC IAC IAC/INTERACTIVECORP
Equity(26578 [GOOG_L]) Google GOOGL GOOG_L GOOG_L Alphabet Inc. Cl A
Equity(27357 [LBTY_A]) Liberty Global LBTYA LBTY_A LBTY_A LIBERTY GLOBAL PLC
Equity(27822 [UAA]) Under Armour UA UAA UAA UNDER ARMOUR INC
Equity(32045 [STRZ_A]) Starz STRZA STRZ_A STRZ_A STARZ
... ... ... ... ... ... ...
2009-03-02 00:00:00+00:00 Equity(2371 [ATGE]) DeVry Education Group DV ATGE ATGE ADTALEM GLOBAL EDUCATION INC
2009-05-01 00:00:00+00:00 Equity(7488 [GL]) Torchmark TMK GL GL GLOBE LIFE INC
2009-06-01 00:00:00+00:00 Equity(1332 [CCEP]) Coca-Cola Enterprises CCE CCEP CCEP COCA-COLA EUROPEAN PARTNERS
Equity(21380 [EPC]) Energizer Holdings ENR EPC EPC EDGEWELL PERSONAL CARE CO
2010-02-01 00:00:00+00:00 Equity(32393 [WYND]) Wyndham Worldwide WYN WYND WYND WYNDHAM DESTINATIONS INC
2010-03-01 00:00:00+00:00 Equity(36930 [DISC_A]) Discovery Communications DISCA DISC_A DISC_A DISCOVERY INC
2010-10-01 00:00:00+00:00 Equity(32887 [HRI]) Hertz Global Holdings HTZ HRI HRI HERC HOLDINGS INC
2010-12-01 00:00:00+00:00 Equity(8572 [AGN]) Actavis ACT AGN AGN ALLERGAN PLC
2011-02-01 00:00:00+00:00 Equity(16661 [TIVO]) Rovi ROVI TIVO TIVO TIVO CORP
2011-10-03 00:00:00+00:00 Equity(23269 [WW]) Weight Watchers WTW WW WW WW INTL INC
2012-05-01 00:00:00+00:00 Equity(5484 [ES]) Northeast Utilities NU ES ES EVERSOURCE ENERGY
2012-07-02 00:00:00+00:00 Equity(42270 [CPRI]) Michael Kors Holdings KORS CPRI CPRI CAPRI HOLDINGS LIMITED
2012-10-01 00:00:00+00:00 Equity(42173 [APTV]) Delphi Automotive DLPH APTV APTV APTIV PLC
2013-12-02 00:00:00+00:00 Equity(41730 [ZG]) Zillow Z ZG ZG ZILLOW GROUP INC CLASS A
2014-03-03 00:00:00+00:00 Equity(41872 [VER]) American Realty Capital ARCP VER VER VEREIT INC
2014-10-01 00:00:00+00:00 Equity(4031 [IONS]) Isis Pharmaceuticals ISIS IONS IONS IONIS PHARMACEUTICALS INC
2015-11-02 00:00:00+00:00 Equity(7962 [VIAC]) CBS CBS VIAC VIAC VIACOMCBS INC
2016-06-01 00:00:00+00:00 Equity(42699 [WP]) Vantiv VNTV WP WP WORLDPAY INC
2016-11-01 00:00:00+00:00 Equity(44692 [IQV]) Quintiles IMS Holdings Q IQV IQV IQVIA HOLDINGS INC
2017-07-21 00:00:00+00:00 Equity(357 [TWX]) Time Warner AGN TWX TWX TIME WARNER INC.
Equity(4589 [LUV]) Southwest Airlines HPE LUV LUV SOUTHWEST AIRLINES CO
Equity(7457 [TJX]) TJX Companies USB TJX TJX TJX COMPANIES INC
Equity(7800 [UNP]) Union Pacific BMY UNP UNP UNION PAC CORP
Equity(7883 [UTX]) United Technologies F UTX UTX UNITED TECHNOLOGIES CORP
Equity(7990 [VLO]) Valero Energy UPS VLO VLO VALERO ENERGY CORP
Equity(8329 [X]) United States Steel VLO X X UNITED STATES STEEL CP
Equity(20940 [UPS]) United Parcel Service AXP UPS UPS UNITED PARCEL SERVICE INC.CL B
Equity(25010 [USB]) US Bancorp CRM USB USB U.S.BANCORP (NEW)
Equity(28051 [UAL]) United Continental D UAL UAL UNITED AIRLS HLDGS INC
2017-11-01 00:00:00+00:00 Equity(51157 [DD]) DowDuPont DWDP DD DD DUPONT DE NEMOURS INC

66 rows × 5 columns

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.

In [ ]: