(注:普通用户没有level使用权限,确需使用请联系小Q)
# 本代码由可视化策略环境自动生成 2022年6月18日 10:14
# 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
def m15_func_bigquant_run(df, x, y):
if x == 0 or y == 0:
res = 1
elif np.isnan(x) or np.isnan(y):
res = 1
else:
res = x / y
return res
def m11_func_bigquant_run(df, x, freq):
def jugde_time(x):
if x.strftime('%H:%M:%S') >= '11:31:00' and x.strftime('%H:%M:%S') <= '13:00:00':
return False
else:
return True
a = df[['date', x.name]].set_index('date')
res = a.resample(freq, label='right', closed='right').sum()
res = res.loc[[i for i in res.index if jugde_time(i)==True]]
return res[x.name]
def m3_func_bigquant_run(df, x, freq):
a = df[['date', x.name]].set_index('date')
res = a.resample(freq, label='right', closed='right').last().dropna()
return res[x.name]
def m12_func_bigquant_run(df, close, op):
res = close.pct_change()
res.iloc[0] = close.iloc[0] / op.iloc[0] - 1
return res
def m9_func_bigquant_run(df, ret, RVar):
if RVar == 0:
result = 1
else:
result = (np.power(ret - (ret.mean()), 4).sum()) * (240) / np.power(RVar, 2)
return result
def m8_func_bigquant_run(df, ret, RVar):
if RVar == 0:
result = 1
else:
result = (np.power(ret, 4).sum()) * (240) / np.power(RVar, 2)
return result
def m7_func_bigquant_run(df, ret, RVar):
if RVar == 0:
result = 0
else:
result = (np.power(ret - (ret.mean()), 3).sum()) * np.sqrt(240) / np.power(RVar, 1.5)
return result
def m6_func_bigquant_run(df, ret, RVar):
if RVar == 0:
result = 0
else:
result = (np.power(ret, 3).sum()) * np.sqrt(240) / np.power(RVar, 1.5)
return result
def m5_func_bigquant_run(df, ret):
return np.power(ret - (ret.mean()), 2).sum()
def m4_func_bigquant_run(df, ret):
return (ret**2).sum()
m1 = M.input_features.v1(
features="""# _close = resample(close, '1min') ## 可修改K线粒度参数
# _open = resample(open, '1min') ## 可修改K线粒度参数
# _volume = resample_cumsum(volume, '1min') ## 可修改K线粒度参数
# _amount = resample_cumsum(amount, '1min') ## 可修改K线粒度参数
_close = close
_open = open
_volume = volume
_amount = amount
_ret = ret_sim(_close, _open)
_rk1 = _ret.iloc[:60] ## 开盘后前N分钟
_rk2 = _ret.iloc[60:-60] ## 除开开盘后前N分钟和收盘前N分钟
_rk3 = _ret.iloc[-60:] ## 收盘前N分钟
_vr1 = _volume.iloc[:30]
_vr2 = _volume.iloc[30:60]
_vr3 = _volume.iloc[90:120]
_vr4 = _volume.iloc[-120:-90]
_vr5 = _volume.iloc[-60:-30]
_vr6 = _volume.iloc[-30:]
# 1 收益方差 ht001
RetVar1 = RVar1(_ret)
RetVar2 = RVar2(_ret)
RetVar3 = (RVar2(_rk1) + RVar2(_rk2) + RVar2(_rk3)) / 3
# 2 收益偏度 ht001
RetSkew1 = RSkew1(_ret, RetVar1)
RetSkew2 = RSkew2(_ret, RetVar2)
RetSkew3 = (RSkew2(_rk1, RetVar2) + RSkew2(_rk2, RetVar2) + RSkew2(_rk3, RetVar2)) / 3
# 3 收益峰度 ht001
RetKurt1 = RKurt1(_ret, RetVar1)
RetKurt2 = RKurt2(_ret, RetVar2)
RetKurt3 = (RKurt2(_rk1, RetVar2) + RKurt2(_rk2, RetVar2) + RKurt2(_rk3, RetVar2)) / 3
# 4 收益波动因子 ht002
ret_vol = np.power(RVar1(_ret), 0.5)
# 5 上行波动因子 ht002
up_vol = np.sqrt(np.power(_ret*where(_ret>0, 1, 0), 2).sum())
# 6 下行波动因子 ht002
down_vol = np.sqrt(np.power(_ret*where(_ret<0, 1, 0), 2).sum())
# 上行波动占比因子 ht002
up_vol_ratio = nandivided(up_vol, RVar1(_ret))
# 7 下行波动占比因子 ht002
down_vol_ratio = nandivided(down_vol, RVar1(_ret))
# 8 成交量占比 ht003
volume_ratio_1 = _vr1.sum() / _volume.sum()
volume_ratio_2 = _vr2.sum() / _volume.sum()
volume_ratio_3 = _vr3.sum() / _volume.sum()
volume_ratio_4 = _vr4.sum() / _volume.sum()
volume_ratio_5 = _vr5.sum() / _volume.sum()
volume_ratio_6 = _vr6.sum() / _volume.sum()
# 9 量价相关性 ht003
_corr= _volume.corr(_close, method='pearson')
corr_PV = where(np.isnan(_corr), 0, _corr)
# 10 资金流入因子 ht003
_diff_close = _close.diff().fillna(0)
_amount_sum = _amount.sum()
flowIn_oneday = _volume.mul(_close).mul(_diff_close).div((_diff_close).abs()).fillna(0).sum()
# 11 趋势强度因子 ht003
trend_str = where(_diff_close.iloc[1:].abs().sum()==0, 1, _close.iloc[-1] - _close.iloc[0] / (_diff_close.iloc[1:].abs().sum()))
# 12 改进反转因子 ht003
inverse_plus = (_close.iloc[-1] - _close.iloc[30]) / _close.iloc[30]
# 13 日内动量因子 ht003
daily_mom_1 = (_close.iloc[15] - _close.iloc[0]) / _close.iloc[0]
daily_mom_2 = (_close.iloc[30] - _close.iloc[0]) / _close.iloc[0]
daily_mom_3 = (_close.iloc[-30] - _close.iloc[0]) / _close.iloc[0]
daily_mom_4 = (_close.iloc[-1] - _close.iloc[0]) / _close.iloc[0]""",
m_cached=False
)
m2 = M.instruments.v2(
start_date='2022-01-01',
end_date=T.live_run_param('trading_date', '2022-06-01'),
market='CN_STOCK_A',
instrument_list='000001.SZA',
m_cached=False
)
m15 = M.feature_extractor_user_function.v1(
name='nandivided',
func=m15_func_bigquant_run
)
m11 = M.feature_extractor_user_function.v1(
input_functions=m15.functions,
name='resample_cumsum',
func=m11_func_bigquant_run
)
m3 = M.feature_extractor_user_function.v1(
input_functions=m11.functions,
name='resample',
func=m3_func_bigquant_run
)
m12 = M.feature_extractor_user_function.v1(
input_functions=m3.functions,
name='ret_sim',
func=m12_func_bigquant_run
)
m9 = M.feature_extractor_user_function.v1(
input_functions=m12.functions,
name='RKurt2',
func=m9_func_bigquant_run
)
m8 = M.feature_extractor_user_function.v1(
input_functions=m9.functions,
name='RKurt1',
func=m8_func_bigquant_run
)
m7 = M.feature_extractor_user_function.v1(
input_functions=m8.functions,
name='RSkew2',
func=m7_func_bigquant_run
)
m6 = M.feature_extractor_user_function.v1(
input_functions=m7.functions,
name='RSkew1',
func=m6_func_bigquant_run
)
m5 = M.feature_extractor_user_function.v1(
input_functions=m6.functions,
name='RVar2',
func=m5_func_bigquant_run
)
m4 = M.feature_extractor_user_function.v1(
input_functions=m5.functions,
name='RVar1',
func=m4_func_bigquant_run
)
m14 = M.feature_extractor_1m.v2(
instruments=m2.data,
features=m1.data,
user_functions=m4.functions,
start_date='',
end_date='',
before_start_days=0,
workers=10,
parallel_mode='单机',
table_1m='level2_bar1m_CN_STOCK_A',
m_cached=False
)
[2022-06-16 14:23:10.000981] INFO: moduleinvoker: input_features.v1 开始运行..
[2022-06-16 14:23:10.042156] INFO: moduleinvoker: input_features.v1 运行完成[0.041219s].
[2022-06-16 14:23:10.048493] INFO: moduleinvoker: instruments.v2 开始运行..
[2022-06-16 14:23:10.082201] INFO: moduleinvoker: instruments.v2 运行完成[0.033702s].
[2022-06-16 14:23:10.098803] INFO: moduleinvoker: feature_extractor_user_function.v1 运行完成[0.000146s].
[2022-06-16 14:23:10.106669] INFO: moduleinvoker: feature_extractor_user_function.v1 运行完成[0.000146s].
[2022-06-16 14:23:10.114922] INFO: moduleinvoker: feature_extractor_user_function.v1 运行完成[0.000143s].
[2022-06-16 14:23:10.123821] INFO: moduleinvoker: feature_extractor_user_function.v1 运行完成[0.000138s].
[2022-06-16 14:23:10.131520] INFO: moduleinvoker: feature_extractor_user_function.v1 运行完成[0.000129s].
[2022-06-16 14:23:10.138728] INFO: moduleinvoker: feature_extractor_user_function.v1 运行完成[0.000142s].
[2022-06-16 14:23:10.145808] INFO: moduleinvoker: feature_extractor_user_function.v1 运行完成[0.000134s].
[2022-06-16 14:23:10.152688] INFO: moduleinvoker: feature_extractor_user_function.v1 运行完成[0.000134s].
[2022-06-16 14:23:10.159901] INFO: moduleinvoker: feature_extractor_user_function.v1 运行完成[0.000138s].
[2022-06-16 14:23:10.168853] INFO: moduleinvoker: feature_extractor_user_function.v1 运行完成[0.00015s].
[2022-06-16 14:23:10.178701] INFO: moduleinvoker: feature_extractor_1m.v2 开始运行..
[2022-06-16 14:23:10.228634] INFO: 高频特征抽取-分钟到日频: 【单机模式】, instruments:1, 并发数: 1
[2022-06-16 14:23:10.230354] INFO: 高频特征抽取-分钟到日频: 正在计算因子: 0%
[2022-06-16 14:23:12.911641] INFO: 高频特征抽取-分钟到日频: 计算因子结束 100%...
[2022-06-16 14:23:12.993701] INFO: 高频特征抽取-分钟到日频: extracted chunk 1/1 instruments, (95, 30).
[2022-06-16 14:23:12.995507] INFO: 高频特征抽取-分钟到日频: merge result .......
[2022-06-16 14:23:13.113122] INFO: 高频特征抽取-分钟到日频: extracted 1/1 instruments, (95, 30)
[2022-06-16 14:23:13.115440] INFO: moduleinvoker: feature_extractor_1m.v2 运行完成[2.936735s].
df = DataSource("level2_bar1m_CN_STOCK_A").read("000001.SZA", start_date="2022-01-01", end_date="2022-06-01")
df.head()
将高频因子降频到日频,再加入现有的日频模型中进行训练和预测
feature = m14.data.read()
print(feature.shape)
feature.head()