单个基金

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)

多个基金

import tushare as ts
import xlrd#读取数据
import pandas as pd
import os
import matplotlib.pyplot as plt
ts.set_token('9bbe863d738fc3a71959dc0a753d6f49fb07bfcb360f49a9097d9d92')
pro = ts.pro_api()
JJs=['007491.OF','002974.OF','002385.OF','001632.OF']
dataAll=[]
for JJ in JJs:
    path = "C://Users//fuxingyu//Desktop//基金//%s.xlsx"%(JJ)
    path2 = "C://Users//fuxingyu//Desktop//基金//%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#30天移动平均线值的0.97

    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]#第2列,最后一行,输出数字
    result=JJ+":"+'%.4f' % last_column
    dataAll.append(result)
dataAll='\n'.join(str(i1)for i1 in dataAll)
print(dataAll)

叫我瞄大人
467 声望81 粉丝

喜欢追星的非科班的编程爱好者