【宽客学院】LSTM模型构建

新手专区
标签: #<Tag:0x00007faff8d901e8>

(小Q) #1

本文将介绍LSTM模型的原理与构建其选股模型的流程

LSTM简介

循环神经网络(RNN)

传统的神经网络是基于所有时刻的输入和输出间相互独立的假设来生成已学习数据的静态模型,并根据新接受的数据进行运算。但在很多情景中,如语音识别中预测当前的单词的含义,需要知道之前的输出结合上文语境做出判断,循环神经网络(Recurrent Neural Networks,简称 RNN)可以用于解决这类问题。
循环神经网络也被称为递归神经网络是受到人类对于近期事件会有所保留的背景而启发,循环神经网络会随着数据的输入生成动态模型
理论上,RNN可以支持无限长的时间序列,然而实际情况却没这么简单。RNN模型很容易发散,从而得不出最优解。

长短期记忆模型(LSTM)

长短期记忆模型(long-short term memory)是一种特殊的RNN模型,是为了解决RNN模型梯度弥散的问题而提出的;在传统的RNN中,训练算法使用的是BPTT,当时间比较长时,需要回传的残差会指数下降,导致网络权重更新缓慢,无法体现出RNN的长期记忆的效果,因此需要一个存储单元来存储记忆,因此LSTM模型被提出。

LSTM的流程


LSTM 的整体结构和 RNN 一样,区别在于隐含层的具体实现,LSTM 在其中加入了一个
记录当前状态的单元,这个单元的值由上一个时刻的状态和当前的输入共同决定,以此
来记录之前历史时刻的信息。
具体展开,分以下四个步骤:

  1. 过去信息的记录:由当前的输入和前一个时刻的输出,决定当前的记忆单元需要保存过去多少信息:$f_t=\sigma(W_f·[h_{t-1},x_t]+b_f)$
    激活函数 往往为 sigmoid 函数,将 映射到 0 和 1 之间的数,1 表示过去的信息全部记录,0 表示过去的信息全部舍弃。
  2. 当前信息的记录:由当前的输入和前一个时刻的输出,决定在状态单元中储存哪些新的信息,包含两个部分,一是当前的记忆单元需要保存当前多少信息,二是当前信息的表示:
    $i_t=\sigma(W_i·[h_{t-1},x_t]+b_i) $
    $ \hat{C_T}=tanh(W_c·[h_{t-1},x_t]+b_c)$
    激活函数 往往为 sigmoid 函数,将 映射到 0 和 1 之间的数,1 表示当前的信息全部记录,0 表示当前的信息全部舍弃。 为当前信息的表示,作为当前状态的候选,通过激活函数 tanh 映射得到。
  3. 当前状态:由当前的信息和过去的信息,得到当前状态:
    $ C_t=f_t*C_{t-1}+i_t *\hat{C_T} $
    这一步通过两个权重和,来决定之前信息和当前信息各保留多少,若较大则之后的模型会用到更多更远时间的信息,若较大,则之后的模型会用到更多更近时间的信息,以此避免了普通 RNN 网络结构中对较远时间点信息的遗忘。
  4. 输出结果:由上一时刻的输出、当前时刻的输入和当前状态,得到当前输出:
    $o_t = \sigma(W_o·[h_{t-1},x_t]+b_o)$
    $h_t = o_t*tanh(C_t) $

构建可视化模型

策略附在文末,有几点需要说明:

克隆策略

    {"Description":"实验创建于2017/11/15","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"-281:options_data","SourceOutputPortId":"-214:data_1"},{"DestinationInputPortId":"-316:inputs","SourceOutputPortId":"-210:data"},{"DestinationInputPortId":"-218:inputs","SourceOutputPortId":"-210:data"},{"DestinationInputPortId":"-1474:inputs","SourceOutputPortId":"-218:data"},{"DestinationInputPortId":"-320:input_model","SourceOutputPortId":"-316:data"},{"DestinationInputPortId":"-332:trained_model","SourceOutputPortId":"-320:data"},{"DestinationInputPortId":"-214:input_1","SourceOutputPortId":"-332:data"},{"DestinationInputPortId":"-1126:features","SourceOutputPortId":"-2295:data"},{"DestinationInputPortId":"-1134:features","SourceOutputPortId":"-2295:data"},{"DestinationInputPortId":"-300:features","SourceOutputPortId":"-2295:data"},{"DestinationInputPortId":"-317:features","SourceOutputPortId":"-2295:data"},{"DestinationInputPortId":"-334:features","SourceOutputPortId":"-2295:data"},{"DestinationInputPortId":"-341:features","SourceOutputPortId":"-2295:data"},{"DestinationInputPortId":"-316:outputs","SourceOutputPortId":"-259:data"},{"DestinationInputPortId":"-1126:input_data","SourceOutputPortId":"-2296:data"},{"DestinationInputPortId":"-289:instruments","SourceOutputPortId":"-620:data"},{"DestinationInputPortId":"-300:instruments","SourceOutputPortId":"-620:data"},{"DestinationInputPortId":"-1481:inputs","SourceOutputPortId":"-1403:data"},{"DestinationInputPortId":"-1403:inputs","SourceOutputPortId":"-1474:data"},{"DestinationInputPortId":"-259:inputs","SourceOutputPortId":"-1481:data"},{"DestinationInputPortId":"-320:training_data","SourceOutputPortId":"-1126:data"},{"DestinationInputPortId":"-332:input_data","SourceOutputPortId":"-1134:data"},{"DestinationInputPortId":"-281:benchmark_ds","SourceOutputPortId":"-283:data"},{"DestinationInputPortId":"-307:data1","SourceOutputPortId":"-289:data"},{"DestinationInputPortId":"-317:input_data","SourceOutputPortId":"-300:data"},{"DestinationInputPortId":"-2296:input_data","SourceOutputPortId":"-307:data"},{"DestinationInputPortId":"-1134:input_data","SourceOutputPortId":"-313:data"},{"DestinationInputPortId":"-214:input_2","SourceOutputPortId":"-313:data"},{"DestinationInputPortId":"-307:data2","SourceOutputPortId":"-317:data"},{"DestinationInputPortId":"-334:instruments","SourceOutputPortId":"-325:data"},{"DestinationInputPortId":"-283:instruments","SourceOutputPortId":"-325:data"},{"DestinationInputPortId":"-281:instruments","SourceOutputPortId":"-325:data"},{"DestinationInputPortId":"-341:input_data","SourceOutputPortId":"-334:data"},{"DestinationInputPortId":"-313:input_data","SourceOutputPortId":"-341:data"}],"ModuleNodes":[{"Id":"-214","ModuleId":"BigQuantSpace.cached.cached-v3","ModuleParameters":[{"Name":"run","Value":"# Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端\ndef bigquant_run(input_1, input_2, input_3):\n predictions = input_1.read_pickle()\n pred_result = predictions.reshape(predictions.shape[0]) \n dt = input_2.read_df()['date']\n pred_df = pd.Series(pred_result, index=dt)\n ds = DataSource.write_df(pred_df)\n return Outputs(data_1=ds)\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"post_run","Value":"# 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。\ndef bigquant_run(outputs):\n return outputs\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"input_ports","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"params","Value":"{}","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"output_ports","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-214"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-214"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_3","NodeId":"-214"}],"OutputPortsInternal":[{"Name":"data_1","NodeId":"-214","OutputType":null},{"Name":"data_2","NodeId":"-214","OutputType":null},{"Name":"data_3","NodeId":"-214","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":2,"IsPartOfPartialRun":null,"Comment":"模型预测结果输出","CommentCollapsed":false},{"Id":"-210","ModuleId":"BigQuantSpace.dl_layer_input.dl_layer_input-v1","ModuleParameters":[{"Name":"shape","Value":"5,59","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"batch_shape","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"dtype","Value":"float32","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"sparse","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"name","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"inputs","NodeId":"-210"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-210","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":3,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-218","ModuleId":"BigQuantSpace.dl_layer_lstm.dl_layer_lstm-v1","ModuleParameters":[{"Name":"units","Value":"32","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"activation","Value":"linear","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_activation","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"recurrent_activation","Value":"hard_sigmoid","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_recurrent_activation","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"use_bias","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"kernel_initializer","Value":"glorot_uniform","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_kernel_initializer","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"recurrent_initializer","Value":"Orthogonal","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_recurrent_initializer","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"bias_initializer","Value":"Ones","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_bias_initializer","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"unit_forget_bias","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"kernel_regularizer","Value":"None","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"kernel_regularizer_l1","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"kernel_regularizer_l2","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_kernel_regularizer","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"recurrent_regularizer","Value":"None","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"recurrent_regularizer_l1","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"recurrent_regularizer_l2","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_recurrent_regularizer","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"bias_regularizer","Value":"None","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"bias_regularizer_l1","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"bias_regularizer_l2","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_bias_regularizer","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"activity_regularizer","Value":"None","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"activity_regularizer_l1","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"activity_regularizer_l2","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_activity_regularizer","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"kernel_constraint","Value":"None","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_kernel_constraint","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"recurrent_constraint","Value":"None","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_recurrent_constraint","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"bias_constraint","Value":"None","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_bias_constraint","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"dropout","Value":"0","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"recurrent_dropout","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"return_sequences","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"implementation","Value":"0","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"name","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"inputs","NodeId":"-218"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-218","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":4,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-316","ModuleId":"BigQuantSpace.dl_model_init.dl_model_init-v1","ModuleParameters":[],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"inputs","NodeId":"-316"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"outputs","NodeId":"-316"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-316","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":5,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-320","ModuleId":"BigQuantSpace.dl_model_train.dl_model_train-v1","ModuleParameters":[{"Name":"optimizer","Value":"RMSprop","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_optimizer","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"loss","Value":"mean_squared_error","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_loss","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"metrics","Value":"mse","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"batch_size","Value":"512","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"epochs","Value":"10","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"n_gpus","Value":"0","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"verbose","Value":"1:输出进度条记录","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_model","NodeId":"-320"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"training_data","NodeId":"-320"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"validation_data","NodeId":"-320"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-320","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":6,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-332","ModuleId":"BigQuantSpace.dl_model_predict.dl_model_predict-v1","ModuleParameters":[{"Name":"batch_size","Value":"1024","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"n_gpus","Value":"2","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"verbose","Value":"2:每个epoch输出一行记录","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"trained_model","NodeId":"-332"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-332"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-332","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":7,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-2295","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"return_5-1\nreturn_10-1\nreturn_20-1\navg_amount_0/avg_amount_5-1\navg_amount_5/avg_amount_20-1\nrank_avg_amount_0-rank_avg_amount_5\nrank_avg_amount_5-rank_avg_amount_10\nrank_return_0-rank_return_5\nrank_return_5-rank_return_10\nbeta_csi300_30_0/10\nbeta_csi300_60_0/10\nswing_volatility_5_0/swing_volatility_30_0-1\nswing_volatility_30_0/swing_volatility_60_0-1\nta_atr_14_0/ta_atr_28_0-1\nta_sma_5_0/ta_sma_20_0-1\nta_sma_10_0/ta_sma_20_0-1\nta_sma_20_0/ta_sma_30_0-1\nta_sma_30_0/ta_sma_60_0-1\nta_rsi_14_0/100\nta_rsi_28_0/100\nta_cci_14_0/500\nta_cci_28_0/500\nbeta_industry_30_0/10\nbeta_industry_60_0/10\nta_sma(amount_0, 10)/ta_sma(amount_0, 20)-1\nta_sma(amount_0, 20)/ta_sma(amount_0, 30)-1\nta_sma(amount_0, 30)/ta_sma(amount_0, 60)-1\nta_sma(amount_0, 50)/ta_sma(amount_0, 100)-1\nta_sma(turn_0, 10)/ta_sma(turn_0, 20)-1\nta_sma(turn_0, 20)/ta_sma(turn_0, 30)-1\nta_sma(turn_0, 30)/ta_sma(turn_0, 60)-1\nta_sma(turn_0, 50)/ta_sma(turn_0, 100)-1\nhigh_0/low_0-1\nclose_0/open_0-1\nshift(close_0,1)/close_0-1\nshift(close_0,2)/close_0-1\nshift(close_0,3)/close_0-1\nshift(close_0,4)/close_0-1\nshift(close_0,5)/close_0-1\nshift(close_0,10)/close_0-1\nshift(close_0,20)/close_0-1\nta_sma(high_0-low_0, 5)/ta_sma(high_0-low_0, 20)-1\nta_sma(high_0-low_0, 10)/ta_sma(high_0-low_0, 20)-1\nta_sma(high_0-low_0, 20)/ta_sma(high_0-low_0, 30)-1\nta_sma(high_0-low_0, 30)/ta_sma(high_0-low_0, 60)-1\nta_sma(high_0-low_0, 50)/ta_sma(high_0-low_0, 100)-1\nrank_avg_amount_5\nrank_avg_turn_5\nrank_volatility_5_0\nrank_swing_volatility_5_0\nrank_avg_mf_net_amount_5\nrank_beta_industry_5_0\nrank_return_5\nrank_return_2\nstd(close_0,5)/std(close_0,20)-1\nstd(close_0,10)/std(close_0,20)-1\nstd(close_0,20)/std(close_0,30)-1\nstd(close_0,30)/std(close_0,60)-1\nstd(close_0,50)/std(close_0,100)-1","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"-2295"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-2295","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":8,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-259","ModuleId":"BigQuantSpace.dl_layer_dense.dl_layer_dense-v1","ModuleParameters":[{"Name":"units","Value":"1","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"activation","Value":"sigmoid","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_activation","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"use_bias","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"kernel_initializer","Value":"glorot_uniform","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_kernel_initializer","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"bias_initializer","Value":"Zeros","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_bias_initializer","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"kernel_regularizer","Value":"None","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"kernel_regularizer_l1","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"kernel_regularizer_l2","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_kernel_regularizer","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"bias_regularizer","Value":"None","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"bias_regularizer_l1","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"bias_regularizer_l2","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_bias_regularizer","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"activity_regularizer","Value":"None","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"activity_regularizer_l1","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"activity_regularizer_l2","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_activity_regularizer","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"kernel_constraint","Value":"None","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_kernel_constraint","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"bias_constraint","Value":"None","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_bias_constraint","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"name","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"inputs","NodeId":"-259"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-259","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":9,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-2296","ModuleId":"BigQuantSpace.dropnan.dropnan-v1","ModuleParameters":[],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-2296"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-2296","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":18,"IsPartOfPartialRun":null,"Comment":"去掉为nan的数据","CommentCollapsed":true},{"Id":"-620","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2010-01-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2017-03-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"market","Value":"CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"600009.SHA","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_count","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"rolling_conf","NodeId":"-620"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-620","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":24,"IsPartOfPartialRun":null,"Comment":"证券标的及起始截止时间","CommentCollapsed":false},{"Id":"-281","ModuleId":"BigQuantSpace.trade.trade-v4","ModuleParameters":[{"Name":"start_date","Value":"2017-04-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"handle_data","Value":"# 回测引擎:每日数据处理函数,每天执行一次\ndef bigquant_run(context, data):\n # 按日期过滤得到今日的预测数据\n try:\n prediction = context.prediction[data.current_dt.strftime('%Y-%m-%d')]\n except KeyError as e:\n return\n instrument = context.instruments[0]\n sid = context.symbol(instrument)\n cur_position = context.portfolio.positions[sid].amount\n \n # 交易逻辑\n if prediction > 0.5 and cur_position == 0:\n context.order_target_percent(context.symbol(instrument), 1)\n #print(data.current_dt, '买入!')\n \n elif prediction < 0.5 and cur_position > 0:\n context.order_target_percent(context.symbol(instrument), 0)\n #print(data.current_dt, '卖出!')\n ","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"prepare","Value":"# 回测引擎:准备数据,只执行一次\ndef bigquant_run(context):\n pass\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"initialize","Value":"# 回测引擎:初始化函数,只执行一次\ndef bigquant_run(context):\n # 加载预测数据\n context.prediction = context.options['data'].read_df()\n\n # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数\n context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"before_trading_start","Value":"# 回测引擎:每个单位时间开始前调用一次,即每日开盘前调用一次。\ndef bigquant_run(context, data):\n pass\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"volume_limit","Value":0.025,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"order_price_field_buy","Value":"open","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"order_price_field_sell","Value":"close","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"capital_base","Value":1000000,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"auto_cancel_non_tradable_orders","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"data_frequency","Value":"daily","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"price_type","Value":"真实价格","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"product_type","Value":"股票","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"plot_charts","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"backtest_only","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"benchmark","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-281"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"options_data","NodeId":"-281"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"history_ds","NodeId":"-281"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"benchmark_ds","NodeId":"-281"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"trading_calendar","NodeId":"-281"}],"OutputPortsInternal":[{"Name":"raw_perf","NodeId":"-281","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":1,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-1403","ModuleId":"BigQuantSpace.dl_layer_lstm.dl_layer_lstm-v1","ModuleParameters":[{"Name":"units","Value":"32","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"activation","Value":"sigmoid","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_activation","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"recurrent_activation","Value":"hard_sigmoid","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_recurrent_activation","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"use_bias","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"kernel_initializer","Value":"glorot_uniform","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_kernel_initializer","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"recurrent_initializer","Value":"Orthogonal","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_recurrent_initializer","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"bias_initializer","Value":"Zeros","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_bias_initializer","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"unit_forget_bias","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"kernel_regularizer","Value":"None","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"kernel_regularizer_l1","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"kernel_regularizer_l2","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_kernel_regularizer","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"recurrent_regularizer","Value":"None","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"recurrent_regularizer_l1","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"recurrent_regularizer_l2","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_recurrent_regularizer","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"bias_regularizer","Value":"None","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"bias_regularizer_l1","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"bias_regularizer_l2","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_bias_regularizer","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"activity_regularizer","Value":"None","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"activity_regularizer_l1","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"activity_regularizer_l2","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_activity_regularizer","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"kernel_constraint","Value":"None","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_kernel_constraint","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"recurrent_constraint","Value":"None","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_recurrent_constraint","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"bias_constraint","Value":"None","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_bias_constraint","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"dropout","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"recurrent_dropout","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"return_sequences","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"implementation","Value":"0","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"name","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"inputs","NodeId":"-1403"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-1403","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":25,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-1474","ModuleId":"BigQuantSpace.dl_layer_dropout.dl_layer_dropout-v1","ModuleParameters":[{"Name":"rate","Value":"0.1","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"noise_shape","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"seed","Value":"0","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"name","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"inputs","NodeId":"-1474"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-1474","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":10,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-1481","ModuleId":"BigQuantSpace.dl_layer_dropout.dl_layer_dropout-v1","ModuleParameters":[{"Name":"rate","Value":"0.1","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"noise_shape","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"seed","Value":"0","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"name","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"inputs","NodeId":"-1481"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-1481","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":11,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-1126","ModuleId":"BigQuantSpace.dl_convert_to_bin.dl_convert_to_bin-v2","ModuleParameters":[{"Name":"window_size","Value":"5","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"feature_clip","Value":5,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"flatten","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"window_along_col","Value":"instrument","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-1126"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-1126"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-1126","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":13,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-1134","ModuleId":"BigQuantSpace.dl_convert_to_bin.dl_convert_to_bin-v2","ModuleParameters":[{"Name":"window_size","Value":"5","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"feature_clip","Value":5,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"flatten","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"window_along_col","Value":"instrument","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-1134"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-1134"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-1134","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":14,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-283","ModuleId":"BigQuantSpace.use_datasource.use_datasource-v1","ModuleParameters":[{"Name":"datasource_id","Value":"bar1d_CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-283"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-283"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-283","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":15,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-289","ModuleId":"BigQuantSpace.advanced_auto_labeler.advanced_auto_labeler-v2","ModuleParameters":[{"Name":"label_expr","Value":"# #号开始的表示注释\n# 0. 每行一个,顺序执行,从第二个开始,可以使用label字段\n# 1. 可用数据字段见 https://bigquant.com/docs/develop/datasource/deprecated/history_data.html\n# 添加benchmark_前缀,可使用对应的benchmark数据\n# 2. 可用操作符和函数见 `表达式引擎 <https://bigquant.com/docs/develop/bigexpr/usage.html>`_\n\n# 计算收益:5日收盘价(作为卖出价格)除以明日开盘价(作为买入价格)\nshift(close, -5) / shift(open, -1)\n\n# 极值处理:用1%和99%分位的值做clip\nclip(label, all_quantile(label, 0.01), all_quantile(label, 0.99))\n\n# 过滤掉一字涨停的情况 (设置label为NaN,在后续处理和训练中会忽略NaN的label)\nwhere(shift(high, -1) == shift(low, -1), NaN, label)\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"benchmark","Value":"000300.SHA","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"drop_na_label","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"cast_label_int","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_functions","Value":"{}","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-289"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-289","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":21,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-300","ModuleId":"BigQuantSpace.general_feature_extractor.general_feature_extractor-v7","ModuleParameters":[{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"before_start_days","Value":90,"ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-300"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-300"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-300","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":16,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-307","ModuleId":"BigQuantSpace.join.join-v3","ModuleParameters":[{"Name":"on","Value":"date,instrument","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"how","Value":"inner","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"sort","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"data1","NodeId":"-307"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"data2","NodeId":"-307"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-307","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":17,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-313","ModuleId":"BigQuantSpace.dropnan.dropnan-v1","ModuleParameters":[],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-313"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-313","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":19,"IsPartOfPartialRun":null,"Comment":"去掉为nan的数据","CommentCollapsed":true},{"Id":"-317","ModuleId":"BigQuantSpace.derived_feature_extractor.derived_feature_extractor-v3","ModuleParameters":[{"Name":"date_col","Value":"date","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_col","Value":"instrument","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"drop_na","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"remove_extra_columns","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_functions","Value":"{}","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-317"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-317"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-317","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":22,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-325","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2017-03-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2019-03-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"market","Value":"CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"600009.SHA","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_count","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"rolling_conf","NodeId":"-325"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-325","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":23,"IsPartOfPartialRun":null,"Comment":"证券标的及起始截止时间","CommentCollapsed":true},{"Id":"-334","ModuleId":"BigQuantSpace.general_feature_extractor.general_feature_extractor-v7","ModuleParameters":[{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"before_start_days","Value":90,"ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-334"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-334"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-334","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":26,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-341","ModuleId":"BigQuantSpace.derived_feature_extractor.derived_feature_extractor-v3","ModuleParameters":[{"Name":"date_col","Value":"date","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_col","Value":"instrument","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"drop_na","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"remove_extra_columns","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_functions","Value":"{}","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-341"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-341"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-341","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":27,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true}],"SerializedClientData":"<?xml version='1.0' encoding='utf-16'?><DataV1 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Meta /><NodePositions><NodePosition Node='-214' Position='755,784,200,200'/><NodePosition Node='-210' Position='64,-126,200,200'/><NodePosition Node='-218' Position='216,20,200,200'/><NodePosition Node='-316' Position='84,483,200,200'/><NodePosition Node='-320' Position='338,607,200,200'/><NodePosition Node='-332' Position='550,695,200,200'/><NodePosition Node='-2295' Position='1120,-4.610298156738281,200,200'/><NodePosition Node='-259' Position='201,379,200,200'/><NodePosition Node='-2296' Position='838,418,200,200'/><NodePosition Node='-620' Position='773,-22,200,200'/><NodePosition Node='-281' Position='937,905,200,200'/><NodePosition Node='-1403' Position='207,193,200,200'/><NodePosition Node='-1474' Position='209,107,200,200'/><NodePosition Node='-1481' Position='206,273,200,200'/><NodePosition Node='-1126' Position='863,518,200,200'/><NodePosition Node='-1134' Position='1302,545,200,200'/><NodePosition Node='-283' Position='1159,660,200,200'/><NodePosition Node='-289' Position='631,195,200,200'/><NodePosition Node='-300' Position='935,119,200,200'/><NodePosition Node='-307' Position='798,304,200,200'/><NodePosition Node='-313' Position='1378,346,200,200'/><NodePosition Node='-317' Position='961,200,200,200'/><NodePosition Node='-325' Position='1423,61,200,200'/><NodePosition Node='-334' Position='1369,168,200,200'/><NodePosition Node='-341' Position='1389,252,200,200'/></NodePositions><NodeGroups /></DataV1>"},"IsDraft":true,"ParentExperimentId":null,"WebService":{"IsWebServiceExperiment":false,"Inputs":[],"Outputs":[],"Parameters":[{"Name":"交易日期","Value":"","ParameterDefinition":{"Name":"交易日期","FriendlyName":"交易日期","DefaultValue":"","ParameterType":"String","HasDefaultValue":true,"IsOptional":true,"ParameterRules":[],"HasRules":false,"MarkupType":0,"CredentialDescriptor":null}}],"WebServiceGroupId":null,"SerializedClientData":"<?xml version='1.0' encoding='utf-16'?><DataV1 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Meta /><NodePositions></NodePositions><NodeGroups /></DataV1>"},"DisableNodesUpdate":false,"Category":"user","Tags":[],"IsPartialRun":true}
    In [1]:
    # 本代码由可视化策略环境自动生成 2019年4月11日 16:34
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    # 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 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_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_before_trading_start_bigquant_run(context, data):
        pass
    
    
    m3 = M.dl_layer_input.v1(
        shape='5,59',
        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.1,
        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='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="""return_5-1
    return_10-1
    return_20-1
    avg_amount_0/avg_amount_5-1
    avg_amount_5/avg_amount_20-1
    rank_avg_amount_0-rank_avg_amount_5
    rank_avg_amount_5-rank_avg_amount_10
    rank_return_0-rank_return_5
    rank_return_5-rank_return_10
    beta_csi300_30_0/10
    beta_csi300_60_0/10
    swing_volatility_5_0/swing_volatility_30_0-1
    swing_volatility_30_0/swing_volatility_60_0-1
    ta_atr_14_0/ta_atr_28_0-1
    ta_sma_5_0/ta_sma_20_0-1
    ta_sma_10_0/ta_sma_20_0-1
    ta_sma_20_0/ta_sma_30_0-1
    ta_sma_30_0/ta_sma_60_0-1
    ta_rsi_14_0/100
    ta_rsi_28_0/100
    ta_cci_14_0/500
    ta_cci_28_0/500
    beta_industry_30_0/10
    beta_industry_60_0/10
    ta_sma(amount_0, 10)/ta_sma(amount_0, 20)-1
    ta_sma(amount_0, 20)/ta_sma(amount_0, 30)-1
    ta_sma(amount_0, 30)/ta_sma(amount_0, 60)-1
    ta_sma(amount_0, 50)/ta_sma(amount_0, 100)-1
    ta_sma(turn_0, 10)/ta_sma(turn_0, 20)-1
    ta_sma(turn_0, 20)/ta_sma(turn_0, 30)-1
    ta_sma(turn_0, 30)/ta_sma(turn_0, 60)-1
    ta_sma(turn_0, 50)/ta_sma(turn_0, 100)-1
    high_0/low_0-1
    close_0/open_0-1
    shift(close_0,1)/close_0-1
    shift(close_0,2)/close_0-1
    shift(close_0,3)/close_0-1
    shift(close_0,4)/close_0-1
    shift(close_0,5)/close_0-1
    shift(close_0,10)/close_0-1
    shift(close_0,20)/close_0-1
    ta_sma(high_0-low_0, 5)/ta_sma(high_0-low_0, 20)-1
    ta_sma(high_0-low_0, 10)/ta_sma(high_0-low_0, 20)-1
    ta_sma(high_0-low_0, 20)/ta_sma(high_0-low_0, 30)-1
    ta_sma(high_0-low_0, 30)/ta_sma(high_0-low_0, 60)-1
    ta_sma(high_0-low_0, 50)/ta_sma(high_0-low_0, 100)-1
    rank_avg_amount_5
    rank_avg_turn_5
    rank_volatility_5_0
    rank_swing_volatility_5_0
    rank_avg_mf_net_amount_5
    rank_beta_industry_5_0
    rank_return_5
    rank_return_2
    std(close_0,5)/std(close_0,20)-1
    std(close_0,10)/std(close_0,20)-1
    std(close_0,20)/std(close_0,30)-1
    std(close_0,30)/std(close_0,60)-1
    std(close_0,50)/std(close_0,100)-1"""
    )
    
    m24 = M.instruments.v2(
        start_date='2010-01-01',
        end_date='2017-03-01',
        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日收盘价(作为卖出价格)除以明日开盘价(作为买入价格)
    shift(close, -5) / shift(open, -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)
    """,
        start_date='',
        end_date='',
        benchmark='000300.SHA',
        drop_na_label=True,
        cast_label_int=True,
        user_functions={}
    )
    
    m16 = M.general_feature_extractor.v7(
        instruments=m24.data,
        features=m8.data,
        start_date='',
        end_date='',
        before_start_days=90
    )
    
    m22 = 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={}
    )
    
    m17 = M.join.v3(
        data1=m21.data,
        data2=m22.data,
        on='date,instrument',
        how='inner',
        sort=False
    )
    
    m18 = M.dropnan.v1(
        input_data=m17.data
    )
    
    m13 = M.dl_convert_to_bin.v2(
        input_data=m18.data,
        features=m8.data,
        window_size=5,
        feature_clip=5,
        flatten=False,
        window_along_col='instrument'
    )
    
    m6 = M.dl_model_train.v1(
        input_model=m5.data,
        training_data=m13.data,
        optimizer='RMSprop',
        loss='mean_squared_error',
        metrics='mse',
        batch_size=512,
        epochs=10,
        n_gpus=0,
        verbose='1:输出进度条记录'
    )
    
    m23 = M.instruments.v2(
        start_date='2017-03-01',
        end_date='2019-03-01',
        market='CN_STOCK_A',
        instrument_list='600009.SHA',
        max_count=0
    )
    
    m26 = M.general_feature_extractor.v7(
        instruments=m23.data,
        features=m8.data,
        start_date='',
        end_date='',
        before_start_days=90
    )
    
    m27 = M.derived_feature_extractor.v3(
        input_data=m26.data,
        features=m8.data,
        date_col='date',
        instrument_col='instrument',
        drop_na=False,
        remove_extra_columns=False,
        user_functions={}
    )
    
    m19 = M.dropnan.v1(
        input_data=m27.data
    )
    
    m14 = M.dl_convert_to_bin.v2(
        input_data=m19.data,
        features=m8.data,
        window_size=5,
        feature_clip=5,
        flatten=False,
        window_along_col='instrument'
    )
    
    m7 = M.dl_model_predict.v1(
        trained_model=m6.data,
        input_data=m14.data,
        batch_size=1024,
        n_gpus=2,
        verbose='2:每个epoch输出一行记录'
    )
    
    m2 = M.cached.v3(
        input_1=m7.data,
        input_2=m19.data,
        run=m2_run_bigquant_run,
        post_run=m2_post_run_bigquant_run,
        input_ports='',
        params='{}',
        output_ports=''
    )
    
    m15 = M.use_datasource.v1(
        instruments=m23.data,
        datasource_id='bar1d_CN_STOCK_A',
        start_date='',
        end_date=''
    )
    
    m1 = M.trade.v4(
        instruments=m23.data,
        options_data=m2.data_1,
        benchmark_ds=m15.data,
        start_date='2017-04-01',
        end_date='',
        handle_data=m1_handle_data_bigquant_run,
        prepare=m1_prepare_bigquant_run,
        initialize=m1_initialize_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=''
    )
    
    Using TensorFlow backend.
    
    DataSource(fb36dcc2a6184901a6e50f01fc42ba18, v3)
    
    • 收益率48.13%
    • 年化收益率23.67%
    • 基准收益率102.85%
    • 阿尔法0.05
    • 贝塔0.37
    • 夏普比率0.95
    • 胜率0.71
    • 盈亏比1.13
    • 收益波动率21.81%
    • 信息比率-0.05
    • 最大回撤12.63%

    [量化学堂-机器学习]基于LSTM的股票价格预测模型
    序列窗口滚动报错bigquant: module name: dl_convert_to_bin, module version: v2, trackeback: Traceback (most recent call last): KeyError: 'instrument'
    (youke) #2

    一年就一笔交易啊???


    (lpl22) #3

    考虑到计算速度的问题,这里只选了一支股票作了预测。在m24模块可以调整


    如果需要对多个股票预测,建议用机器学习的模型。