Back to Community
Rolling Factor

I am trying to add a column to my pipeline that contains the rolling z-score of returns and volume, similar to the 1st Lesson of the Tutorial (Getting Started):

aapl_sma20 = aapl_close.rolling(20).mean()
aapl_sma50 = aapl_close.rolling(50).mean()

What would be the simplest approach?

I tried this approach but to no avail:

class Vol_z-score(CustomFactor):  
    inputs = [USEquityPricing.volume,USEquityPricing.volume.latest]  
    window_length = 21  
    def compute(self, today, assets, out, volume):  
        volume[np.isnan(volume)] = 0  
        mean    = np.mean(volume, 0)#.shift(1)  
        std     = np.std(volume, 0)#.shift(1)  
        z_score = (latest - mean)/std  
        out[:]  = z_score  
1 response

hello,

class Vol_z_score(CustomFactor):  
    inputs = [USEquityPricing.volume]  
    window_length = 21  
    def compute(self, today, assets, out, volume):  
        latest  = volume[0, :]  
        mean    = np.nanmean(volume, axis=0)  
        std     = np.nanstd(volume, axis=0)  
        z_score = (latest - mean)/std  
        out[:]  = z_score  

Should work:

Loading notebook preview...
Notebook previews are currently unavailable.