投资组合优化器

组合优化
优化器
小市值策略
标签: #<Tag:0x00007f615e5a4418> #<Tag:0x00007f615e5a4288> #<Tag:0x00007f615e5a4008>

(iQuant) #1

概述

投资组合优化是指应用概率论与数理统计、最优化方法以及线性代数等相关数学理论方法,根据既定目标收益和风险容许程度(例如最大化收益,最小化风险等),重新调整组合权重的过程,它体现了投资者的意愿和投资者所受到的约束。
投资组合管理者在设定了投资收益预期、风险预算、相关约束和风险模型之后, 依托优化器得到资产配置最优化结果。

由于不同的约束条件、目标函数,会形成不同的优化器。我们可以通过使用组合优化器,进行一段时间的回测,测试整个投资过程,不同的组合优化的方式会带来哪些细微的变化,找到更加符合自身需求的仓位分配方案。

组合优化器支持对股票进行投资优化,目前支持的目标函数如下:

  • 最大化收益
  • 最小化风险
  • 最大化夏普比率
  • 风险平价

对使用优化器的投资组合管理者来说,只需根据收益预期、风险预算,选择恰当的优化模型,并设定相关的约束限制条件。优化器模块可以基于选定的目标函数,输出优化后的投资权重调整建议。我们会对投资组合优化器的进行持续创新与改进。

模块使用

组合优化器的模块名称为:经典组合优化器。在画布左侧可直接搜索出来:

简单介绍下模块的参数、返回、输入。

参数

symbols

含义:标的代码
类型:list
示例:[‘000002.SZA’,‘000333.SZA’]
状态:可选

 注:未来会单独介绍该参数,目前可以忽略。

date

含义:日期
类型:str 或list
示例:‘2018-01-01’ 或[‘2018-01-01’,‘2018-01-02’,‘2018-01-03’]
状态:可选

 注:未来会单独介绍该参数,目前可以忽略。

target

含义:目标函数
类型:str
示例:‘max_sharpe’
状态:必选

weight_sum

含义:权重之和
类型:float
示例:1,表明全部组合权重之和为1
状态:必选

upper_weight

含义:单个标的权重上限
类型:float
示例:0.3,表明组合中单只标的权重低于30%
状态:必选

lower_weight

含义:单个标的权重下限
类型:float
示例:0.05,表明组合中单只标的权重高于5%
状态:必选

return_equal_weight_if_fail

含义:如果优化失败,返回等权重
类型:bool
示例:True

before_start_days

含义:向前取数据天数
类型:int
示例:100,即通过过去100天的股票价格数据确定如何优化
状态:必选

输入
该模块具有一个输入项,目前为可选。输入项的格式是Pandas的Series格式,索引是字符串的日期,值是标的列表,形如:

注:这里之所以设定这样形式的输入,是为了便于回测时可以方便计算出优化后的权重。输入对应的是策略每日买入的股票列表,因此我们需要对买入的股票进行权重分配。

返回
含义:组合优化后的权重数据
类型:dict,其中key为日期(类型为str),value为标的代码和标的权重(类型为dict)

举个栗子

我们以小市值策略为例介绍组合优化器的应用,策略的详细内容可以参考:浅析小市值策略

我们尝试了多种目标函数的组合优化,以等权重组合为对比基准,看到了优化后有一定的改进效果!

优化目标:最大化夏普比率

优化目标:最大化收益

优化目标:风险平价

策略源代码如下,欢迎克隆研究及提出宝贵意见。

克隆策略

    {"Description":"实验创建于2019/4/9","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"-798:features","SourceOutputPortId":"-785:data"},{"DestinationInputPortId":"-798:instruments","SourceOutputPortId":"-789:data"},{"DestinationInputPortId":"-207:instruments","SourceOutputPortId":"-789:data"},{"DestinationInputPortId":"-231:instruments","SourceOutputPortId":"-789:data"},{"DestinationInputPortId":"-808:input_1","SourceOutputPortId":"-798:data"},{"DestinationInputPortId":"-231:options_data","SourceOutputPortId":"-808:data_1"},{"DestinationInputPortId":"-100:input_1","SourceOutputPortId":"-808:data_1"},{"DestinationInputPortId":"-255:input_2","SourceOutputPortId":"-207:raw_perf"},{"DestinationInputPortId":"-255:input_1","SourceOutputPortId":"-231:raw_perf"},{"DestinationInputPortId":"-207:options_data","SourceOutputPortId":"-100:data_1"}],"ModuleNodes":[{"Id":"-785","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"market_cap_0\n","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"-785"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-785","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":2,"Comment":"","CommentCollapsed":true},{"Id":"-789","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2016-01-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2017-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":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"rolling_conf","NodeId":"-789"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-789","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":3,"Comment":"","CommentCollapsed":true},{"Id":"-798","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":"-798"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-798"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-798","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":4,"Comment":"","CommentCollapsed":true},{"Id":"-808","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 df = input_1.read_df()\n tmp = m4.data.read_df().groupby('date').apply(lambda x:list(x.sort_values('market_cap_0').instrument[:20])) \n data_1 = DataSource.write_pickle(tmp)\n return Outputs(data_1=data_1)\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":"-808"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-808"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_3","NodeId":"-808"}],"OutputPortsInternal":[{"Name":"data_1","NodeId":"-808","OutputType":null},{"Name":"data_2","NodeId":"-808","OutputType":null},{"Name":"data_3","NodeId":"-808","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":5,"Comment":"","CommentCollapsed":true},{"Id":"-207","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 if context.trading_day_index % context.hold_days !=0:\n return \n # 当前的日期\n date = data.current_dt.strftime('%Y-%m-%d')\n \n # 根据日期获取调仓需要买入的股票的列表\n cur_data = context.weight_data[date]\n stock_to_buy = list(cur_data.keys())\n \n # 通过positions对象,使用列表生成式的方法获取目前持仓的股票列表\n stock_hold_now = [equity.symbol for equity in context.portfolio.positions]\n # 继续持有的股票:调仓时,如果买入的股票已经存在于目前的持仓里,那么应继续持有\n no_need_to_sell = [i for i in stock_hold_now if i in stock_to_buy]\n # 需要卖出的股票\n stock_to_sell = [i for i in stock_hold_now if i not in no_need_to_sell]\n \n # 卖出\n for stock in stock_to_sell:\n # 如果该股票停牌,则没法成交。因此需要用can_trade方法检查下该股票的状态\n # 如果返回真值,则可以正常下单,否则会出错\n # 因为stock是字符串格式,我们用symbol方法将其转化成平台可以接受的形式:Equity格式\n\n if data.can_trade(context.symbol(stock)):\n # order_target_percent是平台的一个下单接口,表明下单使得该股票的权重为0,\n # 即卖出全部股票,可参考回测文档\n \n context.order_target_percent(context.symbol(stock), 0)\n \n # 如果当天没有买入的股票,就返回\n if len(stock_to_buy) == 0:\n return\n\n \n # 买入\n for stock in stock_to_buy:\n if data.can_trade(context.symbol(stock)):\n # 下单使得某只股票的持仓权重达到weight,因为\n # weight大于0,因此是等权重买入\n weight = cur_data[stock]\n context.order_target_percent(context.symbol(stock), weight)","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 print('>>>>>>>: portfolio optimizer')\n # 加载预测数据\n context.weight_data = context.options['data'].read_pickle()\n \n\n # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数\n context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))\n context.hold_days = 22\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.025,"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":"auto_cancel_non_tradable_orders","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"data_frequency","Value":"daily","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"price_type","Value":"后复权","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"product_type","Value":"股票","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"plot_charts","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"backtest_only","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"benchmark","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-207"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"options_data","NodeId":"-207"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"history_ds","NodeId":"-207"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"benchmark_ds","NodeId":"-207"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"trading_calendar","NodeId":"-207"}],"OutputPortsInternal":[{"Name":"raw_perf","NodeId":"-207","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":1,"Comment":"优化权重组合","CommentCollapsed":false},{"Id":"-231","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 if context.trading_day_index % context.hold_days !=0:\n return \n # 当前的日期\n date = data.current_dt.strftime('%Y-%m-%d')\n \n # 根据日期获取调仓需要买入的股票的列表\n stock_to_buy = list(context.buy_list.ix[date])\n \n # 通过positions对象,使用列表生成式的方法获取目前持仓的股票列表\n stock_hold_now = [equity.symbol for equity in context.portfolio.positions]\n # 继续持有的股票:调仓时,如果买入的股票已经存在于目前的持仓里,那么应继续持有\n no_need_to_sell = [i for i in stock_hold_now if i in stock_to_buy]\n # 需要卖出的股票\n stock_to_sell = [i for i in stock_hold_now if i not in no_need_to_sell]\n \n # 卖出\n for stock in stock_to_sell:\n # 如果该股票停牌,则没法成交。因此需要用can_trade方法检查下该股票的状态\n # 如果返回真值,则可以正常下单,否则会出错\n # 因为stock是字符串格式,我们用symbol方法将其转化成平台可以接受的形式:Equity格式\n\n if data.can_trade(context.symbol(stock)):\n # order_target_percent是平台的一个下单接口,表明下单使得该股票的权重为0,\n # 即卖出全部股票,可参考回测文档\n context.order_target_percent(context.symbol(stock), 0)\n \n # 如果当天没有买入的股票,就返回\n if len(stock_to_buy) == 0:\n return\n\n # 等权重买入 \n weight = 1 / len(stock_to_buy)\n \n # 买入\n for stock in stock_to_buy:\n if data.can_trade(context.symbol(stock)):\n # 下单使得某只股票的持仓权重达到weight,因为\n # weight大于0,因此是等权重买入\n context.order_target_percent(context.symbol(stock), weight)","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 print('>>>>>>>: equal weight')\n # 加载预测数据\n context.buy_list = context.options['data'].read_pickle()\n \n # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数\n context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))\n context.hold_days = 22\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.025,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"order_price_field_buy","Value":"open","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"order_price_field_sell","Value":"close","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"capital_base","Value":1000000,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"auto_cancel_non_tradable_orders","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"data_frequency","Value":"daily","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"price_type","Value":"后复权","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"product_type","Value":"股票","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"plot_charts","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"backtest_only","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"benchmark","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-231"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"options_data","NodeId":"-231"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"history_ds","NodeId":"-231"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"benchmark_ds","NodeId":"-231"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"trading_calendar","NodeId":"-231"}],"OutputPortsInternal":[{"Name":"raw_perf","NodeId":"-231","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":7,"Comment":"等权重组合","CommentCollapsed":false},{"Id":"-255","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 nv1 = m1.raw_perf.read_df()['algorithm_period_return']\n nv2 =m7.raw_perf.read_df()['algorithm_period_return']\n\n df = pd.DataFrame({'optimizer weight':nv1,'equal weight':nv2})\n T.plot(df , title='对比图')\n data_1 = DataSource.write_df(df)\n return Outputs(data_1=data_1)\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":"-255"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-255"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_3","NodeId":"-255"}],"OutputPortsInternal":[{"Name":"data_1","NodeId":"-255","OutputType":null},{"Name":"data_2","NodeId":"-255","OutputType":null},{"Name":"data_3","NodeId":"-255","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":8,"Comment":"策略曲线对比","CommentCollapsed":false},{"Id":"-100","ModuleId":"BigQuantSpace.classical_portfolio_optimizer.classical_portfolio_optimizer-v4","ModuleParameters":[{"Name":"symbols","Value":"%7B%22enumItems%22%3A%5B%7B%22value%22%3A%22000002.SZA%22%2C%22displayValue%22%3A%22000002.SZA%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22000333.SZA%22%2C%22displayValue%22%3A%22000333.SZA%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22000009.SZA%22%2C%22displayValue%22%3A%22000009.SZA%22%2C%22selected%22%3Afalse%7D%5D%7D","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"date","Value":" ","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"weight_sum","Value":1,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"upper_weight","Value":"0.3","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"lower_weight","Value":0.01,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"before_start_days","Value":"100","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"target","Value":"最大化夏普比率","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"return_equal_weight_if_fail","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-100"}],"OutputPortsInternal":[{"Name":"data_1","NodeId":"-100","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":9,"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='-785' Position='398,19,200,200'/><NodePosition Node='-789' Position='45,11,200,200'/><NodePosition Node='-798' Position='215,121,200,200'/><NodePosition Node='-808' Position='265,218,200,200'/><NodePosition Node='-207' Position='507,395,200,200'/><NodePosition Node='-231' Position='62,392,200,200'/><NodePosition Node='-255' Position='342.78802490234375,510,200,200'/><NodePosition Node='-100' Position='493,302,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 [8]:
    # 本代码由可视化策略环境自动生成 2019年4月11日 18:17
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
    def m5_run_bigquant_run(input_1, input_2, input_3):
        # 示例代码如下。在这里编写您的代码
        df = input_1.read_df()
        tmp = m4.data.read_df().groupby('date').apply(lambda x:list(x.sort_values('market_cap_0').instrument[:20])) 
        data_1 = DataSource.write_pickle(tmp)
        return Outputs(data_1=data_1)
    
    # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
    def m5_post_run_bigquant_run(outputs):
        return outputs
    
    # 回测引擎:每日数据处理函数,每天执行一次
    def m1_handle_data_bigquant_run(context, data):
        if context.trading_day_index % context.hold_days !=0:
            return 
        # 当前的日期
        date = data.current_dt.strftime('%Y-%m-%d')
        
        # 根据日期获取调仓需要买入的股票的列表
        cur_data =  context.weight_data[date]
        stock_to_buy = list(cur_data.keys())
      
        # 通过positions对象,使用列表生成式的方法获取目前持仓的股票列表
        stock_hold_now = [equity.symbol for equity in context.portfolio.positions]
        # 继续持有的股票:调仓时,如果买入的股票已经存在于目前的持仓里,那么应继续持有
        no_need_to_sell = [i for i in stock_hold_now if i in stock_to_buy]
        # 需要卖出的股票
        stock_to_sell = [i for i in stock_hold_now if i not in no_need_to_sell]
      
        # 卖出
        for stock in stock_to_sell:
            # 如果该股票停牌,则没法成交。因此需要用can_trade方法检查下该股票的状态
            # 如果返回真值,则可以正常下单,否则会出错
            # 因为stock是字符串格式,我们用symbol方法将其转化成平台可以接受的形式:Equity格式
    
            if data.can_trade(context.symbol(stock)):
                # order_target_percent是平台的一个下单接口,表明下单使得该股票的权重为0,
                #   即卖出全部股票,可参考回测文档
                
                context.order_target_percent(context.symbol(stock), 0)
        
        # 如果当天没有买入的股票,就返回
        if len(stock_to_buy) == 0:
            return
    
        
        # 买入
        for stock in stock_to_buy:
            if data.can_trade(context.symbol(stock)):
                # 下单使得某只股票的持仓权重达到weight,因为
                # weight大于0,因此是等权重买入
                weight = cur_data[stock]
                context.order_target_percent(context.symbol(stock), weight)
    # 回测引擎:准备数据,只执行一次
    def m1_prepare_bigquant_run(context):
        pass
    
    # 回测引擎:初始化函数,只执行一次
    def m1_initialize_bigquant_run(context):
        print('>>>>>>>: portfolio optimizer')
        # 加载预测数据
        context.weight_data = context.options['data'].read_pickle()
        
    
        # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数
        context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))
        context.hold_days = 22
        
    # 回测引擎:每个单位时间开始前调用一次,即每日开盘前调用一次。
    def m1_before_trading_start_bigquant_run(context, data):
        pass
    
    # 回测引擎:每日数据处理函数,每天执行一次
    def m7_handle_data_bigquant_run(context, data):
        if context.trading_day_index % context.hold_days !=0:
            return 
        # 当前的日期
        date = data.current_dt.strftime('%Y-%m-%d')
        
        # 根据日期获取调仓需要买入的股票的列表
        stock_to_buy = list(context.buy_list.ix[date])
        
        # 通过positions对象,使用列表生成式的方法获取目前持仓的股票列表
        stock_hold_now = [equity.symbol for equity in context.portfolio.positions]
        # 继续持有的股票:调仓时,如果买入的股票已经存在于目前的持仓里,那么应继续持有
        no_need_to_sell = [i for i in stock_hold_now if i in stock_to_buy]
        # 需要卖出的股票
        stock_to_sell = [i for i in stock_hold_now if i not in no_need_to_sell]
      
        # 卖出
        for stock in stock_to_sell:
            # 如果该股票停牌,则没法成交。因此需要用can_trade方法检查下该股票的状态
            # 如果返回真值,则可以正常下单,否则会出错
            # 因为stock是字符串格式,我们用symbol方法将其转化成平台可以接受的形式:Equity格式
    
            if data.can_trade(context.symbol(stock)):
                # order_target_percent是平台的一个下单接口,表明下单使得该股票的权重为0,
                #   即卖出全部股票,可参考回测文档
                context.order_target_percent(context.symbol(stock), 0)
        
        # 如果当天没有买入的股票,就返回
        if len(stock_to_buy) == 0:
            return
    
        # 等权重买入 
        weight =  1 / len(stock_to_buy)
        
        # 买入
        for stock in stock_to_buy:
            if data.can_trade(context.symbol(stock)):
                # 下单使得某只股票的持仓权重达到weight,因为
                # weight大于0,因此是等权重买入
                context.order_target_percent(context.symbol(stock), weight)
    # 回测引擎:准备数据,只执行一次
    def m7_prepare_bigquant_run(context):
        pass
    
    # 回测引擎:初始化函数,只执行一次
    def m7_initialize_bigquant_run(context):
        print('>>>>>>>: equal weight')
        # 加载预测数据
        context.buy_list = context.options['data'].read_pickle()
       
        # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数
        context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))
        context.hold_days = 22
        
    # 回测引擎:每个单位时间开始前调用一次,即每日开盘前调用一次。
    def m7_before_trading_start_bigquant_run(context, data):
        pass
    
    # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
    def m8_run_bigquant_run(input_1, input_2, input_3):
        
        nv1 = m1.raw_perf.read_df()['algorithm_period_return']
        nv2 =m7.raw_perf.read_df()['algorithm_period_return']
    
        df = pd.DataFrame({'optimizer weight':nv1,'equal weight':nv2})
        T.plot(df , title='对比图')
        data_1 = DataSource.write_df(df)
        return Outputs(data_1=data_1)
    
    # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
    def m8_post_run_bigquant_run(outputs):
        return outputs
    
    
    m2 = M.input_features.v1(
        features="""market_cap_0
    """
    )
    
    m3 = M.instruments.v2(
        start_date='2016-01-01',
        end_date='2017-01-01',
        market='CN_STOCK_A',
        instrument_list=''
    )
    
    m4 = M.general_feature_extractor.v7(
        instruments=m3.data,
        features=m2.data,
        start_date='',
        end_date='',
        before_start_days=0
    )
    
    m5 = M.cached.v3(
        input_1=m4.data,
        run=m5_run_bigquant_run,
        post_run=m5_post_run_bigquant_run,
        input_ports='',
        params='{}',
        output_ports=''
    )
    
    m9 = M.classical_portfolio_optimizer.v4(
        input_1=m5.data_1,
        symbols=[],
        date=' ',
        weight_sum=1,
        upper_weight=0.3,
        lower_weight=0.01,
        before_start_days=100,
        target='最大化夏普比率',
        return_equal_weight_if_fail=True
    )
    
    m1 = M.trade.v4(
        instruments=m3.data,
        options_data=m9.data_1,
        start_date='',
        end_date='',
        handle_data=m1_handle_data_bigquant_run,
        prepare=m1_prepare_bigquant_run,
        initialize=m1_initialize_bigquant_run,
        before_trading_start=m1_before_trading_start_bigquant_run,
        volume_limit=0.025,
        order_price_field_buy='open',
        order_price_field_sell='open',
        capital_base=1000000,
        auto_cancel_non_tradable_orders=True,
        data_frequency='daily',
        price_type='后复权',
        product_type='股票',
        plot_charts=True,
        backtest_only=False,
        benchmark=''
    )
    
    m7 = M.trade.v4(
        instruments=m3.data,
        options_data=m5.data_1,
        start_date='',
        end_date='',
        handle_data=m7_handle_data_bigquant_run,
        prepare=m7_prepare_bigquant_run,
        initialize=m7_initialize_bigquant_run,
        before_trading_start=m7_before_trading_start_bigquant_run,
        volume_limit=0.025,
        order_price_field_buy='open',
        order_price_field_sell='close',
        capital_base=1000000,
        auto_cancel_non_tradable_orders=True,
        data_frequency='daily',
        price_type='后复权',
        product_type='股票',
        plot_charts=True,
        backtest_only=False,
        benchmark=''
    )
    
    m8 = M.cached.v3(
        input_1=m7.raw_perf,
        input_2=m1.raw_perf,
        run=m8_run_bigquant_run,
        post_run=m8_post_run_bigquant_run,
        input_ports='',
        params='{}',
        output_ports='',
        m_cached=False
    )
    
    • 收益率82.19%
    • 年化收益率85.81%
    • 基准收益率-11.28%
    • 阿尔法0.71
    • 贝塔0.69
    • 夏普比率2.37
    • 胜率0.79
    • 盈亏比2.38
    • 收益波动率26.44%
    • 信息比率0.21
    • 最大回撤15.53%
    • 收益率65.45%
    • 年化收益率68.21%
    • 基准收益率-11.28%
    • 阿尔法0.65
    • 贝塔0.94
    • 夏普比率1.82
    • 胜率0.89
    • 盈亏比2.23
    • 收益波动率29.31%
    • 信息比率0.2
    • 最大回撤17.36%

    (华尔街的猫) #4

    建议优化的股票数量不要超过200只,否则性能会降低,运行时间会加长。


    (focus666) #5

    这个有没有用AI选股的策略的例子?


    (iQuant) #6

    您好, 学院策略开发板块有许多例子,您可以作为参考。