Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Multiple get_fundamentals or an 'OR' for adding fundamentals on individual securities

The goal is to pull a list of securities and their fundamentals information normally, and also fundamental info on sets of securities that one would specify by their symbols, where they would be independent from the filters.

Is an 'OR' available?

Generic example calling twice:

    f = fundamentals  
    panel1 = get_fundamentals(    # Fundamentals by filter  
        query(  
            f.valuation_ratios.pe_ratio,  
        )  
        .filter(f.share_class_reference.is_primary_share == True)  
        .order_by(f.valuation.market_cap.desc())  
        .limit(20)  
    )  
    panel2 = get_fundamentals(    # Fundamentals for specific symbols  
        query(  
            f.valuation_ratios.pe_ratio,  
        )  
        .filter(f.company_reference.primary_symbol.in_(['AAPL', 'TSLA']))  
        .order_by(f.valuation.market_cap.desc())  
    )  
    panel3 = pd.concat([panel1, panel2], axis=1)  

... however the concat failed with:

"Runtime exception: TypeError: cannot concatenate a non-NDFrame object"

In get_fundamentals help, the return is said to be a pandas.Panel rather than a DataFrame.
So I'm wondering if this can be done in one call with an 'OR' inside get_fundamentals or whether the two panels can be combined.

Thanks

PS Tip: For those who did not know, the query() section populates the panel with specific values. If only the filters and ordering are being utilized, for the resulting securities list, then query() can be empty, that's faster.

3 responses

I didn't solve your direct problem, but I accomplished your goal simply by concatenating the arrays of securities from separate fundamentals queries:

import numpy as np

def initialize(context):  
    pass

def before_trading_start(context):

    df1 = get_fundamentals(  
        query(  
            fundamentals.valuation.market_cap  
        )  
        .filter(fundamentals.company_reference.primary_symbol == 'AAPL')  
        ,'2015-08-20')

    df2 = get_fundamentals(  
        query(  
            fundamentals.valuation.market_cap  
        )  
        .filter(fundamentals.company_reference.primary_symbol == 'GOOGL')  
        ,'2015-08-20')  
    all_securities = np.concatenate((df2.columns.values, df1.columns.values), axis=0)  
    update_universe(all_securities)

def handle_data(context, data):

    for stock in data:  
        log.info("Stock in universe: %s" % stock.symbol)  
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.

Could be useful for someone, although optimally for utilizing the fundamental data, all would be combined in one panel or dataframe.
all_securities is made up of security objects lacking the fundamental data.

Maybe http://stackoverflow.com/questions/11714768/concat-pandas-dataframe-along-timeseries-indexes

Or to_frame(). I don't understand this:
http://stackoverflow.com/questions/22648773/what-is-the-simplest-way-to-concatenate-all-of-the-dataframes-in-a-pandas-panel

If only it were as easy as df1.append(df2)

An alternative using SQLAlchemy (i.e. getting that OR into the SQL query so you only need to query the DB once):

from sqlalchemy import or_  
fundamental_df_1 = get_fundamentals(  
        query(  
            fundamentals.valuation_ratios.pe_ratio)  
        .filter(or_(fundamentals.share_class_reference.is_primary_share == True,  
                    fundamentals.company_reference.primary_symbol.in_(['AAPL', 'TSLA'])))  
        .order_by(fundamentals.valuation.market_cap.desc())  
        .limit(2000)  
        , '2015-08-20'  
    )

(This is using get_fundamentals() in research)