Thanks for your answer. I actually solved this by declaring it as @staticmethod, even if it is outside the class definition.
Quite weird, I think, but now it works.
I did get some errors when the dataseries for the different stocks started at different times. I solved this by aligning the datetime indexes in the input data (pandas) and returning a very low momentum if there are nans in the array. The idea is that with the low momentum, the stocks will never be sorted at the top, so there will not be any positions in the stock. I do consider this risky though. Any idea as to how to include all data?
The momentum function:
if np.nan in the_array: momentum = -10**10 else: # convert 'Close' to returns, create x-axis and do linear regression on returns returns = np.log(the_array) x = np.arange(len(returns)) slope, _, rvalue, _, _ = linregress(x, returns) # annualize the slope, multiply with R2 for best momentum when best linear-fit annualized = (1 + slope) ** 252 momentum = annualized * (rvalue ** 2) return momentum