Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Custom pipeline factor for return volatility (3-yr)

Hi all, really struggling to build a custom vol factor. If someone could help that would really appreciate it. Here is how far I got, it may be completely off.

class ComputeVolSignal(CustomFactor):
inputs = [returns(window_lenght=2)]
window_length = 750

def compute(self, today, assets, returns):  
    out[:] = returns.std()*np.sqrt(250)  
2 responses

You are pretty close. Should be something like this.

class ComputeVolSignal(CustomFactor):  
    inputs = [Returns(window_length=2)]  
    window_length = 750

    def compute(self, today, assets, out, returns):  
        out[:] = returns.std(axis=0) * np.sqrt(250) 

Not sure if it was a type but 'window_length 'was spelled wrong. Also, didn't include the required parameters in the compute method. However, the biggest issue is to include the 'axis=0' parameter in the 'std' method. Otherwise, numpy will use the flattened array. Actually a better method is 'numpy.nanstd' that will ignore any NaNs (which happen a lot). So maybe the same as above but with this line.

        out[:] = np.nanstd(returns, axis=0) * np.sqrt(250) 

See attached notebook.

BTW one can get some great ideas by looking at the open source code for zipline. In this case do a google search for 'zipline volatility factor github'. The second link returned (for me) is https://github.com/quantopian/zipline/blob/master/zipline/pipeline/factors/basic.py . Take a look towards the end and see the code for the 'AnnualizedVolatility' class.

Thanks a lot Dan. Really appreciate it. Will take a look at the zipline as well, great resource.