from openpyxl import load_workbook
# 得到日增长率,净值,
def getdata(path,bfb):
datalist = []
pricelist = []
workbook = load_workbook(filename=path)
sheet = workbook.active
length = int(sheet.dimensions.split(':')[1][1:])
for i in range(2,int(length*bfb*0.01)):
if i < length:
a = float(sheet.cell(row=i, column=2).value)
price = a
b = float(sheet.cell(row=i+1, column=2).value)
data = round((a/b-1),4)
pricelist.append(price)
datalist.append(data)
return datalist,pricelist
# 基金级别的运算
def function(datalist,pricelist,startmoney,pocketmoney,show):
max = 0
fitbuyrate = 0
fitsellrate = 0
fituprate = 0
fitdownrate = 0
for buyrate in range(90,101,5):
for sellrate in range(90,101,5):
for uprate in range(0,101,5):
for downrate in range(0,50):
sum = calculation(datalist,pricelist,startmoney,pocketmoney,buyrate,sellrate,uprate,downrate,False)
if sum > max:
fitbuyrate = buyrate
fitsellrate = sellrate
fituprate = uprate
fitdownrate = downrate
# print('收益:' + str(round(sum,2)), end='')
# print(' 入仓比例:' + str(buyrate), end='')
# print(' 出仓比例:' + str(sellrate), end='')
# print(' 涨幅出仓比例:' + str(uprate), end='')
# print(' 降幅入仓比例:' + str(downrate))
max = sum
if show:
print('入仓比例:' + str(fitbuyrate), end='')
print(' 出仓比例:' + str(fitsellrate), end='')
print(' 涨幅出仓比例:' + str(fituprate), end='')
print(' 降幅入仓比例:' + str(fitdownrate), end='')
return fitbuyrate,fitsellrate,fituprate,fitdownrate
def time_function(path,bfb,show):
datalist, pricelist = getdata(path, bfb)
datalist.reverse()
pricelist.reverse()
startmoney = 10000
pocketmoney = 0
buyrate,sellrate,uprate,downrate,score = do(datalist, pricelist, startmoney, pocketmoney,show)
sum = calculation(datalist,pricelist,startmoney,pocketmoney,buyrate,sellrate,uprate,downrate,False)
return sum
def calculation(datalist,pricelist,startmoney,pocketmoney,buyrate,sellrate,uprate,downrate,show):
price = 1
temp = 0
for day in range(0, int(len(datalist))):
temp = temp + datalist[day]
startmoney = startmoney / price
price = pricelist[day]
startmoney = startmoney * price
if show:
print('')
print('早上账户:' + str(round(startmoney, 2)),end='')
print(' 早上口袋:' + str(round(pocketmoney, 2)), end='')
print(' 总金额:' + str(round((startmoney + pocketmoney), 2)), end='')
print(" 今日股价:" + str(round(price, 4)), end='')
print(' 日增长率:' + str(round(datalist[day] * 100, 4)) + "%", end='')
if temp * 100 >= uprate:
changemoney = startmoney * sellrate * 0.01
startmoney = startmoney - changemoney
pocketmoney = pocketmoney + changemoney
if show:
print(' 出仓:' + str(round(changemoney, 2)), end='')
temp = 0
elif temp * 100 <= -(downrate):
changemoney = pocketmoney * buyrate * 0.01
startmoney = startmoney + changemoney
pocketmoney = pocketmoney - changemoney
if show:
print(' 入仓:' + str(round(changemoney, 2)), end='')
temp = 0
# print(round(startmoney, 2), round(pocketmoney, 2), round((startmoney + pocketmoney) / totalmoney * 100, 2))
return startmoney + pocketmoney
def rank(pathlist):
for path in pathlist:
print('基金代码:' + str(path[:-5]) + ' ')
sum1 = time_function(path,65,False)
sum2 = time_function(path,70,False)
sum3 = time_function(path,80,False)
sum4 = time_function(path,100,True)
p5 = str(round((sum1 - sum2) / sum1,2))
p10 = str(round((sum2 - sum3) / sum2,2))
p20 = str(round((sum3 - sum4) / sum3,2))
r = (float(p5)*3 + float(p10)*3+ float(p20)*3)/9
print("5%预测差值:" + p5 + "%", end='')
print(" 10%预测差值:" + p10 + "%", end='')
print(" 20%预测差值:" + p20 + "%", end='')
print(" 发展趋势:" + str(round(r*100,2)) + "%",end='')
print('')
print('')
def do(datalist,pricelist,startmoney,pocketmoney,show):
buyrate, sellrate, uprate, downrate = function(datalist, pricelist, startmoney, pocketmoney,show)
sum = calculation(datalist, pricelist, startmoney, pocketmoney, buyrate=buyrate, sellrate=sellrate, uprate=uprate,
downrate=downrate, show=False)
if show:
print(" 结余:" + str(round(sum, 2)), end='')
score = round(((sum / (startmoney + pocketmoney) / pricelist[-1] * 100-100)/ round(len(pricelist) / 365, 2)+100),2)
print(" 评分(总金额/成本/股价*100):" + str(score), end='')
print(" 年数:" + str(round(len(pricelist) / 365, 2)), end='')
print(' 最终股价:' + str(pricelist[-1]) + ' ')
return buyrate, sellrate, uprate, downrate, str(int(sum / (startmoney + pocketmoney) / pricelist[-1] * 100))
def text(path,bfb):
print('基金代码:' + str(path[:-5]) + ' ', end='')
datalist, pricelist = getdata(path,bfb)
datalist.reverse()
pricelist.reverse()
startmoney = 10000
pocketmoney = 0
buyrate, sellrate, uprate, downrate = function(datalist, pricelist, startmoney, pocketmoney,True)
sum = calculation(datalist, pricelist, startmoney, pocketmoney, buyrate=100, sellrate=100, uprate=uprate,
downrate=downrate, show=True)
print(" 结余:" + str(round(sum, 2)), end='')
score = round(
((sum / (startmoney + pocketmoney) / pricelist[-1] * 100 - 100) / round(len(pricelist) / 365, 2) + 100), 2)
print(" 评分(总金额/成本/股价*100):" + str(score), end='')
print(" 年数:" + str(round(len(pricelist) / 365, 2)), end='')
print(' 最终股价:' + str(pricelist[-1]) + ' ')
if __name__ == '__main__':
pathlist = ['005911.xlsx', '002910.xlsx', '005969.xlsx', '001691.xlsx', '001156.xlsx', '001216.xlsx', '001951.xlsx', '002083.xlsx', '002910.xlsx', '002959.xlsx', '004745.xlsx', '006252.xlsx', '400015.xlsx']
text('004745.xlsx',bfb=100)
# rank(pathlist)
基金评价
最新推荐文章于 2024-07-26 21:19:18 发布