【模板案例】获取最大化收益及所在天数

策略分享
标签: #<Tag:0x00007fc574a52580>

(iQuant) #1

7月30日Meetup 模板案例:

获取最大化收益及所在天数

克隆策略

    {"Description":"实验创建于2017/8/26","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"-346:instruments","SourceOutputPortId":"-338:data"}],"ModuleNodes":[{"Id":"-338","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2010-01-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2010-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":"-338"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-338","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":5,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-346","ModuleId":"BigQuantSpace.advanced_auto_labeler.advanced_auto_labeler-v2","ModuleParameters":[{"Name":"label_expr","Value":"# #号开始的表示注释\n# 0. 每行一个,顺序执行,从第二个开始,可以使用label字段\n# 1. 可用数据字段见 https://bigquant.com/docs/develop/datasource/deprecated/history_data.html\n# 添加benchmark_前缀,可使用对应的benchmark数据\n# 2. 可用操作符和函数见 `表达式引擎 <https://bigquant.com/docs/develop/bigexpr/usage.html>`_\n\n# 未来5日最大相对收益率\n# shift(cal_max_ret(close, open,benchmark_close,benchmark_open, 5), -1)\n# 未来5日最大相对收益率所在天数\nshift(cal_max_ret_date(close, open,benchmark_close,benchmark_open, 5), -1) + 1","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":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"cast_label_int","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_functions","Value":"def cal_max_ret(df, close, open,benchmark_close,benchmark_open, N):\n ret = df.groupby('instrument', as_index=False, sort=False, group_keys=False).apply(lambda x: (pd.concat([x['close'].shift(-i) / x['open'] - x['benchmark_close'].shift(-i) / x['benchmark_open'] for i in range(N)], axis=1)).max(axis=1))\n \n return ret\n\ndef cal_max_ret_date(df, close, open,benchmark_close,benchmark_open, N):\n ret = df.groupby('instrument', as_index=False, sort=False, group_keys=False).apply(lambda x: pd.concat([x['close'].shift(-i) / x['open'] - x['benchmark_close'].shift(-i) / x['benchmark_open'] for i in range(N)], axis=1))\n return np.nanargmax(np.array(ret), axis=1)\nbigquant_run={'cal_max_ret':cal_max_ret, 'cal_max_ret_date': cal_max_ret_date}","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-346"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-346","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":6,"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='-338' Position='469,193,200,200'/><NodePosition Node='-346' Position='495.491455078125,329,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]:
    # 本代码由可视化策略环境自动生成 2020年7月30日 14:05
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    def cal_max_ret(df, close, open,benchmark_close,benchmark_open, N):
        ret = df.groupby('instrument', as_index=False, sort=False, group_keys=False).apply(lambda x: (pd.concat([x['close'].shift(-i) / x['open'] - x['benchmark_close'].shift(-i) / x['benchmark_open'] for i in range(N)], axis=1)).max(axis=1))
        
        return ret
    
    def cal_max_ret_date(df, close, open,benchmark_close,benchmark_open, N):
        ret = df.groupby('instrument', as_index=False, sort=False, group_keys=False).apply(lambda x: pd.concat([x['close'].shift(-i) / x['open'] - x['benchmark_close'].shift(-i) / x['benchmark_open'] for i in range(N)], axis=1))
        return np.nanargmax(np.array(ret), axis=1)
    m6_user_functions_bigquant_run={'cal_max_ret':cal_max_ret, 'cal_max_ret_date': cal_max_ret_date}
    
    m5 = M.instruments.v2(
        start_date='2010-01-01',
        end_date='2010-12-31',
        market='CN_STOCK_A',
        instrument_list='',
        max_count=0,
        m_cached=False
    )
    
    m6 = M.advanced_auto_labeler.v2(
        instruments=m5.data,
        label_expr="""# #号开始的表示注释
    # 0. 每行一个,顺序执行,从第二个开始,可以使用label字段
    # 1. 可用数据字段见 https://bigquant.com/docs/develop/datasource/deprecated/history_data.html
    #   添加benchmark_前缀,可使用对应的benchmark数据
    # 2. 可用操作符和函数见 `表达式引擎 <https://bigquant.com/docs/develop/bigexpr/usage.html>`_
    
    # 未来5日最大相对收益率
    # shift(cal_max_ret(close, open,benchmark_close,benchmark_open, 5), -1)
    # 未来5日最大相对收益率所在天数
    shift(cal_max_ret_date(close, open,benchmark_close,benchmark_open, 5), -1) + 1""",
        start_date='',
        end_date='',
        benchmark='000300.SHA',
        drop_na_label=False,
        cast_label_int=False,
        user_functions=m6_user_functions_bigquant_run,
        m_cached=False
    )
    
    In [172]:
    test = d.head(6)
    
    Out[172]:
    date instrument m:amount m:close m:open m:benchmark_instrument m:benchmark_close m:benchmark_open label
    0 2010-01-04 000001.SZA 5.802495e+08 851.320190 880.403625 000300.SHA 3535.229004 3592.468018 1.0
    1 2010-01-04 000002.SZA 1.034345e+09 1174.523560 1202.224609 000300.SHA 3535.229004 3592.468018 4.0
    2 2010-01-04 000005.SZA 1.334784e+08 55.512924 55.698277 000300.SHA 3535.229004 3592.468018 1.0
    3 2010-01-04 000006.SZA 7.054856e+07 122.581490 124.896423 000300.SHA 3535.229004 3592.468018 4.0
    4 2010-01-04 000007.SZA 1.810142e+07 38.261860 39.145252 000300.SHA 3535.229004 3592.468018 2.0
    5 2010-01-04 000009.SZA 1.894014e+08 41.917858 42.536572 000300.SHA 3535.229004 3592.468018 4.0

    BigQuant AI量化专家Meetup(7月30日场回放及案例模板)