Notebook

Getting Fundamentals Example

In [1]:
#Import basic pipeline stuff
from quantopian.pipeline import Pipeline
from quantopian.research import run_pipeline

# Import the StaticAsset filter since we already know the specific stocks we want data for
from quantopian.pipeline.filters import StaticAssets

# Finally import the fundamental data (really just the object which accesses it)
from quantopian.pipeline.data import Fundamentals
In [2]:
# Define which fields of data you want to get
# Look here for what's available https://www.quantopian.com/help/fundamentals#fundamentals-overview
# Just picking a few for demonstration purposes
my_fundamentals = {
                    'current_ratio': Fundamentals.current_ratio.latest,
                    'financial_leverage': Fundamentals.financial_leverage.latest,
                    'gross_margin': Fundamentals.gross_margin.latest,
                    'invested_capital': Fundamentals.invested_capital.latest,
                    'is_depositary_receipt': Fundamentals.is_depositary_receipt.latest,
                    'long_term_debt_equity_ratio': Fundamentals.long_term_debt_equity_ratio.latest,
                    'market_cap': Fundamentals.market_cap.latest,
                    'morningstar_sector_code': Fundamentals.morningstar_sector_code.latest,
                    'operating_cash_flow': Fundamentals.operating_cash_flow.latest,
                    'total_assets': Fundamentals.total_assets.latest,
                    'total_revenue': Fundamentals.total_revenue.latest,}

# Define which stocks you want to get data for
my_stocks = StaticAssets(symbols([
                        'AAPL',
                        'IBM',
                        'AMZN',
                        'NFLX'])
                        )

# Now define a 'pipeline' with what fields you want and for which stocks
# Note that this is just a data definition. The actual data is retrieved when the pipeline is run
my_pipeline = Pipeline(
            columns = my_fundamentals,
            screen = my_stocks
            )
In [3]:
# Finally run the pipeline and display the resulting data
# A pipeline can return data for multiple days but lets just get data for a single random day 2016-3-28
my_data = run_pipeline(my_pipeline, '2016-3-28', '2016-3-28')
my_data
Out[3]:
current_ratio financial_leverage gross_margin invested_capital is_depositary_receipt long_term_debt_equity_ratio market_cap morningstar_sector_code operating_cash_flow total_assets total_revenue
2016-03-28 00:00:00+00:00 Equity(24 [AAPL]) 1.001669 2.286512 0.400978 1.912300e+11 False 0.414791 5.858961e+11 311 2.746300e+10 2.932840e+11 7.587200e+10
Equity(3766 [IBM]) 1.240305 7.747511 0.517068 5.415100e+10 False 2.343851 1.421623e+11 311 5.279000e+09 1.104950e+11 2.205900e+10
Equity(16841 [AMZN]) 1.075961 4.889719 0.319076 2.161900e+10 False 1.059698 2.744774e+11 102 8.812000e+09 6.544400e+10 3.574700e+10
Equity(23709 [NFLX]) 1.538929 4.588806 0.314791 4.594788e+09 False 1.066535 4.210607e+10 308 -2.447450e+08 1.020287e+10 1.823333e+09
In [4]:
# The result is a pandas dataframe with a multi index (ie indexed by date AND security)
# This is handy since one can use all the built in dataframe methods
# If you are just looking at a single date it may be convenient to drop the date index
# Use the .xs method 
my_single_day_data = my_data.xs('2016-3-28', axis=0, drop_level=True)
my_single_day_data
Out[4]:
current_ratio financial_leverage gross_margin invested_capital is_depositary_receipt long_term_debt_equity_ratio market_cap morningstar_sector_code operating_cash_flow total_assets total_revenue
Equity(24 [AAPL]) 1.001669 2.286512 0.400978 1.912300e+11 False 0.414791 5.858961e+11 311 2.746300e+10 2.932840e+11 7.587200e+10
Equity(3766 [IBM]) 1.240305 7.747511 0.517068 5.415100e+10 False 2.343851 1.421623e+11 311 5.279000e+09 1.104950e+11 2.205900e+10
Equity(16841 [AMZN]) 1.075961 4.889719 0.319076 2.161900e+10 False 1.059698 2.744774e+11 102 8.812000e+09 6.544400e+10 3.574700e+10
Equity(23709 [NFLX]) 1.538929 4.588806 0.314791 4.594788e+09 False 1.066535 4.210607e+10 308 -2.447450e+08 1.020287e+10 1.823333e+09
In [5]:
# One can sort, filter and manipulate this dataframe
# Maybe filter by everything with long_term_debt_equity_ratio < 1.5 then sort by market_cap
my_single_day_data.query('long_term_debt_equity_ratio < 1.5').sort_values('market_cap')
Out[5]:
current_ratio financial_leverage gross_margin invested_capital is_depositary_receipt long_term_debt_equity_ratio market_cap morningstar_sector_code operating_cash_flow total_assets total_revenue
Equity(23709 [NFLX]) 1.538929 4.588806 0.314791 4.594788e+09 False 1.066535 4.210607e+10 308 -2.447450e+08 1.020287e+10 1.823333e+09
Equity(16841 [AMZN]) 1.075961 4.889719 0.319076 2.161900e+10 False 1.059698 2.744774e+11 102 8.812000e+09 6.544400e+10 3.574700e+10
Equity(24 [AAPL]) 1.001669 2.286512 0.400978 1.912300e+11 False 0.414791 5.858961e+11 311 2.746300e+10 2.932840e+11 7.587200e+10