Trailing Twelve Months (TTM) with As Of Date

Hi,

I'm new to Quantopian. Here's my first code contribution: an easy to use TTM CustomFactor. For good performance, it has a single expression Python list comprehension. Results are accurate for all sample cases I have tested so far.

class TrailingTwelveMonths(CustomFactor):
window_length=400
window_safe = True
outputs="""
factor
asof_date
""".split()
def compute(self, today, assets, out, values, dates):
out.factor[:] = [
(v[d + np.timedelta64(52, 'W') > d[-1]])[
np.unique(
d[d + np.timedelta64(52, 'W') > d[-1]],
return_index=True
)[1]
].sum()
for v, d in zip(values.T, dates.T)
]
out.asof_date[:] = dates[-1]
def make_pipeline():
(
total_revenue_ttm,
total_revenue_ttm_asof_date
) = TrailingTwelveMonths(
inputs=[
Fundamentals.total_revenue,
Fundamentals.total_revenue_asof_date,
]
)
return Pipeline(
columns={
'total_revenue_ttm': total_revenue_ttm,
'total_revenue_ttm_asof_date': total_revenue_ttm_asof_date
},
screen=symbols
)
df = run_pipeline(make_pipeline(), '2017-11-03', '2017-11-03')
df['total_revenue_ttm_asof_date'] = df['total_revenue_ttm_asof_date'].astype('datetime64[ns]')
df

67
Notebook previews are currently unavailable.
4 responses

Thank you for your contribution! I'm new to Quantopian too, and this example helped me understand better how to work with Fundamental Data. I've been trying to compute the Pietroski F-Score and studying your workbook gave me some good ideas for how to get data for start-of-year, previous year, and current date, comparing across assets with varying reporting dates.

Thank you for this. Very useful for anyone starting out with Fundamental data

this just made my day. thank you!

ok, devil is in the details. when running this for apple for EPS TTM for much of 2019, I do see a sudden drop 2.17-->11.32 on 2019-03-22, and then go back up to 2.16 on 2019-04-24. Check against other sources says that 2.16 is where is should be, but the 11.32 do not make any sense, both from a timing perspective and the value itself. Any idea why this methodology would result in that drop?

2