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?