5条线连出一个价值选股策略

bigstudio
标签: #<Tag:0x00007f60a5a4a1c0>

(iQuant) #1

本文简单介绍如何通过可视化的方式开发出一个价值选股策略。

标题也许看着有点蒙,什么意思呢?其实就是通过5条线连接的几个模块就能开发出一个简单的价值选股策略。我们先来看看策略截图。

%E7%AD%96%E7%95%A5%E6%88%AA%E5%9B%BE

该策略一共包括:证券代码列表、输入特征列表、基础特征抽取、数据过滤、Trade(回测/模拟)共5个模块。首先我们先介绍下策略的思想。

  • 开始日期:2013-01-01
  • 结束日期:2017-11-07
  • 选股依据:根据市净率指标全市场选股,选出该指标最小的10%的股票;
  • 调仓规则:买入持有22天(接近自然日30天),进行调仓,卖出全部股票,然后重新买入股票;
  • 资金分配:等权重买入

接下来,详细介绍策略开发的各个流程。

  • 新建一个可视化空白策略模板

  • 拖入证券代码列表模块,并在右侧参数窗口输入开始日期和结束日期
    %E6%8B%96%E5%85%A5%E8%AF%81%E5%88%B8

  • 拖入输入特征列表模块,并在右侧代码编辑器窗口输入市净率排序因子
    %E7%89%B9%E5%BE%81%E5%80%BC%E5%88%97%E8%A1%A8

  • 拖入基础特征抽取模块,并将证券代码列表和输入特征列表两个模块的连线到该模块

    补充:在连线时,应该注意对应的节点,不同的节点对应不同的模块输入。此外,基础特征抽取模块的右侧参数窗口可以不填,不填就会继承输入模块的相关参数。这里,就会继承证券代码列表模块的开始日期和结束日期。
    %E5%9F%BA%E7%A1%80%E7%89%B9%E5%BE%81%E6%8A%BD%E5%8F%96

  • 拖入数据过滤模块,并设置过滤条件。因为我们是通过市净率选股,我们选择市净率最小的10%的股票 。

    补充:rank_pb_lf_0因子是横截面排序因子,其详细释义可以在文档-因子库查询,根据当日全市场股票市净率大小升序排序所得。通过数据过滤,我们将市盈率最小的10%的股票的过滤出来。

  • 拖入交易模块——Trade(回测/模拟)

  • 将策略思想添加在 主函数

  • 点击运行或运行全部,得到回测结果

可能有小伙伴比较纳闷,如果我关注中间某些模块的数据,怎样才能查看呢?BigQuant的策略研究平台为“可见即可得”的研究环境,中间模块的数据我们可以这样查询,以数据过滤模块为例:

  • 首先,选中该模块,右键运行该模块

    补充:运行该模块的话,就不会运行全部模块,这样方便调试策略。运行某模块的时候,系统会自动运行该模块的继承模块,即那些连线到该模块的模块也会运行。
    %E8%BF%90%E8%A1%8C

  • 然后,该模块是m4,直接在画布上方或者下方新建一个单元格(cell),就可查看m4模块的输出数据

是不是觉得开发策略没有想象中的那么困难啊?欢迎大家克隆进行深入研究,本文只是一个简单的demo,还有许多尝试的方向。

克隆策略

    {"Description":"实验创建于2017/11/27","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"-17:instruments","SourceOutputPortId":"-4:data"},{"DestinationInputPortId":"-30:instruments","SourceOutputPortId":"-4:data"},{"DestinationInputPortId":"-17:features","SourceOutputPortId":"-12:data"},{"DestinationInputPortId":"-24:input_data","SourceOutputPortId":"-17:data"},{"DestinationInputPortId":"-30:options_data","SourceOutputPortId":"-24:data"}],"ModuleNodes":[{"Id":"-4","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2013-01-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2017-11-27","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":"-4"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-4","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":1,"Comment":"","CommentCollapsed":true},{"Id":"-12","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"rank_pb_lf_0","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"-12"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-12","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":2,"Comment":"","CommentCollapsed":true},{"Id":"-17","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":"-17"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-17"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-17","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":3,"Comment":"","CommentCollapsed":true},{"Id":"-24","ModuleId":"BigQuantSpace.filter.filter-v3","ModuleParameters":[{"Name":"expr","Value":"rank_pb_lf_0<=0.1","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"output_left_data","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-24"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-24","OutputType":null},{"Name":"left_data","NodeId":"-24","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":4,"Comment":"","CommentCollapsed":true},{"Id":"-30","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\t\n\tif context.trading_day_index % 22 !=0:\n\t\treturn\n\t# 按日期过滤得到当日的数据\n\tcur_data = context.data[context.data.date==data.current_dt.strftime('%Y-%m-%d')]\n\tbuy_stock = list(cur_data['instrument']) # 选出市净率最小的10%股票列表\n\t\n\t# 持仓股票列表,为字符串\n\tequities = {e.symbol :p for e,p in context.portfolio.positions.items() if p.amount>0}\n\tfor instrument in equities:\n\t\tif data.can_trade(context.symbol(instrument)):\n\t\t\tcontext.order_target_percent(context.symbol(instrument),0)\n\t# 买入股票\n\tfor i in buy_stock:\n\t\tif data.can_trade(context.symbol(i)):\n\t\t\tcontext.order_target_percent(context.symbol(i), 1/len(buy_stock)) # 等权重买入\n\t\n\t\t\t\n\t ","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.data = context.options['data'].read_df()\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 = 30\n \n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"before_trading_start","Value":"# 回测引擎:每个单位时间开始前调用一次,即每日开盘前调用一次。\ndef bigquant_run(context, data):\n pass\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"volume_limit","Value":"0","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"order_price_field_buy","Value":"open","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"order_price_field_sell","Value":"open","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":"plot_charts","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"backtest_only","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-30"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"options_data","NodeId":"-30"}],"OutputPortsInternal":[{"Name":"raw_perf","NodeId":"-30","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":5,"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='-4' Position='84,110.44477844238281,200,200'/><NodePosition Node='-12' Position='385.97657775878906,117.28346252441406,200,200'/><NodePosition Node='-17' Position='254.0958251953125,224.250732421875,200,200'/><NodePosition Node='-24' Position='242.70773315429688,342.6867370605469,200,200'/><NodePosition Node='-30' Position='74.16421508789062,477.0660095214844,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 [17]:
    # 本代码由可视化策略环境自动生成 2017年11月28日 09:18
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    m1 = M.instruments.v2(
        start_date='2013-01-01',
        end_date='2017-11-27',
        market='CN_STOCK_A',
        instrument_list='',
        max_count=0
    )
    
    m2 = M.input_features.v1(
        features='rank_pb_lf_0'
    )
    
    m3 = M.general_feature_extractor.v6(
        instruments=m1.data,
        features=m2.data,
        start_date='',
        end_date='',
        before_start_days=0
    )
    
    m4 = M.filter.v3(
        input_data=m3.data,
        expr='rank_pb_lf_0<=0.1',
        output_left_data=False
    )
    
    # 回测引擎:每日数据处理函数,每天执行一次
    def m5_handle_data_bigquant_run(context, data):
    	
    	if context.trading_day_index % 22 !=0:
    		return
    	# 按日期过滤得到当日的数据
    	cur_data = context.data[context.data.date==data.current_dt.strftime('%Y-%m-%d')]
    	buy_stock = list(cur_data['instrument']) # 选出市净率最小的10%股票列表
    	
    	# 持仓股票列表,为字符串
    	equities = {e.symbol :p for e,p in context.portfolio.positions.items() if p.amount>0}
    	for instrument in equities:
    		if data.can_trade(context.symbol(instrument)):
    			context.order_target_percent(context.symbol(instrument),0)
    	# 买入股票
    	for i  in buy_stock:
    		if data.can_trade(context.symbol(i)):
    			context.order_target_percent(context.symbol(i), 1/len(buy_stock)) # 等权重买入
    	
    			
    	 
    # 回测引擎:准备数据,只执行一次
    def m5_prepare_bigquant_run(context):
        pass
    
    # 回测引擎:初始化函数,只执行一次
    def m5_initialize_bigquant_run(context):
        # 加载预测数据
        context.data = 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 = 30
         
    
    # 回测引擎:每个单位时间开始前调用一次,即每日开盘前调用一次。
    def m5_before_trading_start_bigquant_run(context, data):
        pass
    
    m5 = M.trade.v3(
        instruments=m1.data,
        options_data=m4.data,
        start_date='',
        end_date='',
        handle_data=m5_handle_data_bigquant_run,
        prepare=m5_prepare_bigquant_run,
        initialize=m5_initialize_bigquant_run,
        before_trading_start=m5_before_trading_start_bigquant_run,
        volume_limit=0,
        order_price_field_buy='open',
        order_price_field_sell='open',
        capital_base=1000000,
        benchmark='000300.SHA',
        auto_cancel_non_tradable_orders=True,
        data_frequency='daily',
        plot_charts=True,
        backtest_only=False
    )
    
    [2017-11-28 09:13:00.085405] INFO: bigquant: instruments.v2 开始运行..
    [2017-11-28 09:13:00.088732] INFO: bigquant: 命中缓存
    [2017-11-28 09:13:00.090226] INFO: bigquant: instruments.v2 运行完成[0.00485s].
    [2017-11-28 09:13:00.094569] INFO: bigquant: input_features.v1 开始运行..
    [2017-11-28 09:13:00.097016] INFO: bigquant: 命中缓存
    [2017-11-28 09:13:00.098065] INFO: bigquant: input_features.v1 运行完成[0.003494s].
    [2017-11-28 09:13:00.104386] INFO: bigquant: general_feature_extractor.v6 开始运行..
    [2017-11-28 09:13:00.106497] INFO: bigquant: 命中缓存
    [2017-11-28 09:13:00.107487] INFO: bigquant: general_feature_extractor.v6 运行完成[0.003161s].
    [2017-11-28 09:13:00.113516] INFO: bigquant: filter.v3 开始运行..
    [2017-11-28 09:13:00.115629] INFO: bigquant: 命中缓存
    [2017-11-28 09:13:00.116673] INFO: bigquant: filter.v3 运行完成[0.003135s].
    [2017-11-28 09:13:00.138185] INFO: bigquant: backtest.v7 开始运行..
    [2017-11-28 09:14:58.308032] INFO: Performance: Simulated 1191 trading days out of 1191.
    [2017-11-28 09:14:58.309222] INFO: Performance: first open: 2013-01-04 14:30:00+00:00
    [2017-11-28 09:14:58.310044] INFO: Performance: last close: 2017-11-27 20:00:00+00:00
    
    • 收益率77.38%
    • 年化收益率12.89%
    • 基准收益率62.67%
    • 阿尔法0.05
    • 贝塔0.53
    • 夏普比率0.56
    • 收益波动率15.2%
    • 信息比率0.15
    • 最大回撤21.68%
    [2017-11-28 09:15:07.277174] INFO: bigquant: backtest.v7 运行完成[127.138946s].
    


    本文由BigQuant宽客学院推出,版权归BigQuant所有,转 载请注明出处。


    (lilong) #5

    这个感觉挺方便的啊,赞一个