Hello everyone,
I've implemented the following class the get TTM data for a given fundamental factor:
class TTM_unique(CustomFactor):
# Get the sum of the last 4 reported values
window_length = 260
def compute(self, today, assets, out, asof_date, values):
for asset in range(len(assets)):
# unique asof dates indicate availability of new figures
_, filing_dates = np.unique(asof_date[:, asset], return_index=True)
quarterly_values = values[filing_dates[-4:], asset]
# ignore annual windows with <4 quarterly data points
if len(~np.isnan(quarterly_values)) != 4:
out[asset] = np.nan
else:
out[asset] = np.sum(quarterly_values)
It works correctly but the problem is, that it is quite slow and sometime I get a timeout using it in the backtest environment.
Of course, it's slow because of the for-loop but I don't know how to vectorise it.
Any help?
I've attached also a research notebook that shows also as a näive TTM implementation without np.unique, which is 7x faster.
Thanks in advance,
Costantino