复制链接
克隆策略
In [1]:
df = DataSource('level2_bar1m_CN_STOCK_A').read(instruments=['000001.SZA'])

    {"description":"实验创建于2017/8/26","graph":{"edges":[{"to_node_id":"-43:instruments","from_node_id":"-143:data"},{"to_node_id":"-43:features","from_node_id":"-151:data"},{"to_node_id":"-43:user_functions","from_node_id":"-52:functions"},{"to_node_id":"-145:features","from_node_id":"-62:data"},{"to_node_id":"-145:input_data","from_node_id":"-43:data"}],"nodes":[{"node_id":"-143","module_id":"BigQuantSpace.instruments.instruments-v2","parameters":[{"name":"start_date","value":"2020-06-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":"000001.SZA\n000002.SZA\n000005.SZA","type":"Literal","bound_global_parameter":null},{"name":"max_count","value":"0","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"rolling_conf","node_id":"-143"}],"output_ports":[{"name":"data","node_id":"-143"}],"cacheable":true,"seq_num":20,"comment":"","comment_collapsed":true},{"node_id":"-151","module_id":"BigQuantSpace.input_features.input_features-v1","parameters":[{"name":"features","value":"# 支持 np=numpy, pd=pandas, ta=talib, math 库,支持 pandas series 内建函数\n# _ 开始的表示中间变量,不会出现在最终结果中,可以用于中间复用计算结果,加快速度\n# 自定义表达式\n\n\n\n\nret5m = close.loc[145700] / close.loc[145200] -1 \n\n\n\nclose_ = close.loc[145700]\n\n\n\n(close*volume).sum()/volume.sum()\nvwap(close,volume)\nmean_4 = close.loc[103000]+close.loc[113000]+ close.loc[140000]+close.loc[145700] \n_ret = close.pct_change().fillna(close.iloc[0]/open.iloc[0])\n\n# 分钟收益率的各阶矩\t\nskew = _ret.skew()\nkurt = _ret.kurt()\ninday_ret = close.loc[145700]/close.loc[95900] - 1 # 日内涨跌幅累积\nlow_volume_cov = low.cov(volume) #日内成交量最低价的协方差\n \nsome_rsi = ta.RSI(close).loc[95900] # RSI技术指标\nsar = ta.SAR(high,low, 0.02, 0.2).loc[145700] # SAR抛物线转向\n\n_pvt = (_ret * volume).cumsum()\npvt = _pvt.iloc[-1] - _pvt.mean() # PVT量价趋势因子\n\n# 聪明钱指标\n_st = ((close / close.shift(1) - 1).abs() / volume.pow(0.5)).sort_values(ascending=False)\n_volume = volume[_st.index]\n_close = close[_st.index]\n_smart_money = (_volume.cumsum() / volume.sum()) >= 0.2\nsmart_money = ((_volume[_smart_money] * _close[_smart_money]).sum() / _volume[_smart_money].sum()) / ((volume * close).sum() / volume.sum())\n \n# 成交量个阶矩\nmean_v =volume.mean()\nstd_v = volume.std()\nskew_v = volume.skew()\nkurt_v =volume.kurt()\n\n# 日内最优动量\nmom1 = close.loc[103000]/close.loc[93100] - 1\nmom2 = close.loc[113000]/close.loc[103000] - 1\nmom3 = close.loc[140000]/close.loc[130100] - 1\nmom4 = close.loc[145700]/close.loc[140000] - 1\n\nopen_ = open.loc[93100]\nhigh_ = high.max() \nlow_ = low.min()\n \n# 人气指标 \n_mid = (high+low+close)/3\n_strong_sum = where(high>_mid.shift(1),high-_mid.shift(1),0).sum()\n_weak_sum = where(low<_mid.shift(1),_mid.shift(1)-low,0).sum()\ncr = _strong_sum / _weak_sum \n \n# MFI资金流向因子\n_mf = _mid * volume \n_mf_p = where(_mid>_mid.shift(1), _mf, 0)\n_mf_n = where(_mid<_mid.shift(1), -1*_mf, 0)\n_positive_mf = _mf_p.loc[100000:145700].sum()\n_negative_mf = _mf_n.loc[100000:145700].sum()\n_mr = _positive_mf/_negative_mf\nmfi = 100-(100/(1+_mr))\n\n# MACD指数平滑异同平均\n_dif = ta.EMA(close,12) - ta.EMA(close,26)\nmacd = _dif.loc[145700]\n\n# SRDM动向速度比率\n_dmz = where(high+low<high.shift(1)+low.shift(1),0,max(abs(high-high.shift(1)),abs(low-low.shift(1))))\n_dmf = where(high+low>=high.shift(1)+low.shift(1),0,max(abs(high-high.shift(1)),abs(low-low.shift(1))))\n_admz = mean(_dmz,10)\n_admf = mean(_dmf,10)\n_srdm = where(_admz>_admf,(_admz-_admf)/_admz,where(_admz==_admf,0,(_admz-_admf)/_admf))\nasrdm = mean(_srdm, 10).loc[145700]\n\n# 真正强度指数TSI\t\n_mom = close - close.shift(1)\n_mom_real = np.array(_mom,dtype='f8')\n_tsi_series = (ta.EMA(ta.EMA(_mom_real,25),13) / ta.EMA(ta.EMA(abs(_mom_real),25),13) )*100\ntsi = _tsi_series.loc[145700]\n\n# 日内大单流入\n_up_volumes = volume[_ret > 0]\nmy_mf_net_amount_l = _up_volumes.nlargest(math.floor(0.1 * len(_up_volumes))).sum()\n ","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"features_ds","node_id":"-151"}],"output_ports":[{"name":"data","node_id":"-151"}],"cacheable":true,"seq_num":21,"comment":"","comment_collapsed":true},{"node_id":"-52","module_id":"BigQuantSpace.feature_extractor_user_function.feature_extractor_user_function-v1","parameters":[{"name":"name","value":"vwap","type":"Literal","bound_global_parameter":null},{"name":"func","value":"def bigquant_run(df, close, volume):\n vwap=(close*volume).sum()/volume.sum()\n return vwap\n","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_functions","node_id":"-52"}],"output_ports":[{"name":"functions","node_id":"-52"}],"cacheable":false,"seq_num":1,"comment":"","comment_collapsed":true},{"node_id":"-145","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 cal_m(df, amount_avg, price_chg, N):\n def get_m_rolling(s, df_1, N):\n _media_num = (N + 1)//2\n _temp = pd.Series(s).sort_values(ascending=False)\n _m_high = (df_1.iloc[_temp.iloc[:_media_num].index].price_chg + 1).cumprod().iloc[-1] - 1\n _m_low = (df_1.iloc[_temp.iloc[_media_num:].index].price_chg + 1).cumprod().iloc[-1] - 1\n return _m_high - _m_low\n return pd.rolling_apply(df.amount_avg, N, lambda x: get_m_rolling(x, df, N))\n\n# Series对象 rolling 分块求回归系数\ndef rolling_series(series, roll_period):\n series.index= range(1,len(series)+1)\n start_lst = list(series[:roll_period -1])\n rolllists = [series[1].copy()] * (roll_period - 1)\n for i in range(len(start_lst)):\n rolllists[i] = start_lst[i]\n \n for row in series.index:\n index = row\n values = series.ix[index]\n if index > roll_period - 1: # or -2 if zero-indexed\n res = []\n for i in range(index - roll_period, index):\n res.append(series.loc[i + 1]) # or i if 0-indexed\n rolllists.append(res)\n \n new_roll = []\n for li in rolllists:\n while isinstance(li[0], list):\n li = [item for sublist in li for item in sublist] # flatten nested list\n new_roll.append(li)\n return new_roll\n\ndef calcu_rnyd_ret(df, x_name, y_name, N):\n \n reg_df = pd.DataFrame({'x': rolling_series(x_name, N), 'y':rolling_series(y_name, N)})\n \n beta_lst = []\n \n for i in reg_df.index:\n \n x = pd.Series(reg_df.ix[i]['x']).fillna(0)\n y = pd.Series(reg_df.ix[i]['y']).fillna(0)\n \n from scipy import stats\n import statsmodels.api as sm\n \n beta, stockalpha, r_value, p_value, slope_std_error = stats.linregress( x, y)\n beta_lst.append(beta) \n return pd.Series(beta_lst)\n \nbigquant_run = {\n 'calcu_rnyd_ret': calcu_rnyd_ret,\n 'cal_m':cal_m\n}","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_data","node_id":"-145"},{"name":"features","node_id":"-145"}],"output_ports":[{"name":"data","node_id":"-145"}],"cacheable":true,"seq_num":2,"comment":"日频因子进行加工","comment_collapsed":false},{"node_id":"-62","module_id":"BigQuantSpace.input_features.input_features-v1","parameters":[{"name":"features","value":"mom0 = open_/close_.shift(1)\nma_mom1 = mean(mom1,22)\nma_mom2 = mean(mom2,22)\nma_mom3 = mean(mom3,22)\nma_mom4 = mean(mom4,22)\n \n ","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"features_ds","node_id":"-62"}],"output_ports":[{"name":"data","node_id":"-62"}],"cacheable":false,"seq_num":4,"comment":"","comment_collapsed":true},{"node_id":"-43","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":90,"type":"Literal","bound_global_parameter":null},{"name":"workers","value":2,"type":"Literal","bound_global_parameter":null},{"name":"parallel_mode","value":"测试","type":"Literal","bound_global_parameter":null},{"name":"table_1m","value":"降频分钟数据(股票)","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"instruments","node_id":"-43"},{"name":"features","node_id":"-43"},{"name":"user_functions","node_id":"-43"}],"output_ports":[{"name":"data","node_id":"-43"}],"cacheable":false,"seq_num":3,"comment":"","comment_collapsed":true}],"node_layout":"<node_postions><node_position Node='-143' Position='-235,-681,200,200'/><node_position Node='-151' Position='80,-746,200,200'/><node_position Node='-52' Position='383,-687,200,200'/><node_position Node='-145' Position='246,-394,200,200'/><node_position Node='-62' Position='489,-572,200,200'/><node_position Node='-43' Position='74.93719100952148,-514.6241149902344,200,200'/></node_postions>"},"nodes_readonly":false,"studio_version":"v2"}
    In [4]:
    # 本代码由可视化策略环境自动生成 2023年1月3日 17:39
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    def m1_func_bigquant_run(df, close, volume):
        vwap=(close*volume).sum()/volume.sum()
        return vwap
    
    def cal_m(df, amount_avg, price_chg, N):
        def get_m_rolling(s, df_1, N):
            _media_num = (N + 1)//2
            _temp = pd.Series(s).sort_values(ascending=False)
            _m_high = (df_1.iloc[_temp.iloc[:_media_num].index].price_chg + 1).cumprod().iloc[-1] - 1
            _m_low = (df_1.iloc[_temp.iloc[_media_num:].index].price_chg + 1).cumprod().iloc[-1] - 1
            return _m_high - _m_low
        return pd.rolling_apply(df.amount_avg, N, lambda x: get_m_rolling(x, df, N))
    
    # Series对象 rolling 分块求回归系数
    def rolling_series(series, roll_period):
        series.index= range(1,len(series)+1)
        start_lst = list(series[:roll_period -1])
        rolllists =  [series[1].copy()] * (roll_period - 1)
        for i in range(len(start_lst)):
            rolllists[i] = start_lst[i]
        
        for row in series.index:
            index = row
            values = series.ix[index]
            if index > roll_period - 1:  # or -2 if zero-indexed
                res = []
                for i in range(index - roll_period, index):
                    res.append(series.loc[i + 1])  # or i if 0-indexed
                rolllists.append(res)
        
        new_roll = []
        for li in rolllists:
            while isinstance(li[0], list):
                li = [item for sublist in li for item in sublist]  # flatten nested list
            new_roll.append(li)
        return  new_roll
    
    def calcu_rnyd_ret(df, x_name, y_name, N):
      
        reg_df = pd.DataFrame({'x': rolling_series(x_name, N), 'y':rolling_series(y_name, N)})
        
        beta_lst = []
     
        for i in reg_df.index:
           
            x = pd.Series(reg_df.ix[i]['x']).fillna(0)
            y = pd.Series(reg_df.ix[i]['y']).fillna(0)
          
            from scipy import stats
            import statsmodels.api as sm
            
            beta, stockalpha, r_value, p_value, slope_std_error = stats.linregress( x, y)
            beta_lst.append(beta) 
        return pd.Series(beta_lst)
      
    m2_user_functions_bigquant_run = {
        'calcu_rnyd_ret': calcu_rnyd_ret,
        'cal_m':cal_m
    }
    
    m20 = M.instruments.v2(
        start_date='2020-06-01',
        end_date='2020-12-31',
        market='CN_STOCK_A',
        instrument_list="""000001.SZA
    000002.SZA
    000005.SZA""",
        max_count=0
    )
    
    m21 = M.input_features.v1(
        features="""# 支持 np=numpy, pd=pandas, ta=talib, math 库,支持 pandas series 内建函数
    # _ 开始的表示中间变量,不会出现在最终结果中,可以用于中间复用计算结果,加快速度
    # 自定义表达式
    
    
    
    
    ret5m = close.loc[145700] / close.loc[145200] -1 
    
    
    
    close_ = close.loc[145700]
    
    
    
    (close*volume).sum()/volume.sum()
    vwap(close,volume)
    mean_4 = close.loc[103000]+close.loc[113000]+ close.loc[140000]+close.loc[145700] 
    _ret = close.pct_change().fillna(close.iloc[0]/open.iloc[0])
    
    # 分钟收益率的各阶矩	
    skew = _ret.skew()
    kurt = _ret.kurt()
    inday_ret = close.loc[145700]/close.loc[95900] - 1  # 日内涨跌幅累积
    low_volume_cov = low.cov(volume) #日内成交量最低价的协方差
     
    some_rsi = ta.RSI(close).loc[95900] # RSI技术指标
    sar = ta.SAR(high,low, 0.02, 0.2).loc[145700] # SAR抛物线转向
    
    _pvt = (_ret * volume).cumsum()
    pvt = _pvt.iloc[-1]  -  _pvt.mean() # PVT量价趋势因子
    
    # 聪明钱指标
    _st = ((close / close.shift(1) - 1).abs() / volume.pow(0.5)).sort_values(ascending=False)
    _volume = volume[_st.index]
    _close = close[_st.index]
    _smart_money = (_volume.cumsum() / volume.sum()) >= 0.2
    smart_money = ((_volume[_smart_money] * _close[_smart_money]).sum() / _volume[_smart_money].sum()) / ((volume * close).sum() / volume.sum())
     
    # 成交量个阶矩
    mean_v =volume.mean()
    std_v = volume.std()
    skew_v = volume.skew()
    kurt_v =volume.kurt()
    
    # 日内最优动量
    mom1 = close.loc[103000]/close.loc[93100] - 1
    mom2 = close.loc[113000]/close.loc[103000] - 1
    mom3 = close.loc[140000]/close.loc[130100] - 1
    mom4 = close.loc[145700]/close.loc[140000] - 1
    
    open_ = open.loc[93100]
    high_ = high.max() 
    low_ = low.min()
     
    # 人气指标 
    _mid = (high+low+close)/3
    _strong_sum = where(high>_mid.shift(1),high-_mid.shift(1),0).sum()
    _weak_sum = where(low<_mid.shift(1),_mid.shift(1)-low,0).sum()
    cr = _strong_sum / _weak_sum 
      
    # MFI资金流向因子
    _mf = _mid * volume 
    _mf_p = where(_mid>_mid.shift(1), _mf, 0)
    _mf_n = where(_mid<_mid.shift(1), -1*_mf, 0)
    _positive_mf = _mf_p.loc[100000:145700].sum()
    _negative_mf = _mf_n.loc[100000:145700].sum()
    _mr = _positive_mf/_negative_mf
    mfi = 100-(100/(1+_mr))
    
    # MACD指数平滑异同平均
    _dif = ta.EMA(close,12) - ta.EMA(close,26)
    macd = _dif.loc[145700]
    
    # SRDM动向速度比率
    _dmz = where(high+low<high.shift(1)+low.shift(1),0,max(abs(high-high.shift(1)),abs(low-low.shift(1))))
    _dmf = where(high+low>=high.shift(1)+low.shift(1),0,max(abs(high-high.shift(1)),abs(low-low.shift(1))))
    _admz = mean(_dmz,10)
    _admf = mean(_dmf,10)
    _srdm = where(_admz>_admf,(_admz-_admf)/_admz,where(_admz==_admf,0,(_admz-_admf)/_admf))
    asrdm = mean(_srdm, 10).loc[145700]
    
    # 真正强度指数TSI	
    _mom = close - close.shift(1)
    _mom_real = np.array(_mom,dtype='f8')
    _tsi_series = (ta.EMA(ta.EMA(_mom_real,25),13) / ta.EMA(ta.EMA(abs(_mom_real),25),13) )*100
    tsi = _tsi_series.loc[145700]
    
    # 日内大单流入
    _up_volumes = volume[_ret > 0]
    my_mf_net_amount_l = _up_volumes.nlargest(math.floor(0.1 * len(_up_volumes))).sum()
     """
    )
    
    m1 = M.feature_extractor_user_function.v1(
        name='vwap',
        func=m1_func_bigquant_run
    )
    
    m3 = M.feature_extractor_1m.v2(
        instruments=m20.data,
        features=m21.data,
        user_functions=m1.functions,
        start_date='',
        end_date='',
        before_start_days=90,
        workers=2,
        parallel_mode='测试',
        table_1m='降频分钟数据(股票)',
        m_cached=False
    )
    
    m4 = M.input_features.v1(
        features="""mom0 = open_/close_.shift(1)
    ma_mom1 = mean(mom1,22)
    ma_mom2 = mean(mom2,22)
    ma_mom3 = mean(mom3,22)
    ma_mom4 = mean(mom4,22)
     
     """,
        m_cached=False
    )
    
    m2 = M.derived_feature_extractor.v3(
        input_data=m3.data,
        features=m4.data,
        date_col='date',
        instrument_col='instrument',
        drop_na=False,
        remove_extra_columns=False,
        user_functions=m2_user_functions_bigquant_run
    )
    
    In [5]:
    m2.data.read().tail(2).T
    
    Out[5]:
    616 617
    ret5m -0.000348 0.0
    close_ 28.690001 2.52
    (close*volume).sum()/volume.sum() 28.568573 2.509488
    vwap(close,volume) 28.568575 2.509488
    mean_4 114.389999 10.059999
    skew 15.48593 15.45714
    kurt 239.875473 239.278458
    inday_ret -0.000348 0.008
    low_volume_cov 3412.52579 26.023082
    some_rsi 67.874067 45.057207
    sar 28.743789 2.511922
    pvt 2335.125 -38.734375
    smart_money 0.999965 1.000198
    mean_v 271662.9625 28252.829167
    std_v 213188.740796 52044.620178
    skew_v 2.964984 3.548522
    kurt_v 18.371975 16.3161
    mom1 0.017705 0.0
    mom2 -0.010786 0.0
    mom3 0.00387 0.008
    mom4 0.005608 0.0
    open_ 28.290001 2.5
    high_ 28.790001 2.53
    low_ 28.24 2.5
    cr 1.082899 1.081802
    mfi -1954.83272 -1015.671805
    macd 0.019839 0.001554
    asrdm -0.246548 0.291667
    tsi 16.726664 4.986561
    my_mf_net_amount_l 7986233.0 469200.0
    date 2020-12-31 00:00:00 2020-12-31 00:00:00
    instrument 000002.SZA 000005.SZA
    mom0 1.465803 0.087138
    ma_mom1 -0.000562 -0.00135
    ma_mom2 -0.001171 0.001239
    ma_mom3 -0.001682 -0.000521
    ma_mom4 0.00046 0.000733
    In [ ]: