复制链接
克隆策略

    {"description":"实验创建于2017/8/26","graph":{"edges":[{"to_node_id":"-215:instruments","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-8:data"},{"to_node_id":"-215:features","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"to_node_id":"-222:features","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"to_node_id":"-222:input_data","from_node_id":"-215:data"}],"nodes":[{"node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-8","module_id":"BigQuantSpace.instruments.instruments-v2","parameters":[{"name":"start_date","value":"2020-01-01","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"2020-12-31","type":"Literal","bound_global_parameter":null},{"name":"market","value":"CN_STOCK_A","type":"Literal","bound_global_parameter":null},{"name":"instrument_list","value":"","type":"Literal","bound_global_parameter":null},{"name":"max_count","value":"0","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"rolling_conf","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-8"}],"output_ports":[{"name":"data","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-8"}],"cacheable":true,"seq_num":1,"comment":"","comment_collapsed":true},{"node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24","module_id":"BigQuantSpace.input_features.input_features-v1","parameters":[{"name":"features","value":"sharpe_ratio_20 = sharpe_ratio(close_0, 20)","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"features_ds","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24"}],"output_ports":[{"name":"data","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24"}],"cacheable":true,"seq_num":3,"comment":"","comment_collapsed":true},{"node_id":"-215","module_id":"BigQuantSpace.general_feature_extractor.general_feature_extractor-v7","parameters":[{"name":"start_date","value":"","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"","type":"Literal","bound_global_parameter":null},{"name":"before_start_days","value":90,"type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"instruments","node_id":"-215"},{"name":"features","node_id":"-215"}],"output_ports":[{"name":"data","node_id":"-215"}],"cacheable":true,"seq_num":15,"comment":"","comment_collapsed":true},{"node_id":"-222","module_id":"BigQuantSpace.derived_feature_extractor.derived_feature_extractor-v3","parameters":[{"name":"date_col","value":"date","type":"Literal","bound_global_parameter":null},{"name":"instrument_col","value":"instrument","type":"Literal","bound_global_parameter":null},{"name":"drop_na","value":"False","type":"Literal","bound_global_parameter":null},{"name":"remove_extra_columns","value":"False","type":"Literal","bound_global_parameter":null},{"name":"user_functions","value":"def sharpe_ratio(df, x, N):\n import empyrical\n _df = pd.DataFrame({\"instrument\": df[\"instrument\"], \"x\": x}, index=df.index)\n _g = _df.groupby(\"instrument\", as_index=True, group_keys=False)\n _r = _g[\"x\"].rolling(abs(N), 1)\n def _gen_sharpe_ratio(s):\n return empyrical.sharpe_ratio(s, risk_free=0)\n \n _s = _r.apply(_gen_sharpe_ratio).droplevel(\"instrument\")\n return _s.loc[df.index]\n\nbigquant_run = {\n \"sharpe_ratio\": sharpe_ratio\n}","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_data","node_id":"-222"},{"name":"features","node_id":"-222"}],"output_ports":[{"name":"data","node_id":"-222"}],"cacheable":true,"seq_num":16,"comment":"","comment_collapsed":true}],"node_layout":"<node_postions><node_position Node='287d2cb0-f53c-4101-bdf8-104b137c8601-8' Position='211,61,200,200'/><node_position Node='287d2cb0-f53c-4101-bdf8-104b137c8601-24' Position='570,61,200,200'/><node_position Node='-215' Position='381,188,200,200'/><node_position Node='-222' Position='382,278,200,200'/></node_postions>"},"nodes_readonly":false,"studio_version":"v2"}
    In [9]:
    # 本代码由可视化策略环境自动生成 2022年10月18日 13:36
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    def sharpe_ratio(df, x, N):
        import empyrical
        _df = pd.DataFrame({"instrument": df["instrument"], "x": x}, index=df.index)
        _g = _df.groupby("instrument", as_index=True, group_keys=False)
        _r = _g["x"].rolling(abs(N), 1)
        def _gen_sharpe_ratio(s):
            return empyrical.sharpe_ratio(s, risk_free=0)
        
        _s = _r.apply(_gen_sharpe_ratio).droplevel("instrument")
        return _s.loc[df.index]
    
    m16_user_functions_bigquant_run = {
        "sharpe_ratio": sharpe_ratio
    }
    
    m1 = M.instruments.v2(
        start_date='2020-01-01',
        end_date='2020-12-31',
        market='CN_STOCK_A',
        instrument_list='',
        max_count=0
    )
    
    m3 = M.input_features.v1(
        features='sharpe_ratio_20 = sharpe_ratio(close_0, 20)'
    )
    
    m15 = M.general_feature_extractor.v7(
        instruments=m1.data,
        features=m3.data,
        start_date='',
        end_date='',
        before_start_days=90
    )
    
    m16 = M.derived_feature_extractor.v3(
        input_data=m15.data,
        features=m3.data,
        date_col='date',
        instrument_col='instrument',
        drop_na=False,
        remove_extra_columns=False,
        user_functions=m16_user_functions_bigquant_run
    )
    
    In [11]:
    df = m16.data.read()
    df
    
    Out[11]:
    close_0 date instrument sharpe_ratio_20
    0 1768.544678 2019-10-08 000001.SZA NaN
    1 1774.003174 2019-10-09 000001.SZA 7284.973796
    2 1772.911499 2019-10-10 000001.SZA 9737.940210
    3 1835.138062 2019-10-11 000001.SZA 893.885483
    4 1879.897583 2019-10-14 000001.SZA 578.315237
    ... ... ... ... ...
    1171685 75.099998 2020-12-25 689009.SHA 305.500072
    1171686 80.099998 2020-12-28 689009.SHA 268.219263
    1171687 77.459999 2020-12-29 689009.SHA 256.202298
    1171688 80.910004 2020-12-30 689009.SHA 233.522814
    1171689 85.820000 2020-12-31 689009.SHA 202.192840

    1171690 rows × 4 columns

    In [ ]: