使用最近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时并且没有空仓,进场做空。(如果有多单,需要先平掉多单)
# 本代码由可视化策略环境自动生成 2021年8月19日 09:14
# 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
# 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_IF1906.CFE').read(instruments=ins,start_date=start_date,end_date=end_date)
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 = {
}
import pandas as pd
# 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)
pred_df = pd.DataFrame(pred_df)
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):
# 加载预测数据
print("in initial", context.future_symbol("IF1906.CFX"))
context.prediction = context.options['data'].read().reset_index().rename(columns={0:"value"})
# 交易引擎:每个单位时间开盘前调用一次。
def m19_before_trading_start_bigquant_run(context, data):
pass
# 交易引擎:bar数据处理函数,每个单位执行一次
def m19_handle_tick_bigquant_run(context, data):
pass
# 交易引擎:bar数据处理函数,每个单位执行一次
def m19_handle_data_bigquant_run(context, data):
# 按日期过滤得到今日的预测数据
now = data.current_dt.strftime('%Y-%m-%d %H:%M:%S')
# 按日期过滤得到今日的预测数据
try:
prediction = context.prediction[context.prediction.date==now].iloc[0]["value"]
except Exception as e:
return
instrument = context.instruments[0]
# sid = context.symbol(instrument)
# cur_position = context.portfolio.positions[sid].amount
# 分别获取多头持仓,和空头持仓
position_long = context.get_position(instrument, Direction.LONG)
# position_short = context.get_position(instrument, Direction.SHORT)
#获取当前最新价
price = data.current(instrument, "close")
order_num = 1
# 交易逻辑
if prediction > 0.2 and position_long.avail_qty == 0:
context.buy_open(instrument, order_num, price, order_type=OrderType.MARKET)
print(data.current_dt, '开多!')
elif prediction < -0.2 and position_long.avail_qty > 0:
context.sell_close(instrument, position_long.avail_qty, price, order_type=OrderType.MARKET)
print(data.current_dt, '平多!')
# 交易引擎:成交回报处理函数,每个成交发生时执行一次
def m19_handle_trade_bigquant_run(context, data):
pass
# 交易引擎:委托回报处理函数,每个委托变化时执行一次
def m19_handle_order_bigquant_run(context, data):
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.CFE
""",
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=''
)
m18 = M.instruments.v2(
start_date='2019-06-01',
end_date='2019-06-22',
market='CN_FUTURE',
instrument_list="""IF1906.CFX
""",
max_count=0
)
m19 = M.hftrade.v1(
instruments=m18.data,
options_data=m2.data_1,
start_date='',
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=200000,
frequency='minute',
price_type='真实价格',
product_type='期货',
before_start_days='',
benchmark='000300.HIX',
plot_charts=True,
disable_cache=False,
show_debug_info=False,
backtest_only=False
)
[2021-08-18 09:33:10.725418] INFO: moduleinvoker: dl_layer_input.v1 运行完成[0.001557s].
[2021-08-18 09:33:10.850485] INFO: moduleinvoker: dl_layer_lstm.v1 运行完成[0.122188s].
[2021-08-18 09:33:10.856683] INFO: moduleinvoker: dl_layer_dropout.v1 运行完成[0.003259s].
[2021-08-18 09:33:10.979940] INFO: moduleinvoker: dl_layer_lstm.v1 运行完成[0.120503s].
[2021-08-18 09:33:10.985799] INFO: moduleinvoker: dl_layer_dropout.v1 运行完成[0.00305s].
[2021-08-18 09:33:10.997088] INFO: moduleinvoker: dl_layer_dense.v1 运行完成[0.008717s].
[2021-08-18 09:33:11.021622] INFO: moduleinvoker: cached.v3 开始运行..
[2021-08-18 09:33:11.028659] INFO: moduleinvoker: 命中缓存
[2021-08-18 09:33:11.030153] INFO: moduleinvoker: cached.v3 运行完成[0.008546s].
[2021-08-18 09:33:11.031752] INFO: moduleinvoker: dl_model_init.v1 运行完成[0.032277s].
[2021-08-18 09:33:11.033673] INFO: moduleinvoker: input_features.v1 开始运行..
[2021-08-18 09:33:11.039474] INFO: moduleinvoker: 命中缓存
[2021-08-18 09:33:11.040997] INFO: moduleinvoker: input_features.v1 运行完成[0.007324s].
[2021-08-18 09:33:11.043278] INFO: moduleinvoker: instruments.v2 开始运行..
[2021-08-18 09:33:11.048806] INFO: moduleinvoker: 命中缓存
[2021-08-18 09:33:11.050539] INFO: moduleinvoker: instruments.v2 运行完成[0.007252s].
[2021-08-18 09:33:11.055762] INFO: moduleinvoker: cached.v3 开始运行..
[2021-08-18 09:33:11.061863] INFO: moduleinvoker: 命中缓存
[2021-08-18 09:33:11.063237] INFO: moduleinvoker: cached.v3 运行完成[0.007475s].
[2021-08-18 09:33:11.065928] INFO: moduleinvoker: derived_feature_extractor.v3 开始运行..
[2021-08-18 09:33:11.071506] INFO: moduleinvoker: 命中缓存
[2021-08-18 09:33:11.072738] INFO: moduleinvoker: derived_feature_extractor.v3 运行完成[0.006808s].
[2021-08-18 09:33:11.075493] INFO: moduleinvoker: filter.v3 开始运行..
[2021-08-18 09:33:11.080572] INFO: moduleinvoker: 命中缓存
[2021-08-18 09:33:11.081750] INFO: moduleinvoker: filter.v3 运行完成[0.006256s].
[2021-08-18 09:33:11.084761] INFO: moduleinvoker: dropnan.v1 开始运行..
[2021-08-18 09:33:11.090066] INFO: moduleinvoker: 命中缓存
[2021-08-18 09:33:11.091252] INFO: moduleinvoker: dropnan.v1 运行完成[0.006492s].
[2021-08-18 09:33:11.093858] INFO: moduleinvoker: filter.v3 开始运行..
[2021-08-18 09:33:11.099123] INFO: moduleinvoker: 命中缓存
[2021-08-18 09:33:11.100286] INFO: moduleinvoker: filter.v3 运行完成[0.00643s].
[2021-08-18 09:33:11.102831] INFO: moduleinvoker: dropnan.v1 开始运行..
[2021-08-18 09:33:11.107934] INFO: moduleinvoker: 命中缓存
[2021-08-18 09:33:11.109137] INFO: moduleinvoker: dropnan.v1 运行完成[0.006307s].
[2021-08-18 09:33:11.110971] INFO: moduleinvoker: input_features.v1 开始运行..
[2021-08-18 09:33:11.116415] INFO: moduleinvoker: 命中缓存
[2021-08-18 09:33:11.117622] INFO: moduleinvoker: input_features.v1 运行完成[0.006652s].
[2021-08-18 09:33:11.128787] INFO: moduleinvoker: dl_convert_to_bin.v2 开始运行..
[2021-08-18 09:33:11.133864] INFO: moduleinvoker: 命中缓存
[2021-08-18 09:33:11.135103] INFO: moduleinvoker: dl_convert_to_bin.v2 运行完成[0.006321s].
[2021-08-18 09:33:11.142224] INFO: moduleinvoker: dl_convert_to_bin.v2 开始运行..
[2021-08-18 09:33:11.148174] INFO: moduleinvoker: 命中缓存
[2021-08-18 09:33:11.149401] INFO: moduleinvoker: dl_convert_to_bin.v2 运行完成[0.00718s].
[2021-08-18 09:33:11.151351] INFO: moduleinvoker: dl_model_train.v1 开始运行..
[2021-08-18 09:33:11.156444] INFO: moduleinvoker: 命中缓存
[2021-08-18 09:33:11.157655] INFO: moduleinvoker: dl_model_train.v1 运行完成[0.006308s].
[2021-08-18 09:33:11.159593] INFO: moduleinvoker: dl_model_predict.v1 开始运行..
[2021-08-18 09:33:11.165187] INFO: moduleinvoker: 命中缓存
[2021-08-18 09:33:11.166459] INFO: moduleinvoker: dl_model_predict.v1 运行完成[0.00687s].
[2021-08-18 09:33:11.170667] INFO: moduleinvoker: cached.v3 开始运行..
[2021-08-18 09:33:11.176349] INFO: moduleinvoker: 命中缓存
[2021-08-18 09:33:11.177543] INFO: moduleinvoker: cached.v3 运行完成[0.006877s].
[2021-08-18 09:33:11.179660] INFO: moduleinvoker: instruments.v2 开始运行..
[2021-08-18 09:33:11.185654] INFO: moduleinvoker: 命中缓存
[2021-08-18 09:33:11.186971] INFO: moduleinvoker: instruments.v2 运行完成[0.007306s].
[2021-08-18 09:33:11.199996] INFO: moduleinvoker: hfbacktest.v1 开始运行..
[2021-08-18 09:33:11.204285] INFO: hfbacktest: biglearning V1.2.5
[2021-08-18 09:33:11.205427] INFO: hfbacktest: bigtrader v1.7.7
[2021-08-18 09:33:11.245812] INFO: moduleinvoker: cached.v2 开始运行..
[2021-08-18 09:33:11.253185] INFO: moduleinvoker: 命中缓存
[2021-08-18 09:33:11.254719] INFO: moduleinvoker: cached.v2 运行完成[0.008912s].
[2021-08-18 09:33:11.299679] INFO: moduleinvoker: cached.v2 开始运行..
[2021-08-18 09:33:11.314000] INFO: moduleinvoker: 命中缓存
[2021-08-18 09:33:11.315519] INFO: moduleinvoker: cached.v2 运行完成[0.015845s].
[2021-08-18 09:33:11.356146] INFO: moduleinvoker: cached.v2 开始运行..
[2021-08-18 09:33:11.369220] INFO: moduleinvoker: 命中缓存
[2021-08-18 09:33:11.370742] INFO: moduleinvoker: cached.v2 运行完成[0.014599s].
[2021-08-18 09:33:16.758998] INFO: hfbacktest: backtest done, raw_perf_ds:DataSource(d089616a4aed42268046552785b6b912T)
[2021-08-18 09:33:17.211878] INFO: moduleinvoker: hfbacktest.v1 运行完成[6.011888s].
[2021-08-18 09:33:17.213432] INFO: moduleinvoker: hftrade.v1 运行完成[6.024013s].