Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Sharpe Ratio Factor?

Hi,

I'm trying to implement a strategy that depends on the sharpe ratio over customized lookback period, e.g.,

Factor = average_returns[start:end] / std[start:end]

However, there seems no such Factor built in, is that right?

Thanks
Ben

1 response

If you haven't figured out already this is a possible solution:

class SharpeRatio(CustomFactor):  
    #inputs = [returns]  
    window_safe = True  
    def compute(self, today, assets, out, returns):  
        out[:] = np.nanmean(returns,axis=0) / np.nanstd(returns,axis=0)


returns = Returns(window_length=2, mask=universe)  
#returns = returns.log1p() # if you like to use log returns uncomment this line  
sharpe = SharpeRatio(inputs=[returns],  window_length=30, mask=universe)  

In the example above window_length corresponds to your start and if you need to provide an end too, you need to modify SharpeRatio for that :


class SharpeRatio(CustomFactor):  
    #inputs = [returns]  
    params = ('exclude_days',)  
    window_safe = True  
    def compute(self, today, assets, out, returns, exclude_days):  
        returns = returns[:-exclude_days]  
        out[:] = np.nanmean(returns,axis=0) / np.nanstd(returns,axis=0)

start = 30  
end  = 10  
returns = Returns(window_length=2, mask=universe)  
sharpe = SharpeRatio(inputs=[returns],  window_length=start, exclude_days=end, mask=universe)