金融统计分析与挖掘实战7.3-7.5

# 7.3 上市公式净利润增长率计算
import os
os.chdir("C:\\Users\\Administrator\\Desktop")
import pandas as pd
dt = pd.read_excel('data2.xlsx')  #获取数据
dt.head(6)
StkcdAccperB002000101
0162014-12-315.262353e+07
1162015-12-31-1.256819e+09
2162016-12-319.567303e+07
3162017-12-315.057025e+09
4202014-12-317.687620e+06
5202015-12-31-4.200846e+06
#选择满足2014~2017年都存在利润数据上市公司股票代码,即存在4个会计年度
code=dt['Stkcd'].value_counts()
code=list(code[code==4].index)
#将股票基本信息表转化为序列,其中index为股票代码,值为股票名称
info = pd.read_excel('info.xlsx')
S = pd.Series(info.iloc[:,1].values,index=info.iloc[:,0].values)
#预定义4个list,依次存放股票名称、2015、2016、2017年的净利润增长率
list1=[]
list2=[]
list3=[]
list4=[]
for t in range(len(code)):
    d = dt.iloc[dt.iloc[:,0].values==code[t],2].values
    r=(d[1:]-d[0:-1])/d[0:-1]
    if len(r[r > 0.4])==3:
        list1.append(S[code[t]])
        list2.append(r[0])
        list3.append(r[1])
        list4.append(r[2])
#将净利润增长率数据定义为字典
D={'2015':list2,'2016':list3,'2017':list4}
#将字典转化为数据框,index为股票名称
D=pd.DataFrame(D,index=list1)
print(D)
           2015      2016      2017
欧比特    1.307383  0.462634  0.428948
长信科技   0.434675  0.460146  0.568439
太极实业   0.660745  8.864148  0.796785
信维通信   2.509017  1.401281  0.672495
洲明科技   0.866089  0.465667  0.707984
利亚德    1.050507  1.021921  0.808804
凯乐科技   1.517285  0.523497  3.058523
天通股份   4.394320  0.507627  0.421749
合众思壮   0.512233  0.592181  1.508319
和而泰    0.663487  0.596609  0.488410
领益智造  11.952004  4.083113  5.001538
# 7.4 股票价、量走势图的绘制
import numpy as np
import matplotlib.pyplot as plt
data = pd.read_excel('trd.xlsx')
dt=data.loc[data['股票代码']==600000,['交易日期','收盘价','交易量']]
I1=dt['交易日期'].values>='2017-01-03'
I2=dt['交易日期'].values<='2017-01-20'
dta=dt.iloc[I1&I2,:]
y1=dta['收盘价']
x1=range(len(y1))
I3=dt['交易日期'].values>='2017-01-03'
I4=dt['交易日期'].values<='2017-01-24'
dta=dt.iloc[I3&I4,:]
y2=dta['交易量']
x2= range(len(y2))
print(y2.head(6))
0    16237125
1    29658734
2    26437646
3    17195598
4    14908745
5     7996756
Name: 交易量, dtype: int64
D = np.zeros((11))
list1=list()
for m in range(11):
    m = m + 1
    if m<10:
        m1='2017-0'+str(m)+'-01'
        m2='2017-0'+str(m)+'-31'
        mon='0'+str(m)
    else:
        m1='2017-'+str(m)+'-01'
        m2='2017-'+str(m)+'-31'
        mon=str(m)
    I1=dt['交易日期'].values >= m1
    I2=dt['交易日期'].values <= m2
    D[m-1]=dt.iloc[I1&I2,[2]].sum()[0]
    list1.append(mon)

plt.figure(1)
plt.plot(x1,y1)
plt.xlabel(u'日期',fontproperties='SimHei')
plt.ylabel(u'收盘价',fontproperties='SimHei')
plt.title(u'收盘价走势图',fontproperties='SimHei')
plt.savefig('1')

plt.figure(2)
plt.bar(x2,y2)
plt.xlabel(u'日期',fontproperties='SimHei')
plt.ylabel(u'交易量 ',fontproperties='SimHei')
plt.title(u'交易量趋势图',fontproperties='SimHei')
plt.savefig('2')

plt.figure(3)
plt.pie(D,labels=list1,autopct='%1.2f%%') #保留小数点后两位
plt.title(u'月交易量分布图',fontproperties='SimHei')
plt.savefig('3')

plt.figure(4)
plt.figure(figsize=(14,6))
plt.subplot(1,3,1)
plt.plot(x1,y1)
plt.xlabel(u'日期',fontproperties='SimHei')
plt.ylabel(u'收盘价',fontproperties='SimHei')
plt.title(u'收盘价走势图',fontproperties='SimHei')
plt.subplot(1,3,2)
plt.bar(x2,y2)
plt.xlabel(u'日期',fontproperties='SimHei')
plt.ylabel(u'交易量',fontproperties='SimHei')
plt.title(u'交易量趋势图',fontproperties='SimHei')
plt.subplot(1,3,3)
plt.pie(D,labels=list1,autopct='%1.2f%%') #保留小数点后两位
plt.title(u'月交易量分布图',fontproperties='SimHei')
plt.savefig('4')

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

<Figure size 432x288 with 0 Axes>

在这里插入图片描述

# 7.5 股票价格移动平均线的绘制
trd= pd.read_excel('trd.xlsx')
c=trd['Stkcd'].value_counts()
print(c)
2001    63
2002    63
2019    63
2018    63
2017    63
2016    63
2015    63
2014    63
2013    63
2011    63
2010    63
2009    63
2008    63
2007    63
2006    63
2005    63
2004    63
2003    63
2020    63
2012    11
Name: Stkcd, dtype: int64
code = list(c.index)
print(code)
[2001, 2002, 2019, 2018, 2017, 2016, 2015, 2014, 2013, 2011, 2010, 2009, 2008, 2007, 2006, 2005, 2004, 2003, 2020, 2012]
#动态计算需要q个figure,其中每个figure绘制4个子图,每个子图代表一个股票,在子
#初始值设置q=0
q=0
#循环对每一个股票绘制其图形
for i in range(20):
    #第i个股票代码的收盘价,记为p,并计算其移动平均价
    #并构造绘图的x,y值
    p=trd.loc[trd['Stkcd'].values==code[i],'Clsprc'].values
    avg_p=pd.rolling_mean(p,10)    # 新版本已经不能使用此命令
    x1=np.arange(0,len(p))
    y1=p
    y2=avg_p[9:]
    x2=np.arange(9,len(p))
    
    #如果i与4整除,代表需要重新建一个figure(因为每个figure有4个子图)
    if i%4==0: 
        q=q+1                    
        plt.figure(q)               
        plt.figure(figsize=(8,6))      
    plt.subplot(2,2,i%4+1)
    plt.tight_layout() #用于设置图像外部边缘自动调整
    plt.plot(x1,y1)
    plt.plot(x2,y2)
    plt.savefig(str(q))


---------------------------------------------------------------------------

AttributeError                            Traceback (most recent call last)

<ipython-input-12-dfcf7b02d998> in <module>
      7     #并构造绘图的x,y值
      8     p=trd.loc[trd['Stkcd'].values==code[i],'Clsprc'].values
----> 9     avg_p=pd.rolling_mean(p,10)
     10     x1=np.arange(0,len(p))
     11     y1=p


C:\ProgramData\Anaconda3\lib\site-packages\pandas\__init__.py in __getattr__(name)
    242         return _SparseArray
    243 
--> 244     raise AttributeError(f"module 'pandas' has no attribute '{name}'")
    245 
    246 


AttributeError: module 'pandas' has no attribute 'rolling_mean'
#动态计算需要q个figure,其中每个figure绘制4个子图,每个子图代表一个股票,在子
#初始值设置q=0
q=0
#循环对每一个股票绘制其图形
for i in range(20):
    #第i个股票代码的收盘价,记为p,并计算其移动平均价
    #并构造绘图的x,y值
    p=trd.loc[trd['Stkcd'].values==code[i],'Clsprc'].values
    p = pd.Series(p)
    avg_p = p.rolling(3).mean()   # 3期移动平均
    x1=np.arange(0,len(p))
    y1=p
    y2=avg_p[9:]
    x2=np.arange(9,len(p))
    
    #如果i与4整除,代表需要重新建一个figure(因为每个figure有4个子图)
    if i%4==0: 
        q=q+1                    
        plt.figure(q)               
        plt.figure(figsize=(8,6))      
    plt.subplot(2,2,i%4+1)
    plt.tight_layout() #用于设置图像外部边缘自动调整
    plt.plot(x1,y1)
    plt.plot(x2,y2)
    plt.savefig(str(q))
<Figure size 432x288 with 0 Axes>

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

   
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

哈伦2019

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值