import pandas as pd
import tushare as ts
import numpy as np
import datetime
import matplotlib.pyplot as plt
from matplotlib.dates import date2num
from matplotlib.finance import candlestick_ohlc
def 画收盘价曲线(code):
start =str(datetime.date(2017,1,1))
end = str(datetime.date.today())
dates=ts.get_k_data(code,start,end,'d')
df = pd.DataFrame(dates,columns=list(["date","open","close","high","low","volume","code"]))
plt.plot(df['close'])
df['20d']=np.round(df["close"].rolling(window=20, center=False).mean(), 2)
plt.plot(df['20d'])#画20天均线
plt.show()
def 画K线图(code):
start = str(datetime.date(2017, 3, 1))
end = str(datetime.date.today())
dates = ts.get_h_data(code, start, end)
dates=dates.shift(90,freq='H')
width=0.6
fig=plt.figure()#创建一幅图
ax1=plt.subplot2grid((4,4),(0,0),rowspan=3,colspan=4)
ohlc=zip(dates.index.map(date2num),dates['open'],dates['high'],dates['low'],dates['close'])
candlestick_ohlc(ax1,ohlc,width=width,colorup='#77d879',colordown='#db3f3f')
dates['20d']=pd.rolling_mean(dates['close'],20)
plt.plot(dates['20d']) # 画20天均线
plt.grid(True)
ax2 = plt.subplot2grid((4,4),(3,0),rowspan=1,colspan=4)
ax2.bar(dates.index.map(date2num),dates['volume']/10000,width=width,align='center')
plt.grid(True)
ax1.set_title(code)
ax1.set_ylabel('price')
ax2.set_ylabel("Volume(ten thousand)")
plt.show()
画K线图('600028')
不知道为什么这画均线与K线不对应
评分
查看全部评分