多因子合成|MF
多因子本质是在时间截面上对候选标的进行评分,所以实际需要配合 Selector (策略选择算法) 使用。
因子计算时的标准化与中性化,参见: 因子标准化与中性化|NORM
公共参数:
fill_null (bool|True): 数据缺失时是否填充 nan 值, 否则以最近值填充
ic_n (int|5): 用于计算合成后因子 IC 时的 ic_n 日收益率
spearman (bool|True): 使用 spearman 获取相关系数,否则为 pearson
mode (int|2): 获取截面数据时排序模式: 0-降序, 1-升序, 2-不排序
save_all_factors (bool|False): 是否保存所有因子值,影响 get_actor/get_all_factors 方法
内建对因子合成算法
- hikyuu.trade_sys.MF_Weight(input, stks, weights, query, ref_stk[, ic_n=5, spearman=True, mode=0, save_all_factors=False])
按指定权重合成因子 = ind1 * weight1 + ind2 * weight2 + ... + indn * weightn,支持多种输入类型
- 参数:
input -- 因子输入,可以是FactorSet对象或Indicator序列
stks (sequense(stock)) -- 计算证券列表
weights (sequense(float)) -- 权重列表(需和因子数量等长)
query (Query) -- 日期范围
ref_stk (Stock) -- 参考证券用于日期对齐 (未指定时,默认为 sh000001)
ic_n (int) -- 默认 IC 对应的 N 日收益率
spearman (bool) -- 默认使用 spearman 计算相关系数,否则为 pearson
mode (int) -- 获取截面数据时排序模式: 0-降序, 1-升序, 2-不排序
save_all_factors (bool) -- 是否保存所有因子值,影响 get_actor/get_all_factors 方法
- 返回类型:
# 使用Indicator列表 indicators = [MA(CLOSE(), 5), MA(CLOSE(), 10)] weights = [0.6, 0.4] mf1 = MF_Weight(indicators, stocks, weights, query) # 使用FactorSet factor_set = FactorSet(indicators) mf2 = MF_Weight(factor_set, stocks, weights, query)
- hikyuu.trade_sys.MF_EqualWeight(input, stks, query, ref_stk[, ic_n=5])
等权重合成因子,支持多种输入类型
- 参数:
input -- 因子输入,可以是FactorSet对象或Indicator序列
stks (sequense(stock)) -- 计算证券列表
query (Query) -- 日期范围
ref_stk (Stock) -- 参考证券用于日期对齐 (未指定时,默认为 sh000001)
ic_n (int) -- 默认 IC 对应的 N 日收益率
spearman (bool) -- 默认使用 spearman 计算相关系数,否则为 pearson
mode (int) -- 获取截面数据时排序模式: 0-降序, 1-升序, 2-不排序
save_all_factors (bool) -- 是否保存所有因子值,影响 get_actor/get_all_factors 方法
- 返回类型:
# 使用Indicator列表 indicators = [MA(CLOSE(), 5), MA(CLOSE(), 10)] mf1 = MF_EqualWeight(indicators, stocks, query) # 使用FactorSet factor_set = FactorSet(indicators) mf2 = MF_EqualWeight(factor_set, stocks, query)
- hikyuu.trade_sys.MF_ICWeight(input, stks, query, ref_stk[, ic_n=5, ic_rolling_n=120])
滚动IC权重合成因子,支持多种输入类型
- 参数:
input -- 因子输入,可以是FactorSet对象或Indicator序列
stks (sequense(stock)) -- 计算证券列表
query (Query) -- 日期范围
ref_stk (Stock) -- 用于日期对齐的参考证券 (未指定时,默认为 sh000001)
ic_n (int) -- 默认 IC 对应的 N 日收益率
ic_rolling_n (int) -- IC 滚动周期
spearman (bool) -- 默认使用 spearman 计算相关系数,否则为 pearson
mode (int) -- 获取截面数据时排序模式: 0-降序, 1-升序, 2-不排序
save_all_factors (bool) -- 是否保存所有因子值,影响 get_actor/get_all_factors 方法
- 返回类型:
# 使用Indicator列表 indicators = [MA(CLOSE(), 5), MA(CLOSE(), 10)] mf1 = MF_ICWeight(indicators, stocks, query) # 使用FactorSet factor_set = FactorSet(indicators) mf2 = MF_ICWeight(factor_set, stocks, query)
- hikyuu.trade_sys.MF_ICIRWeight(input, stks, query, ref_stk[, ic_n=5, ic_rolling_n=120])
滚动ICIR权重合成因子,支持多种输入类型
- 参数:
input -- 因子输入,可以是FactorSet对象或Indicator序列
stks (sequense(stock)) -- 计算证券列表
query (Query) -- 日期范围
ref_stk (Stock) -- 用于日期对齐的参考证券 (未指定时,默认为 sh000001)
ic_n (int) -- 默认 IC 对应的 N 日收益率
ic_rolling_n (int) -- IC 滚动周期
spearman (bool) -- 默认使用 spearman 计算相关系数,否则为 pearson
mode (int) -- 获取截面数据时排序模式: 0-降序, 1-升序, 2-不排序
save_all_factors (bool) -- 是否保存所有因子值,影响 get_actor/get_all_factors 方法
- 返回类型:
# 使用Indicator列表 indicators = [MA(CLOSE(), 5), MA(CLOSE(), 10)] mf1 = MF_ICIRWeight(indicators, stocks, query) # 使用FactorSet factor_set = FactorSet(indicators) mf2 = MF_ICIRWeight(factor_set, stocks, query)
自定义多因子合成算法基类
自定义多因子合成算法接口:
MultiFactorBase._calculate()- 【必须】计算合成因子
多因子合成算法基类
- class hikyuu.trade_sys.MultiFactorBase
多因子合成基类
- name 名称
- query 查询条件
- __init__(self)
初始化构造函数
- 参数:
name (str) -- 名称
- get_param(self, name)
获取指定的参数
- 参数:
name (str) -- 参数名称
- 返回:
参数值
- 抛出:
out_of_range -- 无此参数
- set_param(self, name, value)
设置参数
- 参数:
name (str) -- 参数名称
value (int | bool | float | string) -- 参数值
- 抛出:
logic_error -- Unsupported type! 不支持的参数类型
- clone(self)
克隆操作
- get_ref_stock(self)
获取参考证券
- get_stock_list(self)
获取创建时指定的证券列表
- set_stock_list(self, stks)
重新指定证券列表
- 参数:
stks (list) -- 指定的证券列表
- get_stock_list_num(self)
获取创建时指定的证券列表中证券数量
- get_datetime_list(self)
获取参考日期列表(由参考证券通过查询条件获得)
- get_ref_indicators(self)
获取创建时输入的原始因子列表
- set_ref_indicators(self, inds)
重新设置原始因子列表
- 参数:
inds (list) -- 新的原始因子列表
- get_all_factors(self)
获取所有证券合成后的因子列表
- 返回:
[factor1, factor2, ...] 顺序与参考证券顺序相同
- get_ic(self[, ndays=0])
获取合成因子的IC, 长度与参考日期同
ndays 对于使用 IC/ICIR 加权的新因子,最好保持好 ic_n 一致, 但对于等权计算的新因子,不一定非要使用 ic_n 计算。 所以,ndays 增加了一个特殊值 0, 表示直接使用 ic_n 参数计算 IC
- 参数:
ndays (int) -- ic 的 ndays 日收益率
- 返回类型:
- get_icir(self, ir_n[, ic_n=0])
获取合成因子的 ICIR
- 参数:
ir_n (int) -- 计算 IR 的 n 窗口
ic_n (int) -- 计算 IC 的 n 窗口 (同 get_ic 中的 ndays)
- get_score(self, date[, start=0, end=Null])
获取指定日期截面的所有因子值,已经降序排列,相当于各证券日期截面评分。
- 参数:
date (Datetime) -- 指定日期
start (int) -- 取当日排名开始
end (int) -- 取当日排名结束(不包含本身)
func (function) -- (ScoreRecord)->bool 或 (Datetime, ScoreRecord)->bool 为原型的可调用对象
- 返回类型:
ScoreRecordList
- get_all_scores(self)
获取所有日期的所有评分,长度与参考日期相同
- 返回:
ScoreRecordList
- get_all_src_factors(self)
获取所有原始因子列表(如果指定了标准化、行业中性化, 返回为已处理的因子列表)
- 返回类型:
list
- 返回:
list IndicatorList stks x inds
- set_normalize(self, norm)
设置标准化或归一化方法(影响全部因子)
- 参数:
norm (NormalizeBase) -- 标准化或归一化方法实例
- add_special_normalize(self, name[, norm=None, category="", style_inds=[]])
对指定名称的指标应用特定的标准化/归一化、行业中性化、风格因子中性化操作。标准化操作、行业中性化、风格因子中性化彼此无关,可同时指定也可分开指定。
- 参数:
name (str) -- 特殊归一化方法名称
norm (Normalize) -- 特殊归一化方法
category (str) -- 行业中性化时,指定板块类别
style_inds (list[Indicator]) -- 用于中性化的风格指标列表
- _calculate(self, stks_inds)
计算每日证券合成因子,输入参数由上层函数计算后传入,如:
待计算的证券列表 - stk1, stk2 原始因子列表 - ind1, ind2 则传入的 stks_inds 为:[IndicatorList(stk1)[ind1, ind2], IndicatorList(stk2)[ind1, ind2]]
- 参数:
stks_inds (list) -- 与证券列表顺序相同已经计算好的所有证券的原始因子列表
- 返回:
按证券列表顺序存放的所有新的因子