风险平价之上的目标波动率控制7%,年化7%。(源代码+数据)

今日策略:目标波动率。

在风险平价的基础上,我们可以预设目标波动率,以此调节风险资产的比重。但又尽量保持在风险平价最优。

目标波动率是在风险平价的基础上,测算组合的目标波动率,如果波动率过高,则降低风险资产比例,反之提升风险资产比例。

我们把目标波动率设定在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

目前积累的策略如下:

图片

历史文章:

今日策略:全球资产风险平价,年化8%,就图个省心(源代码+数据)

Quantlab v3.9.2:策略集合——创成长与红利低波动的智能Beta策略(年化29.3%,最大回撤24%)(附源码)

Quantlab3.9代码:内置大模型LLM因子挖掘,全A股数据源以及自带GUI界面

AI量化实验室——2024量化投资的星辰大海

发布者:股市刺客,转载请注明出处:https://www.95sca.cn/archives/103425
站内所有文章皆来自网络转载或读者投稿,请勿用于商业用途。如有侵权、不妥之处,请联系站长并出示版权证明以便删除。敬请谅解!

(0)
股市刺客的头像股市刺客
上一篇 2024 年 7 月 29 日
下一篇 2024 年 7 月 29 日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注