一般而言,当市场中资金流动强度平缓时,风格投资者保持原有的风格方向;当资金流动强度强烈时,风格投资者转换原有的风格方向,例如:小市值投资者在资金流动强烈时,会转向大市值风格。本篇文章将从规模因子(大小盘风格)入手,研究关于资金流动强度与风格投资的话题。
风格下资金流入强度的分析
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_all | sell_all | data_net | |
|---|---|---|---|
| 2018-12-26 | 108688097.0 | 176514956.0 | -67826859.0 |
| 2018-12-27 | 191021237.0 | 258350579.0 | -67329342.0 |
| 2018-12-28 | 249609737.0 | 161550282.0 | 88059455.0 |
| 2019-01-02 | 115789628.0 | 270478617.0 | -154688989.0 |
| 2019-01-03 | 173588438.0 | 115469752.0 | 58118686.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_factor | data_net_inflow | data_volume | |
|---|---|---|---|
| 0 | 4.776260e+11 | -4.869376e+08 | 1.257051e+09 |
| 1 | 6.446934e+11 | -1.012772e+09 | 1.832052e+09 |
| 2 | 8.006962e+11 | -7.643456e+08 | 2.153484e+09 |
| 3 | 9.747939e+11 | -9.009924e+08 | 2.541013e+09 |
| 4 | 1.179759e+12 | -1.555130e+09 | 2.463412e+09 |
| 5 | 1.447312e+12 | -1.324253e+09 | 2.500842e+09 |
| 6 | 1.927586e+12 | -1.900550e+09 | 3.013475e+09 |
| 7 | 2.735843e+12 | -1.816132e+09 | 3.432070e+09 |
| 8 | 4.709769e+12 | -3.016223e+09 | 4.757325e+09 |
| 9 | 2.866271e+13 | -4.227482e+09 | 7.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_factor | data_net_inflow | data_volume | inflow_by_volume | |
|---|---|---|---|---|
| 0 | 5.286407e+11 | -2.581254e+08 | 8.066170e+08 | -0.320010 |
| 1 | 6.379624e+11 | -5.345425e+08 | 6.639116e+08 | -0.805141 |
| 2 | 7.397299e+11 | -2.947834e+08 | 7.246609e+08 | -0.406788 |
| 3 | 8.484290e+11 | -3.367236e+08 | 6.470733e+08 | -0.520379 |
| 4 | 9.987154e+11 | -2.293879e+08 | 4.208110e+08 | -0.545109 |
| 5 | 1.266877e+12 | 2.328543e+08 | 6.338160e+08 | 0.367385 |
| 6 | 1.651160e+12 | -3.330558e+08 | 8.245164e+08 | -0.403941 |
| 7 | 2.343153e+12 | -4.061579e+08 | 1.258775e+09 | -0.322661 |
| 8 | 4.038133e+12 | -1.200875e+09 | 9.095614e+08 | -1.320279 |
| 9 | 1.485925e+13 | -9.858907e+08 | 1.104909e+09 | -0.892282 |
| Col_sum | 5.582410e+13 | -8.693376e+09 | 1.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_factor | data_net_inflow | data_volume | inflow_by_volume | |
|---|---|---|---|---|
| 0 | 4.776260e+11 | -4.869376e+08 | 1.257051e+09 | -0.387365 |
| 1 | 6.446934e+11 | -1.012772e+09 | 1.832052e+09 | -0.552808 |
| 2 | 8.006962e+11 | -7.643456e+08 | 2.153484e+09 | -0.354934 |
| 3 | 9.747939e+11 | -9.009924e+08 | 2.541013e+09 | -0.354580 |
| 4 | 1.179759e+12 | -1.555130e+09 | 2.463412e+09 | -0.631291 |
| 5 | 1.447312e+12 | -1.324253e+09 | 2.500842e+09 | -0.529523 |
| 6 | 1.927586e+12 | -1.900550e+09 | 3.013475e+09 | -0.630684 |
| 7 | 2.735843e+12 | -1.816132e+09 | 3.432070e+09 | -0.529165 |
| 8 | 4.709769e+12 | -3.016223e+09 | 4.757325e+09 | -0.634017 |
| 9 | 2.866271e+13 | -4.227482e+09 | 7.884248e+09 | -0.536193 |
| Col_sum | 4.356079e+13 | -1.700482e+10 | 3.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-10 | 2.711643 |
| 2018-01-11 | -0.775624 |
| 2018-01-12 | 1.912241 |
| 2018-01-15 | 2.579242 |
| 2018-01-16 | 1.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-10 | 1.566003 |
| 2018-01-11 | 0.002963 |
| 2018-01-12 | 0.968620 |
| 2018-01-15 | 1.316387 |
| 2018-01-16 | 0.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_flow | return_daily_top | return_daily_bottom | |
|---|---|---|---|
| 2018-01-10 | 2.711643 | -0.973173 | 1.566003 |
| 2018-01-11 | -0.775624 | 0.047317 | 0.002963 |
| 2018-01-12 | 1.912241 | -0.553447 | 0.968620 |
| 2018-01-15 | 2.579242 | -3.239903 | 1.316387 |
| 2018-01-16 | 1.852556 | 0.428570 | 0.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()
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()
查看以上策略详情请到supermind量化交易官网查看:同花顺Supermind量化交易 资金面专题2-风格下资金流入强度的分析 附源代码