1.从tushare获取茅台股票交易数据
#-*- coding: utf-8 -*-
import tushare as ts
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
font = FontProperties(fname=r"C:\Windows\Fonts\simhei.ttf", size=14)
df=ts.get_k_data('600519','2022-01-01')
df['date']=pd.to_datetime(df['date'])
df.info()
#将data作为行索引
df.set_index('date',inplace=True)
#j计算5日均线
ma5=df['close'].rolling(5).mean()
ma30=df['close'].rolling(30).mean()
fig=plt.figure(figsize=(9,5))
plt.title(u'茅台均线', fontproperties=font)
plt.xlabel(u'时间', fontproperties=font)
plt.ylabel(u'收盘价', fontproperties=font)
plt.plot(ma5,label=u'ma5')
plt.plot(ma30,label=u'ma30')
plt.legend(ncol=2) #让图例生效
plt.show()