Back to Community
Passing state information to custom factor

Hi,

I am relatively new to writing a Quantopian algorithm, please bear with me if I ask something obvious.

I am trying to write a custom factor that takes the USEquityPricing.close as its input. However, besides the close price, I'd also want to know if the equity being considered is currently held in my positions. Basically, I will use different strategies to calculate the factor value based on whether I hold the equity or not. To do this, I constructed a dictionary that contains all the equities I hold based on context.portfolio.positions. However, when I try to pass this dict to the custom factor I build

def compute(self, today, assets, out, close, position_info):  

it gave me a compile error below

NonPipelineInputs: Unexpected input types in str. Inputs to Pipeline expressions must be Filters, Factors, Classifiers, or BoundColumns.
Got the following type(s) instead: []

It seems the compute method of a custom factor does not take input of type dict. Then I tried to make the dict a global variable, again it failed, which I saw a few posts mentioning global variables do not work. My question is, is there a way for me to pass the information to a custom factor?

5 responses

If you have access to context within the function you are trying to pass the values to, should you try to access context.portfolio.positions from there and obtain this list without passing parameters?

The context.portfolio.positions was accessed in rebalance function where context is the first input argument. The custom factor did not have access to context. I have tried to pass context as input to the custom factor, but got the same type error.

Had to use global variable to solve it. Thank you for the help!

Hi Yao Wang. I'm stucked with the same stuff, can you explain to me how you solved the problem.

'Cause I've same problem here, I calculated the factor in a different way and now I get this error when I'm trying to use this column in my CustomFactor Class.

NonPipelineInputs: Unexpected input types in str. Inputs to Pipeline expressions must be Filters, Factors, Classifiers, or BoundColumns.
Got the following type(s) instead: [class 'numpy.float64']

One can't pass algo state information to a pipeline. Pipelines are calculated asynchronous to the backtest. Before the very first day of a backtest, the pipeline will calculate the first 1 weeks worth of data (to get feedback quickly for algo). After that, it executes in 6 month 'chunks' (to gain efficiency in data fetches). Therefore, it can't know anything about the 'current' state of the algo. There is a little more info about this in the docs (https://www.quantopian.com/docs/user-guide/tools/algo-api#pipeline-execution).

The whole raison d'ĂȘtre of pipeline is to efficiently fetch data. By fetching data in chunks rather than synchronously, as the the algo executes, the data fetch time is dramatically reduced. Take a look at this post for a bit more on this https://www.quantopian.com/posts/need-help-with-pipeline-just-a-beginner

If one needs to fetch data for current holdings, as an example, simply ensure the pipeline includes a large enough universe of data to include it. In the worst case, simply screen the pipeline with the QTradableStocks universe. That will include data for any current holdings 99% of the time.

Note that one can use context 'global constants' in the pipeline definition. But, be aware these should only be used as constants to set up the pipeline. They shouldn't be used a variables to pass state.

Hope that makes sense.

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.