当前位置: 首页 > 后端技术 > Python

Python获取基金数据求基金30日均线

时间:2023-03-26 17:40:49 Python

单基金importtushareastsimportxlrd#读取数据importpandasaspdts.set_token('xxxxxxxxxxxxxxx')pro=ts.pro_api()path="C://Users//fuxingyu//Desktop//fund.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//fund.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()#reverseunit_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#0.97的30日均线值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))#aget超越l有几行f=sheet2.col_values(colx=2,start_rowx=a-1)[0]#第二列,最后一行,输出数字print('%.4f'%f)multiplefundsimporttushareastsimportxlrd#readdataimportpandasaspdimportosimportmatplotlib.pyplotaspltts.set_token('9bbe863d738fc3a71959dc0a753d6f49fb07bfcb360f49a9097d9d92')pro=ts.pro_api()JJs=['007491.OF9',.OF','001632.OF']dataAll=[]forJJinJJs:path="C://Users//fuxingyu//Desktop//funds//%s.xlsx"%(JJ)path2="C://Users//fuxingyu//Desktop//fund//%s分析结果.xlsx"%(JJ)df=pro.fund_nav(ts_code=JJ)df.to_excel(path)book=xlrd.open_workbook(path)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['0.97mean30']=(data.test.rolling(30).mean())*0.97#0.97的30日均线值data.to_excel(path2)data.plot(title=JJ)#30日均线绘图#plt.show()book2=xlrd.open_workbook(path2)sheet2=book2.sheet_by_index(0)a=len(sheet2.col_values(colx=0,start_rowx=0))#a得到excel中有多少行last_column=sheet2.col_values(colx=2,start_rowx=a-1)[0]#第二列,最后一行,输出数result=JJ+":"+'%.4f'%last_columndataAll.append(result)dataAll='\n'.join(str(i1)对于dataAll中的i1)print(dataAll)