同花顺Supermind量化交易 资金面专题2-风格下资金流入强度的分析 附源代码

78 阅读25分钟

一般而言,当市场中资金流动强度平缓时,风格投资者保持原有的风格方向;当资金流动强度强烈时,风格投资者转换原有的风格方向,例如:小市值投资者在资金流动强烈时,会转向大市值风格。本篇文章将从规模因子(大小盘风格)入手,研究关于资金流动强度与风格投资的话题。

风格下资金流入强度的分析

1 导入库包

In [2]:

import pandas as pd
import numpy as np
import datetime
import math
import matplotlib.pyplot as plt
plt.style.use('seaborn')

2.资金流入净值取数和计算

In [3]:

#取数平安银行主动买入量
data_buy=get_money_flow_step(security_list='000001.SZ',start_date=None,end_date='20190103',fre_step='1d',\
    fields=['act_buy_xl','act_buy_l','act_buy_m'],count=250,is_panel=0)['000001.SZ']

#取数平安银行主动卖出量
data_sell=get_money_flow_step(security_list='000001.SZ',start_date=None,end_date='20190103',fre_step='1d',\
    fields=['act_sell_xl','act_sell_l','act_sell_m'],count=250,is_panel=0)['000001.SZ']

#横向加总
data_buyall=pd.DataFrame(data_buy.apply(lambda x:x.sum(),axis=1),columns=['buy_all'])
data_sellall=pd.DataFrame(data_sell.apply(lambda x:x.sum(),axis=1),columns=['sell_all'])
data_all=pd.concat([data_buyall,data_sellall],axis=1)

#得出平安银行流入净值
data_all['data_net']=data_all['buy_all']-data_all['sell_all']
data_all.tail()

Out[3]:

buy_allsell_alldata_net
2018-12-26108688097.0176514956.0-67826859.0
2018-12-27191021237.0258350579.0-67329342.0
2018-12-28249609737.0161550282.088059455.0
2019-01-02115789628.0270478617.0-154688989.0
2019-01-03173588438.0115469752.058118686.0

3.一个截面的资金流动强度分析

In [43]:

start_date='2019-01-10'
start_date=datetime.datetime.strptime(start_date,'%Y-%m-%d')

def inflow(stock_list,start_date):
    
    for stock in stock_list:
        #print(stock)
        data_buy=get_money_flow_step(security_list=stock,start_date=None,end_date=start_date,fre_step='1d',\
        fields=['act_buy_xl','act_buy_l','act_buy_m'],count=1,is_panel=0)[stock]
 
        data_sell=get_money_flow_step(security_list=stock,start_date=None,end_date=start_date,fre_step='1d',\
        fields=['act_sell_xl','act_sell_l','act_sell_m'],count=1,is_panel=0)[stock]
        
        data_buyall=pd.DataFrame(data_buy.apply(lambda x:x.sum(),axis=1),columns=['buy_all'])
        data_sellall=pd.DataFrame(data_sell.apply(lambda x:x.sum(),axis=1),columns=['sell_all'])
        data_all=pd.concat([data_buyall,data_sellall],axis=1) 
        data_net=data_all['buy_all']-data_all['sell_all']
        try:
            data_net=pd.Series(data_net[0],index=[stock])
        except:
            continue
        
        if 'inflow_df' in locals():
            inflow_df = inflow_df.append(data_net)
        else:
            inflow_df = data_net
    inflow_df=inflow_df.to_frame()
    inflow_df.columns=['net_inflow']

    return inflow_df
             
stock_list=list(get_all_securities('stock',date=start_date).index)
    
#取全市场股票各股票市值因子
df = get_fundamentals(query(asharevalue.symbol, asharevalue.total_mv).filter(\
                        asharevalue.symbol.in_(stock_list)).order_by(asharevalue.total_mv.asc()),date=start_date)
df.set_index('asharevalue_symbol',inplace=True)
df.index.name='symbol'
    
#取全市场股票各股票流入净值
df_inflow=inflow(stock_list,start_date)
    
#取全市场股票各股票成交量
df_volume = get_price(stock_list, None, '20170303', '1d', ['volume'], True, None, 1, is_panel=1)['volume']
df_volume=df_volume.T
df_volume.columns=['df_volume']
    
#合并三表
df=pd.concat([df,df_inflow,df_volume],axis=1)
    
#按照市值排序
df.sort_values(by='asharevalue_total_mv',ascending = True,inplace=True)
df.dropna(inplace=True)
df_slice=round(len(df)/10)
#print(df)
    
#将dataframe的净流入和成交量分成10份切片,并在切片内进行加总
data_net_inflow=[]
data_volume=[]
data_factor=[]
for i in range(10):
    data_sum=df.ix[i*df_slice:(i+1)*df_slice,['asharevalue_total_mv','net_inflow','df_volume']].apply(lambda x: x.sum())
    data_net_inflow.append(data_sum['net_inflow'])
    data_volume.append(data_sum['df_volume'])
    data_factor.append(data_sum['asharevalue_total_mv'])
    
data_net_inflow=pd.Series(data_net_inflow)
data_volume=pd.Series(data_volume)
data_factor=pd.Series(data_factor)
    
data_total=pd.DataFrame([data_factor,data_net_inflow,data_volume])
data_total=data_total.T
data_total.columns=['data_factor','data_net_inflow','data_volume']
data_total.head(10) 

Out[43]:

data_factordata_net_inflowdata_volume
04.776260e+11-4.869376e+081.257051e+09
16.446934e+11-1.012772e+091.832052e+09
28.006962e+11-7.643456e+082.153484e+09
39.747939e+11-9.009924e+082.541013e+09
41.179759e+12-1.555130e+092.463412e+09
51.447312e+12-1.324253e+092.500842e+09
61.927586e+12-1.900550e+093.013475e+09
72.735843e+12-1.816132e+093.432070e+09
84.709769e+12-3.016223e+094.757325e+09
92.866271e+13-4.227482e+097.884248e+09

3.1 按照市值风格分组的资金净流入与成交量图

In [44]:

data_total[['data_net_inflow','data_volume']].plot(figsize=(10,8))

Out[44]:

<matplotlib.axes._subplots.AxesSubplot at 0x7fd94c340c18>

3.2 不同市值分组下的资金净流入和成交量展示图

In [45]:

ind = np.arange(len(data_total['data_net_inflow'])) 
width = 0.35

fig,ax = plt.subplots(figsize=(10,8))
rects1 = ax.bar(ind - width/2, data_total['data_net_inflow'], width, color='SkyBlue', label='Net_inflow')
rects2 = ax.bar(ind + width/2, data_total['data_volume'], width,color='IndianRed', label='Volume')

ax.set_title('net_inflow & volume',size=20)
plt.xticks(ind,('group1', 'group2', 'group3', 'group4', 'group5','group6','group7','group8','group9','group10'))
ax.legend()

Out[45]:

<matplotlib.legend.Legend at 0x7fd9b32a74e0>

In [41]:

data_total.loc['Col_sum'] = data_total.apply(lambda x: x.sum())
data_total

Out[41]:

data_factordata_net_inflowdata_volumeinflow_by_volume
05.286407e+11-2.581254e+088.066170e+08-0.320010
16.379624e+11-5.345425e+086.639116e+08-0.805141
27.397299e+11-2.947834e+087.246609e+08-0.406788
38.484290e+11-3.367236e+086.470733e+08-0.520379
49.987154e+11-2.293879e+084.208110e+08-0.545109
51.266877e+122.328543e+086.338160e+080.367385
61.651160e+12-3.330558e+088.245164e+08-0.403941
72.343153e+12-4.061579e+081.258775e+09-0.322661
84.038133e+12-1.200875e+099.095614e+08-1.320279
91.485925e+13-9.858907e+081.104909e+09-0.892282
Col_sum5.582410e+13-8.693376e+091.598930e+10-5.712905

In [9]:

data_total['inflow_by_volume']=data_total['data_net_inflow']/data_total['data_volume']
data_total

Out[9]:

data_factordata_net_inflowdata_volumeinflow_by_volume
04.776260e+11-4.869376e+081.257051e+09-0.387365
16.446934e+11-1.012772e+091.832052e+09-0.552808
28.006962e+11-7.643456e+082.153484e+09-0.354934
39.747939e+11-9.009924e+082.541013e+09-0.354580
41.179759e+12-1.555130e+092.463412e+09-0.631291
51.447312e+12-1.324253e+092.500842e+09-0.529523
61.927586e+12-1.900550e+093.013475e+09-0.630684
72.735843e+12-1.816132e+093.432070e+09-0.529165
84.709769e+12-3.016223e+094.757325e+09-0.634017
92.866271e+13-4.227482e+097.884248e+09-0.536193
Col_sum4.356079e+13-1.700482e+103.183497e+10-0.534155

3.3 剔除长期偏好的资金流动强度

In [10]:

mean_overinflow=data_total.ix[0:10,'inflow_by_volume'].sum()-data_total.ix[10,'inflow_by_volume']*10
var_f=0
for i in range(10):
    dev=data_total.ix[i,'inflow_by_volume']-data_total.ix[10,'inflow_by_volume']-mean_overinflow
    var_f+=pow(dev,2)*(data_total.ix[i,'data_factor']/data_total.ix[10,'data_factor'])
    
std_f=math.sqrt(var_f)
print(std_f)
0.2183871088326275

4.时间窗口内的资金流动强度

In [11]:

start_date='2018-01-10'
end_date='2019-01-10'

start_date=datetime.datetime.strptime(start_date,'%Y-%m-%d')
end_date=datetime.datetime.strptime(end_date,'%Y-%m-%d')+datetime.timedelta(days=1)

flag=True

def inflow(stock_list,start_date):
    
    for stock in stock_list:
        #print(stock)
        data_buy=get_money_flow_step(security_list=stock,start_date=None,end_date=start_date,fre_step='1d',\
        fields=['act_buy_xl','act_buy_l','act_buy_m'],count=1,is_panel=0)[stock]
 
        data_sell=get_money_flow_step(security_list=stock,start_date=None,end_date=start_date,fre_step='1d',\
        fields=['act_sell_xl','act_sell_l','act_sell_m'],count=1,is_panel=0)[stock]
        
        data_buyall=pd.DataFrame(data_buy.apply(lambda x:x.sum(),axis=1),columns=['buy_all'])
        data_sellall=pd.DataFrame(data_sell.apply(lambda x:x.sum(),axis=1),columns=['sell_all'])
        data_all=pd.concat([data_buyall,data_sellall],axis=1) 
        data_net=data_all['buy_all']-data_all['sell_all']
        try:
            data_net=pd.Series(data_net[0],index=[stock])
        except:
            continue
        
        if 'inflow_df' in locals():
            inflow_df = inflow_df.append(data_net)
        else:
            inflow_df = data_net
    inflow_df=inflow_df.to_frame()
    inflow_df.columns=['net_inflow']

    return inflow_df


#标准化
def zscore(factor, df):
    mean = df[factor].mean()
    std = df[factor].std()
    df = (df - mean) / std
    return df 
    
while start_date<end_date: 
    #print(start_date)
    stock_list=list(get_index_stocks('000300.SH',date=start_date))
    
    #取全市场股票各股票市值因子
    df = get_fundamentals(query(asharevalue.symbol, asharevalue.total_mv).filter(\
                        asharevalue.symbol.in_(stock_list)).order_by(asharevalue.total_mv.asc()),date=start_date)
    df.set_index('asharevalue_symbol',inplace=True)
    df.index.name='symbol'
    
    #取全市场股票各股票流入净值
    df_inflow=inflow(stock_list,start_date)
    
    #取全市场股票各股票成交量
    df_volume = get_price(stock_list, None, '20170303', '1d', ['volume'], True, None, 1, is_panel=1)['volume']
    df_volume=df_volume.T
    df_volume.columns=['df_volume']
    
    #合并三表
    df=pd.concat([df,df_inflow,df_volume],axis=1) 
    
    #按照市值排序
    df.sort_values(by='asharevalue_total_mv',ascending = True,inplace=True)
    df.dropna(inplace=True)
    df_slice=round(len(df)/10)
    
    #将dataframe的净流入和成交量分成10份切片,并在切片内进行加总
    data_net_inflow=[]
    data_volume=[]
    data_factor=[]
    for i in range(10):
        data_sum=df.ix[i*df_slice:(i+1)*df_slice,['asharevalue_total_mv','net_inflow','df_volume']].apply(lambda x: x.sum())
        data_net_inflow.append(data_sum['net_inflow'])
        data_volume.append(data_sum['df_volume'])
        data_factor.append(data_sum['asharevalue_total_mv'])
    
    data_net_inflow=pd.Series(data_net_inflow)
    data_volume=pd.Series(data_volume)
    data_factor=pd.Series(data_factor)
    
    data_total=pd.DataFrame([data_factor,data_net_inflow,data_volume])
    data_total=data_total.T
    data_total.columns=['data_factor','data_net_inflow','data_volume']
    data_total.loc['Col_sum'] = data_total.apply(lambda x: x.sum())
    data_total['inflow_by_volume']=data_total['data_net_inflow']/data_total['data_volume']
    mean_overinflow=data_total.ix[0:10,'inflow_by_volume'].sum()-data_total.ix[10,'inflow_by_volume']*10
    var_f=0
    for i in range(10):
        dev=data_total.ix[i,'inflow_by_volume']-data_total.ix[10,'inflow_by_volume']-mean_overinflow
        var_f+=pow(dev,2)*(data_total.ix[i,'data_factor']/data_total.ix[10,'data_factor'])
    
    std_f=math.sqrt(var_f)
    std_f_frame=pd.DataFrame([std_f],index=[start_date])
    #合并时间窗口的流动强度数据
    if flag:
        flag=False
        std_total = std_f_frame
    else:
        std_total = std_total.append(std_f_frame)
    
    print(start_date)
    start_date+=datetime.timedelta(days=1)
    
std_total.dropna(inplace=True)
std_total.columns=['std_flow']

new_std_total=zscore('std_flow',std_total)
     
2018-01-10 00:00:00
2018-01-11 00:00:00
2018-01-12 00:00:00
2018-01-13 00:00:00
2018-01-14 00:00:00
2018-01-15 00:00:00
2018-01-16 00:00:00
2018-01-17 00:00:00
2018-01-18 00:00:00
2018-01-19 00:00:00
2018-01-20 00:00:00
2018-01-21 00:00:00
2018-01-22 00:00:00
2018-01-23 00:00:00
2018-01-24 00:00:00
2018-01-25 00:00:00
2018-01-26 00:00:00
2018-01-27 00:00:00
2018-01-28 00:00:00
2018-01-29 00:00:00
2018-01-30 00:00:00
2018-01-31 00:00:00
2018-02-01 00:00:00
2018-02-02 00:00:00
2018-02-03 00:00:00
2018-02-04 00:00:00
2018-02-05 00:00:00
2018-02-06 00:00:00
2018-02-07 00:00:00
2018-02-08 00:00:00
2018-02-09 00:00:00
2018-02-10 00:00:00
2018-02-11 00:00:00
2018-02-12 00:00:00
2018-02-13 00:00:00
2018-02-14 00:00:00
2018-02-15 00:00:00
2018-02-16 00:00:00
2018-02-17 00:00:00
2018-02-18 00:00:00
2018-02-19 00:00:00
2018-02-20 00:00:00
2018-02-21 00:00:00
2018-02-22 00:00:00
2018-02-23 00:00:00
2018-02-24 00:00:00
2018-02-25 00:00:00
2018-02-26 00:00:00
2018-02-27 00:00:00
2018-02-28 00:00:00
2018-03-01 00:00:00
2018-03-02 00:00:00
2018-03-03 00:00:00
2018-03-04 00:00:00
2018-03-05 00:00:00
2018-03-06 00:00:00
2018-03-07 00:00:00
2018-03-08 00:00:00
2018-03-09 00:00:00
2018-03-10 00:00:00
2018-03-11 00:00:00
2018-03-12 00:00:00
2018-03-13 00:00:00
2018-03-14 00:00:00
2018-03-15 00:00:00
2018-03-16 00:00:00
2018-03-17 00:00:00
2018-03-18 00:00:00
2018-03-19 00:00:00
2018-03-20 00:00:00
2018-03-21 00:00:00
2018-03-22 00:00:00
2018-03-23 00:00:00
2018-03-24 00:00:00
2018-03-25 00:00:00
2018-03-26 00:00:00
2018-03-27 00:00:00
2018-03-28 00:00:00
2018-03-29 00:00:00
2018-03-30 00:00:00
2018-03-31 00:00:00
2018-04-01 00:00:00
2018-04-02 00:00:00
2018-04-03 00:00:00
2018-04-04 00:00:00
2018-04-05 00:00:00
2018-04-06 00:00:00
2018-04-07 00:00:00
2018-04-08 00:00:00
2018-04-09 00:00:00
2018-04-10 00:00:00
2018-04-11 00:00:00
2018-04-12 00:00:00
2018-04-13 00:00:00
2018-04-14 00:00:00
2018-04-15 00:00:00
2018-04-16 00:00:00
2018-04-17 00:00:00
2018-04-18 00:00:00
2018-04-19 00:00:00
2018-04-20 00:00:00
2018-04-21 00:00:00
2018-04-22 00:00:00
2018-04-23 00:00:00
2018-04-24 00:00:00
2018-04-25 00:00:00
2018-04-26 00:00:00
2018-04-27 00:00:00
2018-04-28 00:00:00
2018-04-29 00:00:00
2018-04-30 00:00:00
2018-05-01 00:00:00
2018-05-02 00:00:00
2018-05-03 00:00:00
2018-05-04 00:00:00
2018-05-05 00:00:00
2018-05-06 00:00:00
2018-05-07 00:00:00
2018-05-08 00:00:00
2018-05-09 00:00:00
2018-05-10 00:00:00
2018-05-11 00:00:00
2018-05-12 00:00:00
2018-05-13 00:00:00
2018-05-14 00:00:00
2018-05-15 00:00:00
2018-05-16 00:00:00
2018-05-17 00:00:00
2018-05-18 00:00:00
2018-05-19 00:00:00
2018-05-20 00:00:00
2018-05-21 00:00:00
2018-05-22 00:00:00
2018-05-23 00:00:00
2018-05-24 00:00:00
2018-05-25 00:00:00
2018-05-26 00:00:00
2018-05-27 00:00:00
2018-05-28 00:00:00
2018-05-29 00:00:00
2018-05-30 00:00:00
2018-05-31 00:00:00
2018-06-01 00:00:00
2018-06-02 00:00:00
2018-06-03 00:00:00
2018-06-04 00:00:00
2018-06-05 00:00:00
2018-06-06 00:00:00
2018-06-07 00:00:00
2018-06-08 00:00:00
2018-06-09 00:00:00
2018-06-10 00:00:00
2018-06-11 00:00:00
2018-06-12 00:00:00
2018-06-13 00:00:00
2018-06-14 00:00:00
2018-06-15 00:00:00
2018-06-16 00:00:00
2018-06-17 00:00:00
2018-06-18 00:00:00
2018-06-19 00:00:00
2018-06-20 00:00:00
2018-06-21 00:00:00
2018-06-22 00:00:00
2018-06-23 00:00:00
2018-06-24 00:00:00
2018-06-25 00:00:00
2018-06-26 00:00:00
2018-06-27 00:00:00
2018-06-28 00:00:00
2018-06-29 00:00:00
2018-06-30 00:00:00
2018-07-01 00:00:00
2018-07-02 00:00:00
2018-07-03 00:00:00
2018-07-04 00:00:00
2018-07-05 00:00:00
2018-07-06 00:00:00
2018-07-07 00:00:00
2018-07-08 00:00:00
2018-07-09 00:00:00
2018-07-10 00:00:00
2018-07-11 00:00:00
2018-07-12 00:00:00
2018-07-13 00:00:00
2018-07-14 00:00:00
2018-07-15 00:00:00
2018-07-16 00:00:00
2018-07-17 00:00:00
2018-07-18 00:00:00
2018-07-19 00:00:00
2018-07-20 00:00:00
2018-07-21 00:00:00
2018-07-22 00:00:00
2018-07-23 00:00:00
2018-07-24 00:00:00
2018-07-25 00:00:00
2018-07-26 00:00:00
2018-07-27 00:00:00
2018-07-28 00:00:00
2018-07-29 00:00:00
2018-07-30 00:00:00
2018-07-31 00:00:00
2018-08-01 00:00:00
2018-08-02 00:00:00
2018-08-03 00:00:00
2018-08-04 00:00:00
2018-08-05 00:00:00
2018-08-06 00:00:00
2018-08-07 00:00:00
2018-08-08 00:00:00
2018-08-09 00:00:00
2018-08-10 00:00:00
2018-08-11 00:00:00
2018-08-12 00:00:00
2018-08-13 00:00:00
2018-08-14 00:00:00
2018-08-15 00:00:00
2018-08-16 00:00:00
2018-08-17 00:00:00
2018-08-18 00:00:00
2018-08-19 00:00:00
2018-08-20 00:00:00
2018-08-21 00:00:00
2018-08-22 00:00:00
2018-08-23 00:00:00
2018-08-24 00:00:00
2018-08-25 00:00:00
2018-08-26 00:00:00
2018-08-27 00:00:00
2018-08-28 00:00:00
2018-08-29 00:00:00
2018-08-30 00:00:00
2018-08-31 00:00:00
2018-09-01 00:00:00
2018-09-02 00:00:00
2018-09-03 00:00:00
2018-09-04 00:00:00
2018-09-05 00:00:00
2018-09-06 00:00:00
2018-09-07 00:00:00
2018-09-08 00:00:00
2018-09-09 00:00:00
2018-09-10 00:00:00
2018-09-11 00:00:00
2018-09-12 00:00:00
2018-09-13 00:00:00
2018-09-14 00:00:00
2018-09-15 00:00:00
2018-09-16 00:00:00
2018-09-17 00:00:00
2018-09-18 00:00:00
2018-09-19 00:00:00
2018-09-20 00:00:00
2018-09-21 00:00:00
2018-09-22 00:00:00
2018-09-23 00:00:00
2018-09-24 00:00:00
2018-09-25 00:00:00
2018-09-26 00:00:00
2018-09-27 00:00:00
2018-09-28 00:00:00
2018-09-29 00:00:00
2018-09-30 00:00:00
2018-10-01 00:00:00
2018-10-02 00:00:00
2018-10-03 00:00:00
2018-10-04 00:00:00
2018-10-05 00:00:00
2018-10-06 00:00:00
2018-10-07 00:00:00
2018-10-08 00:00:00
2018-10-09 00:00:00
2018-10-10 00:00:00
2018-10-11 00:00:00
2018-10-12 00:00:00
2018-10-13 00:00:00
2018-10-14 00:00:00
2018-10-15 00:00:00
2018-10-16 00:00:00
2018-10-17 00:00:00
2018-10-18 00:00:00
2018-10-19 00:00:00
2018-10-20 00:00:00
2018-10-21 00:00:00
2018-10-22 00:00:00
2018-10-23 00:00:00
2018-10-24 00:00:00
2018-10-25 00:00:00
2018-10-26 00:00:00
2018-10-27 00:00:00
2018-10-28 00:00:00
2018-10-29 00:00:00
2018-10-30 00:00:00
2018-10-31 00:00:00
2018-11-01 00:00:00
2018-11-02 00:00:00
2018-11-03 00:00:00
2018-11-04 00:00:00
2018-11-05 00:00:00
2018-11-06 00:00:00
2018-11-07 00:00:00
2018-11-08 00:00:00
2018-11-09 00:00:00
2018-11-10 00:00:00
2018-11-11 00:00:00
2018-11-12 00:00:00
2018-11-13 00:00:00
2018-11-14 00:00:00
2018-11-15 00:00:00
2018-11-16 00:00:00
2018-11-17 00:00:00
2018-11-18 00:00:00
2018-11-19 00:00:00
2018-11-20 00:00:00
2018-11-21 00:00:00
2018-11-22 00:00:00
2018-11-23 00:00:00
2018-11-24 00:00:00
2018-11-25 00:00:00
2018-11-26 00:00:00
2018-11-27 00:00:00
2018-11-28 00:00:00
2018-11-29 00:00:00
2018-11-30 00:00:00
2018-12-01 00:00:00
2018-12-02 00:00:00
2018-12-03 00:00:00
2018-12-04 00:00:00
2018-12-05 00:00:00
2018-12-06 00:00:00
2018-12-07 00:00:00
2018-12-08 00:00:00
2018-12-09 00:00:00
2018-12-10 00:00:00
2018-12-11 00:00:00
2018-12-12 00:00:00
2018-12-13 00:00:00
2018-12-14 00:00:00
2018-12-15 00:00:00
2018-12-16 00:00:00
2018-12-17 00:00:00
2018-12-18 00:00:00
2018-12-19 00:00:00
2018-12-20 00:00:00
2018-12-21 00:00:00
2018-12-22 00:00:00
2018-12-23 00:00:00
2018-12-24 00:00:00
2018-12-25 00:00:00
2018-12-26 00:00:00
2018-12-27 00:00:00
2018-12-28 00:00:00
2018-12-29 00:00:00
2018-12-30 00:00:00
2018-12-31 00:00:00
2019-01-01 00:00:00
2019-01-02 00:00:00
2019-01-03 00:00:00
2019-01-04 00:00:00
2019-01-05 00:00:00
2019-01-06 00:00:00
2019-01-07 00:00:00
2019-01-08 00:00:00
2019-01-09 00:00:00
2019-01-10 00:00:00

4.1 标准化处理过的资金流动强度

In [12]:

new_std_total.head()

Out[12]:

std_flow
2018-01-102.711643
2018-01-11-0.775624
2018-01-121.912241
2018-01-152.579242
2018-01-161.852556

In [24]:

new_std_total.index=pd.to_datetime(new_std_total.index)
ind = new_std_total.index
width = 0.35 
fig, ax = plt.subplots(figsize=(20,6))
rects1=ax.bar(ind, new_std_total['std_flow'], width, color='IndianRed', label='std_flow')
ax.set_title('Cashflow Liquidity Strength Bar',size=20)
ax.legend()
plt.show()

In [25]:

fig = plt.figure(figsize = (20, 8))
ax = new_std_total['std_flow'].plot.kde(label = 'std_flow')
ax.set_title('Cashflow Liquidity Strength Kde',size=20)
ax.legend()

Out[25]:

<matplotlib.legend.Legend at 0x7fd94570ecf8>

4.2 市值风格下的涨跌幅

In [26]:

flag1=True

start_date1='2018-01-10'
end_date1='2019-01-10'

start_date=datetime.datetime.strptime(start_date1,'%Y-%m-%d')
end_date=datetime.datetime.strptime(end_date1,'%Y-%m-%d')+datetime.timedelta(days=1)

while start_date<end_date: 
    next_date=start_date+datetime.timedelta(days=1)
    #print(next_date)
    
    stock_list=list(get_index_stocks('000300.SH',date=start_date))
    
    #取全市场股票各股票市值因子
    df = get_fundamentals(query(asharevalue.symbol, asharevalue.total_mv).filter(\
                        asharevalue.symbol.in_(stock_list)).order_by(asharevalue.total_mv.asc()),date=start_date)
    df.set_index('asharevalue_symbol',inplace=True)
    df.index.name='symbol'
    
    stocklist_cap_top=list(df[:int(len(df)*0.1)].index)
    stocklist_cap_bottom=list(df[int(len(df)*0.9):].index)
   
    try:
        value_top = get_price(stocklist_cap_top,None, start_date, '1d', \
                              ['quote_rate'],True, None,1,is_panel=1)['quote_rate']
        return_sum_top=value_top.apply(lambda x:x.mean(),axis=1)    
        
        value_bottom = get_price(stocklist_cap_bottom,None, start_date, '1d', \
                                 ['quote_rate'], True, None,1,is_panel=1)['quote_rate']
        return_sum_bottom=value_bottom.apply(lambda x:x.mean(),axis=1) 
     
    except:
        start_date=start_date+datetime.timedelta(days=1)
        continue
  
    if flag1:
        flag1=False
        return_date_total_top = return_sum_top
        return_date_total_bottom=return_sum_bottom
    else:
        return_date_total_top = return_date_total_top.append(return_sum_top)
        return_date_total_bottom = return_date_total_bottom.append(return_sum_bottom)
    
    start_date=start_date+datetime.timedelta(days=1)

In [28]:

return_date_total_top=return_date_total_top.to_frame()
return_date_total_top.columns=['return_daily_top']
return_date_total_bottom=return_date_total_bottom.to_frame()
return_date_total_bottom.columns=['return_daily_bottom']
return_date_total_bottom.head()

Out[28]:

return_daily_bottom
2018-01-101.566003
2018-01-110.002963
2018-01-120.968620
2018-01-151.316387
2018-01-160.259030

In [29]:

#合并风格涨跌幅表和风格下资金流动强度表
data_return_stdflow=pd.concat([new_std_total,return_date_total_top,return_date_total_bottom],axis=1)
data_return_stdflow.head()

Out[29]:

std_flowreturn_daily_topreturn_daily_bottom
2018-01-102.711643-0.9731731.566003
2018-01-11-0.7756240.0473170.002963
2018-01-121.912241-0.5534470.968620
2018-01-152.579242-3.2399031.316387
2018-01-161.8525560.4285700.259030

4.3 Top市值风格涨跌幅与资金流入强度关系图

In [30]:

data_return_stdflow_copy=data_return_stdflow
data_return_stdflow_copy.index=pd.to_datetime(data_return_stdflow_copy.index)
ind = data_return_stdflow_copy.index
width = 0.65
fig, ax = plt.subplots(figsize=(20,8))
rects1=ax.bar(ind, data_return_stdflow_copy['std_flow'], width,  label='std_flow')
ax.set_ylabel('std_flow')
ax=ax.twinx()
rects2 = ax.plot(ind,data_return_stdflow_copy['return_daily_top'],color='IndianRed')
ax.set_ylabel('return_daily_top')
ax.set_title('Top Group & Cashflow Liquidity Strength',size=20)
ax.legend()
plt.show()

image.png

4.4 Bottom市值风格涨跌幅与资金流入强度关系图

In [31]:

data_return_stdflow_copy.index=pd.to_datetime(data_return_stdflow_copy.index)
ind = data_return_stdflow_copy.index
width = 0.65
fig, ax = plt.subplots(figsize=(20,8))
rects1=ax.bar(ind, data_return_stdflow_copy['std_flow'], width,  label='std_flow')
ax.set_ylabel('std_flow')
ax=ax.twinx()
rects2 = ax.plot(ind,data_return_stdflow_copy['return_daily_bottom'],color='IndianRed')
ax.set_ylabel('return_daily_bottom')
ax.set_title('Bottom Group & Cashflow Liquidity Strength',size=20)
ax.legend()
plt.show()

image.png

查看以上策略详情请到supermind量化交易官网查看:同花顺Supermind量化交易 资金面专题2-风格下资金流入强度的分析 附源代码