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.