PLUS会员

组合优化器使用文档

由nolamebrand创建,最终由nolamebrand 被浏览 589 用户

组合优化器是宽邦科技为满足机构投资者对于股票组合优化、绩效归因、风险控制和指数增强需求而提供的一款优化器。

使用概览

  1. 调用接口:T.PORTFOLIO_OPTIMIZERS
  2. 每日初始化:T.PORTFOLIO_OPTIMIZERS.get_today_factor_data
  3. 权重优化:T.PORTFOLIO_OPTIMIZERS.optimize

\

调用接口

opt=T.PORTFOLIO_OPTIMIZERS(stock_pool, start_date, end_date, model_type='daily', benchmark='000905.HIX')

该命令是整个组合优化器的初始化接口,即使用组合优化进行权重选择之前,必须得先运行此行命令并赋值于一个变量

字段解释:

  • stock_pool: 标的池数据(比如中证500成份股数据,需包含字段:date, instrument, weight/score)

  • start_date: 标的池中数据的起始日期

  • end_date: 标的池中数据的结束日期

  • model_type: 因子收益协方差矩阵和特意收益率矩阵的数据类型,三选一:daily, short, long(默认为daily)

  • benchmark: 基准数据代码(默认为’000905.HIX’)

    \

输出定义为opt的方法,可继续调用函数有:opt.get_today_factor_data, opt.optimize, 以及其余所有的目标函数和约束条件等

\

每日初始化接口

opt.get_today_factor_data(init_pool,current_date) (无需定义变量赋值)

该命令的用法是在整体初始化之后,进行每日权重分配以及约束条件和目标函数确定之前调用该接口

字段解释:

  • init_pool: 每日标的池数据(比如中证500成份股数据,需包含字段:date, instrument, weight/score)
  • current_date: 当前初始化日期

无输出结果,仅作每日初始化使用

\

权重优化接口

weights = opt.optimize(target_func, current_date, cons, stock_count=None, response=True, hard=True, verbose=False)

字段解释:

  • target_func: 目标函数(类型: method)
  • current_date: 当前日期(类型: string)
  • cons: 约束条件,其中包含边界约束bounds(类型: methods)
  • stock_count: 权重优化最大股票数量,若为None则表示没有设定限制(类型: int),默认为None
  • response: 优化错误时,设置为True则返回0值,设置为False则不返回值(类型: bool),默认为True
  • hard: 软硬约束定义,设置为True的话则为软约束,为False则为硬约束(类型: bool),默认为True
  • verbose: 是否需要输出优化日志(类型: bool),默认为False

输出结果为每天优化后的组合权重结果,主要列名有【date, instrument, weights】

目标函数 & 约束条件

目标函数target_func和约束条件constraints会以方法的形式传入权重优化接口opt.optimize,所以需要在传入之前进行方法的定义

目标函数 接口 可调参数
最大化风险调整后收益 MaxReturn 风险厌恶指数lam,默认为0
最小化风险 MinRisk
最小化风格偏离 MinStyleDeviation target_function: style - 风格因子名称以及偏离值(类型:dict),例如 {‘size‘:1} 表达的是将size因子暴露于其均值+1倍标准差之上 \n if_pred - 是否为预测数据(类型:bool),默认为False \n relative - 是否相对于基准(类型:bool),默认为False
最大化预测得分 MaxScore
约束条件or边界条件 接口 可调参数
总预算权重约束 TotalWeightsConstraint upper_limit - 资金组合总权重上限(类型:int / float)
个股权重边界 Bounds lower_limit - 个股权重下限(类型:int / float) \n upper_limit - 个股权重上限(类型:int / float) \n relative - 是否基于基准(类型:bool;默认为False)
个股风格约束 StyleConstraint style - 约束风格名称(类型:str / list)- 可使用风格因子列表见后表 \n lower_limit - 风格约束下限(单位为均值加n倍标准差,类型:int / float) \n upper_limit - 风格约束上限(单位为均值加n倍标准差,类型:int / float) \n relative - 是否基于基准(类型:bool;默认为False) \n priority - 软约束优先级(类型:int)
个股风格除外约束 ExcludeStyleConstraint exclude_style - 除外风格名称(类型:str / list) \n lower_limit - 风格约束下限(单位为均值加n倍标准差,类型:int / float) \n upper_limit - 风格约束上限(单位为均值加n倍标准差,类型:int / float) \n relative - 是否基于基准(类型:bool;默认为False) \n priority - 软约束优先级(类型:int)
个股行业约束 IndustryWeigtedConstraint style - 约束行业名称(类型:str / list)- 可使用行业因子列表见后表 \n lower_limit - 行业约束下限(单位为均值加n倍标准差,类型:int / float) \n upper_limit - 行业约束上限(单位为均值加n倍标准差,类型:int / float) \n relative - 是否基于基准(类型:bool;默认为False) \n priority - 软约束优先级(类型:int)
个股行业除外约束 ExcludeIndustryWeigtedConstraint industry - 除外行业(类型:str / list) \n lower_limit - 除外行业约束下限(单位为均值加n倍标准差,类型:int / float) \n upper_limit - 除外行业约束上限(单位为均值加n倍标准差,类型:int / float) \n relative - 是否基于基准(类型:bool;默认为False) \n priority - 软约束优先级(类型:int)
预期收益约束 PredictReturnConstraint lower_limit - 收益约束下限(类型:int / float) \n priority - 软约束优先级(类型:int)
波动率约束 VolatilityConstraint upper_limit - 约束上限(类型:int / float) \n relative - 是否基于基准(类型:bool;默认为False) \n priority - 软约束优先级(类型:int)
换手率约束 TurnoverConstraint turnrate - 换手率约束(类型:int / float) \n priority - 软约束优先级(类型:int)
追踪误差上限约束 TrackingError upper_limit - 追踪误差上限(类型:int / float) \n priority - 软约束优先级(类型:int)
基准成份股权重约束 BenchmarkWeightedConstraint lower_limit - 最低基准成份占比(类型:int / float) \n priority - 软约束优先级(类型:int)
行业内个股权重约束 IndustryComponentConstraint industry - 行业名称(类型:int / list) \n limit - 最低行业内个股占比(类型:int / float) \n priority - 软约束优先级(类型:int)

可用风格因子&行业因子

  • 风格因子
    • leverage
    • beta
    • momentum
    • growth
    • nonlinear_size
    • liquidity
    • size
    • residual_volatility
    • value
  • 行业因子
    • industry_prefix_* → 其中星号为申万一级行业code,具体code可见:
DataSource("basic_info_IndustrySw").read()

使用方法示例

目标函数&约束条件定义方法

目标函数只能有一个,约束条件可以通过列表的方式进行多个方法的存入

## 定义多个约束条件(list / str)
constraints = [
               opt.StyleConstraint('beta', lower_limit=0.1, upper_limit=1, relative=True, priority=1), ## 风格约束
               opt.ExcludeStyleConstraint('beta', lower_limit=-0.1, upper_limit=0.1, relative=True, priority=0), ## 风格除外约束
               opt.TotalWeightsConstraint(upper_limit=1), ## 总权重约束
               opt.Bounds(lower_limit=0, upper_limit=0.04, relative=True) ## 个股边界约束
              ]
## 定义目标函数
objective= opt.MaxReturn(lam=0.1)
## 权重优化
weights = opt.optimize(objective, current_date, constraints, stock_count=50, hard=False)

软硬约束定义:

其中,priority为当optimize中的hard=False时,即定义优化约束为软约束的时候,优化失败时会优先去掉priority值越小的约束。比如约束A的参数priority=1,约束B的参数priority=0,则在hard=False的时候,首次优化失败则会删除约束B进行第二次约束,若再次优化失败则删掉约束A,依次类推。约束条件中的TotalWeightsConstraint和Bounds是不带priority参数的,也就是说其不参与软硬约束的判断。

\

基于成长因子的中证500指数增强策略示例

标签

风险控制
评论
  • 1. 能否提供一个对pres运用优化器提高绩效表现的例子。 2. 有些参数,比如style - 约束风格名称(类型:str / list)里面str有哪些可选,能否补充下。 \
  • 已经对因子列表做了补充 不是很清楚“对pres运用优化器”中pres具体是指什么含义呢?
  • 就是一个股票预测集,里面有评分,能否有个应用优化器分配权重提高原先预测集轮仓效果的实例?
  • current_date is: 2022-06-24 amount of unique_stocks : 2, weights objective should be considered 初始权重之和: 0.0 Using Soft Constraints, and the Optimization result is infeasible before the relaxation The times of trial 1, and the Optimize result is infeasible removing constraints... Relaxation removing constraints Successfully New Result within the Rest of Constraints is infeasible The times of trial 2, and the Optimize result is infeasible removing constraints... Relaxation removing constraints Successfully New Result within the Rest of Constraints is infeasible The Problem should be Reconsidered \ 自己写了一个策略,一直报错
  • 检查下约束条件是否过于严格和凹凸性,不是所有的约束条件都有解的
{link}