Back to Community
Custom Factor help

How do i make the following into a custom factor so that i can implement in the pipeline? Thanks

def slope(ts):
x = np.arange(len(ts))
log_ts = np.log(ts)
slope, intercept, r_value, p_value, std_err = stats.linregress(x, log_ts)
annualized_slope = (np.power(np.exp(slope), 250) - 1) * 100
return (annualized_slope * (r_value ** 2))

2 responses

Manfred,

You could use something like this:

class Momentum(CustomFactor):

    # Pre-declare inputs and window_length  
    inputs = [USEquityPricing.close]  
    window_length = 80

    # Compute factor1 value  
    def compute(self, today, assets, out, pricing):  
        pricing = pricing[-self.window_length:-20]  
        scores = np.empty(len(pricing.T), dtype=np.float64)  
        x = np.arange(len(pricing))  
        log = np.log(pricing)  
        i = 0  
        for col in log.T:  
            slope, intercept, r_value, p_value, std_err = stats.linregress(x, col)  
            score =  (np.power(np.exp(slope), 250) - 1) * 100 * r_value**2  
            scores[i] = score  
            i+=1  
        out[:] = scores  

Thank you Donny!