复制链接
克隆策略
In [58]:
instruments = ['601700.SHA', '000002.SZA', '000333.SZA']
#这里尽量包含从上市日期开始到最后的数据
df_all = D.history_data(instruments, start_date='2022-01-01', end_date='2022-08-26',
fields=['open','close','adjust_factor','turn','volume']) #获取历史数据
In [59]:
def cal_concentration_ratio(df_all):
    concentration_ratio_lst=[]
    for k in list(df_all.date):
        df=df_all[df_all.date<=k]
        
        df['real_close']=df['close']/df['adjust_factor'] #获取真实收盘价
        df['real_open']=df['open']/df['adjust_factor'] #获取真实开盘价
        df['turn']=df['turn']/100 #获取换手率
        df['avg_price']=np.round(df['real_close']+df['real_open'])/2 #计算每日平均成本,这里按照0.5元一个价位做分析

        df=df.sort_values(by='date',ascending=False).reset_index(drop=True) #日期按降序排列
        df['turn_tomo']=df['turn'].shift(1) #计算明日的换手率
        df['remain_day']=1-df['turn_tomo'] #计算当日的剩余筹码比例
        #假设N日后,上市第一天的剩余筹码比率就是每日剩余比例的累乘即:剩余筹码比例=(1-明天换手率)*(1-后日换手率)*...*(1-最新日换手率),以此类推各日的剩余筹码
        df['remain_his']=df['remain_day'].cumprod()*df['turn'] 
        df['remain_his']=df['remain_his'].fillna(df['turn']) #最新一日的筹码就是当日的换手率

        #关键统计,统计最后一天的各价位历史筹码堆积量(百分比)
        ss=df.groupby('avg_price')[['remain_his']].sum().rename(columns={'remain_his':'筹码量'})
        ss['筹码累积量']=ss['筹码量'].cumsum()
        sp = ss[ss['筹码累积量']<0.9].head(1).index[0]
        ep =  ss[ss['筹码累积量']<0.9].tail(1).index[0]

        concentration_ratio = (ep-sp)/(ep+sp)
       
        concentration_ratio_lst.append(concentration_ratio)
    result=pd.DataFrame({'concentration_ratio':concentration_ratio_lst},index=df_all.date)
    return result

concentration_ratio_df=df_all.groupby('instrument').apply(cal_concentration_ratio).reset_index()
In [60]:
concentration_ratio_df[concentration_ratio_df['concentration_ratio'] < 0.1]
Out[60]:
instrument date concentration_ratio
0 000002.SZA 2022-01-04 0.000000
1 000002.SZA 2022-01-05 0.024390
2 000002.SZA 2022-01-06 0.024390
3 000002.SZA 2022-01-07 0.036145
4 000002.SZA 2022-01-10 0.047619
... ... ... ...
381 601700.SHA 2022-04-14 0.047619
382 601700.SHA 2022-04-15 0.047619
383 601700.SHA 2022-04-18 0.047619
384 601700.SHA 2022-04-19 0.047619
385 601700.SHA 2022-04-20 0.047619

110 rows × 3 columns