Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Newb question; trouble accessing fundamental data in handle_data? dataframe indexing?

Hi all,

This is probably a dumb question related to my inexperience with Pandas dataframes, but I'm trying to access fundamental information in the handle_data function. I am initially setting up a screen in before_trading_start and including this line as I've seen in the examples,

        query(  
            fundamentals.valuation.market_cap  
        )  
        .filter(fundamentals.valuation.market_cap > 2.5e9)  
        .filter(fundamentals.valuation.market_cap < 3e9)  
        # order by market cap  
        .order_by(fundamentals.valuation.market_cap.desc())  
        .limit(10)

    context.fundamental_df = fundamental_df  
    context.stocks = context.fundamental_df  

then, in handle_data, I'm trying to access the market cap for these stocks. If I do the following,
log.info(context.stocks) then I get the following output:
handle_data:88 INFO security Security(41280 [AL]) Security(24489 [GLNG]) Security(18582 [LHO]) Security(7364 [TDW]) Security(11086 [AEO]) Security(1315 [CBT]) Security(8188 [JW_B]) Security(39204 [PDM]) Security(2404 [EAT]) Security(7844 [USG]) market_cap 2991576478 2990319502 2988820773 2985963474 2985360174 2981665098 2978814819 2977367611 2977252454 2968431801 So I know the market_cap information is in here. I can get a bit closer if I do this,
log.info(context.stocks.loc['market_cap']) then I get the following output,
2014-01-07 handle_data:88 INFO security Security(41280 [AL]) 2991576478 Security(24489 [GLNG]) 2990319502 Security(18582 [LHO]) 2988820773 Security(7364 [TDW]) 2985963474 Security(11086 [AEO]) 2985360174 Security(1315 [CBT]) 2981665098 Security(8188 [JW_B]) 2978814819 Security(39204 [PDM]) 2977367611 Security(2404 [EAT]) 2977252454 Security(7844 [USG]) 2968431801 Name: market_cap, dtype: int64

SO: I'm wondering how I can access the market cap info for each stock and then use it? I think this is probably a simple indexing question, but I'm having trouble sorting it out. any thoughts?? Thanks!!

Jason

2 responses

Hi Jason,

Good question. This is something that stumped me too when I first started with Quantopian.

The code sample is storing the fundamentals metrics (like Market Cap) in context.fundamental_df.

In before_trading_start(), you are copying over the dataframe fundamental_df over to context. So access it there in handle_data. So something like:

context.fundamental_df.loc['market_cap']  

should work.

Also, I found it very helpful to use the debugger when I was ramping up on this. You can learn more about the debugger in the forum post when it was introduced.

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.

Hi Josh,

Thank you for the note, this is helpful. I've started using the debugger and that is immensely helpful as well for figuring these things out.

Best,

Jason