import pandas as pd
from functools import reduce
import numpy as np
import matplotlib.pyplot as plt
import talib as tl
HY_ZY = ['HY001','HY002','HY003','HY004','HY005','HY006','HY007','HY008','HY009','HY010','HY011']
HY_CN=['能源','材料', '工业', '可选消费指', '日常消费', '医疗保健', '金融', '信息技术', '电信服务', '公用事业',
'房地产']
SW_ZY_num= [801010, 801020, 801030, 801040, 801050, 801080, 801110, 801120,
801130, 801140, 801150, 801160, 801170, 801180, 801200, 801210,
801230, 801710, 801720, 801730, 801740, 801750, 801760, 801770,
801780, 801790, 801880, 801890]
SW_ZY=[str(i) for i in SW_ZY_num]
SW_CN=['农林牧渔I', '采掘I', '化工I', '钢铁I', '有色金属I', '电子I', '家用电器I', '食品饮料I',
'纺织服装I', '轻工制造I', '医药生物I', '公用事业I', '交通运输I', '房地产I', '商业贸易I',
'休闲服务I', '综合I', '建筑材料I', '建筑装饰I', '电气设备I', '国防军工I', '计算机I', '传媒I',
'通信I', '银行I', '非银金融I', '汽车I', '机械设备I']
df=pd.DataFrame(SW_CN,index=SW_ZY,columns=['行业分类'])
plt.style.use('ggplot')
list_bili=[]
list_macdz_bili=[]
list_macds_bili=[]
list1=[]
for i in SW_ZY:
list_i=get_industry_stocks(i,date='2017-6-2')
a,b,c=0,0,0
for j in list_i:
stock=j
security_data = get_price(stock,end_date='2017-6-2',frequency='daily',fields=['close'],fq='pre',skip_paused=True,count=200)
close = security_data['close'].values
macd=tl.MACD(close,fastperiod=12,slowperiod=26,signalperiod=9)[2]
if macd[-1]>0:
b+=1
if macd[-2]<=0:
c+=1
ma5=close[-5:].mean()
if close[-1]>=ma5:
a += 1
list_macdz_bili.append(c/len(list_i))
list_macds_bili.append(b/len(list_i))
list1.append(i)
list_bili.append(a/len(list_i))
df['bili']=pd.Series(list_bili,index=df.index)
df['macdz_bili']=pd.Series(list_macdz_bili,index=df.index)
df['macds_bili']=pd.Series(list_macds_bili,index=df.index)
df.index=df['行业分类']
df=df.sort('bili',ascending=False)
se=df['bili']
fig=plt.figure(figsize=(20,10))
ax1=fig.add_subplot(221)
ax1.set_ylim([0,1])
x=np.linspace(0,30,30)
y=[0.5]*30
ax1.plot(x,y,color='green',alpha=0.3)
ax1.set_title('各行业成分股高于5日均线占比')
se.plot(ax=ax1,kind='bar',color='red',alpha=0.7,grid=True,width=0.8)
df=df.sort('macds_bili',ascending=False)
se1=df['macds_bili']
ax2=fig.add_subplot(222)
ax2.set_title('macd为正占比')
se1.plot(ax=ax2,kind='bar',color='red',alpha=0.5,grid=True,width=0.8)
#df=df.sort('macds_bili',ascending=False)
se2=df['macdz_bili']
#ax2=fig.add_subplot(222)
#ax2.set_title('macd为正的个数')
se2.plot(ax=ax2,kind='bar',color='red',alpha=0.5,grid=True,width=0.8)
#plt.bar(left=range(len(se)),height=se.values,color='red')
<matplotlib.axes._subplots.AxesSubplot at 0x7f534d74ae80>
#波动率
df5=get_price('399006.XSHE',end_date='2017-6-2',fields=['close','low','high'],count=200)
tt=tl.ATR(df5['high'].values,df5['low'].values,df5['close'].values,timeperiod=12)
df5['tt']=pd.Series(tt,index=df5.index)
se5=df5['tt']
se5_fudu=(df5['high']-df5['low'])/df5['close']
df6=get_price('000905.XSHG',end_date='2017-6-2',fields=['close','low','high'],count=200)
tt=tl.ATR(df6['high'].values,df6['low'].values,df6['close'].values,timeperiod=12)
df6['tt']=pd.Series(tt,index=df6.index)
se6=df6['tt']
se6_fudu=(df6['high']-df6['low'])/df6['close']
df7=get_price('000300.XSHG',end_date='2017-6-2',fields=['close','low','high'],count=200)
tt=tl.ATR(df7['high'].values,df7['low'].values,df7['close'].values,timeperiod=12)
df7['tt']=pd.Series(tt,index=df7.index)
se7=df7['tt']
se7_fudu=(df7['high']-df7['low'])/df7['close']
fig=plt.figure(figsize=(30,10))
ax3=fig.add_subplot(221)
ax4=fig.add_subplot(223)
fig = plt.figure(figsize=(30,15))
se5.plot(ax=ax3,color='y',grid=True,label='创业板指:'+'昨收'+str(df5['close'][-1]))
se6.plot(ax=ax3,color='b',grid=True,label='中证500:'+'昨收'+str(df6['close'][-1]))
#se6.plot(ax=ax3,color='b',grid=True,label='中证500:'+'昨收'+str(df6['close'][-1])
se7.plot(ax=ax3,color='r',grid=True,label='沪深300:'+'昨收'+str(df7['close'][-1]))
se5_fudu.plot(ax=ax4,color='y',grid=True,label='创业板指:'+'昨收'+str(df5['close'][-1]))
se6_fudu.plot(ax=ax4,color='b',grid=True,label='中证500:'+'昨收'+str(df6['close'][-1]))
se7_fudu.plot(ax=ax4,color='r',grid=True,label='沪深300:'+'昨收'+str(df7['close'][-1]))
ax3.legend(loc=2)
ax4.legend(loc=2)
plt.show()
<matplotlib.figure.Figure at 0x7f531c3eefd0>
#行业涨跌个数统计
plt.style.use('ggplot')
list_zong=[]
list_zhang=[]
list_zhangt=[]
list_die=[]
list1=[]
for i in SW_ZY:
list_i=get_industry_stocks(i,date='2017-6-2')
zhang,zhangt,die=0,0,0
zong=0
for j in list_i:
stock=j
security_data = get_price(stock,end_date='2017-6-2',frequency='daily',fields=['close'],fq='pre',skip_paused=True,count=200)
close = security_data['close'].values
#macd=tl.MACD(close,fastperiod=12,slowperiod=26,signalperiod=9)[2]
if close[-1]>close[-2]:
zhang+=1
if close[-1]>=close[-2]*1.095:
zhangt+=1
ma5=close[-5:].mean()
if close[-1]<=close[-2]:
die += 1
zong += 1
list_zhang.append(zhang)
list_zhangt.append(zhangt)
list1.append(i)
list_die.append(die)
list_zong.append(zong)
df['zong']=pd.Series(list_zong,index=df.index)
df['zhang']=pd.Series(list_zhang,index=df.index)
df['die']=pd.Series(list_die,index=df.index)
df['zhangt']=pd.Series(list_zhangt,index=df.index)
df['zhanbi']=df['zhang']/df['zong']
df.index=df['行业分类']
df=df.sort('zhanbi',ascending=False)
se=df['zhang']
fig=plt.figure(figsize=(20,15))
'''
ax1=fig.add_subplot(221)
#ax1.set_ylim([0,1])
x=np.linspace(0,11,20)
y=[0.5]*20
ax1.plot(x,y,color='r',alpha=0.3)
ax1.set_title('各行业成分股高于5日均线占比')
se.plot(ax=ax1,kind='bar',color='red',alpha=0.7,grid=True,width=0.8)
'''
#df=df.sort('macds_bili',ascending=False)
se1=df['zong']
ax2=fig.add_subplot(222)
ax2.set_title('涨跌个数统计')
se1.plot(ax=ax2,kind='bar',color='red',alpha=0.3,grid=True,width=0.8)
#df=df.sort('macds_bili',ascending=False)
se2=df['zhang']
#ax2=fig.add_subplot(222)
#ax2.set_title('macd为正的个数')
se2.plot(ax=ax2,kind='bar',color='red',alpha=0.5,grid=True,width=0.8)
#plt.bar(left=range(len(se)),height=se.values,color='red')
se3=df['zhangt']
se3.plot(ax=ax2,kind='bar',color='b',alpha=0.5,grid=True,width=0.8)
<matplotlib.axes._subplots.AxesSubplot at 0x7f532638d780>
#行业涨跌个数统计
import pandas as pd
from functools import reduce
import numpy as np
import matplotlib.pyplot as plt
HY_ZY = ['HY001','HY002','HY003','HY004','HY005','HY006','HY007','HY008','HY009','HY010','HY011']
HY_CN=['能源','材料', '工业', '可选消费指', '日常消费', '医疗保健', '金融', '信息技术', '电信服务', '公用事业',
'房地产']
SW_ZY_num= [801010, 801020, 801030, 801040, 801050, 801080, 801110, 801120,
801130, 801140, 801150, 801160, 801170, 801180, 801200, 801210,
801230, 801710, 801720, 801730, 801740, 801750, 801760, 801770,
801780, 801790, 801880, 801890]
SW_ZY=[str(i) for i in SW_ZY_num]
SW_CN=['农林牧渔I', '采掘I', '化工I', '钢铁I', '有色金属I', '电子I', '家用电器I', '食品饮料I',
'纺织服装I', '轻工制造I', '医药生物I', '公用事业I', '交通运输I', '房地产I', '商业贸易I',
'休闲服务I', '综合I', '建筑材料I', '建筑装饰I', '电气设备I', '国防军工I', '计算机I', '传媒I',
'通信I', '银行I', '非银金融I', '汽车I', '机械设备I']
df=pd.DataFrame(SW_CN,index=SW_ZY,columns=['行业分类'])
plt.style.use('ggplot')
list_zhangfu=[]
list1=[]
for i in SW_ZY:
list_i=get_industry_stocks(i,date='2017-6-2')
zhangfu_ge=[]
for j in list_i:
stock=j
security_data = get_price(stock,end_date='2017-6-2',frequency='daily',fields=['close'],fq='pre',skip_paused=True,count=100)
close = security_data['close'].values
#macd=tl.MACD(close,fastperiod=12,slowperiod=26,signalperiod=9)[2]
zhangfu=(close[-1]-close[-6])/close[-6]
zhangfu_ge.append(zhangfu)
zhangfu=sum(zhangfu_ge)/len(zhangfu_ge)
list_zhangfu.append(zhangfu)
list1.append(i)
df['zhangfu']=pd.Series(list_zhangfu,index=df.index)
df.index=df['行业分类']
df=df.sort('zhangfu',ascending=False)
#se=df['zhangfu']
fig=plt.figure(figsize=(20,15))
#ax1=fig.add_subplot(221)
'''
#ax1.set_ylim([0,1])
x=np.linspace(0,11,20)
y=[0.5]*20
ax1.plot(x,y,color='r',alpha=0.3)
ax1.set_title('各行业成分股高于5日均线占比')
se.plot(ax=ax1,kind='bar',color='red',alpha=0.7,grid=True,width=0.8)
'''
#df=df.sort('macds_bili',ascending=False)
se1=df['zhangfu']
ax2=fig.add_subplot(222)
ax2.set_title('5日行业涨跌幅统计')
se1.plot(ax=ax2,kind='bar',color='b',alpha=1,grid=True,width=0.8)
'''
#df=df.sort('macds_bili',ascending=False)
se2=df['zhang']
#ax2=fig.add_subplot(222)
#ax2.set_title('macd为正的个数')
se2.plot(ax=ax2,kind='bar',color='red',alpha=0.5,grid=True,width=0.8)
#plt.bar(left=range(len(se)),height=se.values,color='red')
se3=df['zhangt']
se3.plot(ax=ax2,kind='bar',color='b',alpha=0.5,grid=True,width=0.8)
'''
plt.show()
本社区仅针对特定人员开放
查看需注册登录并通过风险意识测评
5秒后跳转登录页面...
移动端课程