今日策略:目标波动率。
在风险平价的基础上,我们可以预设目标波动率,以此调节风险资产的比重。但又尽量保持在风险平价最优。
目标波动率是在风险平价的基础上,测算组合的目标波动率,如果波动率过高,则降低风险资产比例,反之提升风险资产比例。
我们把目标波动率设定在7%左右,这个大家可以算行测试:
策略配置文件在代码发如下位置:
class TargetVol(Algo): def __init__( self, target_volatility, lookback=pd.DateOffset(months=3), lag=pd.DateOffset(days=0), covar_method="standard", annualization_factor=252, exclude=[] ): super(TargetVol, self).__init__() self.target_volatility = target_volatility self.lookback = lookback self.lag = lag self.covar_method = covar_method self.annualization_factor = annualization_factor self.exclude = exclude def __call__(self, target): #current_weights = target.temp["weights"] #selected = current_weights.keys() # if there were no weights already set then skip #if len(selected) == 0: # return True selected = target.temp["selected"] curr_symbols = target.df_bar.index selected = [s for s in selected if s in curr_symbols] t0 = target.now - self.lag prc = target.df_close.loc[t0 - self.lookback: t0, selected] returns = prc.pct_change().dropna() if len(returns) < 10: return True tw = calc_erc_weights( returns, initial_weights=None, risk_weights=None, covar_method="ledoit-wolf", risk_parity_method="ccd", maximum_iterations=100, tolerance=1e-8, ) current_weights = tw.dropna().to_dict() target.temp['weights'] = current_weights # calc covariance matrix # if self.covar_method == "ledoit-wolf": # covar = sklearn.covariance.ledoit_wolf(returns) if self.covar_method == "standard": covar = returns.cov() else: raise NotImplementedError("covar_method not implemented") weights = pd.Series( [current_weights[x] for x in covar.columns], index=covar.columns ) vol = np.sqrt( np.matmul(weights.values.T, np.matmul(covar.values, weights.values)) * self.annualization_factor ) # 波动率偏小 count = 0 if vol < self.target_volatility: while vol < self.target_volatility: count += 1 if count > 10: break mul = self.target_volatility / vol for k in target.temp["weights"].keys(): if k in self.exclude: # exclude通常为债券等低风险 continue target.temp["weights"][k] = ( target.temp["weights"][k] * mul ) weights = pd.Series( [target.temp["weights"][x] for x in covar.columns], index=covar.columns ) vol = np.sqrt( np.matmul(weights.values.T, np.matmul(covar.values, weights.values)) * self.annualization_factor ) if vol is float('NaN'): return True weights = pd.Series( [target.temp["weights"][x] for x in covar.columns], index=covar.columns ) # print(target.temp["weights"]) target.temp["weights"] = weights / weights.sum() # print(target.temp["weights"]) return True for k in target.temp["weights"].keys(): if k in self.exclude: # exclude通常为债券等低风险 continue target.temp["weights"][k] = ( target.temp["weights"][k] * self.target_volatility / vol ) ''' print(self.target_volatility[k] / vol,weights, new_weights) print(new_vol) ''' return True
目前积累的策略如下:
发布者:股市刺客,转载请注明出处:https://www.95sca.cn/archives/103424
站内所有文章皆来自网络转载或读者投稿,请勿用于商业用途。如有侵权、不妥之处,请联系站长并出示版权证明以便删除。敬请谅解!