Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Create a custom factor, TypeError

There probably already is information on this but I can't seem to find it. I want to create a simple custom factor to use in my pipeline, for example the difference between the close and open price of a stock. I create a column in my pandas df that is the difference but I can't use that format for my pipeline and get this error:

TypeError: zipline.pipeline.pipeline.validate_column() expected a value of type zipline.pipeline.term.Term for argument 'term', but got pandas.core.series.Series instead.

This is the code

def initialize(context):
attach_pipeline(make_pipeline(), 'pipe')

Apple_df['daygain'] = Apple_df['close_price'] - Apple_df['open_price']

def make_pipeline():

daygain_factor = Apple_df['daygain']  

universe = QTradableStocksUS()  

pipe = Pipeline(columns={'daygain_factor':daygain_factor,  
               'longs': (daygain_factor >= 0),  
               'shorts': (daygain_factor < 0)},  
               screen = universe)  
return pipe

result = run_pipeline(make_pipeline(), start_date='2018-01-01', end_date='2019-01-01')

Any help or links would be greatly appreciated.

1 response

The error 'TypeError: zipline.pipeline.pipeline.validate_column() expected a value of type zipline.pipeline.term.Term for argument 'term' but got pandas.core.series.Series instead. is saying that a pipeline column is being defined using a pandas series and not with what is expected which is either a factor, filter, or classifier. Pipeline columns can only be defined as one of those three types. The offending line is this

# Apple_df isn't explicitly defned but python interprets it as a series  
Apple_df['daygain'] = Apple_df['close_price'] - Apple_df['open_price']  
daygain_factor = Apple_df['daygain']  

# this series is then assigned to a column which pipeline doesn't like  
pipe = Pipeline(columns={'daygain_factor':daygain_factor,  

So how to create a "days gain" which is a factor (and therefor pipeline will accept as a column)? One could do it a couple of ways. 1) use built in operators and methods or 2) create a custom factor. Here's how one could do it using some built in operators

from quantopian.pipeline.data.builtin import USEquityPricing


# create factors directly from datasets using the latest method  
open_price = USEquityPricing.open.latest  
close_price = USEquityPricing.close.latest  

# create a 'gain' factor by combining existing factors using built-in operators  
# could have also done (close_price / open_price)-1  
 day_gain = (close_price - open_price) / open_price

Here's a custom factor which does the same thing

# create a custom factor to calc the days gain  
class DayGain(CustomFactor):  
        '''  
        Returns the daily open to close gain  
        '''  
        inputs = [USEquityPricing.open, USEquityPricing.close]  
        window_length = 1  
        def compute(self, today, assets, out, open_price, close_price):  
            out[:] =  (close_price / open_price) - 1.0  


day_gain_custom_factor = DayGain()

Attached is a notebook showing both of these approaches in action and how they return the same value. Hope that helps.

Good luck!

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.