本例子希望: 通过talib库构建自定义因子,本案例是构建kdj指标
# 本代码由可视化策略环境自动生成 2022年1月24日 14:57
# 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
def talib_KDJ(df,high, low, close, fastk_period=9, slowk_period=3, slowd_period=3):
#计算kd指标
k, d = talib.STOCH(high, low, close,
fastk_period=fastk_period,
slowk_period=slowk_period,
slowd_period=slowd_period)
j = 3 * k - 2 *d
return j
# 因为这俩表达式是我们新自定义的表达式,因此需要声明,以便在输入特征列表可使用
m4_user_functions_bigquant_run = {
'talib_KDJ_j':talib_KDJ
}
m1 = M.instruments.v2(
start_date='2021-01-01',
end_date='2021-12-31',
market='CN_STOCK_A',
instrument_list='',
max_count=0
)
m2 = M.input_features.v1(
features='talib_KDJ_j(high_0,low_0,close_0)'
)
m3 = M.general_feature_extractor.v7(
instruments=m1.data,
features=m2.data,
start_date='',
end_date='',
before_start_days=0
)
m4 = M.derived_feature_extractor.v3(
input_data=m3.data,
features=m2.data,
date_col='date',
instrument_col='instrument',
drop_na=False,
remove_extra_columns=False,
user_functions=m4_user_functions_bigquant_run
)
[2022-01-24 14:42:16.762989] INFO: moduleinvoker: instruments.v2 开始运行..
[2022-01-24 14:42:16.780111] INFO: moduleinvoker: 命中缓存
[2022-01-24 14:42:16.782608] INFO: moduleinvoker: instruments.v2 运行完成[0.019626s].
[2022-01-24 14:42:16.789479] INFO: moduleinvoker: input_features.v1 开始运行..
[2022-01-24 14:42:16.820661] INFO: moduleinvoker: input_features.v1 运行完成[0.03118s].
[2022-01-24 14:42:16.836897] INFO: moduleinvoker: general_feature_extractor.v7 开始运行..
[2022-01-24 14:42:16.847534] INFO: moduleinvoker: 命中缓存
[2022-01-24 14:42:16.850087] INFO: moduleinvoker: general_feature_extractor.v7 运行完成[0.013202s].
[2022-01-24 14:42:16.860232] INFO: moduleinvoker: derived_feature_extractor.v3 开始运行..
[2022-01-24 14:42:18.477401] INFO: derived_feature_extractor: 提取完成 talib_KDJ_j(high_0,low_0,close_0), 0.043s
[2022-01-24 14:42:19.997050] INFO: derived_feature_extractor: /y_2021, 1061527
[2022-01-24 14:42:20.245985] INFO: moduleinvoker: derived_feature_extractor.v3 运行完成[3.385762s].