Accessing the previous period morningstar growth grade

Is it possible to get the growth_grade from a previous period? I tried the following CustomFactor and get the error
"ValueError: setting an array element with a sequence error" at the very end of running the pipeline. The CustomFactor works with other fundamental factors like PE Ratio, FCF Yield etc. So my guess is that because the growth grade is alpha that the CustomFactor has an issue. Apologies in advance i am not a very good python coder

class Previous(CustomFactor):
def compute(self, today, assets, out, inputs):
out[:] = inputs[0]

2 responses

Correct guess! The error "ValueError: setting an array element with a sequence error" is because factors expect a numerical output and the growth grade is a string. The growth_grade technically is a Classifier (see https://www.quantopian.com/docs/api-reference/pipeline-api-reference#zipline.pipeline.Classifier). The output is a string rather than a number.

However, this isn't a problem. Rather than creating a CustomFactor one can create a CustomClassifier. Very similar to the Previous(CustomFactor) class created above. The only additional items which need to be specified for classifiers are the output datatype and a value to use for 'missing' data. Something like this should do it.


class Previous_Classifer(CustomClassifier):
# Specify the lookback length in the window_length parameter. Default is 2.
window_length = 2

# Need to specify the output data type. In this case it's a string which to zipline is just an object
# Also need to specify what to use for a missing value
dtype = np.dtype('O')
missing_value = 'none'

def compute(self, today, assets, out, input_classifier):
out[:] = input_classifier[0]



There's a bit more on CustomClassifiers in this post https://www.quantopian.com/posts/custom-classifiers-possible

Attached is a notebook which shows it in action. Good luck!

5