MichaelJMath
5/4/2018 - 5:09 PM

TTM Fundamental Field Custom Factor

TTM Fundamental Field Custom Factor in quantopian pipeline

class NetIncomeTTM(CustomFactor):
    
    inputs = [Fundamentals.net_income_common_stockholders_asof_date, 
              Fundamentals.net_income_common_stockholders]
    window_length=252
    
    def compute(self, today, asset, out, asof_date, values):

        for column_ix in range(asof_date.shape[1]):
            _, unique_indices = np.unique(asof_date[:, column_ix], return_index=True)
            quarterly_values = values[unique_indices, column_ix]

            # Fill empty values with NANs in output array
            if len(quarterly_values) < 4:
                quarterly_values = np.hstack([
                    np.repeat([np.nan], 4 - len(quarterly_values)),
                    quarterly_values])
            quarterly_values = quarterly_values[-4:]
            
            out[column_ix] = np.sum(quarterly_values)