使用最近50分钟的价格、成交量、持仓量数据预测未来50分钟的涨跌幅。
股指期货 IF1906
LSTM深度学习算法
未来50分钟收益率
mean(open_intl,5)/amount
mean(open_intl,10)/amount
mean(open_intl,20)/amount
mean(open_intl,30)/amount
mean(open_intl,50)/amount
close/shift(close,30)
close/shift(close,20)
close/shift(close,10)
close/shift(close,5)
mean(amount,30)
max(high,10)/close
open_intl
sum(open_intl,10)/amount
amount/open_intl
mean(amount/open_intl,5)
mean(amount/open_intl,10)
mean(open_intl,5)
当预测值大于0.2时并且没有多仓,进场做多。
当预测值小于0.2时平掉所持有的多仓。
# 本代码由可视化策略环境自动生成 2022年6月24日 09:49
# 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
# Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
def m20_run_bigquant_run(input_1, input_2, input_3):
start_date=input_1.read_pickle()['start_date']
end_date=input_1.read_pickle()['end_date']
ins=input_1.read_pickle()['instruments']
df = DataSource('bar1m_CN_FUTURE').read(instruments=ins,start_date=start_date,end_date=end_date) #bar1m_IF1906.CFE
df['adjust_factor']=1.0
data_1 = DataSource.write_df(df)
return Outputs(data_1=data_1, data_2=None, data_3=None)
# 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
def m20_post_run_bigquant_run(outputs):
return outputs
# 用户的自定义层需要写到字典中,比如
# {
# "MyLayer": MyLayer
# }
m6_custom_objects_bigquant_run = {
}
# Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
def m2_run_bigquant_run(input_1, input_2, input_3):
predictions = input_1.read_pickle()
pred_result = predictions.reshape(predictions.shape[0])
dt = input_2.read_df()['date']
pred_df = pd.Series(pred_result, index=dt)
ds = DataSource.write_df(pred_df)
return Outputs(data_1=ds)
# 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
def m2_post_run_bigquant_run(outputs):
return outputs
# 交易引擎:初始化函数,只执行一次
def m19_initialize_bigquant_run(context):
# 加载预测数据
context.prediction = context.options['data'].read_df()
# 交易引擎:每个单位时间开盘前调用一次。
def m19_before_trading_start_bigquant_run(context, data):
# 盘前处理,订阅行情等
pass
# 交易引擎:tick数据处理函数,每个tick执行一次
def m19_handle_tick_bigquant_run(context, tick):
pass
# 交易引擎:bar数据处理函数,每个时间单位执行一次
def m19_handle_data_bigquant_run(context, data):
# 按日期过滤得到今日的预测数据
try:
prediction = context.prediction[data.current_dt]
except KeyError as e:
return
instrument = context.instruments[0]
cur_position = context.get_position(instrument, Direction.LONG)
# 交易逻辑
if prediction > 0.2 and cur_position.current_qty == 0:
context.order_target(instrument, 1)
print(data.current_dt, '买入!')
elif prediction < -0.2 and cur_position.current_qty > 0:
context.order_target(instrument, 0)
print(data.current_dt, '卖出!')
# 交易引擎:成交回报处理函数,每个成交发生时执行一次
def m19_handle_trade_bigquant_run(context, trade):
pass
# 交易引擎:委托回报处理函数,每个委托变化时执行一次
def m19_handle_order_bigquant_run(context, order):
pass
# 交易引擎:盘后处理函数,每日盘后执行一次
def m19_after_trading_bigquant_run(context, data):
pass
m3 = M.dl_layer_input.v1(
shape='50,17',
batch_shape='',
dtype='float32',
sparse=False,
name=''
)
m4 = M.dl_layer_lstm.v1(
inputs=m3.data,
units=32,
activation='linear',
recurrent_activation='hard_sigmoid',
use_bias=True,
kernel_initializer='glorot_uniform',
recurrent_initializer='Orthogonal',
bias_initializer='Ones',
unit_forget_bias=True,
kernel_regularizer='None',
kernel_regularizer_l1=0,
kernel_regularizer_l2=0,
recurrent_regularizer='None',
recurrent_regularizer_l1=0,
recurrent_regularizer_l2=0,
bias_regularizer='None',
bias_regularizer_l1=0,
bias_regularizer_l2=0,
activity_regularizer='None',
activity_regularizer_l1=0,
activity_regularizer_l2=0,
kernel_constraint='None',
recurrent_constraint='None',
bias_constraint='None',
dropout=0,
recurrent_dropout=0,
return_sequences=True,
implementation='0',
name=''
)
m10 = M.dl_layer_dropout.v1(
inputs=m4.data,
rate=0.2,
noise_shape='',
seed=0,
name=''
)
m25 = M.dl_layer_lstm.v1(
inputs=m10.data,
units=32,
activation='sigmoid',
recurrent_activation='hard_sigmoid',
use_bias=True,
kernel_initializer='glorot_uniform',
recurrent_initializer='Orthogonal',
bias_initializer='Zeros',
unit_forget_bias=True,
kernel_regularizer='None',
kernel_regularizer_l1=0,
kernel_regularizer_l2=0,
recurrent_regularizer='None',
recurrent_regularizer_l1=0,
recurrent_regularizer_l2=0,
bias_regularizer='None',
bias_regularizer_l1=0,
bias_regularizer_l2=0,
activity_regularizer='None',
activity_regularizer_l1=0,
activity_regularizer_l2=0,
kernel_constraint='None',
recurrent_constraint='None',
bias_constraint='None',
dropout=0,
recurrent_dropout=0,
return_sequences=False,
implementation='0',
name=''
)
m11 = M.dl_layer_dropout.v1(
inputs=m25.data,
rate=0.1,
noise_shape='',
seed=0,
name=''
)
m9 = M.dl_layer_dense.v1(
inputs=m11.data,
units=1,
activation='linear',
use_bias=True,
kernel_initializer='glorot_uniform',
bias_initializer='Zeros',
kernel_regularizer='None',
kernel_regularizer_l1=0,
kernel_regularizer_l2=0,
bias_regularizer='None',
bias_regularizer_l1=0,
bias_regularizer_l2=0,
activity_regularizer='None',
activity_regularizer_l1=0,
activity_regularizer_l2=0,
kernel_constraint='None',
bias_constraint='None',
name=''
)
m5 = M.dl_model_init.v1(
inputs=m3.data,
outputs=m9.data
)
m8 = M.input_features.v1(
features="""mean(open_intl,5)/amount
mean(open_intl,10)/amount
mean(open_intl,20)/amount
mean(open_intl,30)/amount
mean(open_intl,50)/amount
close/shift(close,30)
close/shift(close,20)
close/shift(close,10)
close/shift(close,5)
mean(amount,30)
max(high,10)/close
open_intl
sum(open_intl,10)/amount
amount/open_intl
mean(amount/open_intl,5)
mean(amount/open_intl,10)
mean(open_intl,5)
label= shift(close,-50)/close-1
"""
)
m24 = M.instruments.v2(
start_date='2019-04-01',
end_date='2019-06-22',
market='CN_FUTURE',
instrument_list="""IF1906.CFX
""",
max_count=0
)
m20 = M.cached.v3(
input_1=m24.data,
run=m20_run_bigquant_run,
post_run=m20_post_run_bigquant_run,
input_ports='',
params='{}',
output_ports=''
)
m12 = M.derived_feature_extractor.v3(
input_data=m20.data_1,
features=m8.data,
date_col='date',
instrument_col='instrument',
drop_na=False,
remove_extra_columns=False,
user_functions={}
)
m16 = M.filter.v3(
input_data=m12.data,
expr='date<\'2019-06-01\'',
output_left_data=False
)
m28 = M.dropnan.v1(
input_data=m16.data
)
m21 = M.filter.v3(
input_data=m12.data,
expr='date>=\'2019-06-01\'',
output_left_data=False
)
m13 = M.dropnan.v1(
input_data=m21.data
)
m15 = M.input_features.v1(
features="""mean(open_intl,5)/amount
mean(open_intl,10)/amount
mean(open_intl,20)/amount
mean(open_intl,30)/amount
mean(open_intl,50)/amount
close/shift(close,30)
close/shift(close,20)
close/shift(close,10)
close/shift(close,5)
mean(amount,30)
max(high,10)/close
open_intl
sum(open_intl,10)/amount
amount/open_intl
mean(amount/open_intl,5)
mean(amount/open_intl,10)
mean(open_intl,5)"""
)
m17 = M.dl_convert_to_bin.v2(
input_data=m13.data,
features=m15.data,
window_size=50,
feature_clip=5,
flatten=False,
window_along_col='instrument'
)
m14 = M.dl_convert_to_bin.v2(
input_data=m28.data,
features=m15.data,
window_size=50,
feature_clip=5,
flatten=False,
window_along_col='instrument'
)
m6 = M.dl_model_train.v1(
input_model=m5.data,
training_data=m14.data,
optimizer='RMSprop',
loss='mean_squared_error',
metrics='mse',
batch_size=256,
epochs=5,
custom_objects=m6_custom_objects_bigquant_run,
n_gpus=0,
verbose='1:输出进度条记录'
)
m7 = M.dl_model_predict.v1(
trained_model=m6.data,
input_data=m17.data,
batch_size=128,
n_gpus=0,
verbose='0:不显示'
)
m2 = M.cached.v3(
input_1=m7.data,
input_2=m13.data,
run=m2_run_bigquant_run,
post_run=m2_post_run_bigquant_run,
input_ports='',
params='{}',
output_ports=''
)
m19 = M.hftrade.v2(
instruments=m24.data,
options_data=m2.data_1,
start_date='2019-06-01',
end_date='',
initialize=m19_initialize_bigquant_run,
before_trading_start=m19_before_trading_start_bigquant_run,
handle_tick=m19_handle_tick_bigquant_run,
handle_data=m19_handle_data_bigquant_run,
handle_trade=m19_handle_trade_bigquant_run,
handle_order=m19_handle_order_bigquant_run,
after_trading=m19_after_trading_bigquant_run,
capital_base=1000000,
frequency='minute',
price_type='真实价格',
product_type='期货',
before_start_days='0',
order_price_field_buy='open',
order_price_field_sell='open',
benchmark='000300.HIX',
plot_charts=True,
disable_cache=False,
replay_bdb=False,
show_debug_info=False,
backtest_only=False
)
[2022-06-24 09:46:20.873792] INFO: moduleinvoker: dl_layer_input.v1 运行完成[0.001986s].
[2022-06-24 09:46:21.024309] INFO: moduleinvoker: dl_layer_lstm.v1 运行完成[0.140708s].
[2022-06-24 09:46:21.040341] INFO: moduleinvoker: dl_layer_dropout.v1 运行完成[0.006091s].
[2022-06-24 09:46:21.184992] INFO: moduleinvoker: dl_layer_lstm.v1 运行完成[0.136072s].
[2022-06-24 09:46:21.199877] INFO: moduleinvoker: dl_layer_dropout.v1 运行完成[0.004809s].
[2022-06-24 09:46:21.221431] INFO: moduleinvoker: dl_layer_dense.v1 运行完成[0.010826s].
[2022-06-24 09:46:21.275633] INFO: moduleinvoker: cached.v3 开始运行..
[2022-06-24 09:46:21.290567] INFO: moduleinvoker: 命中缓存
[2022-06-24 09:46:21.292212] INFO: moduleinvoker: cached.v3 运行完成[0.016589s].
[2022-06-24 09:46:21.298752] INFO: moduleinvoker: dl_model_init.v1 运行完成[0.066291s].
[2022-06-24 09:46:21.304276] INFO: moduleinvoker: input_features.v1 开始运行..
[2022-06-24 09:46:21.310458] INFO: moduleinvoker: 命中缓存
[2022-06-24 09:46:21.311984] INFO: moduleinvoker: input_features.v1 运行完成[0.007696s].
[2022-06-24 09:46:21.319319] INFO: moduleinvoker: instruments.v2 开始运行..
[2022-06-24 09:46:21.353741] INFO: moduleinvoker: 命中缓存
[2022-06-24 09:46:21.356000] INFO: moduleinvoker: instruments.v2 运行完成[0.036667s].
[2022-06-24 09:46:21.380312] INFO: moduleinvoker: cached.v3 开始运行..
[2022-06-24 09:46:21.387844] INFO: moduleinvoker: 命中缓存
[2022-06-24 09:46:21.390014] INFO: moduleinvoker: cached.v3 运行完成[0.009705s].
[2022-06-24 09:46:21.402905] INFO: moduleinvoker: derived_feature_extractor.v3 开始运行..
[2022-06-24 09:46:21.410385] INFO: moduleinvoker: 命中缓存
[2022-06-24 09:46:21.412700] INFO: moduleinvoker: derived_feature_extractor.v3 运行完成[0.009794s].
[2022-06-24 09:46:21.424647] INFO: moduleinvoker: filter.v3 开始运行..
[2022-06-24 09:46:21.467305] INFO: moduleinvoker: 命中缓存
[2022-06-24 09:46:21.469259] INFO: moduleinvoker: filter.v3 运行完成[0.044581s].
[2022-06-24 09:46:21.486295] INFO: moduleinvoker: dropnan.v1 开始运行..
[2022-06-24 09:46:21.493472] INFO: moduleinvoker: 命中缓存
[2022-06-24 09:46:21.495375] INFO: moduleinvoker: dropnan.v1 运行完成[0.009084s].
[2022-06-24 09:46:21.508779] INFO: moduleinvoker: filter.v3 开始运行..
[2022-06-24 09:46:21.516875] INFO: moduleinvoker: 命中缓存
[2022-06-24 09:46:21.518807] INFO: moduleinvoker: filter.v3 运行完成[0.010029s].
[2022-06-24 09:46:21.530910] INFO: moduleinvoker: dropnan.v1 开始运行..
[2022-06-24 09:46:21.538350] INFO: moduleinvoker: 命中缓存
[2022-06-24 09:46:21.540682] INFO: moduleinvoker: dropnan.v1 运行完成[0.009771s].
[2022-06-24 09:46:21.547664] INFO: moduleinvoker: input_features.v1 开始运行..
[2022-06-24 09:46:21.555758] INFO: moduleinvoker: 命中缓存
[2022-06-24 09:46:21.558426] INFO: moduleinvoker: input_features.v1 运行完成[0.01077s].
[2022-06-24 09:46:21.584609] INFO: moduleinvoker: dl_convert_to_bin.v2 开始运行..
[2022-06-24 09:46:21.594119] INFO: moduleinvoker: 命中缓存
[2022-06-24 09:46:21.596653] INFO: moduleinvoker: dl_convert_to_bin.v2 运行完成[0.012087s].
[2022-06-24 09:46:21.618260] INFO: moduleinvoker: dl_convert_to_bin.v2 开始运行..
[2022-06-24 09:46:21.626086] INFO: moduleinvoker: 命中缓存
[2022-06-24 09:46:21.628348] INFO: moduleinvoker: dl_convert_to_bin.v2 运行完成[0.010117s].
[2022-06-24 09:46:21.634952] INFO: moduleinvoker: dl_model_train.v1 开始运行..
[2022-06-24 09:46:21.647488] INFO: moduleinvoker: 命中缓存
[2022-06-24 09:46:21.649432] INFO: moduleinvoker: dl_model_train.v1 运行完成[0.014484s].
[2022-06-24 09:46:21.655509] INFO: moduleinvoker: dl_model_predict.v1 开始运行..
[2022-06-24 09:46:21.664910] INFO: moduleinvoker: 命中缓存
[2022-06-24 09:46:21.666953] INFO: moduleinvoker: dl_model_predict.v1 运行完成[0.011446s].
[2022-06-24 09:46:21.687754] INFO: moduleinvoker: cached.v3 开始运行..
[2022-06-24 09:46:21.701848] INFO: moduleinvoker: 命中缓存
[2022-06-24 09:46:21.704236] INFO: moduleinvoker: cached.v3 运行完成[0.016495s].
[2022-06-24 09:46:21.737567] INFO: moduleinvoker: hfbacktest.v1 开始运行..
[2022-06-24 09:46:21.743662] INFO: hfbacktest: biglearning V1.4.12
[2022-06-24 09:46:21.745629] INFO: hfbacktest: bigtrader v1.9.6 2022-06-20
[2022-06-24 09:46:21.783211] INFO: moduleinvoker: cached.v2 开始运行..
[2022-06-24 09:46:21.791417] INFO: moduleinvoker: 命中缓存
[2022-06-24 09:46:21.793948] INFO: moduleinvoker: cached.v2 运行完成[0.010751s].
[2022-06-24 09:46:21.870548] INFO: moduleinvoker: cached.v2 开始运行..
[2022-06-24 09:46:21.879211] INFO: moduleinvoker: 命中缓存
[2022-06-24 09:46:21.881765] INFO: moduleinvoker: cached.v2 运行完成[0.011234s].
[2022-06-24 09:46:21.957791] INFO: moduleinvoker: cached.v2 开始运行..
[2022-06-24 09:46:21.964733] INFO: moduleinvoker: 命中缓存
[2022-06-24 09:46:21.971778] INFO: moduleinvoker: cached.v2 运行完成[0.013984s].
[2022-06-24 09:46:27.195148] INFO: hfbacktest: backtest done, raw_perf_ds:DataSource(a3a988b2a5b14b66b25a0fe5f3e9b8e5T)
[2022-06-24 09:46:27.755343] INFO: moduleinvoker: hfbacktest.v1 运行完成[6.017789s].
[2022-06-24 09:46:27.756962] INFO: moduleinvoker: hftrade.v2 运行完成[6.042642s].