optimizers.Adam(lr=0.0002, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.00, amsgrad=False) median 夏普= 98特征 optimizers.Adam(lr=0.0002, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.00, amsgrad=False) median 特征变成105,夏普=0.29 optimizers.Adam(lr=0.0002, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.00, amsgrad=False) mean 特征变成105,夏普=0.37 optimizers.Adam(lr=0.0002, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.00, amsgrad=False) 0 特征变成105,夏普=0.27 optimizers.Adam(lr=0.00025, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.00, amsgrad=False) mean 特征变成105,夏普=0.28 optimizers.Adam(lr=0.00015, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.00, amsgrad=False) mean 特征变成105,夏普=0.38,kerel—size=6,drop=0.1,batch=1024 optimizers.Adam(lr=0.0002, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.00, amsgrad=False) mean 特征变成105,夏普=0.25,kerel—size=6 optimizers.Adam(lr=0.0001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.00, amsgrad=False) mean 特征变成105,夏普=0.21,kerel—size=6 optimizers.Adam(lr=0.0001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.00, amsgrad=False) mean 特征变成105,夏普=0.4,kerel—size=6,drop=0.2,batch=256 optimizers.Adam(lr=0.0001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.00, amsgrad=False) mean 特征变成105,夏普=0.37,kerel—size=2-5,drop=0.2,batch=256 optimizers.Adam(lr=0.0001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.00, amsgrad=False) mean 特征变成105,夏普=0.44/0.34,kerel—size=2-4,drop=0.2,batch=256,中证800,去除ST退市 optimizers.Adam(lr=0.0002, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.00, amsgrad=False) mean 特征变成105,夏普=0.56,kerel—size=2-4,drop=0.2,batch=256,中证800,非退市含ST,去极3 optimizers.Adam(lr=0.0002, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.00, amsgrad=False) mean 特征变成105,夏普=0.57,kerel—size=2-4,drop=0.2,batch=128,中证800,非退市含ST,去极3 optimizers.Adam(lr=0.0001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.00, amsgrad=False) mean 特征变成105,夏普=0.47,kerel—size=2-4,drop=0.2,batch=256,中证800,非退市含ST,去极3 optimizers.Adam(lr=0.0003, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.00, amsgrad=False) mean 特征变成105,夏普=0.41,kerel—size=2-4,drop=0.2,batch=256,中证800,非退市含ST,去极3 optimizers.Adam(lr=0.00025, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.00, amsgrad=False) mean 特征变成105,夏普=0.32,kerel—size=2-4,drop=0.2,batch=256,中证800,非退市含ST,去极3
import tensorflow as tf
# gpus = tf.config.list_physical_devices("GPU")
# tf.config.experimental.set_memory_growth(gpus[0], True)
from tensorflow.keras import optimizers
# 本代码由可视化策略环境自动生成 2021年12月26日 08:53
# 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
# Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
def m4_run_bigquant_run(input_1, input_2, input_3):
# 示例代码如下。在这里编写您的代码
from sklearn.model_selection import train_test_split
data = input_1.read()
x_train, x_val, y_train, y_val = train_test_split(data["x"], data['y'], shuffle=False, test_size=0.2)
data_1 = DataSource.write_pickle({'x': x_train, 'y': y_train})
data_2 = DataSource.write_pickle({'x': x_val, 'y': y_val})
return Outputs(data_1=data_1, data_2=data_2, data_3=None)
# 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
def m4_post_run_bigquant_run(outputs):
return outputs
from tensorflow.keras.callbacks import EarlyStopping
m35_earlystop_bigquant_run=EarlyStopping(monitor='val_mse', min_delta=0.0001, patience=5)
# 用户的自定义层需要写到字典中,比如
# {
# "MyLayer": MyLayer
# }
m35_custom_objects_bigquant_run = {
}
# Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
def m24_run_bigquant_run(input_1, input_2, input_3):
# 示例代码如下。在这里编写您的代码
pred_label = input_1.read_pickle()
df = input_2.read_df()
df = pd.DataFrame({'pred_label':pred_label[:,0], 'instrument':df.instrument, 'date':df.date})
df.sort_values(['date','pred_label'],inplace=True, ascending=[True,False])
return Outputs(data_1=DataSource.write_df(df), data_2=None, data_3=None)
# 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
def m24_post_run_bigquant_run(outputs):
return outputs
# 回测引擎:初始化函数,只执行一次
def m19_initialize_bigquant_run(context):
# 加载预测数据
context.ranker_prediction = context.options['data'].read_df()
# 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数
context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0003, min_cost=5))
# 预测数据,通过options传入进来,使用 read_df 函数,加载到内存 (DataFrame)
# 设置买入的股票数量,这里买入预测股票列表排名靠前的5只
stock_count = 20
# 每只的股票的权重,如下的权重分配会使得靠前的股票分配多一点的资金,[0.339160, 0.213986, 0.169580, ..]
context.stock_weights = T.norm([1 / math.log(i + 2) for i in range(0, stock_count)])
# 设置每只股票占用的最大资金比例
context.max_cash_per_instrument = 0.05
context.options['hold_days'] = 5
# 回测引擎:每日数据处理函数,每天执行一次
def m19_handle_data_bigquant_run(context, data):
# 按日期过滤得到今日的预测数据
ranker_prediction = context.ranker_prediction[
context.ranker_prediction.date == data.current_dt.strftime('%Y-%m-%d')]
# 1. 资金分配
# 平均持仓时间是hold_days,每日都将买入股票,每日预期使用 1/hold_days 的资金
# 实际操作中,会存在一定的买入误差,所以在前hold_days天,等量使用资金;之后,尽量使用剩余资金(这里设置最多用等量的1.5倍)
is_staging = context.trading_day_index < context.options['hold_days'] # 是否在建仓期间(前 hold_days 天)
cash_avg = context.portfolio.portfolio_value / context.options['hold_days']
cash_for_buy = min(context.portfolio.cash, (1 if is_staging else 1.5) * cash_avg)
cash_for_sell = cash_avg - (context.portfolio.cash - cash_for_buy)
positions = {e.symbol: p.amount * p.last_sale_price
for e, p in context.perf_tracker.position_tracker.positions.items()}
# 2. 生成卖出订单:hold_days天之后才开始卖出;对持仓的股票,按机器学习算法预测的排序末位淘汰
if not is_staging and cash_for_sell > 0:
equities = {e.symbol: e for e, p in context.perf_tracker.position_tracker.positions.items()}
instruments = list(reversed(list(ranker_prediction.instrument[ranker_prediction.instrument.apply(
lambda x: x in equities and not context.has_unfinished_sell_order(equities[x]))])))
# print('rank order for sell %s' % instruments)
for instrument in instruments:
context.order_target(context.symbol(instrument), 0)
cash_for_sell -= positions[instrument]
if cash_for_sell <= 0:
break
# 3. 生成买入订单:按机器学习算法预测的排序,买入前面的stock_count只股票
buy_cash_weights = context.stock_weights
buy_instruments = list(ranker_prediction.instrument[:len(buy_cash_weights)])
max_cash_per_instrument = context.portfolio.portfolio_value * context.max_cash_per_instrument
for i, instrument in enumerate(buy_instruments):
cash = cash_for_buy * buy_cash_weights[i]
if cash > max_cash_per_instrument - positions.get(instrument, 0):
# 确保股票持仓量不会超过每次股票最大的占用资金量
cash = max_cash_per_instrument - positions.get(instrument, 0)
if cash > 0:
context.order_value(context.symbol(instrument), cash)
# 回测引擎:准备数据,只执行一次
def m19_prepare_bigquant_run(context):
pass
g = T.Graph({
'm1': 'M.instruments.v2',
'm1.start_date': '2010-01-01',
'm1.end_date': '2017-12-31',
'm1.market': 'CN_STOCK_A',
'm1.instrument_list': '',
'm1.max_count': 0,
'm2': 'M.advanced_auto_labeler.v2',
'm2.instruments': T.Graph.OutputPort('m1.data'),
'm2.label_expr': """# #号开始的表示注释
# 0. 每行一个,顺序执行,从第二个开始,可以使用label字段
# 1. 可用数据字段见 https://bigquant.com/docs/data_history_data.html
# 添加benchmark_前缀,可使用对应的benchmark数据
# 2. 可用操作符和函数见 `表达式引擎 <https://bigquant.com/docs/big_expr.html>`_
# 计算收益:5日收盘价(作为卖出价格)除以明日开盘价(作为买入价格)
shift(close, -5) / shift(open, -1) - 1
# 极值处理:用1%和99%分位的值做clip
clip(label, all_quantile(label, 0.01), all_quantile(label, 0.99))
# 过滤掉一字涨停的情况 (设置label为NaN,在后续处理和训练中会忽略NaN的label)
where(shift(high, -1) == shift(low, -1), NaN, label)
""",
'm2.start_date': '',
'm2.end_date': '',
'm2.benchmark': '000300.SHA',
'm2.drop_na_label': True,
'm2.cast_label_int': False,
'm13': 'M.standardlize.v8',
'm13.input_1': T.Graph.OutputPort('m2.data'),
'm13.columns_input': 'label',
'm3': 'M.input_features.v1',
'm3.features': """close_0
open_0
high_0
low_0
amount_0
turn_0
return_0
close_1
open_1
high_1
low_1
return_1
amount_1
turn_1
close_2
open_2
high_2
low_2
amount_2
turn_2
return_2
close_3
open_3
high_3
low_3
amount_3
turn_3
return_3
close_4
open_4
high_4
low_4
amount_4
turn_4
return_4
mean(close_0, 5)
mean(low_0, 5)
mean(open_0, 5)
mean(high_0, 5)
mean(turn_0, 5)
mean(amount_0, 5)
mean(return_0, 5)
ts_max(close_0, 5)
ts_max(low_0, 5)
ts_max(open_0, 5)
ts_max(high_0, 5)
ts_max(turn_0, 5)
ts_max(amount_0, 5)
ts_max(return_0, 5)
ts_min(close_0, 5)
ts_min(low_0, 5)
ts_min(open_0, 5)
ts_min(high_0, 5)
ts_min(turn_0, 5)
ts_min(amount_0, 5)
ts_min(return_0, 5)
std(close_0, 5)
std(low_0, 5)
std(open_0, 5)
std(high_0, 5)
std(turn_0, 5)
std(amount_0, 5)
std(return_0, 5)
ts_rank(close_0, 5)
ts_rank(low_0, 5)
ts_rank(open_0, 5)
ts_rank(high_0, 5)
ts_rank(turn_0, 5)
ts_rank(amount_0, 5)
ts_rank(return_0, 5)
decay_linear(close_0, 5)
decay_linear(low_0, 5)
decay_linear(open_0, 5)
decay_linear(high_0, 5)
decay_linear(turn_0, 5)
decay_linear(amount_0, 5)
decay_linear(return_0, 5)
correlation(volume_0, return_0, 5)
correlation(volume_0, high_0, 5)
correlation(volume_0, low_0, 5)
correlation(volume_0, close_0, 5)
correlation(volume_0, open_0, 5)
correlation(volume_0, turn_0, 5)
correlation(return_0, high_0, 5)
correlation(return_0, low_0, 5)
correlation(return_0, close_0, 5)
correlation(return_0, open_0, 5)
correlation(return_0, turn_0, 5)
correlation(high_0, low_0, 5)
correlation(high_0, close_0, 5)
correlation(high_0, open_0, 5)
correlation(high_0, turn_0, 5)
correlation(low_0, close_0, 5)
correlation(low_0, open_0, 5)
correlation(low_0, turn_0, 5)
correlation(close_0, open_0, 5)
correlation(close_0, turn_0, 5)
correlation(open_0, turn_0, 5)
delta(close_0, 5)
delta(low_0, 5)
delta(open_0, 5)
delta(high_0, 5)
delta(turn_0, 5)
delta(amount_0, 5)
delta(return_0, 5)""",
'm15': 'M.general_feature_extractor.v7',
'm15.instruments': T.Graph.OutputPort('m1.data'),
'm15.features': T.Graph.OutputPort('m3.data'),
'm15.start_date': '',
'm15.end_date': '',
'm15.before_start_days': 10,
'm16': 'M.derived_feature_extractor.v3',
'm16.input_data': T.Graph.OutputPort('m15.data'),
'm16.features': T.Graph.OutputPort('m3.data'),
'm16.date_col': 'date',
'm16.instrument_col': 'instrument',
'm16.drop_na': True,
'm16.remove_extra_columns': False,
'm5': 'M.chinaa_stock_filter.v1',
'm5.input_data': T.Graph.OutputPort('m16.data'),
'm5.index_constituent_cond': ['中证800'],
'm5.board_cond': ['全部'],
'm5.industry_cond': ['全部'],
'm5.st_cond': ['全部'],
'm5.delist_cond': ['非退市'],
'm5.output_left_data': False,
'm14': 'M.standardlize.v8',
'm14.input_1': T.Graph.OutputPort('m5.data'),
'm14.input_2': T.Graph.OutputPort('m3.data'),
'm14.columns_input': '',
'm10': 'M.winsorize.v6',
'm10.input_data': T.Graph.OutputPort('m14.data'),
'm10.features': T.Graph.OutputPort('m3.data'),
'm10.columns_input': '',
'm10.median_deviate': 3,
'm21': 'M.fillnan.v1',
'm21.input_data': T.Graph.OutputPort('m10.data'),
'm21.features': T.Graph.OutputPort('m3.data'),
'm21.fill_value': 'mean',
'm7': 'M.join.v3',
'm7.data1': T.Graph.OutputPort('m13.data'),
'm7.data2': T.Graph.OutputPort('m21.data'),
'm7.on': 'date,instrument',
'm7.how': 'inner',
'm7.sort': False,
'm26': 'M.dl_convert_to_bin.v2',
'm26.input_data': T.Graph.OutputPort('m7.data'),
'm26.features': T.Graph.OutputPort('m3.data'),
'm26.window_size': 1,
'm26.feature_clip': 5,
'm26.flatten': True,
'm26.window_along_col': 'instrument',
'm4': 'M.cached.v3',
'm4.input_1': T.Graph.OutputPort('m26.data'),
'm4.input_2': T.Graph.OutputPort('m3.data'),
'm4.run': m4_run_bigquant_run,
'm4.post_run': m4_post_run_bigquant_run,
'm4.input_ports': '',
'm4.params': '{}',
'm4.output_ports': '',
'm9': 'M.instruments.v2',
'm9.start_date': T.live_run_param('trading_date', '2018-01-01'),
'm9.end_date': T.live_run_param('trading_date', '2021-10-31'),
'm9.market': 'CN_STOCK_A',
'm9.instrument_list': '',
'm9.max_count': 0,
'm17': 'M.general_feature_extractor.v7',
'm17.instruments': T.Graph.OutputPort('m9.data'),
'm17.features': T.Graph.OutputPort('m3.data'),
'm17.start_date': '',
'm17.end_date': '',
'm17.before_start_days': 10,
'm18': 'M.derived_feature_extractor.v3',
'm18.input_data': T.Graph.OutputPort('m17.data'),
'm18.features': T.Graph.OutputPort('m3.data'),
'm18.date_col': 'date',
'm18.instrument_col': 'instrument',
'm18.drop_na': True,
'm18.remove_extra_columns': False,
'm8': 'M.chinaa_stock_filter.v1',
'm8.input_data': T.Graph.OutputPort('m18.data'),
'm8.index_constituent_cond': ['中证800'],
'm8.board_cond': ['全部'],
'm8.industry_cond': ['全部'],
'm8.st_cond': ['全部'],
'm8.delist_cond': ['非退市'],
'm8.output_left_data': False,
'm25': 'M.standardlize.v8',
'm25.input_1': T.Graph.OutputPort('m8.data'),
'm25.input_2': T.Graph.OutputPort('m3.data'),
'm25.columns_input': '',
'm20': 'M.winsorize.v6',
'm20.input_data': T.Graph.OutputPort('m25.data'),
'm20.features': T.Graph.OutputPort('m3.data'),
'm20.columns_input': '',
'm20.median_deviate': 3,
'm22': 'M.fillnan.v1',
'm22.input_data': T.Graph.OutputPort('m20.data'),
'm22.features': T.Graph.OutputPort('m3.data'),
'm22.fill_value': 'mean',
'm27': 'M.dl_convert_to_bin.v2',
'm27.input_data': T.Graph.OutputPort('m22.data'),
'm27.features': T.Graph.OutputPort('m3.data'),
'm27.window_size': 1,
'm27.feature_clip': 5,
'm27.flatten': True,
'm27.window_along_col': 'instrument',
'm6': 'M.dl_layer_input.v1',
'm6.shape': '105,1',
'm6.batch_shape': '',
'm6.dtype': 'float32',
'm6.sparse': False,
'm6.name': '',
'm37': 'M.dl_layer_batchnormalization.v1',
'm37.inputs': T.Graph.OutputPort('m6.data'),
'm37.axis': -1,
'm37.momentum': 0.99,
'm37.epsilon': 0.001,
'm37.center': True,
'm37.scale': True,
'm37.beta_initializer': 'Zeros',
'm37.gamma_initializer': 'Ones',
'm37.moving_mean_initializer': 'Zeros',
'm37.moving_variance_initializer': 'Ones',
'm37.beta_regularizer': 'None',
'm37.beta_regularizer_l1': 0,
'm37.beta_regularizer_l2': 0,
'm37.gamma_regularizer': 'None',
'm37.gamma_regularizer_l1': 0,
'm37.gamma_regularizer_l2': 0,
'm37.beta_constraint': 'None',
'm37.gamma_constraint': 'None',
'm37.name': '',
'm43': 'M.dl_layer_conv1d.v1',
'm43.inputs': T.Graph.OutputPort('m37.data'),
'm43.filters': 64,
'm43.kernel_size': '2',
'm43.strides': '1',
'm43.padding': 'same',
'm43.dilation_rate': 1,
'm43.activation': 'relu',
'm43.use_bias': True,
'm43.kernel_initializer': 'glorot_uniform',
'm43.bias_initializer': 'Zeros',
'm43.kernel_regularizer': 'None',
'm43.kernel_regularizer_l1': 0,
'm43.kernel_regularizer_l2': 0,
'm43.bias_regularizer': 'None',
'm43.bias_regularizer_l1': 0,
'm43.bias_regularizer_l2': 0,
'm43.activity_regularizer': 'None',
'm43.activity_regularizer_l1': 0,
'm43.activity_regularizer_l2': 0,
'm43.kernel_constraint': 'None',
'm43.bias_constraint': 'None',
'm43.name': '',
'm44': 'M.dl_layer_conv1d.v1',
'm44.inputs': T.Graph.OutputPort('m43.data'),
'm44.filters': 64,
'm44.kernel_size': '2',
'm44.strides': '1',
'm44.padding': 'same',
'm44.dilation_rate': 1,
'm44.activation': 'relu',
'm44.use_bias': True,
'm44.kernel_initializer': 'glorot_uniform',
'm44.bias_initializer': 'Zeros',
'm44.kernel_regularizer': 'None',
'm44.kernel_regularizer_l1': 0,
'm44.kernel_regularizer_l2': 0,
'm44.bias_regularizer': 'None',
'm44.bias_regularizer_l1': 0,
'm44.bias_regularizer_l2': 0,
'm44.activity_regularizer': 'None',
'm44.activity_regularizer_l1': 0,
'm44.activity_regularizer_l2': 0,
'm44.kernel_constraint': 'None',
'm44.bias_constraint': 'None',
'm44.name': '',
'm41': 'M.dl_layer_maxpooling1d.v1',
'm41.inputs': T.Graph.OutputPort('m44.data'),
'm41.pool_size': 2,
'm41.padding': 'valid',
'm41.name': '',
'm45': 'M.dl_layer_conv1d.v1',
'm45.inputs': T.Graph.OutputPort('m41.data'),
'm45.filters': 128,
'm45.kernel_size': '4',
'm45.strides': '1',
'm45.padding': 'same',
'm45.dilation_rate': 1,
'm45.activation': 'relu',
'm45.use_bias': True,
'm45.kernel_initializer': 'glorot_uniform',
'm45.bias_initializer': 'Zeros',
'm45.kernel_regularizer': 'None',
'm45.kernel_regularizer_l1': 0,
'm45.kernel_regularizer_l2': 0,
'm45.bias_regularizer': 'None',
'm45.bias_regularizer_l1': 0,
'm45.bias_regularizer_l2': 0,
'm45.activity_regularizer': 'None',
'm45.activity_regularizer_l1': 0,
'm45.activity_regularizer_l2': 0,
'm45.kernel_constraint': 'None',
'm45.bias_constraint': 'None',
'm45.name': '',
'm47': 'M.dl_layer_conv1d.v1',
'm47.inputs': T.Graph.OutputPort('m45.data'),
'm47.filters': 128,
'm47.kernel_size': '4',
'm47.strides': '1',
'm47.padding': 'same',
'm47.dilation_rate': 1,
'm47.activation': 'relu',
'm47.use_bias': True,
'm47.kernel_initializer': 'glorot_uniform',
'm47.bias_initializer': 'Zeros',
'm47.kernel_regularizer': 'None',
'm47.kernel_regularizer_l1': 0,
'm47.kernel_regularizer_l2': 0,
'm47.bias_regularizer': 'None',
'm47.bias_regularizer_l1': 0,
'm47.bias_regularizer_l2': 0,
'm47.activity_regularizer': 'None',
'm47.activity_regularizer_l1': 0,
'm47.activity_regularizer_l2': 0,
'm47.kernel_constraint': 'None',
'm47.bias_constraint': 'None',
'm47.name': '',
'm48': 'M.dl_layer_batchnormalization.v1',
'm48.inputs': T.Graph.OutputPort('m47.data'),
'm48.axis': -1,
'm48.momentum': 0.99,
'm48.epsilon': 0.001,
'm48.center': True,
'm48.scale': True,
'm48.beta_initializer': 'Zeros',
'm48.gamma_initializer': 'Ones',
'm48.moving_mean_initializer': 'Zeros',
'm48.moving_variance_initializer': 'Ones',
'm48.beta_regularizer': 'None',
'm48.beta_regularizer_l1': 0,
'm48.beta_regularizer_l2': 0,
'm48.gamma_regularizer': 'None',
'm48.gamma_regularizer_l1': 0,
'm48.gamma_regularizer_l2': 0,
'm48.beta_constraint': 'None',
'm48.gamma_constraint': 'None',
'm48.name': '',
'm50': 'M.dl_layer_conv1d.v1',
'm50.inputs': T.Graph.OutputPort('m48.data'),
'm50.filters': 128,
'm50.kernel_size': '2',
'm50.strides': '1',
'm50.padding': 'same',
'm50.dilation_rate': 1,
'm50.activation': 'relu',
'm50.use_bias': True,
'm50.kernel_initializer': 'glorot_uniform',
'm50.bias_initializer': 'Zeros',
'm50.kernel_regularizer': 'None',
'm50.kernel_regularizer_l1': 0,
'm50.kernel_regularizer_l2': 0,
'm50.bias_regularizer': 'None',
'm50.bias_regularizer_l1': 0,
'm50.bias_regularizer_l2': 0,
'm50.activity_regularizer': 'None',
'm50.activity_regularizer_l1': 0,
'm50.activity_regularizer_l2': 0,
'm50.kernel_constraint': 'None',
'm50.bias_constraint': 'None',
'm50.name': '',
'm51': 'M.dl_layer_batchnormalization.v1',
'm51.inputs': T.Graph.OutputPort('m50.data'),
'm51.axis': -1,
'm51.momentum': 0.99,
'm51.epsilon': 0.001,
'm51.center': True,
'm51.scale': True,
'm51.beta_initializer': 'Zeros',
'm51.gamma_initializer': 'Ones',
'm51.moving_mean_initializer': 'Zeros',
'm51.moving_variance_initializer': 'Ones',
'm51.beta_regularizer': 'None',
'm51.beta_regularizer_l1': 0,
'm51.beta_regularizer_l2': 0,
'm51.gamma_regularizer': 'None',
'm51.gamma_regularizer_l1': 0,
'm51.gamma_regularizer_l2': 0,
'm51.beta_constraint': 'None',
'm51.gamma_constraint': 'None',
'm51.name': '',
'm53': 'M.dl_layer_conv1d.v1',
'm53.inputs': T.Graph.OutputPort('m51.data'),
'm53.filters': 128,
'm53.kernel_size': '4',
'm53.strides': '1',
'm53.padding': 'same',
'm53.dilation_rate': 1,
'm53.activation': 'relu',
'm53.use_bias': True,
'm53.kernel_initializer': 'glorot_uniform',
'm53.bias_initializer': 'Zeros',
'm53.kernel_regularizer': 'None',
'm53.kernel_regularizer_l1': 0,
'm53.kernel_regularizer_l2': 0,
'm53.bias_regularizer': 'None',
'm53.bias_regularizer_l1': 0,
'm53.bias_regularizer_l2': 0,
'm53.activity_regularizer': 'None',
'm53.activity_regularizer_l1': 0,
'm53.activity_regularizer_l2': 0,
'm53.kernel_constraint': 'None',
'm53.bias_constraint': 'None',
'm53.name': '',
'm46': 'M.dl_layer_batchnormalization.v1',
'm46.inputs': T.Graph.OutputPort('m53.data'),
'm46.axis': -1,
'm46.momentum': 0.99,
'm46.epsilon': 0.001,
'm46.center': True,
'm46.scale': True,
'm46.beta_initializer': 'Zeros',
'm46.gamma_initializer': 'Ones',
'm46.moving_mean_initializer': 'Zeros',
'm46.moving_variance_initializer': 'Ones',
'm46.beta_regularizer': 'None',
'm46.beta_regularizer_l1': 0,
'm46.beta_regularizer_l2': 0,
'm46.gamma_regularizer': 'None',
'm46.gamma_regularizer_l1': 0,
'm46.gamma_regularizer_l2': 0,
'm46.beta_constraint': 'None',
'm46.gamma_constraint': 'None',
'm46.name': '',
'm49': 'M.dl_layer_conv1d.v1',
'm49.inputs': T.Graph.OutputPort('m46.data'),
'm49.filters': 128,
'm49.kernel_size': '2',
'm49.strides': '1',
'm49.padding': 'same',
'm49.dilation_rate': 1,
'm49.activation': 'relu',
'm49.use_bias': True,
'm49.kernel_initializer': 'glorot_uniform',
'm49.bias_initializer': 'Zeros',
'm49.kernel_regularizer': 'None',
'm49.kernel_regularizer_l1': 0,
'm49.kernel_regularizer_l2': 0,
'm49.bias_regularizer': 'None',
'm49.bias_regularizer_l1': 0,
'm49.bias_regularizer_l2': 0,
'm49.activity_regularizer': 'None',
'm49.activity_regularizer_l1': 0,
'm49.activity_regularizer_l2': 0,
'm49.kernel_constraint': 'None',
'm49.bias_constraint': 'None',
'm49.name': '',
'm62': 'M.dl_layer_add.v1',
'm62.input1': T.Graph.OutputPort('m49.data'),
'm62.input2': T.Graph.OutputPort('m47.data'),
'm62.name': '',
'm38': 'M.dl_layer_globalmaxpooling1d.v1',
'm38.inputs': T.Graph.OutputPort('m62.data'),
'm38.name': '',
'm61': 'M.dl_layer_dropout.v1',
'm61.inputs': T.Graph.OutputPort('m38.data'),
'm61.rate': 0.15,
'm61.noise_shape': '',
'm61.name': '',
'm57': 'M.dl_layer_dense.v1',
'm57.inputs': T.Graph.OutputPort('m61.data'),
'm57.units': 1,
'm57.activation': 'linear',
'm57.use_bias': True,
'm57.kernel_initializer': 'glorot_uniform',
'm57.bias_initializer': 'Zeros',
'm57.kernel_regularizer': 'None',
'm57.kernel_regularizer_l1': 0,
'm57.kernel_regularizer_l2': 0,
'm57.bias_regularizer': 'None',
'm57.bias_regularizer_l1': 0,
'm57.bias_regularizer_l2': 0,
'm57.activity_regularizer': 'None',
'm57.activity_regularizer_l1': 0,
'm57.activity_regularizer_l2': 0,
'm57.kernel_constraint': 'None',
'm57.bias_constraint': 'None',
'm57.name': '',
'm34': 'M.dl_model_init.v1',
'm34.inputs': T.Graph.OutputPort('m6.data'),
'm34.outputs': T.Graph.OutputPort('m57.data'),
'm35': 'M.dl_model_train.v1',
'm35.input_model': T.Graph.OutputPort('m34.data'),
'm35.training_data': T.Graph.OutputPort('m4.data_1'),
'm35.validation_data': T.Graph.OutputPort('m4.data_2'),
'm35.optimizer': '自定义',
'm35.user_optimizer': optimizers.Adam(lr=0.0002, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.00, amsgrad=False),
'm35.loss': 'mean_squared_error',
'm35.metrics': 'mse',
'm35.batch_size': 512,
'm35.epochs': 10000,
'm35.earlystop': m35_earlystop_bigquant_run,
'm35.custom_objects': m35_custom_objects_bigquant_run,
'm35.n_gpus': 1,
'm35.verbose': '2:每个epoch输出一行记录',
'm35.m_cached': False,
'm11': 'M.dl_model_predict.v1',
'm11.trained_model': T.Graph.OutputPort('m35.data'),
'm11.input_data': T.Graph.OutputPort('m27.data'),
'm11.batch_size': 1024,
'm11.n_gpus': 1,
'm11.verbose': '2:每个epoch输出一行记录',
'm24': 'M.cached.v3',
'm24.input_1': T.Graph.OutputPort('m11.data'),
'm24.input_2': T.Graph.OutputPort('m22.data'),
'm24.run': m24_run_bigquant_run,
'm24.post_run': m24_post_run_bigquant_run,
'm24.input_ports': '',
'm24.params': '{}',
'm24.output_ports': '',
'm19': 'M.trade.v4',
'm19.instruments': T.Graph.OutputPort('m9.data'),
'm19.options_data': T.Graph.OutputPort('m24.data_1'),
'm19.start_date': '',
'm19.end_date': '',
'm19.initialize': m19_initialize_bigquant_run,
'm19.handle_data': m19_handle_data_bigquant_run,
'm19.prepare': m19_prepare_bigquant_run,
'm19.volume_limit': 0.025,
'm19.order_price_field_buy': 'open',
'm19.order_price_field_sell': 'close',
'm19.capital_base': 1000000,
'm19.auto_cancel_non_tradable_orders': True,
'm19.data_frequency': 'daily',
'm19.price_type': '后复权',
'm19.product_type': '股票',
'm19.plot_charts': True,
'm19.backtest_only': False,
'm19.benchmark': '000905.SHA',
})
# g.run({})
def m12_param_grid_builder_bigquant_run():
param_grid = {}
# 在这里设置需要调优的参数备选
# param_grid['m3.features'] = ['close_1/close_0', 'close_2/close_0\nclose_3/close_0']
# param_grid['m6.number_of_trees'] = [5, 10, 20]
param_grid['m61.rate'] = [0.15,0.1]
param_grid['m35.batch_size'] = [256,512]
return param_grid
def m12_scoring_bigquant_run(result):
score = result.get('m19').read_raw_perf()['sharpe'].tail(1)[0]
return {'score': score}
m12 = M.hyper_parameter_search.v1(
param_grid_builder=m12_param_grid_builder_bigquant_run,
scoring=m12_scoring_bigquant_run,
search_algorithm='网格搜索',
search_iterations=10,
workers=1,
worker_distributed_run=False,
worker_silent=False,
run_now=True,
bq_graph=g
)
[2021-12-25 13:28:31.362324] INFO: moduleinvoker: instruments.v2 开始运行..
[2021-12-25 13:28:31.383376] INFO: moduleinvoker: 命中缓存
[2021-12-25 13:28:31.385592] INFO: moduleinvoker: instruments.v2 运行完成[0.023243s].
[2021-12-25 13:28:31.394750] INFO: moduleinvoker: advanced_auto_labeler.v2 开始运行..
[2021-12-25 13:28:31.400618] INFO: moduleinvoker: 命中缓存
[2021-12-25 13:28:31.402178] INFO: moduleinvoker: advanced_auto_labeler.v2 运行完成[0.00743s].
[2021-12-25 13:28:31.407885] INFO: moduleinvoker: standardlize.v8 开始运行..
[2021-12-25 13:28:31.433370] INFO: moduleinvoker: 命中缓存
[2021-12-25 13:28:31.435363] INFO: moduleinvoker: standardlize.v8 运行完成[0.027464s].
[2021-12-25 13:28:31.439825] INFO: moduleinvoker: input_features.v1 开始运行..
[2021-12-25 13:28:31.446588] INFO: moduleinvoker: 命中缓存
[2021-12-25 13:28:31.448270] INFO: moduleinvoker: input_features.v1 运行完成[0.008445s].
[2021-12-25 13:28:31.489561] INFO: moduleinvoker: general_feature_extractor.v7 开始运行..
[2021-12-25 13:28:31.503640] INFO: moduleinvoker: 命中缓存
[2021-12-25 13:28:31.505982] INFO: moduleinvoker: general_feature_extractor.v7 运行完成[0.016456s].
[2021-12-25 13:28:31.513875] INFO: moduleinvoker: derived_feature_extractor.v3 开始运行..
[2021-12-25 13:28:31.520425] INFO: moduleinvoker: 命中缓存
[2021-12-25 13:28:31.522033] INFO: moduleinvoker: derived_feature_extractor.v3 运行完成[0.008159s].
[2021-12-25 13:28:31.531014] INFO: moduleinvoker: chinaa_stock_filter.v1 开始运行..
[2021-12-25 13:28:31.537878] INFO: moduleinvoker: 命中缓存
[2021-12-25 13:28:31.539823] INFO: moduleinvoker: chinaa_stock_filter.v1 运行完成[0.008803s].
[2021-12-25 13:28:31.546373] INFO: moduleinvoker: standardlize.v8 开始运行..
[2021-12-25 13:28:31.577400] INFO: moduleinvoker: 命中缓存
[2021-12-25 13:28:31.579599] INFO: moduleinvoker: standardlize.v8 运行完成[0.033214s].
[2021-12-25 13:28:31.585507] INFO: moduleinvoker: winsorize.v6 开始运行..
[2021-12-25 13:28:31.592263] INFO: moduleinvoker: 命中缓存
[2021-12-25 13:28:31.593914] INFO: moduleinvoker: winsorize.v6 运行完成[0.008403s].
[2021-12-25 13:28:31.602562] INFO: moduleinvoker: fillnan.v1 开始运行..
[2021-12-25 13:28:31.609074] INFO: moduleinvoker: 命中缓存
[2021-12-25 13:28:31.610707] INFO: moduleinvoker: fillnan.v1 运行完成[0.00814s].
[2021-12-25 13:28:31.618504] INFO: moduleinvoker: join.v3 开始运行..
[2021-12-25 13:28:31.624143] INFO: moduleinvoker: 命中缓存
[2021-12-25 13:28:31.625535] INFO: moduleinvoker: join.v3 运行完成[0.007027s].
[2021-12-25 13:28:31.639420] INFO: moduleinvoker: dl_convert_to_bin.v2 开始运行..
[2021-12-25 13:28:31.646081] INFO: moduleinvoker: 命中缓存
[2021-12-25 13:28:31.647663] INFO: moduleinvoker: dl_convert_to_bin.v2 运行完成[0.008267s].
[2021-12-25 13:28:31.658642] INFO: moduleinvoker: cached.v3 开始运行..
[2021-12-25 13:28:31.666066] INFO: moduleinvoker: 命中缓存
[2021-12-25 13:28:31.667526] INFO: moduleinvoker: cached.v3 运行完成[0.008896s].
[2021-12-25 13:28:31.672154] INFO: moduleinvoker: instruments.v2 开始运行..
[2021-12-25 13:28:31.678015] INFO: moduleinvoker: 命中缓存
[2021-12-25 13:28:31.679342] INFO: moduleinvoker: instruments.v2 运行完成[0.007186s].
[2021-12-25 13:28:31.706870] INFO: moduleinvoker: general_feature_extractor.v7 开始运行..
[2021-12-25 13:28:31.715580] INFO: moduleinvoker: 命中缓存
[2021-12-25 13:28:31.717214] INFO: moduleinvoker: general_feature_extractor.v7 运行完成[0.010366s].
[2021-12-25 13:28:31.724337] INFO: moduleinvoker: derived_feature_extractor.v3 开始运行..
[2021-12-25 13:28:31.730337] INFO: moduleinvoker: 命中缓存
[2021-12-25 13:28:31.731740] INFO: moduleinvoker: derived_feature_extractor.v3 运行完成[0.007404s].
[2021-12-25 13:28:31.738970] INFO: moduleinvoker: chinaa_stock_filter.v1 开始运行..
[2021-12-25 13:28:31.744642] INFO: moduleinvoker: 命中缓存
[2021-12-25 13:28:31.746018] INFO: moduleinvoker: chinaa_stock_filter.v1 运行完成[0.007044s].
[2021-12-25 13:28:31.750467] INFO: moduleinvoker: standardlize.v8 开始运行..
[2021-12-25 13:28:31.755798] INFO: moduleinvoker: 命中缓存
[2021-12-25 13:28:31.757071] INFO: moduleinvoker: standardlize.v8 运行完成[0.006602s].
[2021-12-25 13:28:31.761344] INFO: moduleinvoker: winsorize.v6 开始运行..
[2021-12-25 13:28:31.766949] INFO: moduleinvoker: 命中缓存
[2021-12-25 13:28:31.768319] INFO: moduleinvoker: winsorize.v6 运行完成[0.006971s].
[2021-12-25 13:28:31.790228] INFO: moduleinvoker: fillnan.v1 开始运行..
[2021-12-25 13:28:31.796293] INFO: moduleinvoker: 命中缓存
[2021-12-25 13:28:31.797800] INFO: moduleinvoker: fillnan.v1 运行完成[0.00758s].
[2021-12-25 13:28:31.833962] INFO: moduleinvoker: dl_convert_to_bin.v2 开始运行..
[2021-12-25 13:28:31.840503] INFO: moduleinvoker: 命中缓存
[2021-12-25 13:28:31.842104] INFO: moduleinvoker: dl_convert_to_bin.v2 运行完成[0.008167s].
[2021-12-25 13:28:31.854512] INFO: moduleinvoker: dl_layer_input.v1 运行完成[0.004913s].
[2021-12-25 13:28:31.882849] INFO: moduleinvoker: dl_layer_batchnormalization.v1 运行完成[0.020575s].
[2021-12-25 13:28:31.904467] INFO: moduleinvoker: dl_layer_conv1d.v1 运行完成[0.015197s].
[2021-12-25 13:28:31.926456] INFO: moduleinvoker: dl_layer_conv1d.v1 运行完成[0.016052s].
[2021-12-25 13:28:31.939968] INFO: moduleinvoker: dl_layer_maxpooling1d.v1 运行完成[0.00664s].
[2021-12-25 13:28:31.960681] INFO: moduleinvoker: dl_layer_conv1d.v1 运行完成[0.01474s].
[2021-12-25 13:28:31.981851] INFO: moduleinvoker: dl_layer_conv1d.v1 运行完成[0.014976s].
[2021-12-25 13:28:32.004927] INFO: moduleinvoker: dl_layer_batchnormalization.v1 运行完成[0.015928s].
[2021-12-25 13:28:32.028920] INFO: moduleinvoker: dl_layer_conv1d.v1 运行完成[0.015355s].
[2021-12-25 13:28:32.051884] INFO: moduleinvoker: dl_layer_batchnormalization.v1 运行完成[0.014862s].
[2021-12-25 13:28:32.076031] INFO: moduleinvoker: dl_layer_conv1d.v1 运行完成[0.017939s].
[2021-12-25 13:28:32.105182] INFO: moduleinvoker: dl_layer_batchnormalization.v1 运行完成[0.015919s].
[2021-12-25 13:28:32.125959] INFO: moduleinvoker: dl_layer_conv1d.v1 运行完成[0.014795s].
[2021-12-25 13:28:32.137821] INFO: moduleinvoker: dl_layer_add.v1 运行完成[0.005141s].
[2021-12-25 13:28:32.149199] INFO: moduleinvoker: dl_layer_globalmaxpooling1d.v1 运行完成[0.004779s].
[2021-12-25 13:28:32.159845] INFO: moduleinvoker: dl_layer_dropout.v1 运行完成[0.003946s].
[2021-12-25 13:28:32.177409] INFO: moduleinvoker: dl_layer_dense.v1 运行完成[0.011265s].
[2021-12-25 13:28:32.252519] INFO: moduleinvoker: cached.v3 开始运行..
[2021-12-25 13:28:32.342481] INFO: moduleinvoker: cached.v3 运行完成[0.089971s].
[2021-12-25 13:28:32.344712] INFO: moduleinvoker: dl_model_init.v1 运行完成[0.160693s].
[2021-12-25 13:28:32.349836] INFO: moduleinvoker: dl_model_train.v1 开始运行..
[2021-12-25 13:29:05.919829] INFO: dl_model_train: 准备训练,训练样本个数:1176863,迭代次数:10000