Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Using a CustomFactor output as a filter in the pipeline

I create a CustomFactor, the output is 'return_on_capital':

class MyFactor(CustomFactor):  
    inputs = [Fundamentals.ebit.latest,  
              Fundamentals.net_ppe.latest]  
    outputs = ['return_on_capital']  
    window_length = 1  
    def compute(self, today, asset_ids, out, ebit, net_tangible):  
        out.return_on_capital[:] = ebit/net_tangible

now I want to use it as a filter, in the option 'screen' of a Pipeline():

def make_pipeline():  
       pipe = Pipeline(  
              columns={  
                'return_on_capital':  return_on_capital  
            },  
           screen=(MyFactor().return_on_capital>0.5)  
        )  
       return pipe  

it gives me an error, I guess this is not the right way to do it?

1 response

The syntax above is close but (as you noticed) not exactly right. I'll try to step through it...

First is to define the custom factor. It's most common for factors to have a single output value but they can have multiple output values each with different names. One could have a single output and name it but this isn't typical. Both of the following result in the same factor but just referenced a bit different

class MyFactor(CustomFactor):  
    inputs = [Fundamentals.ebit.latest,  
              Fundamentals.net_ppe.latest]  
    outputs = ['return_on_capital']  
    window_length = 1  
    def compute(self, today, asset_ids, out, ebit, net_tangible):  
        out.return_on_capital[:] = ebit/net_tangible

# The above named output factor is instantiated like this  
roc = MyFactor().return_on_capital

class Return_On_Capital(CustomFactor):  
    inputs = [Fundamentals.ebit.latest,  
              Fundamentals.net_ppe.latest]  
    window_length = 1  
    def compute(self, today, asset_ids, out, ebit, net_tangible):  
        out[:] = ebit/net_tangible

# The above typical single output factor is instantiated like this  
roc = Return_On_Capital()  

To make a filter out of these one can apply simple logical operators. This was correct in the original code. I think it didn't like the columns definition.

def make_pipeline():  
   return_on_capital = MyFactor().return_on_capital

   pipe = Pipeline(  
           columns={  
                'return_on_capital':  return_on_capital  
            },  
           screen=(return_on_capital>0.5)  
        )  
   return pipe 

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.