Hi All,
I've been trying to create a 'Risk Adjusted ROIC' CustomFactor (based on Max's TEM CapexVol factor). Basically I'd like it to take the average of a company's past X quarters' (say 12 quarters, or 3 years) ROIC, and divide it by the standard deviation of those ROIC values (sort of like a sharpe ratio, but using ROIC instead of Returns).
This is what I have. Could someone please help me identify any mistakes I've made, or point out any issues with doing it this way? Would I need to adjust for 'seasonality' and if so, how would one do this? Thank you!
import numpy as np
from quantopian.pipeline.data import factset as fsf
from quantopian.pipeline.factors import CustomFactor
from quantopian.pipeline.filters import QTradableStocksUS
class RiskAdjValue(CustomFactor):
"""
RiskAdjValue = Average value of past 12 quarters' / standard deviation of those values
"""
window_length = 65 * 12
def compute(self, today, assets, out, asof_date, value):
values = value
for column_ix in range(asof_date.shape[1]):
_, unique_indices = np.unique(asof_date[:, column_ix], return_index=True)
quarterly_values = values[unique_indices, column_ix]
if len(quarterly_values) < 12:
quarterly_values = np.hstack([
np.repeat([np.nan], 12 - len(quarterly_values)),
quarterly_values,
])
out[column_ix] = np.mean(quarterly_values[-12:]) / np.std(quarterly_values[-12:])
ra_roic = RiskAdjValue(
inputs=[fsf.Fundamentals.cf_roic_qf_asof_date,
fsf.Fundamentals.cf_roic_qf,
], mask=QTradableStocksUS() )