Back to Community
TTM eps

I am trying to write a custom factor to calculate the trailing 12 months eps using the following code:

class EPS(CustomFactor):  
    inputs = [USEquityPricing.close, mstar.earnings_report.basic_continuous_operations]  
    window_length = 3  
    def compute(self, today, assets, out,close,eps):  
        out[:] = np.sum(eps, axis=0)  

I am expecting this code to sum the last 4 quarters eps from morningstar but it seems to take the latest eps and multiplies it by 4. What am I doing wrong?

3 responses

This is a common error, or at least I had the same issue. You are looking back only 3 days, not 3 quarters, which is not intuitive since this data only updates quarterly. Furthermore, if you start really looking back more than a year or so you start running into memory issues. It's on my to-do list to finish a lib that makes this a lot easier and properly stores look back data by the quarter, but its hacky and easy to get tripped up in books/quirks in the data. Attached is a notebook that should show how this can be done.

Loading notebook preview...
Notebook previews are currently unavailable.

That's great. Thanks Kevin.

Hi Kevin,
I have tried the following

# Past 5 years revenue growth rate  
class Growth5yr(CustomFactor):  
    inputs = [morningstar.asset_classification.growth_grade]  
    window_length = trading_days_in_year  
    def compute(self, today, asset_ids, out, value):  
        out[:] = value[-1]  

but i get an error

ValueError: setting an array element with a sequence.  
USER ALGORITHM:169, in compute  
out[:] = value[-1]  

would you know how I could solve this?