Python小程序,查询股票信息,并保存内容和画图

发布于 2016-04-14  4966 次阅读


#-*- coding: utf-8 -*-
from matplotlib.finance import _quotes_historical_yahoo
from datetime import date
import pandas as pd
import matplotlib.pyplot as plt
def search(name):
today=date.today()
start=(today.year,today.month-1,today.day)
quotes=_quotes_historical_yahoo(name,start,today)
titles=['date','open','close','max','min','vol']
df1=pd.DataFrame(quotes,columns=titles)
list1=[]
for i in range (0,len(quotes)):
x=date.fromordinal(int(quotes[i][0]))
y=date.strftime(x,'%y-%m-%d')
list1.append(y)
df=pd.DataFrame(quotes,index=list1,columns=titles)
df=df.drop(['date'],axis=1)
print(df)
return (df,df1)

def getName():
str=input("input the name of the quote\n")
return str

def saveData(data):
data.to_csv(path_or_buf="D:\Python\output.csv",index=True)
def drawGraph(raw):
rawindex=raw.index
plt.title('K-Lines')
plt.xlabel('Days')
plt.ylabel('Price')
plt.plot(rawindex,raw['close'],color='red',label='close')
plt.plot(rawindex,raw['open'],color='blue',label='open')
plt.plot(rawindex,raw['max'],color='green',label='max')
plt.plot(rawindex,raw['min'],color='yellow',label='min')
plt.show()

str=getName()
dataT=search(str)
data,raw=dataT
saveData(data)
drawGraph(raw)