复制链接
克隆策略

    {"description":"实验创建于2017/8/26","graph":{"edges":[{"to_node_id":"-3231:features","from_node_id":"-331:data"},{"to_node_id":"-3231:instruments","from_node_id":"-312:data"},{"to_node_id":"-3345:factors_info","from_node_id":"-3231:data"}],"nodes":[{"node_id":"-331","module_id":"BigQuantSpace.input_features.input_features-v1","parameters":[{"name":"features","value":"_amt = amount\n_amt_buy = initiative_bid_amount.fillna(0)\n_amt_sell = initiative_ask_amount.fillna(0)\n\n# 连续竞价成交占比\n# cont_amt_ratio = _amt.loc[93100:145600].sum() / (_amt.sum())\n\n# 特大单成交占比(大于均值+3倍标准差)\n_amt_great = beyond_13_bid_amount.add(beyond_13_ask_amount).fillna(0)\namt_great_ratio = _amt_great.sum() / (_amt.sum()*2)\n\n# 大单成交占比(大于均值+2倍标准差)\n_amt_big = beyond_11_bid_amount.add(beyond_11_ask_amount).fillna(0)\namt_big_ratio = _amt_great.sub(_amt_big).sum() / (_amt.sum()*2)\n\n# 中单成交占比(大于均值+0.5倍标准差)\n_amt_median = beyond_8_bid_amount.add(beyond_8_ask_amount).fillna(0)\namt_median_ratio = _amt_big.sub(_amt_median).sum() / (_amt.sum()*2)\n\n# 小单成交占比(大于均值-2倍标准差)\n_amt_small = beyond_3_bid_amount.add(beyond_3_ask_amount).fillna(0)\namt_small_ratio = _amt_median.sub(_amt_small).sum() / (_amt.sum()*2)\n\n# 净买入比率\nnet_buy_ratio = (_amt_buy.sum() - _amt_sell.sum()) / (_amt.sum())\n\n# 早盘净买入比率\nearly_net_buy_ratio = (_amt_buy.iloc[:30].sum() - _amt_sell.iloc[:30].sum()) / (_amt.sum())\n\n# 尾盘净买入比率\nlate_net_buy_ratio = (_amt_buy.iloc[-30:].sum() - _amt_sell.iloc[-30:].sum()) / (_amt.sum())\n","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"features_ds","node_id":"-331"}],"output_ports":[{"name":"data","node_id":"-331"}],"cacheable":true,"seq_num":1,"comment":"","comment_collapsed":true},{"node_id":"-312","module_id":"BigQuantSpace.instruments.instruments-v2","parameters":[{"name":"start_date","value":"2018-01-01","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"2018-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":"-312"}],"output_ports":[{"name":"data","node_id":"-312"}],"cacheable":true,"seq_num":2,"comment":"","comment_collapsed":true},{"node_id":"-3231","module_id":"BigQuantSpace.feature_extractor_1m.feature_extractor_1m-v2","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":"0","type":"Literal","bound_global_parameter":null},{"name":"workers","value":"10","type":"Literal","bound_global_parameter":null},{"name":"parallel_mode","value":"集群","type":"Literal","bound_global_parameter":null},{"name":"table_1m","value":"level2_bar1m_CN_STOCK_A","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"instruments","node_id":"-3231"},{"name":"features","node_id":"-3231"},{"name":"user_functions","node_id":"-3231"}],"output_ports":[{"name":"data","node_id":"-3231"}],"cacheable":true,"seq_num":3,"comment":"","comment_collapsed":true},{"node_id":"-3345","module_id":"BigQuantSpace.factorlens_preservation.factorlens_preservation-v2","parameters":[{"name":"factor_fields","value":"{\n 'amt_great_ratio':{'name':'deal_ratio_3x', 'desc':'大于均值+3倍标准差的成交金额占当日总成交金额比例'},\n 'amt_big_ratio':{'name':'deal_ratio_2x', 'desc':'大于均值+2倍标准差的成交金额占当日总成交金额比例'},\n 'amt_median_ratio':{'name':'deal_ratio_05x', 'desc':'大于均值+0.5倍标准差的成交金额占当日总成交金额比例'},\n 'amt_small_ratio':{'name':'deal_ratio_n2x', 'desc':'大于均值-2倍标准差的成交金额占当日总成交金额比例'},\n 'net_buy_ratio':{'name':'net_buy_ratio', 'desc':'净主动买入金额占当日总成交金额比例'},\n 'early_net_buy_ratio':{'name':'early_net_buy_ratio_30m', 'desc':'早盘30分钟净主动买入金额占当日总成交金额比例'},\n 'late_net_buy_ratio':{'name':'late_net_buy_ratio_30m', 'desc':'尾盘30分钟净主动买入金额占当日总成交金额比例'}\n}\n","type":"Literal","bound_global_parameter":null},{"name":"table","value":"hf_factors_cn_stock_a","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"factors_info","node_id":"-3345"}],"output_ports":[{"name":"data","node_id":"-3345"}],"cacheable":false,"seq_num":4,"comment":"","comment_collapsed":true}],"node_layout":"<node_postions><node_position Node='-331' Position='-7.0756072998046875,-46,200,200'/><node_position Node='-312' Position='-338,-40,200,200'/><node_position Node='-3231' Position='5,59,200,200'/><node_position Node='-3345' Position='40,203,200,200'/></node_postions>"},"nodes_readonly":false,"studio_version":"v2"}
    In [15]:
    # 本代码由可视化策略环境自动生成 2021年8月24日 15:09
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    m1 = M.input_features.v1(
        features="""_amt = amount
    _amt_buy = initiative_bid_amount.fillna(0)
    _amt_sell = initiative_ask_amount.fillna(0)
    
    # 连续竞价成交占比
    # cont_amt_ratio = _amt.loc[93100:145600].sum() / (_amt.sum())
    
    # 特大单成交占比(大于均值+3倍标准差)
    _amt_great = beyond_13_bid_amount.add(beyond_13_ask_amount).fillna(0)
    amt_great_ratio = _amt_great.sum() / (_amt.sum()*2)
    
    # 大单成交占比(大于均值+2倍标准差)
    _amt_big = beyond_11_bid_amount.add(beyond_11_ask_amount).fillna(0)
    amt_big_ratio = _amt_great.sub(_amt_big).sum() / (_amt.sum()*2)
    
    # 中单成交占比(大于均值+0.5倍标准差)
    _amt_median = beyond_8_bid_amount.add(beyond_8_ask_amount).fillna(0)
    amt_median_ratio = _amt_big.sub(_amt_median).sum() / (_amt.sum()*2)
    
    # 小单成交占比(大于均值-2倍标准差)
    _amt_small = beyond_3_bid_amount.add(beyond_3_ask_amount).fillna(0)
    amt_small_ratio = _amt_median.sub(_amt_small).sum() / (_amt.sum()*2)
    
    # 净买入比率
    net_buy_ratio = (_amt_buy.sum() - _amt_sell.sum()) / (_amt.sum())
    
    # 早盘净买入比率
    early_net_buy_ratio = (_amt_buy.iloc[:30].sum() - _amt_sell.iloc[:30].sum()) / (_amt.sum())
    
    # 尾盘净买入比率
    late_net_buy_ratio = (_amt_buy.iloc[-30:].sum() - _amt_sell.iloc[-30:].sum()) / (_amt.sum())
    """
    )
    
    m2 = M.instruments.v2(
        start_date='2018-01-01',
        end_date='2018-12-31',
        market='CN_STOCK_A',
        instrument_list='',
        max_count=0
    )
    
    m3 = M.feature_extractor_1m.v2(
        instruments=m2.data,
        features=m1.data,
        start_date='',
        end_date='',
        before_start_days=0,
        workers=10,
        parallel_mode='集群',
        table_1m='level2_bar1m_CN_STOCK_A'
    )
    
    m4 = M.factorlens_preservation.v2(
        factors_info=m3.data,
        factor_fields={
        'amt_great_ratio':{'name':'deal_ratio_3x', 'desc':'大于均值+3倍标准差的成交金额占当日总成交金额比例'},
        'amt_big_ratio':{'name':'deal_ratio_2x', 'desc':'大于均值+2倍标准差的成交金额占当日总成交金额比例'},
        'amt_median_ratio':{'name':'deal_ratio_05x', 'desc':'大于均值+0.5倍标准差的成交金额占当日总成交金额比例'},
        'amt_small_ratio':{'name':'deal_ratio_n2x', 'desc':'大于均值-2倍标准差的成交金额占当日总成交金额比例'},
        'net_buy_ratio':{'name':'net_buy_ratio', 'desc':'净主动买入金额占当日总成交金额比例'},
        'early_net_buy_ratio':{'name':'early_net_buy_ratio_30m', 'desc':'早盘30分钟净主动买入金额占当日总成交金额比例'},
        'late_net_buy_ratio':{'name':'late_net_buy_ratio_30m', 'desc':'尾盘30分钟净主动买入金额占当日总成交金额比例'}
    }
    ,
        table='hf_factors_cn_stock_a'
    )
    
    In [16]:
    dt = m3.data.read()
    dt.shape, dt.date.min(), dt.date.max()
    
    Out[16]:
    ((808000, 9),
     Timestamp('2018-01-02 00:00:00'),
     Timestamp('2018-12-28 00:00:00'))
    In [17]:
    dt.head().T
    
    Out[17]:
    0 1 2 3 4
    amt_great_ratio -0.002893 -0.011493 0.028328 0.011139 -0.002205
    amt_big_ratio 0.001011 0.000214 0.007832 -0.02137 0.008514
    amt_median_ratio -0.003636 -0.002618 -0.048839 -0.043245 0.01333
    amt_small_ratio -0.010699 -0.021369 -0.015255 -0.053618 0.068474
    net_buy_ratio 0.020865 0.02456 0.185398 0.25769 -0.18507
    early_net_small_buy_ratio 0.073613 0.038231 -0.017739 -0.005557 -0.016099
    late_net_small_buy_ratio -0.006729 0.008424 0.031721 0.167141 -0.029127
    date 2018-01-02 00:00:00 2018-01-02 00:00:00 2018-01-02 00:00:00 2018-01-02 00:00:00 2018-01-02 00:00:00
    instrument 000001.SZA 000002.SZA 000004.SZA 000005.SZA 000008.SZA
    In [18]:
    dt.isna().sum()
    
    Out[18]:
    amt_great_ratio              0
    amt_big_ratio                0
    amt_median_ratio             0
    amt_small_ratio              0
    net_buy_ratio                0
    early_net_small_buy_ratio    0
    late_net_small_buy_ratio     0
    date                         0
    instrument                   0
    dtype: int64
    In [19]:
    dt.describe().T
    
    Out[19]:
    count mean std min 25% 50% 75% max
    amt_great_ratio 808000.0 -0.006603 0.025016 -0.380235 -0.021475 -0.006018 0.007744 0.334226
    amt_big_ratio 808000.0 0.005033 0.017267 -0.358929 -0.004255 0.004511 0.014243 0.304878
    amt_median_ratio 808000.0 0.010793 0.034417 -0.459008 -0.008896 0.010256 0.030127 0.440092
    amt_small_ratio 808000.0 0.012753 0.038408 -0.254812 -0.013760 0.013060 0.040323 0.414017
    net_buy_ratio 808000.0 -0.071141 0.199425 -1.000000 -0.207392 -0.073439 0.059513 1.000000
    early_net_small_buy_ratio 808000.0 -0.014481 0.071945 -0.976471 -0.046328 -0.013594 0.017609 0.994475
    late_net_small_buy_ratio 808000.0 -0.006526 0.058425 -0.832273 -0.036421 -0.007930 0.020172 0.980513
    In [34]:
    dt2 = dt.set_index(['date','instrument'])
    dt2.head()
    
    Out[34]:
    amt_great_ratio amt_big_ratio amt_median_ratio amt_small_ratio net_buy_ratio early_net_small_buy_ratio late_net_small_buy_ratio
    date instrument
    2020-01-02 000001.SZA 0.045613 -0.010274 -0.046491 0.007187 0.190383 0.266497 -0.066743
    000002.SZA 0.217644 -0.013087 -0.063739 -0.149064 0.887070 0.246439 0.081625
    000005.SZA -0.256633 0.064156 0.131809 0.003940 -0.913073 -0.346138 -0.077162
    2020-01-03 000001.SZA 0.162043 0.022826 0.044829 0.125175 -0.061576 -0.143203 -0.063054
    000002.SZA 0.271088 -0.021355 -0.094521 -0.203255 1.180437 0.097443 0.212409
    In [38]:
    import seaborn as sns
    import matplotlib.pyplot as plt
    
    In [39]:
    # %%time
    n=4#行数
    m=dt2.shape[1]
    x=m%n
    fig,axes=plt.subplots(n,int((m - x + (n if x else x))/n),figsize=(20, 10))
    axes=axes.reshape((-1,))
    for i in range(dt2.shape[1]):    
        sns.distplot(dt2[dt2.columns[i]],ax=axes[i])
    plt.subplots_adjust(wspace =0.3, hspace =0.3)
    
    In [37]:
    # for i in dt2.columns[:]:
    #     dt2[i].to_hdf(f'XX券商-XXXX研报-{i}.h5', 'fac', mode='w')
    
    In [ ]:
     
    
    In [ ]:
     
    
    In [ ]:
     
    
    In [ ]:
     
    
    In [ ]: