Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
best way to query fundamentals for a specific symbol?

What is the best way to query fundamentals for a specific symbol?

I am interested in fundamentals for a set of symbols ['BRK.A', 'GLD', 'SPY']

Below I try filter based on fundamentals.company_reference.primary_symbol a few different ways below but they all return empty.
I am try to get things like PE ratio, ROE, Price-to-Book Ratio, Dividend yield, etc for some index funds or blue chips

fundamental_df = get_fundamentals(  
    query(  
        # put your query in here by typing "fundamentals."  
        fundamentals.company_reference.primary_symbol,  
        fundamentals.valuation_ratios.pe_ratio,  
        fundamentals.valuation_ratios.pb_ratio,  
        fundamentals.operation_ratios.roe,  
        fundamentals.valuation.market_cap,  
        fundamentals.asset_classification.morningstar_sector_code,  
        fundamentals.asset_classification.growth_grade,  
        fundamentals.asset_classification.profitability_grade,  
    )  

.filter(fundamentals.company_reference.primary_symbol == 'SPY')

.filter(fundamentals.company_reference.primary_symbol.in_(['BRK.A', 'GLD', 'SPY']))

)  
x=[(stock.symbol,fundamental_df[stock]['growth_grade'], fundamental_df[stock]['profitability_grade']) for stock in fundamental_df]  
print 'Benchmarks: Len=%d, %s'%(len(x),x)

Non of the above work.

Any suggestions?

Sarvi

16 responses

Ok.
This(fundamentals.share_class_reference.symbol) worked for 'BRK.A', but GLD and SPY are not found.
Is there a way to get at the ROE, PE, Price to Book Ratio, Yield etc, for indexs and ETFs ?

fundamental_index = get_fundamentals(  
    query(  
        # put your query in here by typing "fundamentals."  
        fundamentals.share_class_reference.symbol,  
        fundamentals.valuation_ratios.pe_ratio,  
        fundamentals.valuation_ratios.pb_ratio,  
        fundamentals.operation_ratios.roe,  
        fundamentals.valuation.market_cap,  
        fundamentals.asset_classification.morningstar_sector_code,  
        fundamentals.asset_classification.growth_grade,  
        fundamentals.asset_classification.profitability_grade,  
    )  
    .filter(fundamentals.share_class_reference.symbol.in_(  
            context.BenchmarkIndex))  
)

Thx,
Sarvi

Hi Sarvi

Unfortunately the fundamentals data doesn't provide information on ETFs, only companies.

Josh

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Well it would be nice if Quantopian provided these values from the index components.

OR

Provide access to the different Index Components/Weights and a Query/function to calculate it on demand.

Is the daily history of Index Components/Weights available some where, may be I can use that to calculate the fundamentals for the index.

Sarvi

Maybe I'm missing something here, but what would be the price to earnings ratio of the gold bullion holding ETF? And are you thinking that the SPY's PE would be some combination of all the PEs of all 500 components weighted by their presence in the ETF? Would it mean anything? What is your thinking here?

Over the years I've built a number of ETF, ETN, closed end funds, syndex arb strats. I'd never heard of anyone trying to synthesize an ETF PE from the PEs of the components. And I'm not sure retrieving and comparing various fundamental metrics for the legs of an index would give you much edge in arb'ing the index. But it might give you rebalance insight. The reweighting of components for equal weighted ETFs might be worth examining. But I'm still a neophyte with regards to most of this stuff.

Market Tech.
I am thinking of using the PE of S&P500 as a one simple measure of the overall valuation of the market.

To decide on over all asset allocation between cash,/bond and stocks

Price is the value of the index of S&P500, calculated as a weighted component of its prices. The same can be done for the Earnings as well to arrive at the PE

Sarvi

Looks like fundamentals.share_class_reference.symbol doesn't work great when there are cases like BRK.A and BRK.B

I tried creating my own index by first creating a query of for example, the top 100 market cap stocks.
and created a python list, called
context.indexcomps
from their fundamentals.share_class_reference.symbol

I then query/filter for
fundamentals.share_class_reference.symbol .in_(context.indexcomps)

I get more stocks than there are in context.indexcomps.

I am guessing from the following
('JW_A', 0.265645, 0.059418, 0.005998013102781656), ('JW_B', 0.265645, 0.059418, 0.006020352613338076)

that if there multiple Class of shares for the same company they both contain the same value for
fundamentals.share_class_reference.symbol

Is the way to query the fundamental data based on the quantopians SID/symbol ?

Catching up on old forum threads I didn't have a chance to answer last week . . . Yes, you can filter by sid.

Each namespace has a sid column so something like:

.filter(fundamentals.valuation.sid == 24)

would work for Apple.

How can you do this for multiple sids?

.filter(fundamentals.valuation.sid in sid_list) does not work.

Specific to your question, check out SQLAlchemy documentation: http://docs.sqlalchemy.org/en/rel_0_9/orm/tutorial.html#common-filter-operators

More broadly, you might want to investigate using the Pipeline API to access Morningstar data to accomplish what you're looking to do.

The following code is derived from this example: https://www.quantopian.com/research/notebooks/Tutorials%20and%20Documentation/4.%20Tutorial%20-%20Using%20Fundamental%20Data.ipynb

starting_sids = [162, 600, 624]
print "Securities", starting_sids

data_panel = get_fundamentals(query(fundamentals.company_reference.sid)
.filter(fundamentals.company_reference.sid.in_((starting_sids))),
'2016-04-01')

However, for some reason it doesn't always return data for the full list of sids and I'm unable to determine why.

Interesting Chad. I'm not sure the answer as to why sid 600 isn't showing up in the more recent data points. It looks to still trade and is present in earlier time periods. We'll have to submit a bug to dig in.

Thanks
Josh

Thanks for the reply Josh. This basically does what Tim S. is asking for but has issues. So do I need to submit a bug on my end or is that something you do? It would be great to have this working reliably.

I've submitted the bug internally.

Great. Thanks.

So it looks like this still doesn't work.