#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
# 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
)
# 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
# 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
# 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')