akshare analyzes daily limit stock data

import package, get date data

import pandas as pd
import numpy as np
import akshare as ak
#画图
import matplotlib.pyplot as plt
#正确显示中文和负号
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
#处理时间
from dateutil.parser import parse
from datetime import datetime,timedelta

 
#获取最新交易日期
#获取交易日历
trade_date = ak.tool_trade_date_hist_sina()
#print(trade_date)
trade_date=trade_date['trade_date'].apply(lambda x:x.strftime('%Y%m%d'))
trade_date
d1=datetime.now().strftime('%Y%m%d')
trade_date=np.array(trade_date)
n1=np.argwhere(trade_date==str(d1))[0][0]+1
#获取最近6年的交易日行情
dates=trade_date[-250*6:n1]

Get daily limit stock data

import time
df=ak.stock_em_zt_pool(date=dates[0])
#print(df)
for date in dates[-100:]:
    print(date)
    df_tem=ak.stock_em_zt_pool(date=date)
    df_tem['date']=date
    #print(df)
    #time.sleep(60*1000/100/2)
    df=pd.concat([df,df_tem])
    #print(df)
df.to_csv('涨停分析.csv')

Feature description

df.iloc[:,1:].describe().round(2)

Data Segmentation and Plotting

def dy_zh(data, cut_points, labels=None): 
    min_num = data.min() 
    max_num = data.max() 
    break_points = [min_num] + cut_points + [max_num]
    print(break_points)
    if not labels: 
        labels = range(len(cut_points)+1)
    else: 
         labels=[labels[i] for i in range(len(cut_points)+1)] 
    dataBin = pd.cut(data,bins=break_points,
         labels=labels,include_lowest=True)    
    return dataBin 
 
cut_points = [10,30,50] 
labels=['10元以下', '10-30元','30-50元','50-100元'] 
df = df.sort_values(by='最新价')
print(df['最新价'])
#调用函数dy_zh,增加新列
df['价格区间'] = dy_zh(df['最新价'], cut_points, labels) 
#查看标签列,取值范围前面加上了序号,是便于后面生成表格时按顺序排列
#df.head()

group_price=df.groupby('价格区间')['date'].count()

plt.figure(figsize=(12,5))
colors=['#1f77b4','#ff7f0e','#2ca02c','#d62728','#9467bd','#8c564b']
fig=plt.bar(group_price.index,group_price.values,color=colors[:5]);
#自动添加标签
def autolabel(fig):
    for f in fig:
        h=f.get_height()
        plt.text(f.get_x()+f.get_width()/2,1.02*h,
        f'{int(h)}',ha='center',va='bottom')
autolabel(fig)

View daily limit data

def plot_bar(group_data):
    plt.figure(figsize=(16,5))
    fig=plt.bar(group_data.index,group_data.values);
    autolabel(fig)
    plt.title('2016-2021涨停板排名前20',size=15);

group_name=df.groupby('名称')['代码'].count().sort_values(ascending=False)[:20]
plot_bar(group_name)
 

#分别剔除ST、*ST和新股(N开头)
df_st=df[-(df['名称'].str.startswith('ST') | df['名称'].str.startswith('*ST')|df['名称'].str.startswith('N'))]
group_name_st=df_st.groupby('名称')['代码'].count().sort_values(ascending=False)[:20]
plot_bar(group_name_st)
 


#使用0.5.11版本的pyecharts
from pyecharts import Bar
count_=df.groupby('date')['date'].count()
attr=count_.index
v1=count_.values
bar=Bar('每日涨停板个数','2016-2021',title_text_size=15)
bar.add('',attr,v1,is_splitline_show=False,is_datazoom_show=True,linewidth=2)
bar

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324323096&siteId=291194637