两个要素,一是科技,二是金融。科技代表先进的生产力,金融代表生产关系再分配。
两大要素成就这个星球上最强的帝国。
后续我们会着重从科技、金融的视角,去看待历史,立足当下,解读未来。
今天是星球每周发布代码和策略集的日子。
今天还要新增一个策略,在昨天风险平价策略稳健的绝对型收益策略:长期年化10.6%的风险平价策略(代码+数据)的基础上,加上波动率。
波动率是用户自己可以调节的,波动率高,预期收益就大,反之亦然。
在风险平价给出权重的基础上,计算预期波动率,若波动率太高,则等比例降低风险资产权重,反之提升。
这是波动率设定为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 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 # 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/103676
站内所有文章皆来自网络转载或读者投稿,请勿用于商业用途。如有侵权、不妥之处,请联系站长并出示版权证明以便删除。敬请谅解!