整百下单的一个例子

策略分享
整百下单
手数下单
标签: #<Tag:0x00007f614e8bc200> #<Tag:0x00007f614e8bc048> #<Tag:0x00007f614e893e40>

(iQuant) #1

在默认的AI策略里,交易股数并不是整百,这和实际交易确实有一些不同。之所以这样做,是因为回测主要是验证思想,不想让资金管理、风险控制影响最初的策略思想。

但是,用户是可以手动修改代码,达到整百下单的目的的。

具体方法是修改handle_data函数里交易接口API,同时修改回测类型为:真实价格回测。相关文档可以参考:交易引擎-order。具体要修改的位置为回测模块trade中的主函数,截图如下:
image

整百下单逻辑部分修改后的代码为:

current_price = data.current(context.symbol(instrument), 'price')
amount = math.floor(cash / current_price / 100) * 100
context.order(context.symbol(instrument), amount)

策略如下,欢迎克隆!

克隆策略

    {"Description":"实验创建于2017/8/26","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-15:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-8:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-29: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":"287d2cb0-f53c-4101-bdf8-104b137c8601-29:features","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-35:features","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-76:features","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-43:features","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-70:features","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-35:input_data","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-29:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-53:data2","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-35:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-60:model","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-43:model"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-84:input_data","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-53:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-81:options_data","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-60:predictions"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-70:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-81:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-76:input_data","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-70:data"},{"DestinationInputPortId":"-86:input_data","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-76:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-43:training_ds","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-84:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-60:data","SourceOutputPortId":"-86:data"}],"ModuleNodes":[{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-8","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2013-01-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2015-01-01","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)\n\n# 极值处理:用1%和99%分位的值做clip\nclip(label, all_quantile(label, 0.01), all_quantile(label, 0.99))\n\n# 将分数映射到分类,这里使用20个分类\nall_wbins(label, 20)\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":"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# 多个特征,每行一个,可以包含基础特征和衍生特征\n \n(close_0 + close_1 + close_2 + close_3 + close_4) / 5 / close_0\nmax(high_0, high_1, high_2) / min(low_0, low_1, low_2)\nmean(close_0,20)/std(close_0,20)\ndelta(open_0/shift(close_0,1), 10)\n \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-29","ModuleId":"BigQuantSpace.general_feature_extractor.general_feature_extractor-v6","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":"287d2cb0-f53c-4101-bdf8-104b137c8601-29"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-29"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-29","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":4,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-35","ModuleId":"BigQuantSpace.derived_feature_extractor.derived_feature_extractor-v2","ModuleParameters":[{"Name":"date_col","Value":"date","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_col","Value":"instrument","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_functions","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-35"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-35"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-35","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":5,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-43","ModuleId":"BigQuantSpace.stock_ranker_train.stock_ranker_train-v5","ModuleParameters":[{"Name":"learning_algorithm","Value":"排序","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"number_of_leaves","Value":30,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"minimum_docs_per_leaf","Value":1000,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"number_of_trees","Value":20,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"learning_rate","Value":0.1,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_bins","Value":1023,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"feature_fraction","Value":1,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"m_lazy_run","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"training_ds","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-43"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-43"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"test_ds","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-43"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"base_model","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-43"}],"OutputPortsInternal":[{"Name":"model","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-43","OutputType":null},{"Name":"feature_gains","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-43","OutputType":null},{"Name":"m_lazy_run","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-43","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":6,"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-60","ModuleId":"BigQuantSpace.stock_ranker_predict.stock_ranker_predict-v5","ModuleParameters":[{"Name":"m_lazy_run","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"model","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-60"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-60"}],"OutputPortsInternal":[{"Name":"predictions","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-60","OutputType":null},{"Name":"m_lazy_run","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-60","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":8,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-62","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2016-01-01","ValueType":"Literal","LinkedGlobalParameter":"交易日期"},{"Name":"end_date","Value":"2017-01-01","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":"287d2cb0-f53c-4101-bdf8-104b137c8601-70","ModuleId":"BigQuantSpace.general_feature_extractor.general_feature_extractor-v6","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":"287d2cb0-f53c-4101-bdf8-104b137c8601-70"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-70"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-70","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":10,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-76","ModuleId":"BigQuantSpace.derived_feature_extractor.derived_feature_extractor-v2","ModuleParameters":[{"Name":"date_col","Value":"date","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_col","Value":"instrument","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_functions","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-76"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-76"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-76","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":11,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-81","ModuleId":"BigQuantSpace.trade.trade-v3","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天之后才开始卖出;对持仓的股票,按StockRanker预测的排序末位淘汰\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. 生成买入订单:按StockRanker预测的排序,买入前面的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 current_price = data.current(context.symbol(instrument), 'price')\n amount = math.floor(cash / current_price / 100) * 100\n context.order(context.symbol(instrument), amount)\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":"benchmark","Value":"000300.SHA","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":"plot_charts","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"backtest_only","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"amount_integer","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-81"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"options_data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-81"}],"OutputPortsInternal":[{"Name":"raw_perf","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-81","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":12,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-84","ModuleId":"BigQuantSpace.dropnan.dropnan-v1","ModuleParameters":[],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-84"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-84","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":13,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-86","ModuleId":"BigQuantSpace.dropnan.dropnan-v1","ModuleParameters":[],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-86"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-86","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":14,"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='211,64,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-15' Position='70,183,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-24' Position='765,21,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-29' Position='381,188,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-35' Position='385,280,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-43' Position='638,561,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-53' Position='249,375,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-60' Position='906,647,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-62' Position='1074,127,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-70' Position='1078,236,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-76' Position='1081,327,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-81' Position='1037,751,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-84' Position='376,467,200,200'/><NodePosition Node='-86' Position='1078,418,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":false}
    In [1]:
    # 本代码由可视化策略环境自动生成 2018年3月6日 23:37
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    m1 = M.instruments.v2(
        start_date='2013-01-01',
        end_date='2015-01-01',
        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%和99%分位的值做clip
    clip(label, all_quantile(label, 0.01), all_quantile(label, 0.99))
    
    # 将分数映射到分类,这里使用20个分类
    all_wbins(label, 20)
    
    # 过滤掉一字涨停的情况 (设置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
    )
    
    m3 = M.input_features.v1(
        features="""# #号开始的表示注释
    # 多个特征,每行一个,可以包含基础特征和衍生特征
     
    (close_0 + close_1 + close_2 + close_3 + close_4) / 5 / close_0
    max(high_0, high_1, high_2) / min(low_0, low_1, low_2)
    mean(close_0,20)/std(close_0,20)
    delta(open_0/shift(close_0,1), 10)
     
    """
    )
    
    m4 = M.general_feature_extractor.v6(
        instruments=m1.data,
        features=m3.data,
        start_date='',
        end_date='',
        before_start_days=0
    )
    
    m5 = M.derived_feature_extractor.v2(
        input_data=m4.data,
        features=m3.data,
        date_col='date',
        instrument_col='instrument'
    )
    
    m7 = M.join.v3(
        data1=m2.data,
        data2=m5.data,
        on='date,instrument',
        how='inner',
        sort=False
    )
    
    m13 = M.dropnan.v1(
        input_data=m7.data
    )
    
    m6 = M.stock_ranker_train.v5(
        training_ds=m13.data,
        features=m3.data,
        learning_algorithm='排序',
        number_of_leaves=30,
        minimum_docs_per_leaf=1000,
        number_of_trees=20,
        learning_rate=0.1,
        max_bins=1023,
        feature_fraction=1,
        m_lazy_run=False
    )
    
    m9 = M.instruments.v2(
        start_date=T.live_run_param('trading_date', '2016-01-01'),
        end_date=T.live_run_param('trading_date', '2017-01-01'),
        market='CN_STOCK_A',
        instrument_list='',
        max_count=0
    )
    
    m10 = M.general_feature_extractor.v6(
        instruments=m9.data,
        features=m3.data,
        start_date='',
        end_date='',
        before_start_days=0
    )
    
    m11 = M.derived_feature_extractor.v2(
        input_data=m10.data,
        features=m3.data,
        date_col='date',
        instrument_col='instrument'
    )
    
    m14 = M.dropnan.v1(
        input_data=m11.data
    )
    
    m8 = M.stock_ranker_predict.v5(
        model=m6.model,
        data=m14.data,
        m_lazy_run=False
    )
    
    # 回测引擎:每日数据处理函数,每天执行一次
    def m12_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天之后才开始卖出;对持仓的股票,按StockRanker预测的排序末位淘汰
        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. 生成买入订单:按StockRanker预测的排序,买入前面的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:
                current_price = data.current(context.symbol(instrument), 'price')
                amount = math.floor(cash / current_price / 100) * 100
                context.order(context.symbol(instrument), amount)
    
    # 回测引擎:准备数据,只执行一次
    def m12_prepare_bigquant_run(context):
        pass
    
    # 回测引擎:初始化函数,只执行一次
    def m12_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
    
    m12 = M.trade.v3(
        instruments=m9.data,
        options_data=m8.predictions,
        start_date='',
        end_date='',
        handle_data=m12_handle_data_bigquant_run,
        prepare=m12_prepare_bigquant_run,
        initialize=m12_initialize_bigquant_run,
        volume_limit=0.025,
        order_price_field_buy='open',
        order_price_field_sell='close',
        capital_base=1000000,
        benchmark='000300.SHA',
        auto_cancel_non_tradable_orders=True,
        data_frequency='daily',
        price_type='真实价格',
        plot_charts=True,
        backtest_only=False,
        amount_integer=False
    )
    
    [2018-03-06 23:32:21.009798] INFO: bigquant: instruments.v2 开始运行..
    [2018-03-06 23:32:21.059874] INFO: bigquant: 命中缓存
    [2018-03-06 23:32:21.061659] INFO: bigquant: instruments.v2 运行完成[0.05193s].
    [2018-03-06 23:32:21.133849] INFO: bigquant: advanced_auto_labeler.v2 开始运行..
    [2018-03-06 23:32:31.431049] INFO: 自动数据标注: 加载历史数据: 1134116 行
    [2018-03-06 23:32:31.432886] INFO: 自动数据标注: 开始标注 ..
    [2018-03-06 23:32:34.807667] INFO: bigquant: advanced_auto_labeler.v2 运行完成[13.673809s].
    [2018-03-06 23:32:34.818730] INFO: bigquant: input_features.v1 开始运行..
    [2018-03-06 23:32:34.822297] INFO: bigquant: 命中缓存
    [2018-03-06 23:32:34.823418] INFO: bigquant: input_features.v1 运行完成[0.004729s].
    [2018-03-06 23:32:34.863559] INFO: bigquant: general_feature_extractor.v6 开始运行..
    [2018-03-06 23:32:44.110117] INFO: 基础特征抽取: 年份 2013, 特征行数=564168
    [2018-03-06 23:32:48.848734] INFO: 基础特征抽取: 年份 2014, 特征行数=569948
    [2018-03-06 23:32:51.767313] INFO: 基础特征抽取: 年份 2015, 特征行数=0
    [2018-03-06 23:32:51.788840] INFO: 基础特征抽取: 总行数: 1134116
    [2018-03-06 23:32:51.792546] INFO: bigquant: general_feature_extractor.v6 运行完成[16.928994s].
    [2018-03-06 23:32:51.807498] INFO: bigquant: derived_feature_extractor.v2 开始运行..
    [2018-03-06 23:32:52.473524] INFO: derived_feature_extractor: 提取完成 (close_0 + close_1 + close_2 + close_3 + close_4) / 5 / close_0, 0.007s
    [2018-03-06 23:32:52.991548] INFO: derived_feature_extractor: 提取完成 delta(open_0/shift(close_0,1), 10), 0.516s
    [2018-03-06 23:32:54.108434] INFO: derived_feature_extractor: 提取完成 max(high_0, high_1, high_2) / min(low_0, low_1, low_2), 1.114s
    [2018-03-06 23:32:58.606535] INFO: derived_feature_extractor: 提取完成 mean(close_0,20)/std(close_0,20), 4.496s
    [2018-03-06 23:33:01.595124] INFO: derived_feature_extractor: /y_2013, 564168
    [2018-03-06 23:33:04.783719] INFO: derived_feature_extractor: /y_2014, 569948
    [2018-03-06 23:33:08.029000] INFO: bigquant: derived_feature_extractor.v2 运行完成[16.221534s].
    [2018-03-06 23:33:08.043935] INFO: bigquant: join.v3 开始运行..
    [2018-03-06 23:33:14.293991] INFO: join: /y_2013, 行数=563132/564168, 耗时=5.971352s
    [2018-03-06 23:33:20.560785] INFO: join: /y_2014, 行数=555191/569948, 耗时=6.24566s
    [2018-03-06 23:33:20.631417] INFO: join: 最终行数: 1118323
    [2018-03-06 23:33:20.633637] INFO: bigquant: join.v3 运行完成[12.589748s].
    [2018-03-06 23:33:20.645793] INFO: bigquant: dropnan.v1 开始运行..
    [2018-03-06 23:33:21.307183] INFO: dropnan: /y_2013, 516392/563132
    [2018-03-06 23:33:21.969142] INFO: dropnan: /y_2014, 553580/555191
    [2018-03-06 23:33:21.987720] INFO: dropnan: 行数: 1069972/1118323
    [2018-03-06 23:33:22.009235] INFO: bigquant: dropnan.v1 运行完成[1.36342s].
    [2018-03-06 23:33:22.025206] INFO: bigquant: stock_ranker_train.v5 开始运行..
    [2018-03-06 23:33:26.763636] INFO: df2bin: prepare bins ..
    [2018-03-06 23:33:27.187699] INFO: df2bin: prepare data: training ..
    [2018-03-06 23:33:29.451400] INFO: df2bin: sort ..
    [2018-03-06 23:33:43.341762] INFO: stock_ranker_train: b3f9bbd8 准备训练: 1069972 行数
    [2018-03-06 23:35:19.812446] INFO: bigquant: stock_ranker_train.v5 运行完成[117.787203s].
    [2018-03-06 23:35:19.819536] INFO: bigquant: instruments.v2 开始运行..
    [2018-03-06 23:35:19.823210] INFO: bigquant: 命中缓存
    [2018-03-06 23:35:19.824366] INFO: bigquant: instruments.v2 运行完成[0.004822s].
    [2018-03-06 23:35:19.838457] INFO: bigquant: general_feature_extractor.v6 开始运行..
    [2018-03-06 23:35:30.653397] INFO: 基础特征抽取: 年份 2016, 特征行数=641546
    [2018-03-06 23:35:48.014913] INFO: 基础特征抽取: 年份 2017, 特征行数=0
    [2018-03-06 23:35:48.029892] INFO: 基础特征抽取: 总行数: 641546
    [2018-03-06 23:35:48.032721] INFO: bigquant: general_feature_extractor.v6 运行完成[28.194291s].
    [2018-03-06 23:35:48.042088] INFO: bigquant: derived_feature_extractor.v2 开始运行..
    [2018-03-06 23:35:49.019778] INFO: derived_feature_extractor: 提取完成 (close_0 + close_1 + close_2 + close_3 + close_4) / 5 / close_0, 0.004s
    [2018-03-06 23:35:49.393092] INFO: derived_feature_extractor: 提取完成 delta(open_0/shift(close_0,1), 10), 0.371s
    [2018-03-06 23:35:50.297697] INFO: derived_feature_extractor: 提取完成 max(high_0, high_1, high_2) / min(low_0, low_1, low_2), 0.903s
    [2018-03-06 23:35:54.321216] INFO: derived_feature_extractor: 提取完成 mean(close_0,20)/std(close_0,20), 4.022s
    [2018-03-06 23:35:54.499514] INFO: derived_feature_extractor: /y_2016, 641546
    [2018-03-06 23:35:56.066001] INFO: bigquant: derived_feature_extractor.v2 运行完成[8.023896s].
    [2018-03-06 23:35:56.074043] INFO: bigquant: dropnan.v1 开始运行..
    [2018-03-06 23:35:57.120520] INFO: dropnan: /y_2016, 584306/641546
    [2018-03-06 23:35:57.132559] INFO: dropnan: 行数: 584306/641546
    [2018-03-06 23:35:57.159776] INFO: bigquant: dropnan.v1 运行完成[1.085689s].
    [2018-03-06 23:35:57.177088] INFO: bigquant: stock_ranker_predict.v5 开始运行..
    [2018-03-06 23:35:58.394028] INFO: df2bin: prepare data: prediction ..
    [2018-03-06 23:36:04.150418] INFO: stock_ranker_predict: 准备预测: 584306 行
    [2018-03-06 23:36:08.335199] INFO: bigquant: stock_ranker_predict.v5 运行完成[11.158029s].
    [2018-03-06 23:36:08.416559] INFO: bigquant: backtest.v7 开始运行..
    [2018-03-06 23:36:08.558531] INFO: algo: set price type:original
    [2018-03-06 23:36:37.563783] INFO: algo: get splits [2016-03-17 00:00:00+00:00] [asset:Equity(2197 [000975.SZA]), ratio:0.9825327485010633]
    [2018-03-06 23:36:37.565622] INFO: Position: position handle split[sid:2197, orig_amount:3300, new_amount:3358.0, orig_cost:11.263378115688415,new_cost:11.07, ratio:0.9825327485010633, last_sale_price:11.249999970337175]
    [2018-03-06 23:36:37.566732] INFO: Position: after split: asset: Equity(2197 [000975.SZA]), amount: 3358.0, cost_basis: 11.07, last_sale_price: 11.45
    [2018-03-06 23:36:37.568069] INFO: Position: returning cash: 7.5
    [2018-03-06 23:36:38.310196] INFO: algo: get splits [2016-04-20 00:00:00+00:00] [asset:Equity(484 [002730.SZA]), ratio:0.4535570498740011]
    [2018-03-06 23:36:38.607404] INFO: algo: get splits [2016-04-29 00:00:00+00:00] [asset:Equity(2972 [300127.SZA]), ratio:0.987421399301558]
    [2018-03-06 23:36:38.608943] INFO: Position: position handle split[sid:2972, orig_amount:2600, new_amount:2633.0, orig_cost:15.564668550180837,new_cost:15.37, ratio:0.987421399301558, last_sale_price:15.699999872223401]
    [2018-03-06 23:36:38.610335] INFO: Position: after split: asset: Equity(2972 [300127.SZA]), amount: 2633.0, cost_basis: 15.37, last_sale_price: 15.899999618530273
    [2018-03-06 23:36:38.611397] INFO: Position: returning cash: 1.9
    [2018-03-06 23:36:39.665967] INFO: algo: get splits [2016-06-03 00:00:00+00:00] [asset:Equity(915 [601933.SHA]), ratio:0.4913694269823426]
    [2018-03-06 23:36:39.667388] INFO: Position: position handle split[sid:915, orig_amount:5600, new_amount:11396.0, orig_cost:8.672601133517642,new_cost:4.26, ratio:0.4913694269823426, last_sale_price:4.27000011428974]
    [2018-03-06 23:36:39.668973] INFO: Position: after split: asset: Equity(915 [601933.SHA]), amount: 11396.0, cost_basis: 4.26, last_sale_price: 8.6899995803833
    [2018-03-06 23:36:39.670536] INFO: Position: returning cash: 3.08
    [2018-03-06 23:36:39.944592] INFO: algo: get splits [2016-06-16 00:00:00+00:00] [asset:Equity(2675 [300397.SZA]), ratio:0.5]
    [2018-03-06 23:36:39.946466] INFO: Position: position handle split[sid:2675, orig_amount:800, new_amount:1600.0, orig_cost:56.326894773135685,new_cost:28.16, ratio:0.5, last_sale_price:30.90999984741211]
    [2018-03-06 23:36:39.947937] INFO: Position: after split: asset: Equity(2675 [300397.SZA]), amount: 1600.0, cost_basis: 28.16, last_sale_price: 61.81999969482422
    [2018-03-06 23:36:39.949570] INFO: Position: returning cash: 0.0
    [2018-03-06 23:36:40.100390] INFO: algo: get splits [2016-06-22 00:00:00+00:00] [asset:Equity(1170 [002357.SZA]), ratio:0.987678227401202]
    [2018-03-06 23:36:40.184172] INFO: algo: get splits [2016-06-24 00:00:00+00:00] [asset:Equity(53 [300477.SZA]), ratio:0.39733563797825194]
    [2018-03-06 23:36:40.185872] INFO: Position: position handle split[sid:53, orig_amount:800, new_amount:2013.0, orig_cost:60.608177152947725,new_cost:24.08, ratio:0.39733563797825194, last_sale_price:23.860005060594027]
    [2018-03-06 23:36:40.187063] INFO: Position: after split: asset: Equity(53 [300477.SZA]), amount: 2013.0, cost_basis: 24.08, last_sale_price: 60.05
    [2018-03-06 23:36:40.188742] INFO: Position: returning cash: 9.81
    [2018-03-06 23:36:41.415356] INFO: algo: get splits [2016-08-30 00:00:00+00:00] [asset:Equity(2059 [300185.SZA]), ratio:0.3302047674826683]
    [2018-03-06 23:36:41.416738] INFO: algo: get splits [2016-08-30 00:00:00+00:00] [asset:Equity(794 [000652.SZA]), ratio:0.9982174661353876]
    [2018-03-06 23:36:41.418017] INFO: Position: position handle split[sid:2059, orig_amount:2900, new_amount:8782.0, orig_cost:11.793537001368218,new_cost:3.89, ratio:0.3302047674826683, last_sale_price:3.8699998748968727]
    [2018-03-06 23:36:41.419145] INFO: Position: after split: asset: Equity(2059 [300185.SZA]), amount: 8782.0, cost_basis: 3.89, last_sale_price: 11.72
    [2018-03-06 23:36:41.420307] INFO: Position: returning cash: 1.66
    [2018-03-06 23:36:41.421747] INFO: Position: position handle split[sid:794, orig_amount:6800, new_amount:6812.0, orig_cost:5.601679911038989,new_cost:5.59, ratio:0.9982174661353876, last_sale_price:5.600000118295935]
    [2018-03-06 23:36:41.423008] INFO: Position: after split: asset: Equity(794 [000652.SZA]), amount: 6812.0, cost_basis: 5.59, last_sale_price: 5.610000133514404
    [2018-03-06 23:36:41.424285] INFO: Position: returning cash: 0.8
    [2018-03-06 23:36:41.478565] INFO: algo: get splits [2016-09-01 00:00:00+00:00] [asset:Equity(2802 [002113.SZA]), ratio:0.25004305549971184]
    [2018-03-06 23:36:41.833776] INFO: algo: get splits [2016-09-27 00:00:00+00:00] [asset:Equity(2693 [300292.SZA]), ratio:0.2501295971253566]
    [2018-03-06 23:36:41.835228] INFO: Position: position handle split[sid:2693, orig_amount:800, new_amount:3198.0, orig_cost:39.46183501448412,new_cost:9.87, ratio:0.2501295971253566, last_sale_price:9.649999857096256]
    [2018-03-06 23:36:41.836360] INFO: Position: after split: asset: Equity(2693 [300292.SZA]), amount: 3198.0, cost_basis: 9.87, last_sale_price: 38.58
    [2018-03-06 23:36:41.837569] INFO: Position: returning cash: 3.3
    [2018-03-06 23:36:41.861966] INFO: algo: get splits [2016-09-28 00:00:00+00:00] [asset:Equity(2027 [002802.SZA]), ratio:0.997721155008083]
    [2018-03-06 23:36:43.309158] INFO: Performance: Simulated 244 trading days out of 244.
    [2018-03-06 23:36:43.310551] INFO: Performance: first open: 2016-01-04 01:30:00+00:00
    [2018-03-06 23:36:43.311559] INFO: Performance: last close: 2016-12-30 07:00:00+00:00
    
    • 收益率-1.94%
    • 年化收益率-2.0%
    • 基准收益率-11.28%
    • 阿尔法0.06
    • 贝塔0.74
    • 夏普比率-0.16
    • 胜率0.554
    • 盈亏比0.887
    • 收益波动率33.69%
    • 信息比率0.32
    • 最大回撤29.39%
    [2018-03-06 23:36:45.749971] INFO: bigquant: backtest.v7 运行完成[37.333414s].
    

    交易提醒让我买36股,一手都不到,这怎么买?
    回测时成交的股票数量应该设为100股的整数倍才合理
    编制的策略如何股数取整呢?
    关于order_lots函数的疑问
    同一策略为什么在回测和交易中运行不一致
    大神帮忙看下代码,如何修改为整百股数
    关于回测/模拟交易中股票数量
    想问如下几个问题,求朋友们帮忙解答
    (1899) #2

    image
    你好,我想在本地写一个这样的函数练练手,奈何python捉急,请问能够给看一下这个函数的源码么,我拿来练练python。。


    (1899) #3

    image
    或者这个函数,我想拿来练一下。


    (user341) #4

    我设置了 整百 回测的时候确实是整百 但是 模拟交易的时候就又变成了不是整百 这个是什么原因造成的呢?


    (iQuant) #5

    您好,收到您的问题,已分配给策略工程师,策略工程师会尽快为您解答。


    (达达) #6

    模拟交易使用真实价格


    (user341) #7

    那如何解决模拟交易不是整数的问题呢? price_type=‘后复权’, 要把这个改成“真实价格”吗?


    (iQuant) #8

    对,模拟交易显示的是真实价格下的数量,如果你有后复权价格会折算数量以保证下单总资金相等。如果你模拟交易设置真实价格,则不会涉及价格变化带来的数量调整,显示的计划下单数量就是策略里面设置的整百下单数量。


    (shenhaiyangthu) #9

    current_price = data.current(context.symbol(instrument), ‘price’)
    这个price是开盘价还是收盘价啊,复权处理是怎么做的呢?
    我打印了一些买入开盘价 感觉跟后复权的也有些差异
    交易详情中的后复权买入开盘价 current_price
    2018-12-28 300588.SZA 17.23 17.2297821
    2018-12-27 300553.SZA 33.62 33.28588486
    2018-12-27 300650.SZA 23.7 23.52791405
    2018-12-26 300640.SZA 18.33 17.87082291
    2018-12-26 300650.SZA 23.58 23.54502487


    (华尔街的猫) #10

    这个接口获取的价格是最新价,其实就是收盘价。


    (shenhaiyangthu) #11

    data.current(context.symbol(instrument), [‘open’])[-1] 那如果用这个呢?
    跟交易详情里的数据也对不上


    (iQuant) #12

    您好,这个是当日的开盘价


    (shenhaiyangthu) #13

    对啊 我在trade模块里设置的也是开盘买 但两边的数据对不上


    (shenhaiyangthu) #14

    我的意思是 如果在回测模块里设置了 使用后复权价格 那我为了股票数量取整调用了 data.current里面的价格 这里面的价格 的复权方式呢? 我打印了一些数据 发现对不上


    (达达) #15

    设置后复权就是后复权价格


    (shenhaiyangthu) #16

    还有一个问题 回测机制是 今天下单 明天执行 所以我在确定下单数量时 实际上要获取的是明天的开盘价和收盘价 这个可以实现么 只是在回测里面用~


    (达达) #17

    不需要 这个直接按照数量下单,不需要纠结价格


    (shenhaiyangthu) #18

    我的数量就是由资金量除以价格得到的啊 所以能够精确的获取当时下单的价格 数量才会更准确啊 不然金额就会对不上


    (shenhaiyangthu) #19

    或者你们在order_value那里直接做个取整就好了 不然跟真实情况总归不一致啊


    (iQuant) #20

    [整百下单一个例子]整百下单的一个例子