python计算移动平均线_用python事移动平均线

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线不对应


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部