Back to Community
Custom Factor Question

Hi,
how can I would like to create a custom factor, which calculates the value of a certain percentile for me:

class Quantile_today(CustomFactor):  
    window_length=1  
    def compute(self, today, assets, out, values,quantile):  
        out[:] = np.percentile(values[-1],quantile)  

I then tried to pass into that Custom factor the following value:

def Q_today():

    return Quantile_today(inputs=[Fundamentals.pe_ratio,50])  

I then get an attribute error:

AttributeError: 'NoneType' object has no attribute 'format'

I would have expected to get the median value of each time stamp across all stocks that were present at that time stamp.
How can I create a custom factor and pass in other inputs, such as which percentile I want? Obviously this is a simplified example to get my point across and in the end I would like to build a more elaborate custom factor.

2 responses

One can pass custom parameters to custom factors by specifying 'param`. Something like this


class ParamTest(CustomFactor):  
    """  
    Simple factor which multiplies the input by a fixed input value  
    """  
    inputs = [USEquityPricing.close]  
    window_length = 1

    # The following creates a named input parameter  
    # Notice the comma -that's required  
    params = ('param1', )

    def compute(self, today, assets, out, values, param1,):  
        out[:] = param1 * values

my_doubled_price = ParamTest(param1=2)

See the attached notebook for some custom factors which may do what you want. I'd also maybe look at the built in factors for zscore and demean. Those may be something similar.

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.

Thank you Dan!