【宽客学院】DNN算法实现股票预测

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

(小Q) #1

在阅读了 深度学习的简要介绍后,本文将介绍深度学习DNN模型及其在量化投资领域中的应用。

深度学习在量化领域应用如何?

机器学习作为人工智能的核心,其传统算法在解决很多问题上都表现出了高效性。随着近些年数据处理技术上的进步和计算能力的提升,深度学习得以在很多问题上也大放光彩,成为近一段时间互联网、金融等领域的大热门。

在量化投资领域,机器学习尤其是由统计学延伸的各种算法一直以来都被尝试应用在选股、择时等策略的开发上,随着深度学习在其他领域上的突破,其在自动化交易甚至投资策略的自开发自学习方面的应用成为了大家探索的焦点。

为什么要用深度学习?

深度学习目前最成功的场景应用是在模式识别上,即利用已知数据,对具有一定空间、时间分布信息的数据与类别标号之间的映射做一个较好的估计。之所以在结构性识别的任务中,深度学习可以表现得比传统机器学习算法更好,主要有以下三点原因:

  1. 深度学习的自动提取特征比传统机器学习的人为提取特征过程更加高效。特定的应用场景中,只需要微调结构,如神经元的激活函数,就可以得到较好的效果。

  2. 深度学习可以通过复杂的结构和多重非线性处理层更好的捕捉各类非线性关系

  3. 深度学习随着数据量的增加模型效果会不断的改善,这也是当前深度学习有逐渐取代传
    统机器学习模型趋势的最大原因。

如何在平台上实现深度学习算法?

我们以DNN为例子,在本文末尾可克隆策略供各位研究。策略缩略图如下:

  • 选定股票及特征/选定回测数据:此处划定了训练集和测试集的数据。
  • 设定序列窗口滚动:设定了每次输入深度学习模型的数据量。(参数——窗口大小:指每次加入学习的数据天数)
  • 预测、回测部分:与机器学习部分相同。

接下来我们着重讲解构建模型部分

构建深度学习模型

DNN模型使用到了全连接层、Dropout层、输入层。

全连接层(Dense)

全连接层中,所有输入层的节点一定和输出层的任一节点相连接。

  • 每条连线对应了一个权重,令这些权值构成的矩阵为kernel权值矩阵。
  • 通过使用偏置向量bias可以避免得出局部最优(只在 use_bias 为 True 时才有用)。
  • activation 是按逐个元素计算的激活函数
    Dense 将实现以下操作: output = activation(dot(input, kernel) + bias)
可调整的参数图:




以下是对参数选择的讲解:

权值初始化

多层网络初始化w的时候,一般不初始化为0,会初始化为一个非0的很小的参数。
w初始值不宜太大,若w初始过大,z值过大,dz值过小,学习会很慢(对于激活函数为sigmoid或tanh函数而言)

常量初始化(constant)

   把权值或者偏置初始化为一个常数,具体是什么常数,可以自己定义

高斯分布初始化(gaussian)

   需要给定高斯函数的均值与标准差 

positive_unitball初始化

   让每一个神经元的输入的权值和为 1,例如:一个神经元有100个输入,让这100个输入的权值和为1.  首先给这100个权值赋值为在(0,1)之间的均匀分布,然后,每一个权值再除以它们的和就可以啦。这么做,可以有助于防止权值初始化过大,从而防止激活函数(sigmoid函数)进入饱和区。所以,它应该比较适合sigmoid形的激活函数

均匀分布初始化(uniform)

   将权值与偏置进行均匀分布的初始化,用min 与 max 来控制它们的的上下限,默认为(0,1)

xavier初始化

   对于权值的分布:均值为0,方差为(1 / 输入的个数) 的 均匀分布。如果我们更注重前向传播的话,我们可以选择 fan_in,即正向传播的输入个数;如果更注重后向传播的话,我们选择 fan_out, 因为在反向传播的时候,fan_out就是神经元的输入个数;如果两者都考虑的话,就选  average = (fan_in + fan_out) /2。

msra初始化

   对于权值的分布:基于均值为0,方差为( 2/输入的个数)的高斯分布;它特别适合 ReLU激活函数,该方法主要是基于Relu函数提出的。
偏置向量

目的是更好地拟合数据,具体效果可参考神经网络中偏置的作用

激活函数的选择

用于分类器时,Sigmoid函数及其组合通常效果更好。

由于梯度消失问题,有时要避免使用sigmoid和tanh函数。

ReLU函数是一个通用的激活函数,目前在大多数情况下使用。

如果神经网络中出现死神经元,那么PReLU函数就是最好的选择。

请记住,ReLU函数只能在隐藏层中使用。

输出空间维度

决定输出层数据的维度。过大容易造成过拟合、运算时间长。过小容易造成欠拟合

Dropout层

Dropout 包括在训练中每次更新时, 将输入单元的按比率随机设置为 0, 这有助于防止过拟合。

可调整的参数图

以下是对参数选择的讲解:

随机种子

计算机并不能产生真正的随机数,如果你不设种子,计算机会用系统时钟来作为种子,如果你要模拟什么的话,每次的随机数都是不一样的,这样就不方便你研究,如果你事先设置了种子,这样每次的随机数都是一样的,便于重现你的研究,也便于其他人检验你的分析结果。

rate

在 0 和 1 之间浮动。需要丢弃的输入比例。适当丢弃数据可以有效防止过拟合

输入层

可调整的参数图

以下是对参数选择的讲解:

dtype

输入所期望的数据类型,字符串表示 (float32, float64, int32…)

shape

一个尺寸元组(整数),不包含批量大小。A shape tuple (integer), not including the batch size. 例如,shape=(32,) 表明期望的输入是按批次的 32 维向量。本模板策略中输入特征有59个,所以此处输入的shape为59

batch_shape

一个尺寸元组(整数),包含批量大小。 例如,batch_shape=(10, 32) 表明期望的输入是 10 个 32 维向量。 batch_shape=(None, 32) 表明任意批次大小的 32 维向量。

克隆策略

    {"Description":"实验创建于2017/8/26","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-15:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-8:data"},{"DestinationInputPortId":"-106:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-8:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-53:data1","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-15:data"},{"DestinationInputPortId":"-106:features","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"-113:features","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"-122:features","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"-129:features","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"-1535:features","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"-1547:features","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"-1535:input_data","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-53:data"},{"DestinationInputPortId":"-122:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62:data"},{"DestinationInputPortId":"-141:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62:data"},{"DestinationInputPortId":"-113:input_data","SourceOutputPortId":"-106:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-53:data2","SourceOutputPortId":"-113:data"},{"DestinationInputPortId":"-129:input_data","SourceOutputPortId":"-122:data"},{"DestinationInputPortId":"-1547:input_data","SourceOutputPortId":"-129:data"},{"DestinationInputPortId":"-2431:input_2","SourceOutputPortId":"-129:data"},{"DestinationInputPortId":"-168:inputs","SourceOutputPortId":"-160:data"},{"DestinationInputPortId":"-682:inputs","SourceOutputPortId":"-160:data"},{"DestinationInputPortId":"-224:inputs","SourceOutputPortId":"-168:data"},{"DestinationInputPortId":"-231:inputs","SourceOutputPortId":"-196:data"},{"DestinationInputPortId":"-196:inputs","SourceOutputPortId":"-224:data"},{"DestinationInputPortId":"-238:inputs","SourceOutputPortId":"-231:data"},{"DestinationInputPortId":"-682:outputs","SourceOutputPortId":"-238:data"},{"DestinationInputPortId":"-1098:input_model","SourceOutputPortId":"-682:data"},{"DestinationInputPortId":"-1540:trained_model","SourceOutputPortId":"-1098:data"},{"DestinationInputPortId":"-1098:training_data","SourceOutputPortId":"-1535:data"},{"DestinationInputPortId":"-2431:input_1","SourceOutputPortId":"-1540:data"},{"DestinationInputPortId":"-1540:input_data","SourceOutputPortId":"-1547:data"},{"DestinationInputPortId":"-141:options_data","SourceOutputPortId":"-2431:data_1"}],"ModuleNodes":[{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-8","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2014-01-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2017-12-31","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"market","Value":"CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_count","Value":"0","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"rolling_conf","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-8"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-8","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":1,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-15","ModuleId":"BigQuantSpace.advanced_auto_labeler.advanced_auto_labeler-v2","ModuleParameters":[{"Name":"label_expr","Value":"# #号开始的表示注释\n# 0. 每行一个,顺序执行,从第二个开始,可以使用label字段\n# 1. 可用数据字段见 https://bigquant.com/docs/data_history_data.html\n# 添加benchmark_前缀,可使用对应的benchmark数据\n# 2. 可用操作符和函数见 `表达式引擎 <https://bigquant.com/docs/big_expr.html>`_\n\n# 计算收益:5日收盘价(作为卖出价格)除以明日开盘价(作为买入价格)\nshift(close, -5) / shift(open, -1)-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)\nwhere(label>0.5, NaN, label)\nwhere(label<-0.5, 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":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_functions","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-15"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-15","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":2,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"# #号开始的表示注释\n# 多个特征,每行一个,可以包含基础特征和衍生特征\nreturn_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\n","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":3,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-53","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":"287d2cb0-f53c-4101-bdf8-104b137c8601-53"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"data2","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-53"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-53","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":7,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-62","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2018-01-01","ValueType":"Literal","LinkedGlobalParameter":"交易日期"},{"Name":"end_date","Value":"2018-12-31","ValueType":"Literal","LinkedGlobalParameter":"交易日期"},{"Name":"market","Value":"CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_count","Value":"0","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"rolling_conf","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":9,"IsPartOfPartialRun":null,"Comment":"预测数据,用于回测和模拟","CommentCollapsed":false},{"Id":"-106","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":0,"ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-106"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-106"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-106","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":15,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-113","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":"True","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":"-113"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-113"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-113","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":16,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-122","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":0,"ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-122"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-122"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-122","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":17,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-129","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":"True","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":"-129"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-129"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-129","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":18,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-141","ModuleId":"BigQuantSpace.trade.trade-v4","ModuleParameters":[{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"handle_data","Value":"# 回测引擎:每日数据处理函数,每天执行一次\ndef bigquant_run(context, data):\n # 按日期过滤得到今日的预测数据\n ranker_prediction = context.ranker_prediction[\n context.ranker_prediction.date == data.current_dt.strftime('%Y-%m-%d')]\n\n # 1. 资金分配\n # 平均持仓时间是hold_days,每日都将买入股票,每日预期使用 1/hold_days 的资金\n # 实际操作中,会存在一定的买入误差,所以在前hold_days天,等量使用资金;之后,尽量使用剩余资金(这里设置最多用等量的1.5倍)\n is_staging = context.trading_day_index < context.options['hold_days'] # 是否在建仓期间(前 hold_days 天)\n cash_avg = context.portfolio.portfolio_value / context.options['hold_days']\n cash_for_buy = min(context.portfolio.cash, (1 if is_staging else 1.5) * cash_avg)\n cash_for_sell = cash_avg - (context.portfolio.cash - cash_for_buy)\n positions = {e.symbol: p.amount * p.last_sale_price\n for e, p in context.perf_tracker.position_tracker.positions.items()}\n\n # 2. 生成卖出订单:hold_days天之后才开始卖出;对持仓的股票,按机器学习算法预测的排序末位淘汰\n if not is_staging and cash_for_sell > 0:\n equities = {e.symbol: e for e, p in context.perf_tracker.position_tracker.positions.items()}\n instruments = list(reversed(list(ranker_prediction.instrument[ranker_prediction.instrument.apply(\n lambda x: x in equities and not context.has_unfinished_sell_order(equities[x]))])))\n # print('rank order for sell %s' % instruments)\n for instrument in instruments:\n context.order_target(context.symbol(instrument), 0)\n cash_for_sell -= positions[instrument]\n if cash_for_sell <= 0:\n break\n\n # 3. 生成买入订单:按机器学习算法预测的排序,买入前面的stock_count只股票\n buy_cash_weights = context.stock_weights\n buy_instruments = list(ranker_prediction.instrument[:len(buy_cash_weights)])\n max_cash_per_instrument = context.portfolio.portfolio_value * context.max_cash_per_instrument\n for i, instrument in enumerate(buy_instruments):\n cash = cash_for_buy * buy_cash_weights[i]\n if cash > max_cash_per_instrument - positions.get(instrument, 0):\n # 确保股票持仓量不会超过每次股票最大的占用资金量\n cash = max_cash_per_instrument - positions.get(instrument, 0)\n if cash > 0:\n context.order_value(context.symbol(instrument), cash)\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.ranker_prediction = context.options['data'].read_df()\n\n # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数\n context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))\n # 预测数据,通过options传入进来,使用 read_df 函数,加载到内存 (DataFrame)\n # 设置买入的股票数量,这里买入预测股票列表排名靠前的5只\n stock_count = 5\n # 每只的股票的权重,如下的权重分配会使得靠前的股票分配多一点的资金,[0.339160, 0.213986, 0.169580, ..]\n context.stock_weights = T.norm([1 / math.log(i + 2) for i in range(0, stock_count)])\n # 设置每只股票占用的最大资金比例\n context.max_cash_per_instrument = 0.2\n context.options['hold_days'] = 5\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"before_trading_start","Value":"","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":"000300.SHA","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-141"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"options_data","NodeId":"-141"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"history_ds","NodeId":"-141"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"benchmark_ds","NodeId":"-141"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"trading_calendar","NodeId":"-141"}],"OutputPortsInternal":[{"Name":"raw_perf","NodeId":"-141","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":19,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-160","ModuleId":"BigQuantSpace.dl_layer_input.dl_layer_input-v1","ModuleParameters":[{"Name":"shape","Value":"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":"-160"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-160","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":6,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-168","ModuleId":"BigQuantSpace.dl_layer_dense.dl_layer_dense-v1","ModuleParameters":[{"Name":"units","Value":"256","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"activation","Value":"relu","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":"-168"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-168","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":8,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-196","ModuleId":"BigQuantSpace.dl_layer_dense.dl_layer_dense-v1","ModuleParameters":[{"Name":"units","Value":"128","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"activation","Value":"relu","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":"-196"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-196","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":20,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-224","ModuleId":"BigQuantSpace.dl_layer_dropout.dl_layer_dropout-v1","ModuleParameters":[{"Name":"rate","Value":"0.9","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"noise_shape","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"seed","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"name","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"inputs","NodeId":"-224"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-224","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":21,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-231","ModuleId":"BigQuantSpace.dl_layer_dropout.dl_layer_dropout-v1","ModuleParameters":[{"Name":"rate","Value":"0.9","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"noise_shape","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"seed","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"name","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"inputs","NodeId":"-231"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-231","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":22,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-238","ModuleId":"BigQuantSpace.dl_layer_dense.dl_layer_dense-v1","ModuleParameters":[{"Name":"units","Value":"1","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"activation","Value":"linear","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":"-238"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-238","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":23,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-682","ModuleId":"BigQuantSpace.dl_model_init.dl_model_init-v1","ModuleParameters":[],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"inputs","NodeId":"-682"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"outputs","NodeId":"-682"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-682","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":4,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-1098","ModuleId":"BigQuantSpace.dl_model_train.dl_model_train-v1","ModuleParameters":[{"Name":"optimizer","Value":"Adam","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":"10240","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"epochs","Value":"2","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"n_gpus","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"verbose","Value":"2:每个epoch输出一行记录","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_model","NodeId":"-1098"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"training_data","NodeId":"-1098"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"validation_data","NodeId":"-1098"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-1098","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":5,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-1535","ModuleId":"BigQuantSpace.dl_convert_to_bin.dl_convert_to_bin-v1","ModuleParameters":[{"Name":"window_size","Value":1,"ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-1535"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-1535"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-1535","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":10,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-1540","ModuleId":"BigQuantSpace.dl_model_predict.dl_model_predict-v1","ModuleParameters":[{"Name":"batch_size","Value":"1024","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"n_gpus","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"verbose","Value":"2:每个epoch输出一行记录","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"trained_model","NodeId":"-1540"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-1540"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-1540","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":11,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-1547","ModuleId":"BigQuantSpace.dl_convert_to_bin.dl_convert_to_bin-v1","ModuleParameters":[{"Name":"window_size","Value":1,"ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-1547"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-1547"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-1547","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":12,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-2431","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 # 示例代码如下。在这里编写您的代码\n pred_label = input_1.read_pickle()\n df = input_2.read_df()\n df = pd.DataFrame({'pred_label':pred_label[:,0], 'instrument':df.instrument, 'date':df.date})\n df.sort_values(['date','pred_label'],inplace=True, ascending=[True,False])\n return Outputs(data_1=DataSource.write_df(df), data_2=None, data_3=None)\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":"-2431"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-2431"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_3","NodeId":"-2431"}],"OutputPortsInternal":[{"Name":"data_1","NodeId":"-2431","OutputType":null},{"Name":"data_2","NodeId":"-2431","OutputType":null},{"Name":"data_3","NodeId":"-2431","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":24,"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='287d2cb0-f53c-4101-bdf8-104b137c8601-8' Position='207,58,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-15' Position='70,183,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-24' Position='763,3,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-53' Position='249,375,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-62' Position='1074,127,200,200'/><NodePosition Node='-106' Position='381,188,200,200'/><NodePosition Node='-113' Position='385,280,200,200'/><NodePosition Node='-122' Position='1078,236,200,200'/><NodePosition Node='-129' Position='1081,327,200,200'/><NodePosition Node='-141' Position='823,1085,200,200'/><NodePosition Node='-160' Position='-283,55,200,200'/><NodePosition Node='-168' Position='-203,147,200,200'/><NodePosition Node='-196' Position='-203,311,200,200'/><NodePosition Node='-224' Position='-203,237,200,200'/><NodePosition Node='-231' Position='-201,395,200,200'/><NodePosition Node='-238' Position='-198,469,200,200'/><NodePosition Node='-682' Position='-244,549,200,200'/><NodePosition Node='-1098' Position='-50,765,200,200'/><NodePosition Node='-1535' Position='388,510,200,200'/><NodePosition Node='-1540' Position='249,844,200,200'/><NodePosition Node='-1547' Position='805,515,200,200'/><NodePosition Node='-2431' Position='533,940,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年1月30日 17:57
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    # 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_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
    
    # 回测引擎:初始化函数,只执行一次
    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.0013, min_cost=5))
        # 预测数据,通过options传入进来,使用 read_df 函数,加载到内存 (DataFrame)
        # 设置买入的股票数量,这里买入预测股票列表排名靠前的5只
        stock_count = 5
        # 每只的股票的权重,如下的权重分配会使得靠前的股票分配多一点的资金,[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.2
        context.options['hold_days'] = 5
    
    
    m1 = M.instruments.v2(
        start_date='2014-01-01',
        end_date='2017-12-31',
        market='CN_STOCK_A',
        instrument_list='',
        max_count=0
    )
    
    m2 = M.advanced_auto_labeler.v2(
        instruments=m1.data,
        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)
    where(label>0.5, NaN, label)
    where(label<-0.5, NaN, label)
    """,
        start_date='',
        end_date='',
        benchmark='000300.SHA',
        drop_na_label=True,
        cast_label_int=False
    )
    
    m3 = 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
    """
    )
    
    m15 = M.general_feature_extractor.v7(
        instruments=m1.data,
        features=m3.data,
        start_date='',
        end_date='',
        before_start_days=0
    )
    
    m16 = M.derived_feature_extractor.v3(
        input_data=m15.data,
        features=m3.data,
        date_col='date',
        instrument_col='instrument',
        drop_na=True,
        remove_extra_columns=False
    )
    
    m7 = M.join.v3(
        data1=m2.data,
        data2=m16.data,
        on='date,instrument',
        how='inner',
        sort=False
    )
    
    m10 = M.dl_convert_to_bin.v1(
        input_data=m7.data,
        features=m3.data,
        window_size=1
    )
    
    m9 = M.instruments.v2(
        start_date=T.live_run_param('trading_date', '2018-01-01'),
        end_date=T.live_run_param('trading_date', '2018-12-31'),
        market='CN_STOCK_A',
        instrument_list='',
        max_count=0
    )
    
    m17 = M.general_feature_extractor.v7(
        instruments=m9.data,
        features=m3.data,
        start_date='',
        end_date='',
        before_start_days=0
    )
    
    m18 = M.derived_feature_extractor.v3(
        input_data=m17.data,
        features=m3.data,
        date_col='date',
        instrument_col='instrument',
        drop_na=True,
        remove_extra_columns=False
    )
    
    m12 = M.dl_convert_to_bin.v1(
        input_data=m18.data,
        features=m3.data,
        window_size=1
    )
    
    m6 = M.dl_layer_input.v1(
        shape='59',
        batch_shape='',
        dtype='float32',
        sparse=False,
        name=''
    )
    
    m8 = M.dl_layer_dense.v1(
        inputs=m6.data,
        units=256,
        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=''
    )
    
    m21 = M.dl_layer_dropout.v1(
        inputs=m8.data,
        rate=0.9,
        noise_shape='',
        name=''
    )
    
    m20 = M.dl_layer_dense.v1(
        inputs=m21.data,
        units=128,
        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=''
    )
    
    m22 = M.dl_layer_dropout.v1(
        inputs=m20.data,
        rate=0.9,
        noise_shape='',
        name=''
    )
    
    m23 = M.dl_layer_dense.v1(
        inputs=m22.data,
        units=1,
        activation='linear',
        use_bias=True,
        kernel_initializer='glorot_uniform',
        bias_initializer='Zeros',
        kernel_regularizer='None',
        kernel_regularizer_l1=0,
        kernel_regularizer_l2=0,
        bias_regularizer='None',
        bias_regularizer_l1=0,
        bias_regularizer_l2=0,
        activity_regularizer='None',
        activity_regularizer_l1=0,
        activity_regularizer_l2=0,
        kernel_constraint='None',
        bias_constraint='None',
        name=''
    )
    
    m4 = M.dl_model_init.v1(
        inputs=m6.data,
        outputs=m23.data
    )
    
    m5 = M.dl_model_train.v1(
        input_model=m4.data,
        training_data=m10.data,
        optimizer='Adam',
        loss='mean_squared_error',
        metrics='mse',
        batch_size=10240,
        epochs=2,
        n_gpus=0,
        verbose='2:每个epoch输出一行记录'
    )
    
    m11 = M.dl_model_predict.v1(
        trained_model=m5.data,
        input_data=m12.data,
        batch_size=1024,
        n_gpus=0,
        verbose='2:每个epoch输出一行记录'
    )
    
    m24 = M.cached.v3(
        input_1=m11.data,
        input_2=m18.data,
        run=m24_run_bigquant_run,
        post_run=m24_post_run_bigquant_run,
        input_ports='',
        params='{}',
        output_ports=''
    )
    
    m19 = M.trade.v4(
        instruments=m9.data,
        options_data=m24.data_1,
        start_date='',
        end_date='',
        handle_data=m19_handle_data_bigquant_run,
        prepare=m19_prepare_bigquant_run,
        initialize=m19_initialize_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.SHA'
    )
    
    [2019-01-03 15:57:42.324053] INFO: bigquant: instruments.v2 开始运行..
    [2019-01-03 15:57:42.337648] INFO: bigquant: 命中缓存
    [2019-01-03 15:57:42.338972] INFO: bigquant: instruments.v2 运行完成[0.014959s].
    [2019-01-03 15:57:42.345243] INFO: bigquant: advanced_auto_labeler.v2 开始运行..
    [2019-01-03 15:57:42.351350] INFO: bigquant: 命中缓存
    [2019-01-03 15:57:42.353004] INFO: bigquant: advanced_auto_labeler.v2 运行完成[0.007779s].
    [2019-01-03 15:57:42.358356] INFO: bigquant: input_features.v1 开始运行..
    [2019-01-03 15:57:42.364833] INFO: bigquant: 命中缓存
    [2019-01-03 15:57:42.365887] INFO: bigquant: input_features.v1 运行完成[0.007548s].
    [2019-01-03 15:57:42.404545] INFO: bigquant: general_feature_extractor.v7 开始运行..
    [2019-01-03 15:57:42.408610] INFO: bigquant: 命中缓存
    [2019-01-03 15:57:42.409595] INFO: bigquant: general_feature_extractor.v7 运行完成[0.005065s].
    [2019-01-03 15:57:42.412852] INFO: bigquant: derived_feature_extractor.v3 开始运行..
    [2019-01-03 15:57:42.416921] INFO: bigquant: 命中缓存
    [2019-01-03 15:57:42.417741] INFO: bigquant: derived_feature_extractor.v3 运行完成[0.004887s].
    [2019-01-03 15:57:42.426825] INFO: bigquant: join.v3 开始运行..
    [2019-01-03 15:57:42.432147] INFO: bigquant: 命中缓存
    [2019-01-03 15:57:42.433227] INFO: bigquant: join.v3 运行完成[0.006371s].
    [2019-01-03 15:57:42.440369] INFO: bigquant: dl_convert_to_bin.v1 开始运行..
    [2019-01-03 15:57:42.444856] INFO: bigquant: 命中缓存
    [2019-01-03 15:57:42.445726] INFO: bigquant: dl_convert_to_bin.v1 运行完成[0.005369s].
    [2019-01-03 15:57:42.448055] INFO: bigquant: instruments.v2 开始运行..
    [2019-01-03 15:57:42.452644] INFO: bigquant: 命中缓存
    [2019-01-03 15:57:42.453692] INFO: bigquant: instruments.v2 运行完成[0.00563s].
    [2019-01-03 15:57:42.463259] INFO: bigquant: general_feature_extractor.v7 开始运行..
    [2019-01-03 15:57:42.467665] INFO: bigquant: 命中缓存
    [2019-01-03 15:57:42.468775] INFO: bigquant: general_feature_extractor.v7 运行完成[0.005594s].
    [2019-01-03 15:57:42.470919] INFO: bigquant: derived_feature_extractor.v3 开始运行..
    [2019-01-03 15:57:42.474454] INFO: bigquant: 命中缓存
    [2019-01-03 15:57:42.475621] INFO: bigquant: derived_feature_extractor.v3 运行完成[0.004681s].
    [2019-01-03 15:57:42.480083] INFO: bigquant: dl_convert_to_bin.v1 开始运行..
    [2019-01-03 15:57:42.483569] INFO: bigquant: 命中缓存
    [2019-01-03 15:57:42.484286] INFO: bigquant: dl_convert_to_bin.v1 运行完成[0.004211s].
    
    Using TensorFlow backend.
    
    [2019-01-03 15:57:44.471844] INFO: bigquant: cached.v3 开始运行..
    [2019-01-03 15:57:45.146342] INFO: bigquant: 命中缓存
    [2019-01-03 15:57:45.147476] INFO: bigquant: cached.v3 运行完成[0.675639s].
    [2019-01-03 15:57:45.156946] INFO: bigquant: dl_model_train.v1 开始运行..
    [2019-01-03 15:57:45.162414] INFO: bigquant: 命中缓存
    [2019-01-03 15:57:45.163508] INFO: bigquant: dl_model_train.v1 运行完成[0.006585s].
    [2019-01-03 15:57:45.166850] INFO: bigquant: dl_model_predict.v1 开始运行..
    [2019-01-03 15:57:45.170708] INFO: bigquant: 命中缓存
    DataSource(e2228dc40f2c11e998490a580a8102d4, v3)
    [2019-01-03 15:57:45.171619] INFO: bigquant: dl_model_predict.v1 运行完成[0.004777s].
    [2019-01-03 15:57:45.174690] INFO: bigquant: cached.v3 开始运行..
    [2019-01-03 15:57:45.177969] INFO: bigquant: 命中缓存
    [2019-01-03 15:57:45.178624] INFO: bigquant: cached.v3 运行完成[0.003926s].
    [2019-01-03 15:57:45.205879] INFO: bigquant: backtest.v8 开始运行..
    [2019-01-03 15:57:45.207919] INFO: bigquant: biglearning backtest:V8.1.6
    [2019-01-03 15:57:45.208751] INFO: bigquant: product_type:stock by specified
    [2019-01-03 15:57:52.815140] INFO: bigquant: 读取股票行情完成:1655305
    [2019-01-03 15:58:08.513778] INFO: algo: TradingAlgorithm V1.4.2
    [2019-01-03 15:58:19.347972] INFO: algo: trading transform...
    [2019-01-03 15:58:22.674326] INFO: Performance: Simulated 243 trading days out of 243.
    [2019-01-03 15:58:22.675790] INFO: Performance: first open: 2018-01-02 09:30:00+00:00
    [2019-01-03 15:58:22.676833] INFO: Performance: last close: 2018-12-28 15:00:00+00:00
    
    • 收益率-27.54%
    • 年化收益率-28.4%
    • 基准收益率-25.31%
    • 阿尔法-0.15
    • 贝塔0.6
    • 夏普比率-1.53
    • 胜率0.41
    • 盈亏比0.92
    • 收益波动率22.16%
    • 信息比率-0.01
    • 最大回撤37.14%
    [2019-01-03 15:58:24.648708] INFO: bigquant: backtest.v8 运行完成[39.44282s].
    

    【宽客学院】深度学习简介
    (tkyz) #2

    深度学习用什么指令查看预测的股票结果?


    (lpl22) #3

    在此代码中调用

    m24.data_1.read_df()
    

    %E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_20190224215557


    (tkyz) #4

    这个pred_label是越大越好,还是越小越好,代表什么意思


    (达达) #5

    这个pred_label就是你训练时定义的目标,这个策略中标注的目标是shift(close, -5) / shift(open, -1)-1也就是未来5日收益率,那么这个模型预测的数据自然也是未来5日收益率,当然是越大越好


    (tkyz) #6

    好的,谢谢


    (zans2009) #7

    你好,m20和m23之间好像缺了一个dropout层


    (达达) #8

    可以自己在左侧导航栏中添加dropout是缓解过拟合的,可以视情况添加


    (andrew) #9

    请问如何实现滚动回测


    (达达) #10

    参考滚动训练模块的学院教程,重新对应模块号码指定训练集和测试集的模块号