Back to Community
Retrieving last year´s data for stock screener

Hey guys,

I want to build a piotroski f-score stock screener. Therefore I am building a pipeline and the respective criteria to filter the stocks.

# criteria 

    # profitability  
    roa = Fundamentals.roa.latest  
    roa_crit = (roa > 0)  
    cash_flow_op = Fundamentals.operating_cash_flow.latest  
    cf_crit = (cash_flow_op > 0)  
    delta_roa = roa / FS_Fundamentals.roa_af  
    roa_crit = (delta_roa > 1)  
    accruals = cash_flow_op / Fundamentals.total_assets.latest  
    accr_crit = (accruals > roa )  
    # leverage, liquidity and source of funds  
    leverage = Fundamentals.financial_leverage.latest  
    delta_leverage = leverage /

For the delta_leverage criterion, I need the prior years leverage. How do I retrieve that?

For the other criteria I used the MS data for this year´s data and the FS data for last year´s (because of the delay). Is there a better way to do this?

Thanks in advance!

2 responses

Previous data, like financial_leverage, can be fetched with the help of a small custom factor. Something like the following (there is also a relevant post with a similar issue here )

class Previous(CustomFactor):  
    def compute(self, today, assets, out, input):  
        out[:] = input[0]

# create a factor for financial_leverage 252 trading days ago (approx 1 calendar year)  
leverage_prev = Previous(inputs=[Fundamentals.financial_leverage], window_length=252)

The Morningstar data is primarily based on the latest quarterly filing. So values like roa will be the last quarter net Income / average total assets. The FactSet FS_Fundamentals.roa_af will be the annual roa or last annual report net Income / total assets. It's probably best not to mix the two. A 'good' way to get a delta quarterly roa would be

roa = Fundamentals.roa.latest  
roa_prev = Previous(inputs=[Fundamentals.roa], window_length=252)  
delta_roa = roa / roa_prev

One could do the same thing with the FactSet annual data but it would only change annually. The quarterly Morningstar data will capture quarterly changes which may be good or bad depending on your application.

Now, I said this is a 'good' way, however simply looking back 252 trading days can lead to comparing the wrong quarters at times, especially near the dates where a quarterly report is released. To be more accurate, one would need to look at the asof_date, subtract a year, then lookup the latest value with that previous asof_date. A little more work but also a little more accurate.

Hope that helps.

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.

Hey,

thanks for your help. I got an error message:

"InputRejected: Insecure built-in function 'input' You tried to import a module that is not whitelisted on Quantopian. To see the list of allowed modules in the IDE, go to https://www.quantopian.com/docs/whitelists/ide-whitelist#algo-ide-whitelist. To see the list of additional modules allowed in Research, go to https://www.quantopian.com/docs/whitelists/research-whitelist#research-whitelist."

Do you know how to fix that?

Alternatively can you explain the "as of date" approach in detail?

Thanks in advance.