主要统计当日市场指数基本情况、涨停板的情况,空间板数,个股所属行业情况,当前市场的主线,市场成交的基本情况等等,一目了然,看完之后对当前市场有个基本的把控,非常有利于复盘。欢迎高手批评指正,有啥好的建议或者意见可以提出来,我再加进去
#分析指数近期走势
import pandas as pd
zhishu_list=['000001.XSHG','000016.XSHG','000300.XSHG','000905.XSHG','399001.XSHE','399005.XSHE','399006.XSHE','399678.XSHE']
zhishu_close=[]
zhishu_pct=[]
zhishu_pct5=[]
zhishu_pct3=[]
zhishu_money=[]
zhishu_money3=[]
zhishu_money5=[]
zhishu_volume=[]
zhishu_volume3=[]
zhishu_volume5=[]
for zhishu in zhishu_list:
zhishu_price=get_price(zhishu,end_date='2019-01-08',count=6,fields=['close','volume','money'])
#指数值
zhishu_close.append(zhishu_price['close'][-1])
#涨跌幅度%
zhishu_pct.append(round((zhishu_price['close'][-1]/zhishu_price['close'][-2]-1)*100,2))
zhishu_pct3.append(round((zhishu_price['close'][-1]/zhishu_price['close'][-3]-1)*100,2))
zhishu_pct5.append(round((zhishu_price['close'][-1]/zhishu_price['close'][-6]-1)*100,2))
#成交量(亿)
zhishu_volume.append(round(zhishu_price['volume'][-1]/(10**8),4))
zhishu_volume3.append(round((zhishu_price['volume'][-3:].mean())/(10**8),4))
zhishu_volume5.append(round((zhishu_price['volume'][-5:].mean())/(10**8),4))
#成交额
zhishu_money.append(round(zhishu_price['money'][-1]/(10**8),2))
zhishu_money3.append(round(zhishu_price['money'][-3:].mean()/(10**8),2))
zhishu_money5.append(round(zhishu_price['money'][-5:].mean()/(10**8),2))
df_zhishu=pd.DataFrame(index=['上证指数','上证50','沪深300','中证500','深成指','中小板指','创业板指','深次新股'],\
columns=['指数值','涨跌幅%','3日涨跌幅%','5日涨跌幅%','成交量(亿)','3日成交均量(亿)',\
'5日成交均量(亿)','成交额(亿)','3日成交均额(亿)','5日成交均额(亿)'])
df_zhishu['指数值']=zhishu_close
df_zhishu['涨跌幅%']=zhishu_pct
df_zhishu['3日涨跌幅%']=zhishu_pct3
df_zhishu['5日涨跌幅%']=zhishu_pct5
df_zhishu['成交量(亿)']=zhishu_volume
df_zhishu['3日成交均量(亿)']=zhishu_volume3
df_zhishu['5日成交均量(亿)']=zhishu_volume5
df_zhishu['成交额(亿)']=zhishu_money
df_zhishu['3日成交均额(亿)']=zhishu_money3
df_zhishu['5日成交均额(亿)']=zhishu_money5
df_zhishu
import pandas as pd
pd.set_option('precision', 8)
pd.set_option('display.width',1000)
import matplotlib.pyplot as plt
plt.style.use('ggplot')
df=get_all_securities(types=['stock'],date='2019-01-08')
#df=get_fundamentals(query(valuation.code))
#print (len(df['code'])) #两市股票数量
u10=[]
u7=[]
u5=[]
u3=[]
u0=[]
u_0=[]
u_3=[]
u_5=[]
u_7=[]
u_10=[]
uuv=[]
uus=[]
uust=[]
uuzhang=[]
uuzhang2=[]
uunost=[]
uu_5=[]
for stock in list(df.index):
df2=get_price(stock,end_date='2019-01-08',count=6,frequency='daily',\
fields=['open','close','volume','high','low','money','high_limit','low_limit'],\
fq='pre')
if df2['close'][-1]==df2['high_limit'][-1]: #没有排除掉当前已经停牌的票,包含ST个股
u10.append(stock)
elif df2['close'][-1]>=1.07*df2['close'][-2] and df2['close'][-1]<df2['high_limit'][-1]:
u7.append(stock) #7个点以上涨幅个股
elif df2['close'][-1]>=1.05*df2['close'][-2] and df2['close'][-1]<1.07*df2['close'][-2]:
u5.append(stock) #5个点以上涨幅个股
elif df2['close'][-1]>=1.03*df2['close'][-2] and df2['close'][-1]<1.05*df2['close'][-2]:
u3.append(stock) #3个点以上涨幅个股
elif df2['close'][-1]>=df2['close'][-2] and df2['close'][-1]<1.03*df2['close'][-2]:
u0.append(stock) #0个点以上涨幅个股数
elif df2['close'][-1]==df2['low_limit'][-1]:
u_10.append(stock) #跌停个股数
elif df2['close'][-1]>df2['low_limit'][-1] and df2['close'][-1]<=0.93*df2['close'][-2]:
u_7.append(stock) #7个点以上跌幅个股数
elif df2['close'][-1]>0.93*df2['close'][-2] and df2['close'][-1]<=0.95*df2['close'][-2]:
u_5.append(stock) #5个点以上跌幅个股数
elif df2['close'][-1]>0.95*df2['close'][-2] and df2['close'][-1]<=0.97*df2['close'][-2]:
u_3.append(stock) #3个点以上跌幅个股数
elif df2['close'][-1]>0.97*df2['close'][-2] and df2['close'][-1]<df2['close'][-2]:
u_0.append(stock) #0个点以上跌幅个股数
if df2['close'][-1]<df2['high_limit'][-1] and df2['high'][-1]==df2['high_limit'][-1]: #当日炸板股票
uuv.append(stock)
if df2['volume'][-1]==df2['money'][-1]==0: #当日停盘股票
uus.append(stock)
#涨停票和停牌票合在一起处理
if df2['close'][-1]==df2['high_limit'][-1] and df2['volume'][-1]>0 and df2['money'][-1]>0:
uuzhang.append(stock) #得到今日涨停个股
if df2['close'][-1]==df2['high_limit'][-1] and df2['close'][-1]<1.07*df2['close'][-2]:
uust.append(stock) #st涨停个股
if df2['close'][-2]==df2['high_limit'][-2] and df2['volume'][-2]>0 and df2['money'][-2]>0 \
and df2['close'][-2]>1.06*df2['close'][-3]:
uuzhang2.append(stock) #昨日涨停个股
if df2['close'][-1]==df2['high_limit'][-1] and df2['volume'][-1]>0 and df2['money'][-1]>0 \
and df2['close'][-1]>1.07*df2['close'][-2]: #得到涨停且非st个股
uunost.append(stock)
#5个点以上个股,用于分析5个点以上个股所属行业
if df2['close'][-1]>=1.05*df2['close'][-2] and df2['volume'][-1]>0 and df2['money'][-1]>0:
uu_5.append(stock)
#print (len(uunost))
#当日涨停个股数(包含st)
uuust=len(u10)-len(uus)
#当日涨停个股(不包含st)
uuu=len(u10)-len(uus)-len(uust)
#当日触板个股总数
uuc=len(uuzhang)+len(uuv)
#当日炸板率
uuz=round((len(uuv)/uuc*100),2)
df_zhangfu=pd.DataFrame(index=['涨停个股(包含ST)','炸板个股','7个点以上涨幅','5-7个点涨幅','3-5个点涨幅','0-3个点涨幅','跌停个股数(包含ST)','0-3个点跌幅','3-5个点跌幅','5-7个点跌幅','7个点以上跌幅'],\
columns=['个数统计'])
df_zhangfu['个数统计']=[len(uuzhang),len(uuv),len(u7),len(u5),len(u3),len(u0),len(u_10),len(u_0),len(u_3),len(u_5),len(u_7)]
df_zhangfu
#今日涨停个股板数统计
uuu1=[]
uuu2=[]
uuu3=[]
uuu4=[]
uuu5=[]
uuu6=[]
uuu7=[]
uuu8=[]
uuu9=[]
uuu10=[]
for stock in uunost:
df2_1=get_price(stock,end_date='2019-01-08',count=10,frequency='daily',\
fields=['open','close','volume','high','low','money','high_limit','low_limit'],\
fq='pre')
if df2_1['close'][-2]<df2_1['high_limit'][-2]:
uuu1.append(stock)
if df2_1['close'][-2]==df2_1['high_limit'][-2] and df2_1['close'][-3]<df2_1['high_limit'][-3]:
uuu2.append(stock)
if df2_1['close'][-2]==df2_1['high_limit'][-2] and df2_1['close'][-3]==df2_1['high_limit'][-3] \
and df2_1['close'][-4]<df2_1['high_limit'][-4]:
uuu3.append(stock)
if df2_1['close'][-2]==df2_1['high_limit'][-2] and df2_1['close'][-3]==df2_1['high_limit'][-3] and \
df2_1['close'][-4]==df2_1['high_limit'][-4] and df2_1['close'][-5]<df2_1['high_limit'][-5]:
uuu4.append(stock)
if df2_1['close'][-2]==df2_1['high_limit'][-2] and df2_1['close'][-3]==df2_1['high_limit'][-3] and \
df2_1['close'][-4]==df2_1['high_limit'][-4] and df2_1['close'][-5]==df2_1['high_limit'][-5] and \
df2_1['close'][-6]<df2_1['high_limit'][-6]:
uuu5.append(stock)
if df2_1['close'][-2]==df2_1['high_limit'][-2] and df2_1['close'][-3]==df2_1['high_limit'][-3] and \
df2_1['close'][-4]==df2_1['high_limit'][-4] and df2_1['close'][-5]==df2_1['high_limit'][-5] and \
df2_1['close'][-6]==df2_1['high_limit'][-6] and df2_1['close'][-7]<df2_1['high_limit'][-7]:
uuu6.append(stock)
if df2_1['close'][-2]==df2_1['high_limit'][-2] and df2_1['close'][-3]==df2_1['high_limit'][-3] and \
df2_1['close'][-4]==df2_1['high_limit'][-4] and df2_1['close'][-5]==df2_1['high_limit'][-5] and \
df2_1['close'][-6]==df2_1['high_limit'][-6] and df2_1['close'][-7]==df2_1['high_limit'][-7] and \
df2_1['close'][-8]<df2_1['high_limit'][-8]:
uuu7.append(stock)
if df2_1['close'][-2]==df2_1['high_limit'][-2] and df2_1['close'][-3]==df2_1['high_limit'][-3] and \
df2_1['close'][-4]==df2_1['high_limit'][-4] and df2_1['close'][-5]==df2_1['high_limit'][-5] and \
df2_1['close'][-6]==df2_1['high_limit'][-6] and df2_1['close'][-7]==df2_1['high_limit'][-7] and \
df2_1['close'][-8]==df2_1['high_limit'][-8] and df2_1['close'][-9]<df2_1['high_limit'][-9]:
uuu8.append(stock)
if df2_1['close'][-2]==df2_1['high_limit'][-2] and df2_1['close'][-3]==df2_1['high_limit'][-3] and \
df2_1['close'][-4]==df2_1['high_limit'][-4] and df2_1['close'][-5]==df2_1['high_limit'][-5] and \
df2_1['close'][-6]==df2_1['high_limit'][-6] and df2_1['close'][-7]==df2_1['high_limit'][-7] and \
df2_1['close'][-8]==df2_1['high_limit'][-8] and df2_1['close'][-9]==df2_1['high_limit'][-9] and \
df2_1['close'][-10]<df2_1['high_limit'][-10]:
uuu9.append(stock)
if df2_1['close'][-2]==df2_1['high_limit'][-2] and df2_1['close'][-3]==df2_1['high_limit'][-3] and \
df2_1['close'][-4]==df2_1['high_limit'][-4] and df2_1['close'][-5]==df2_1['high_limit'][-5] and \
df2_1['close'][-6]==df2_1['high_limit'][-6] and df2_1['close'][-7]==df2_1['high_limit'][-7] and \
df2_1['close'][-8]==df2_1['high_limit'][-8] and df2_1['close'][-9]==df2_1['high_limit'][-9] and \
df2_1['close'][-10]==df2_1['high_limit'][-10] and df2_1['close'][-11]<df2_1['high_limit'][-11]:
uuu10.append(stock)
df_zhangtingtongji=pd.DataFrame(index=['今日涨停个股数','首板个股数','二板个股数','三板个股数','四板个股数','五板个股数',\
'六板个股数','七板个股数','八板个股数',\
'九板个股数','十板个股数'],columns=['个数统计'])
df_zhangtingtongji['个数统计']=[len(uunost),len(uuu1),len(uuu2),len(uuu3),len(uuu4),\
len(uuu5),len(uuu6),len(uuu7),len(uuu8),\
len(uuu9),len(uuu10)]
df_zhangtingtongji
#涨停个股行业统计,扣除掉刚上市的新股吧,不然拉不出来新股的行业
import pandas as pd
industry_name=[]
code=[]
#print (uunost)
#uunost=uunost.remove('600310.XSHG')
#print (uunost)
for stock in uunost:
df3=get_industry(stock,date='2019-01-08')
code.append(stock)
industry_name.append(df3[stock]['zjw']['industry_name'])
#print (df3[stock])
#print (df3[stock]['sw_l2']['industry_name'],stock)
#print (pd.Series(code))
#print (pd.Series(industry_name))
#加一列,便于个数统计
df_1=[]
for i in range(len(uunost)):
df_1.append(1)
#print (df_1)
df_industry=pd.DataFrame({'股票代码':pd.Series(code),'涨停个股所属行业':pd.Series(industry_name),'计数器':pd.Series(df_1)}) #顺利合成两列数据,便于分析涨停板所属行业
#print (df_industry)
df_industry_name=df_industry.groupby(df_industry['涨停个股所属行业']).sum().sort_values('计数器',ascending=False).head(10) #可以直接看出那个行业比较强势
df_industry_name #用软件来分类和处理数据吧
#昨日涨停个股今日表现统计
gaokai=[]
dikai=[]
jinrilianban=[]
shangzhang=[]
xiadie=[]
jinridieting=[]
jinrizhaban=[]
for stock in uuzhang2:
df2_2=get_price(stock,end_date='2019-01-08',count=6,frequency='daily',\
fields=['open','close','volume','high','low','money','high_limit','low_limit'],\
fq='pre')
if df2_2['open'][-1]>df2_2['close'][-2]:
gaokai.append(stock)
elif df2_2['open'][-1]<=df2_2['close'][-2]:
dikai.append(stock)
if df2_2['close'][-1]==df2_2['high_limit'][-1] and df2_2['volume'][-1]>0 and df2_2['money'][-1]>0:
jinrilianban.append(stock)
if df2_2['high'][-1]==df2_2['high_limit'][-1] and df2_2['close'][-1]<df2_2['high_limit'][-1]:
jinrizhaban.append(stock)
if df2_2['close'][-1]>df2_2['close'][-2]:
shangzhang.append(stock)
elif df2_2['close'][-1]<=df2_2['close'][-2]:
xiadie.append(stock)
if df2_2['close'][-1]==df2_2['low_limit'][-1]:
jinridieting.append(stock)
df_zhangting=pd.DataFrame(index=['昨日涨停个股数','高开率','低开率','连板个股','炸板个股','上涨','下跌','跌停',\
'上涨概率','下跌概率'],columns=['个数统计'])
df_zhangting['个数统计']=[len(uuzhang2),round(len(gaokai)/len(uuzhang2),2),round(len(dikai)/len(uuzhang2),2),len(jinrilianban),\
len(jinrizhaban),len(shangzhang),len(xiadie),len(jinridieting),round(len(shangzhang)/len(uuzhang2),2),\
round(len(xiadie)/len(uuzhang2),2)]
df_zhangting
#昨日涨停个股今日收盘表现
uuz10=[]
uuz7=[]
uuz5=[]
uuz3=[]
uuz0=[]
uuz_0=[]
uuz_3=[]
uuz_5=[]
uuz_7=[]
uuz_10=[]
for stock in uuzhang2:
df2_3=get_price(stock,end_date='2019-01-08',count=6,frequency='daily',\
fields=['open','close','volume','high','low','money','high_limit','low_limit'],\
fq='pre')
if df2_3['close'][-1]==df2_3['high_limit'][-1]:
uuz10.append(stock)
if df2_3['close'][-1]<df2_3['high_limit'][-1] and df2_3['close'][-1]>=1.07*df2_3['close'][-2]:
uuz7.append(stock)
if df2_3['close'][-1]<1.07*df2_3['close'][-2] and df2_3['close'][-1]>=1.05*df2_3['close'][-2]:
uuz5.append(stock)
if df2_3['close'][-1]<1.05*df2_3['close'][-2] and df2_3['close'][-1]>=1.03*df2_3['close'][-2]:
uuz3.append(stock)
if df2_3['close'][-1]<1.03*df2_3['close'][-2] and df2_3['close'][-1]>=1.00*df2_3['close'][-2]:
uuz0.append(stock)
if df2_3['close'][-1]<df2_3['close'][-2] and df2_3['close'][-1]>=0.97*df2_3['close'][-2]:
uuz_0.append(stock)
if df2_3['close'][-1]<0.97*df2_3['close'][-2] and df2_3['close'][-1]>=0.95*df2_3['close'][-2]:
uuz_3.append(stock)
if df2_3['close'][-1]<0.95*df2_3['close'][-2] and df2_3['close'][-1]>=0.93*df2_3['close'][-2]:
uuz_5.append(stock)
if df2_3['close'][-1]<0.93*df2_3['close'][-2] and df2_3['close'][-1]>df2_3['low_limit'][-1]:
uuz_7.append(stock)
if df2_3['close'][-1]==df2_3['low_limit'][-1]:
uuz_10.append(stock)
df_zuorizhangting=pd.DataFrame(index=['昨日涨停个股数','今日连板','7个点以上涨幅','5-7个点涨幅','3-5个点涨幅','0-3个点涨幅',\
'0-3个点跌幅','3-5个点跌幅','5-7个点跌幅','7个点以上跌幅','今日跌停'],columns=['个数统计'])
df_zuorizhangting['个数统计']=[len(uuzhang2),len(uuz10),len(uuz7),len(uuz5),len(uuz3),len(uuz0),len(uuz_0),len(uuz_3),len(uuz_5),\
len(uuz_7),len(uuz_10)]
df_zuorizhangting
#寻找5个点以上涨幅个股所属行业
import pandas as pd
industry_name=[]
code=[]
for stock in uu_5:
df3=get_industry(stock,date='2019-01-08')
code.append(stock)
industry_name.append(df3[stock]['zjw']['industry_name'])
#print (df3[stock]['zjw']['industry_name'])
#print (pd.Series(code))
#print (pd.Series(industry_name))
#加一列,便于个数统计
df_1=[]
for i in range(len(uu_5)):
df_1.append(1)
#print (df_1)
df_industry=pd.DataFrame({'股票代码':pd.Series(code),'5个点以上涨幅个股所属行业':pd.Series(industry_name),'计数器':pd.Series(df_1)}) #顺利合成两列数据,便于分析个股所属行业
#print (df_industry)
df_industry_name=df_industry.groupby(df_industry['5个点以上涨幅个股所属行业']).sum().sort_values('计数器',ascending=False).head(10) #可以直接看出那个行业比较强势
df_industry_name #用软件来分类和处理数据吧
import pandas as pd
import numpy as np
df=get_all_securities(types=['stock'],date='2019-01-08')
#df['code']
newhigh_20=[] #20日新高
newhigh_60=[] #60日新高
newhigh_250=[] #250日新高,也就是一年新高
newhigh_10years=[] #10年新高,一般也就意味着是历史新高了
for stock in list(df.index):
df4=get_price(stock,end_date='2019-01-07',count=3000,frequency='daily',fields=['close','money'],fq='pre')
if df4['close'].max()==df4['close'][-1] and df4['money'][-1]>0: #10年新高个股,扣除停牌股
newhigh_10years.append(stock)
if df4['close'][-20:].max()==df4['close'][-1] and df4['money'][-1]>0:
newhigh_20.append(stock)
if df4['close'][-60:].max()==df4['close'][-1] and df4['money'][-1]>0:
newhigh_60.append(stock)
if df4['close'][-250:].max()==df4['close'][-1] and df4['money'][-1]>0:
newhigh_250.append(stock)
df_newhigh_20=pd.DataFrame({'20日新高':newhigh_20})
df_xingao=pd.DataFrame(index=['20日新高','60日新高','一年新高','历史新高'],columns=['个数统计'])
df_xingao['个数统计']=[len(newhigh_20),len(newhigh_60),len(newhigh_250),len(newhigh_10years)]
df_xingao
df_newhigh_250=pd.DataFrame({'一年新高':newhigh_250})
df_newhigh_250
df_newhigh_20=pd.DataFrame({'20日新高':newhigh_20})
df_newhigh_20
df_newhigh_60=pd.DataFrame({'60日新高':newhigh_60})
df_newhigh_60
#统计出今日市场成交情况所占比例
import pandas as pd
def get_money_statistics(date):
money_500=[]
money_1000=[]
money_3000=[]
money_5000=[]
money_10000=[]
money_50000=[]
money_5up=[]
money_10up=[]
df=get_all_securities(types=['stock'],date=date)
for stock in list(df.index):
df5=get_price(stock,end_date=date,count=2,frequency='daily',fields=['money'],fq='pre')
if df5['money'][-1]<5*10**6:
money_500.append(stock)
if df5['money'][-1]<10**7 and df5['money'][-1]>=5*10**6:
money_1000.append(stock)
if df5['money'][-1]<3*10**7 and df5['money'][-1]>=10**7:
money_3000.append(stock)
if df5['money'][-1]<5*10**7 and df5['money'][-1]>=3*10**7:
money_5000.append(stock)
if df5['money'][-1]<10**8 and df5['money'][-1]>=5*10**7:
money_10000.append(stock)
if df5['money'][-1]<5*10**8 and df5['money'][-1]>=10**8:
money_50000.append(stock)
if df5['money'][-1]<10**9 and df5['money'][-1]>=5*10**8:
money_5up.append(stock)
if df5['money'][-1]>=10**9:
money_10up.append(stock)
df_money=pd.DataFrame(index=['500万以下','500-1000万','1000万-3000万','3000万-5000万','5000万-1亿','1亿-5亿','5亿-10亿','10亿以上'],\
columns=['成交额个股数','个股所占比例'])
df_money['成交额个股数']=[len(money_500),len(money_1000),len(money_3000),len(money_5000),len(money_10000),len(money_50000),len(money_5up),len(money_10up)]
df_money['个股所占比例']=round(df_money['成交额个股数']/len(df.index),4)
return df_money
get_money_statistics('2019-01-08')
#今日涨停个股价位区间分布
import pandas as pd
uz10=[]
uz20=[]
uz30=[]
uz40=[]
for stock in uunost:
df2_3=get_price(stock,end_date='2019-01-08',count=10,frequency='daily',\
fields=['open','close'],\
fq='pre')
if df2_3['close'][-1]<=10:
uz10.append(stock)
if df2_3['close'][-1]<=20 and df2_3['close'][-1]>10:
uz20.append(stock)
if df2_3['close'][-1]<=30 and df2_3['close'][-1]>20:
uz30.append(stock)
if df2_3['close'][-1]>30:
uz40.append(stock)
df_closefenbu=pd.DataFrame(index=['10元以内','20元以内','30元以内','30元以上'],columns=['涨停个股数','个股所占总涨停数的比例'])
df_closefenbu['涨停个股数']=[len(uz10),len(uz20),len(uz30),len(uz40)]
df_closefenbu['个股所占总涨停数的比例']=round(df_closefenbu['涨停个股数']/len(uunost),4)
df_closefenbu
#截止到当前日期的交易日列表,涨停个股数,反应的是当前市场的情绪
def get_trade_days(date):
df=get_price('000001.XSHG',end_date=date,count=30,frequency='daily',fields=['close'],fq='pre')
return df['close'].index
#截止到当前日期的涨停个股走势图,看出市场的情绪
import pandas as pd
def get_zhangting(date):
uuzzz=[]
for i in get_trade_days(date):
df2=get_all_securities(types=['stock'],date=i)
uuzz=[]
for stock in list(df2.index):
df3=get_price(stock,end_date=i,count=1,frequency='daily',fields=['close','high_limit','money'],fq='pre')
if df3['close'][-1]==df3['high_limit'][-1] and df3['money'][-1]>0:
uuzz.append(stock)
uuzzz.append(len(uuzz))
df_uuzz=pd.DataFrame(index=get_trade_days(date),columns=['涨停个股数'])
df_uuzz['涨停个股数']=uuzzz
return df_uuzz.plot()
get_zhangting('2019-01-08') #市场情绪指标走势图