Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
get_fundamentals help in backtester

I am trying to write a program in the backtester that picks all stocks which have experienced positive dividend growth for the last three years. Specifically, in the research environment the logic I would use to pick the stocks would be:

fundamentals = init_fundamentals()  
dummydate = '2016-02-01'

# Verifies that dividends have been growing for 3 years  
div_data = get_fundamentals(query(fundamentals.earnings_report.dividend_per_share,  
                                  fundamentals.valuation_ratios.dividend_yield), dummydate, range_specifier='3y')

div = div_data['dividend_per_share'].dropna(axis = 1)

div_growth_stocks = div.T[div.iloc[1] - div.iloc[0] > 0].index.intersection(div.T[div.iloc[2] - div.iloc[1] > 0].index)  

In the backtester, as part of the rebalance function I have put in:

def my_rebalance(context,data):

    today = get_datetime()  
    if today.month == 2 or today.month == 5 or today.month == 8 or today.month == 11:  
        # Verifies that dividends have been growing for 3 years  
        div_data =  get_fundamentals(query(fundamentals.earnings_report.dividend_per_share,fundamentals.valuation_ratios.dividend_yield), today, '3y')  


        div = div_data['dividend_per_share'].dropna(axis = 1)

        div_growth_stocks = div.T[div.iloc[1] - div.iloc[0] > 0].index.intersection(div.T[div.iloc[2] - div.iloc[1] > 0].index)  

but when I actually run the code I get

TypeError: get_fundamentals() takes at most 3 arguments (4 given)

It seems that the get_fundamentals syntax in the backtester is different than in the research environment. Is there any way I can translate the above code from the research environment into the backtester?

1 response

Hi Jim,

Unfortunately get_fundamentals doesn't support getting historical data in the backtester; it always returns the most recent data. However, you can access historical fundamental data through Pipeline. For instance, this thread contains a backtest where custom factors are used to get historical fundamental data. Let me know if you'd like some help implementing custom factors for this purpose.

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.