import tushare as ts
import xlrd#读取数据
import pandas as pd
ts.set_token('xxxxxxxxxxxxxx')
pro = ts.pro_api()
path="C://Users//fuxingyu//Desktop//基金.xlsx"
path2="C://Users//fuxingyu//Desktop//分析结果.xlsx"
df = pro.fund_nav(ts_code='001632.OF')
df.to_excel(path)
book=xlrd.open_workbook("C://Users//fuxingyu//Desktop//基金.xlsx")
sheet=book.sheet_by_index(0)
row1_value=sheet.row_values(rowx=0)#第一行的值
bianhao=sheet.col_values(colx=0,start_rowx=1)#第一列的值
end_date=sheet.col_values(colx=3,start_rowx=1)#第四列的值
unit_nav=sheet.col_values(colx=4,start_rowx=1)#基金净值
end_date.reverse()#逆转
unit_nav.reverse()
data = pd.DataFrame(unit_nav,index=end_date,columns=['test'])
#data['mean30'] = data.test.rolling(30).mean()
data['0.97mean30'] = (data.test.rolling(30).mean())*0.97#30天移动平均线值的0.97
data.to_excel(path2)
data.plot()#30天移动平均线画图
#plt.show()
book2=xlrd.open_workbook("C://Users//fuxingyu//Desktop//分析结果.xlsx")
sheet2=book2.sheet_by_index(0)
a=len(sheet2.col_values(colx=0,start_rowx=0))#a得到excel有几行
f=sheet2.col_values(colx=2,start_rowx=a-1)[0]#第2列,最后一行,输出数字
print('%.4f' % f)