使用wind API查询某股票股价

需求分解

  1. 使用wind 提供的python API接口查询某支股票的收盘价
  2. 使用matplotlib对收盘价进行图形展示
  3. 利用python word相关包生成word格式报告

part1:使用使用wind 提供的python API接口查询某支股票的收盘价

w.start()
#使用wsd函数获取时间序列,这里以宁德时代股票为例
codes = ['300750.SZ','000300.SH']
fields = ['close']
beginTime = '2021-01-01'
endTime = '2022-05-21'
stockInfo = w.wsd(codes,fields,beginTime,endTime,usedf=True)[1]
stockInfo = stockInfo.reset_index()
stockInfo.columns = ['日期','宁德时代','沪深300指数']

part2:使用matplotlib对收盘价进行图形展示

#使用matplotlib包生成图形
#设置字体
plt.rcParams["font.sans-serif"]=["SimHei"]
#该语句解决图像中的“-”负号的乱码问题
plt.rcParams["axes.unicode_minus"]=False
stockData = stockInfo['日期']
stockClose = stockInfo['宁德时代']
stockIndex = stockInfo['沪深300指数']
fig = plt.figure()

ax = fig.add_axes([0,0,1,1])
ax.plot(stockData,stockClose,'b-')
ax.set_title('宁德时代股价波动趋势')
ax.grid(True)
ax1 = ax.twinx()
ax1.plot(stockData,stockIndex,'r-')
ax1.set_ylabel('沪深300指数')
# ax.spines['left'].set_color(None)
# ax.spines['top'].set_color(None)
ax.set_xlabel('日期')
ax.set_ylabel('宁德时代收盘价')
plt.savefig('my.png',dpi=200,bbox_inches = 'tight' )
plt.show()

image

part3:利用python word相关包生成word格式报告

未完待续。。

遇到的问题

  1. matplotlib导出的图形没有标题和坐标轴
    参考1:https://blog.csdn.net/weixin_42279212/article/details/120665026
    参考2:在保存时增加 bbox_inches = 'tight'参数
fig.savefig('my.png',dpi=200,bbox_inches = 'tight')
  1. savefig()函数的具体参数
    参考:https://blog.csdn.net/m0_47384542/article/details/110356507

posted on 2022-05-21 14:40  朝朝暮Mu  阅读(1396)  评论(0)    收藏  举报