版本 v1.0
### 深度学习策略的交易规则
### 策略构建步骤
### 策略的实现
在画布左侧模块列表中依次拖入输入层模块、Reshape层模块、Conv2D层模块、Reshape层模块、LSTM层模块、Dropout层模块和全连接层模块(两组),构成深度学习网络构架,
最后通过“构建(深度学习)”模块组装各层。这里需要注意:
输入层的shape参数是 窗口滚动数据集的大小 X 因子数量 , 本例为 50 行 X 5个因子
ReShape层的参数是 窗口滚动数据集的大小 X 因子数量 X 1 ,本例为 50 行 X 5个因子 X1
Conv2D层中的 kernel_size参数是滑动窗口的尺寸,本例中使用 3行 X 5列 的窗口, 每次滑动的步长为 1行 X 1列 , 卷积核数目为32,这里的窗口设置决定了后面ReShape层的参数
ReShape层中的target_shape 参数,这是由 窗口滚动数据集 X 因子数量 和 Conv2D层中设置的窗口尺寸以及步长决定的。本例中 50行 X 5因子 的输入数据,使用 3行 X5列 的窗口滑动取数据,
每次移动1行,共计可以得到48次数据(即可以通过滑动3行 X 5列的窗口48次来获取完整的数据),因此target_shape= 48 X 卷积核数32
LSTM层的输出空间维度设置为卷积核数32,并设置激活函数
Dropout层是防止过度拟合采用的主动裁剪数据技术,这里设置rate 为0.8
全连接层共两层,第一层的输出空间维度与LSTM的输出维度保持一致为32,第二层将第一层的32维数据转变为1维数据输出,即获取预测的label值,此例为0到1之间的连续值,可以认为是上涨的概率。
如果当日预测的上涨概率大于0.5,则保持持仓或买入
如果当日预测的上涨概率小于0.5,则卖出股票或保持空仓。
通过 trade 模块中的初始化函数定义交易手续费和滑点,通过 context.prediction 获取每日的上涨概率预测结果;
通过 trade 模块中的主函数(handle函数)查看每日的买卖交易信号,按照买卖原则执行相应的买入/卖出操作。
可视化策略实现如下:
# 本代码由可视化策略环境自动生成 2021年12月6日 22:12
# 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
# 用户的自定义层需要写到字典中,比如
# {
# "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):
test_data = input_2.read_pickle()
pred_label = input_1.read_pickle()
pred_result = pred_label.reshape(pred_label.shape[0])
dt = input_3.read_df()['date'][-1*len(pred_result):]
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 m1_initialize_bigquant_run(context):
# 加载预测数据
context.prediction = context.options['data'].read_df()
# 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数
context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))
# 回测引擎:每日数据处理函数,每天执行一次
def m1_handle_data_bigquant_run(context, data):
# 按日期过滤得到今日的预测数据
try:
prediction = context.prediction[data.current_dt.strftime('%Y-%m-%d')]
except KeyError as e:
return
instrument = context.instruments[0]
sid = context.symbol(instrument)
cur_position = context.portfolio.positions[sid].amount
# 交易逻辑
if prediction > 0.5 and cur_position == 0:
context.order_target_percent(context.symbol(instrument), 1)
print(data.current_dt, '买入!')
elif prediction < 0.5 and cur_position > 0:
context.order_target_percent(context.symbol(instrument), 0)
print(data.current_dt, '卖出!')
# 回测引擎:准备数据,只执行一次
def m1_prepare_bigquant_run(context):
pass
# 回测引擎:每个单位时间开始前调用一次,即每日开盘前调用一次。
def m1_before_trading_start_bigquant_run(context, data):
pass
m3 = M.dl_layer_input.v1(
shape='50,5',
batch_shape='',
dtype='float32',
sparse=False,
name=''
)
m13 = M.dl_layer_reshape.v1(
inputs=m3.data,
target_shape='50,5,1',
name=''
)
m14 = M.dl_layer_conv2d.v1(
inputs=m13.data,
filters=32,
kernel_size='3,5',
strides='1,1',
padding='valid',
data_format='channels_last',
dilation_rate='1,1',
activation='relu',
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=''
)
m15 = M.dl_layer_reshape.v1(
inputs=m14.data,
target_shape='48,32',
name=''
)
m4 = M.dl_layer_lstm.v1(
inputs=m15.data,
units=32,
activation='tanh',
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=False,
implementation='2',
name=''
)
m11 = M.dl_layer_dropout.v1(
inputs=m4.data,
rate=0.4,
noise_shape='',
name=''
)
m10 = M.dl_layer_dense.v1(
inputs=m11.data,
units=32,
activation='tanh',
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=''
)
m12 = M.dl_layer_dropout.v1(
inputs=m10.data,
rate=0.8,
noise_shape='',
name=''
)
m9 = M.dl_layer_dense.v1(
inputs=m12.data,
units=1,
activation='sigmoid',
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="""(close_0/close_1-1)*10
(high_0/high_1-1)*10
(low_0/low_1-1)*10
(open_0/open_1-1)*10
(volume_0/volume_1-1)*10"""
)
m24 = M.instruments.v2(
start_date='2017-06-02',
end_date='2017-10-30',
market='CN_STOCK_A',
instrument_list='600009.SHA',
max_count=0
)
m21 = M.advanced_auto_labeler.v2(
instruments=m24.data,
label_expr="""# #号开始的表示注释
# 0. 每行一个,顺序执行,从第二个开始,可以使用label字段
# 1. 可用数据字段见 https://bigquant.com/docs/develop/datasource/deprecated/history_data.html
# 添加benchmark_前缀,可使用对应的benchmark数据
# 2. 可用操作符和函数见 `表达式引擎 <https://bigquant.com/docs/develop/bigexpr/usage.html>`_
# 计算收益:5日收盘价(作为卖出价格)除以明日开盘价(作为买入价格)
where(shift(close, -10) / close -1>0,1,0)
# 过滤掉一字涨停的情况 (设置label为NaN,在后续处理和训练中会忽略NaN的label)
where(shift(high, -1) == shift(low, -1), NaN, label)
""",
start_date='',
end_date='',
benchmark='000300.SHA',
drop_na_label=True,
cast_label_int=True,
user_functions={}
)
m22 = M.general_feature_extractor.v7(
instruments=m24.data,
features=m8.data,
start_date='',
end_date='',
before_start_days=90
)
m23 = M.derived_feature_extractor.v3(
input_data=m22.data,
features=m8.data,
date_col='date',
instrument_col='instrument',
drop_na=False,
remove_extra_columns=False,
user_functions={}
)
m17 = M.join.v3(
data1=m21.data,
data2=m23.data,
on='date',
how='inner',
sort=True
)
m18 = M.dropnan.v1(
input_data=m17.data
)
m25 = M.dl_convert_to_bin.v2(
input_data=m18.data,
features=m8.data,
window_size=50,
feature_clip=5,
flatten=False,
window_along_col=''
)
m6 = M.dl_model_train.v1(
input_model=m5.data,
training_data=m25.data,
optimizer='Adam',
loss='binary_crossentropy',
metrics='accuracy',
batch_size=2048,
epochs=10,
custom_objects=m6_custom_objects_bigquant_run,
n_gpus=1,
verbose='1:输出进度条记录'
)
m28 = M.instruments.v2(
start_date=T.live_run_param('trading_date', '2019-02-11'),
end_date=T.live_run_param('trading_date', '2019-08-01'),
market='CN_STOCK_A',
instrument_list='600009.SHA',
max_count=0
)
m16 = M.general_feature_extractor.v7(
instruments=m28.data,
features=m8.data,
start_date='',
end_date='',
before_start_days=90
)
m26 = M.derived_feature_extractor.v3(
input_data=m16.data,
features=m8.data,
date_col='date',
instrument_col='instrument',
drop_na=False,
remove_extra_columns=False,
user_functions={}
)
m20 = M.dropnan.v1(
input_data=m26.data
)
m27 = M.dl_convert_to_bin.v2(
input_data=m20.data,
features=m8.data,
window_size=50,
feature_clip=5,
flatten=False,
window_along_col=''
)
m7 = M.dl_model_predict.v1(
trained_model=m6.data,
input_data=m27.data,
batch_size=10240,
n_gpus=0,
verbose='2:每个epoch输出一行记录'
)
m2 = M.cached.v3(
input_1=m7.data,
input_2=m27.data,
input_3=m20.data,
run=m2_run_bigquant_run,
post_run=m2_post_run_bigquant_run,
input_ports='',
params='{}',
output_ports=''
)
m1 = M.trade.v4(
instruments=m28.data,
options_data=m2.data_1,
start_date='',
end_date='',
initialize=m1_initialize_bigquant_run,
handle_data=m1_handle_data_bigquant_run,
prepare=m1_prepare_bigquant_run,
before_trading_start=m1_before_trading_start_bigquant_run,
volume_limit=0.025,
order_price_field_buy='open',
order_price_field_sell='close',
capital_base=1000000,
auto_cancel_non_tradable_orders=True,
data_frequency='daily',
price_type='真实价格',
product_type='股票',
plot_charts=True,
backtest_only=False,
benchmark='000300.HIX'
)
[2019-12-25 17:03:43.097180] INFO: bigquant: cached.v3 开始运行..
[2019-12-25 17:03:43.163841] INFO: bigquant: cached.v3 运行完成[0.066663s].
[2019-12-25 17:03:43.166828] INFO: bigquant: input_features.v1 开始运行..
[2019-12-25 17:03:43.360921] INFO: bigquant: 命中缓存
[2019-12-25 17:03:43.363001] INFO: bigquant: input_features.v1 运行完成[0.196099s].
[2019-12-25 17:03:43.366549] INFO: bigquant: instruments.v2 开始运行..
[2019-12-25 17:03:43.477476] INFO: bigquant: 命中缓存
[2019-12-25 17:03:43.479269] INFO: bigquant: instruments.v2 运行完成[0.112714s].
[2019-12-25 17:03:43.481732] INFO: bigquant: advanced_auto_labeler.v2 开始运行..
[2019-12-25 17:03:43.507546] INFO: bigquant: 命中缓存
[2019-12-25 17:03:43.509044] INFO: bigquant: advanced_auto_labeler.v2 运行完成[0.027321s].
[2019-12-25 17:03:43.540133] INFO: bigquant: general_feature_extractor.v7 开始运行..
[2019-12-25 17:03:43.570372] INFO: bigquant: 命中缓存
[2019-12-25 17:03:43.572034] INFO: bigquant: general_feature_extractor.v7 运行完成[0.031913s].
[2019-12-25 17:03:43.574517] INFO: bigquant: derived_feature_extractor.v3 开始运行..
[2019-12-25 17:03:43.601498] INFO: bigquant: 命中缓存
[2019-12-25 17:03:43.603093] INFO: bigquant: derived_feature_extractor.v3 运行完成[0.02857s].
[2019-12-25 17:03:43.605492] INFO: bigquant: join.v3 开始运行..
[2019-12-25 17:03:43.636502] INFO: bigquant: 命中缓存
[2019-12-25 17:03:43.638231] INFO: bigquant: join.v3 运行完成[0.032726s].
[2019-12-25 17:03:43.640528] INFO: bigquant: dropnan.v1 开始运行..
[2019-12-25 17:03:43.668224] INFO: bigquant: 命中缓存
[2019-12-25 17:03:43.669868] INFO: bigquant: dropnan.v1 运行完成[0.029315s].
[2019-12-25 17:03:43.703403] INFO: bigquant: dl_convert_to_bin.v2 开始运行..
[2019-12-25 17:03:43.742831] INFO: bigquant: 命中缓存
[2019-12-25 17:03:43.744461] INFO: bigquant: dl_convert_to_bin.v2 运行完成[0.041069s].
[2019-12-25 17:03:43.746493] INFO: bigquant: dl_model_train.v1 开始运行..
[2019-12-25 17:03:44.186790] INFO: dl_model_train: 准备训练,训练样本个数:102,迭代次数:10
[2019-12-25 17:03:51.730037] INFO: dl_model_train: 训练结束,耗时:7.54s
[2019-12-25 17:03:51.926376] INFO: bigquant: dl_model_train.v1 运行完成[8.179867s].
[2019-12-25 17:03:51.930317] INFO: bigquant: instruments.v2 开始运行..
[2019-12-25 17:03:51.957151] INFO: bigquant: 命中缓存
[2019-12-25 17:03:51.958847] INFO: bigquant: instruments.v2 运行完成[0.028513s].
[2019-12-25 17:03:51.986376] INFO: bigquant: general_feature_extractor.v7 开始运行..
[2019-12-25 17:03:52.012364] INFO: bigquant: 命中缓存
[2019-12-25 17:03:52.013933] INFO: bigquant: general_feature_extractor.v7 运行完成[0.02757s].
[2019-12-25 17:03:52.016067] INFO: bigquant: derived_feature_extractor.v3 开始运行..
[2019-12-25 17:03:52.045989] INFO: bigquant: 命中缓存
[2019-12-25 17:03:52.047599] INFO: bigquant: derived_feature_extractor.v3 运行完成[0.031512s].
[2019-12-25 17:03:52.049875] INFO: bigquant: dropnan.v1 开始运行..
[2019-12-25 17:03:52.076936] INFO: bigquant: 命中缓存
[2019-12-25 17:03:52.078471] INFO: bigquant: dropnan.v1 运行完成[0.028595s].
[2019-12-25 17:03:52.104474] INFO: bigquant: dl_convert_to_bin.v2 开始运行..
[2019-12-25 17:03:52.132071] INFO: bigquant: 命中缓存
[2019-12-25 17:03:52.133753] INFO: bigquant: dl_convert_to_bin.v2 运行完成[0.029305s].
[2019-12-25 17:03:52.135765] INFO: bigquant: dl_model_predict.v1 开始运行..
[2019-12-25 17:03:53.030851] INFO: bigquant: dl_model_predict.v1 运行完成[0.895042s].
[2019-12-25 17:03:53.036693] INFO: bigquant: cached.v3 开始运行..
[2019-12-25 17:03:53.654081] INFO: bigquant: cached.v3 运行完成[0.617397s].
[2019-12-25 17:03:53.683034] INFO: bigquant: backtest.v8 开始运行..
[2019-12-25 17:03:53.685553] INFO: bigquant: biglearning backtest:V8.3.2
[2019-12-25 17:03:53.686639] INFO: bigquant: product_type:stock by specified
[2019-12-25 17:03:53.809947] INFO: bigquant: cached.v2 开始运行..
[2019-12-25 17:03:53.855070] INFO: bigquant: 命中缓存
[2019-12-25 17:03:53.856789] INFO: bigquant: cached.v2 运行完成[0.046833s].
[2019-12-25 17:03:53.883275] INFO: algo: TradingAlgorithm V1.6.2
[2019-12-25 17:03:53.945105] INFO: algo: trading transform...
[2019-12-25 17:03:54.214663] INFO: Performance: Simulated 119 trading days out of 119.
[2019-12-25 17:03:54.216016] INFO: Performance: first open: 2019-02-11 09:30:00+00:00
[2019-12-25 17:03:54.217100] INFO: Performance: last close: 2019-08-01 15:00:00+00:00
[2019-12-25 17:03:56.141149] INFO: bigquant: backtest.v8 运行完成[2.458093s].