Back to Community
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]

growth_grade_1 = Previous(inputs = [morningstar.asset_classification.growth_grade], window_length = 63)

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!

Loading notebook preview...
Notebook previews are currently unavailable.
Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Dan,

Thank you very much. That is exactly what i was looking to do.