I've tried to create two quarter-on-quarter (QonQ) growth/change factors, based off a ttm (trailing twelve month) factor someone else had created and posted in the forum. If I've made a mistake on either of them, please do let me know, as I'm pretty new to python and Quantopian.
The first one is meant to calculate the growth/change between the current (most recently reported) quarter vs the same quarter last year, in order to remove any seasonality effect, so mostly for cumulative time-series items on the income and cashflow statements.
The second one is meant to calculate the growth/change between the current quarter and the direct previous (consecutive) quarter, and is meant mostly for balance sheet/point-in-time related items.
Note that I added a few extra trading days to the standard 63 trading days / quarter, as perhaps not all companies issue their 10Qs exactly 63 trading days after the previous one. Maybe it's better to use a few more extra days for quarter length just to be safe? Or is there a better way of doing this?
import numpy as np from quantopian.pipeline.factors import CustomFactor from quantopian.pipeline.data import Fundamentals class QonQ_Growth(CustomFactor): """ Quarter on Quarter growth (current vs same quarter last year) Call from within Pipeline function, for example: ebit_growth = QonQ_Growth(inputs=[Fundamentals.ebit]) """ # quarter_length = 65 window_length = 3 * 66 def compute(self, today, assets, out, data): q_on_q = np.array([-1, -3 * 66]) out[:] = (data[q_on_q] - data[q_on_q]) / data[q_on_q] class QonCQ_Growth(CustomFactor): """ Quarter on Consecutive Quarter Growth Call from within Pipeline function, for example: equity_growth = QonCQ_Growth(inputs=[Fundamentals.book_value_per_share]) """ # quarter_length = 65 window_length = 1 * 66 def compute(self, today, assets, out, data): q_on_cq = np.array([-1, -66]) out[:] = (data[q_on_cq] - data[q_on_cq]) / data[q_on_cq]