Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
CustomFactor's compute called more than once

I have the following code in my algorithms. As you can see, whenever MY_FACTOR is called it will output the shape of the [values] array

class MY_FACTOR(CustomFactor):  
    def compute(self, today, asset_ids, out, values):  
        print(values.shape)  
        # ... omitted code that populates [out]  


###################################  
def initialize(context):  
    #... omitted code  
    # Create our pipeline and attach it to our algorithm.  
    my_pipe = make_pipeline(context)  
    attach_pipeline(my_pipe, 'my_pipeline')

###################################  
def make_pipeline(context):  
    """  
    Create our pipeline.  
    """

    recent_price = USEquityPricing.close.latest  
    recent_price_filter = ~recent_price.isnan()  
    mean_close = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=context.tradePriceWindowForSelection)  
    # This is another custom factor  
    ageInMonths =  AgeInMonths(inputs=[morningstar.share_class_reference.ipo_date], window_length=1)  
    monthFilter = (ageInMonths >= 36) & (ageInMonths <= 48)  

    my_factor = MY_FACTOR(inputs=[USEquityPricing.close], window_length=250, mask=recent_price_filter & monthFilter)  
    my_filter = my_factor.percentile_between(80,100)  

    mean_close_column_name = '10_day_mean_close'  
    return Pipeline(  
        columns={  
            mean_close_column_name: mean_close,  
            "age": ageInMonths,  
            "my_factor_value": my_factor  
        },  
        screen=recent_price_filter &  
        my_filter &  
        monthFilter  
    )  
###################################  
def monthly_rebalance(context, data):  
        result = pipeline_output('my_pipeline')


###################################  
def before_trading_start(context, data):  
    todaysDate = data.current_dt  
    todaysMonth = todaysDate.month  

    if (todaysMonth != context.previousMonth):  
        monthly_rebalance(context, data)  

However, my algorithm's before_trading_starts call often times out and when I look at the logs, I see something like:

2014-10-01 05:45  PRINT (250, 242)  
2014-10-01 05:45  PRINT (250, 241)  
2014-10-01 05:45  PRINT (250, 242)  
2014-10-01 05:45  PRINT (250, 241)  
2014-10-01 05:45  PRINT (250, 239)  
2014-10-01 05:45  PRINT (250, 242)  
2014-10-01 05:45  PRINT (250, 240)  
2014-10-01 05:45  PRINT (250, 240)  
2014-10-01 05:45  PRINT (250, 240)  
2014-10-01 05:45  PRINT (250, 240)  
2014-10-01 05:45  PRINT (250, 241)  
2014-10-01 05:45  PRINT (250, 244)  
2014-10-01 05:45  PRINT (250, 243)  
2014-10-01 05:45  PRINT (250, 241)  
2014-10-01 05:45  PRINT (250, 243)  
2014-10-01 05:45  PRINT (250, 242)  
2014-10-01 05:45  PRINT (250, 242)  
2014-10-01 05:45  PRINT (250, 244)  
2014-10-01 05:45  PRINT (250, 243)  

Why is my factor called more than once? and why so many times?

2 responses

Hi Boris,

In backtesting, your pipeline is run in chunks in order to reduce the overall runtime. Several months of your pipeline will be computed in the same run through before_trading_start. As a result, debugging a pipeline in the IDE isn't great. I would highly recommend developing your pipeline in research as it is much easier to iterate on different versions. The Pipeline Tutorial actually spends the first 11/12 lessons in research and only moves to the IDE in the 12th lesson.

Does this help explain the behavior you're seeing?

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.

Hi Jamie, thank you for your response

I'm not sure what you mean in "Several months of your pipeline will be computed in the same run through before_trading_start."
Does this mean that if my test starts at, say, January 1st, 2007, then the pipeline output for Jan, Feb, Mar, Apr could be calculated in a single call of pipeline_output?
Is there a way to turn that feature off in case I have a custom factor which takes a long time to compute?