Skip to content

Commit d1b9ae1

Browse files
committed
calc fund week
1 parent 2db78e2 commit d1b9ae1

File tree

2 files changed

+149
-0
lines changed

2 files changed

+149
-0
lines changed

fund_week/calc_random.py

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import requests
2+
import re
3+
import random
4+
import json
5+
from datetime import datetime
6+
import matplotlib
7+
import matplotlib.pyplot as plt
8+
9+
header = {
10+
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0',}
11+
url = 'http://fund.eastmoney.com/js/fundcode_search.js?v=20200301111634'
12+
response=requests.get(url, header)
13+
result=response.text.replace('var r = ', '').replace(';', '')
14+
pattern=re.compile(r'\["(.*?)"\,', re.S)
15+
item=re.findall(pattern,result)
16+
codes=random.sample(item[:200], 20)
17+
codes=['001210', '519670', '002207', '001618', '519670', '590008', '001071', '257040', '590005', '080012']
18+
month=['01','02','03','04','05','06','07','08','09','10','11','12']
19+
day=range(1, 32)
20+
#startDate = '2018-{0}-{1}'.format(random.sample(month,1)[0],random.sample(day,1)[0]) #起始时间
21+
#endDate = '2019-{0}-{1}'.format(random.sample(month,1)[0],random.sample(day,1)[0]) #截止时间
22+
startDate = '2018-01-21' #起始时间
23+
#startDate = '2018-04-17' #起始时间
24+
endDate = '2020-01-28' #截止时间
25+
26+
allpro=[]
27+
name_list = ['周一', '周二', '周三', '周四', '周五']
28+
money = 100 #定投金额
29+
n = 0
30+
while n < 10:
31+
n += 1
32+
startDate = '2018-{0}-{1}'.format(random.sample(month, 1)[0], random.sample(day, 1)[0]) # 起始时间
33+
endDate = '2019-{0}-{1}'.format(random.sample(month,1)[0],random.sample(day,1)[0]) #截止时间
34+
print(startDate, endDate)
35+
for i, j in enumerate(codes):
36+
total = [0] * 5 # 到期后总份额
37+
count = [0] * 5 # 每日定投次数
38+
header = {
39+
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0',
40+
'Referer': 'http://fundf10.eastmoney.com/jjjz_{0}.html'.format(j)
41+
}
42+
url = 'http://api.fund.eastmoney.com/f10/lsjz?fundCode={0}&pageIndex={1}&pageSize=5000&startDate={2}&endDate={3}&_=1555586870418?'.format(
43+
j, 1, startDate, endDate)
44+
response = requests.get(url, headers=header)
45+
result = json.loads(response.text)
46+
for j in result['Data']['LSJZList'][::-1]:
47+
if j['JZZZL']=='':
48+
pass
49+
else:
50+
weekday = int(datetime.strptime(j['FSRQ'], '%Y-%m-%d').weekday())
51+
DWJZ = float(j['DWJZ']) # 净值
52+
total[weekday] += money/DWJZ
53+
count[weekday] += 1
54+
total_money = [] # 根据份额算出总金额
55+
for t, k in enumerate(total):
56+
total_money.append(k * DWJZ)
57+
print("周{0}定投最终金额{1}".format(t + 1, k * DWJZ), "定投{0}次".format(count[t]))
58+
profit_list = [round((i-100*j)/(100*j), 4) for i, j in zip(total_money, count)] # 到期后总收益率
59+
allpro.append(profit_list)
60+
plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
61+
# plt.rcParams["font.family"] = 'Arial Unicode MS' # mac 用来正常显示中文标签
62+
matplotlib.rcParams['axes.unicode_minus'] =False #显示负号
63+
plt.figure(figsize=(15, 10), dpi=80)
64+
plt.title('随机基金模拟定投收益散点图', color='blue', fontweight=800, size=40)
65+
x = range(len(name_list))
66+
for t, i in enumerate(allpro):
67+
plt.scatter(x, i, c='b', s=0.5) #画每个基金的散点图
68+
average=[]
69+
for i in range(5):
70+
eve_average = 0
71+
for j in allpro:
72+
eve_average=eve_average+j[i]
73+
average.append(eve_average/100)
74+
for a, b in zip(x, average):
75+
plt.text(a+0.4, b, '%.5f' % b, ha='center', va='bottom', fontsize=20)
76+
plt.scatter(x, average, s=50, c='r', label='平均值')
77+
plt.legend(loc="upper left") # 防止label和图像重合显示不出来
78+
plt.xticks(x, name_list, size=20, color='r') # x坐标
79+
plt.ylabel('收益率', color='r', size=20)
80+
plt.grid(axis="y") #生成网格'''
81+
plt.show()
82+

fund_week/demo.py

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import requests
2+
import json
3+
from datetime import datetime
4+
import matplotlib.pyplot as plt
5+
6+
money = 100
7+
fundCode = '160135' #基金代码
8+
pageIndex = 1
9+
startDate = '2018-08-21' #起始时间
10+
endDate = '2020-03-03' #截止时间
11+
header = {
12+
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0',
13+
'Referer': 'http://fundf10.eastmoney.com/jjjz_{0}.html'.format(fundCode)
14+
}
15+
url = 'http://api.fund.eastmoney.com/f10/lsjz?fundCode={0}&pageIndex={1}&pageSize=5000&startDate={2}&endDate={3}&_=1555586870418?'.format(fundCode, pageIndex, startDate, endDate)
16+
response = requests.get(url, headers=header)
17+
result=json.loads(response.text)
18+
print(result)
19+
total = [0] * 5 # 到期后总份额
20+
count = [0] * 5 # 每日定投次数
21+
for j in result['Data']['LSJZList'][::-1]:
22+
if j['JZZZL']=='':
23+
pass
24+
else:
25+
weekday = int(datetime.strptime(j['FSRQ'], '%Y-%m-%d').weekday())
26+
DWJZ = float(j['DWJZ']) # 净值
27+
total[weekday] += money/DWJZ
28+
count[weekday] += 1
29+
total_money=[] #根据份额算出总金额
30+
for t, i in enumerate(total):
31+
total_money.append(i*DWJZ)
32+
print("周{0}定投最终金额{1}".format(t+1, i*DWJZ), "定投{0}次".format(count[t]))
33+
plt.rcParams['font.sans-serif'] = ['SimHei'] # windows 用来正常显示中文标签
34+
# plt.rcParams["font.family"] = 'Arial Unicode MS' # mac 用来正常显示中文标签
35+
plt.figure(figsize=(15, 10), dpi=80)
36+
plt.title('{0}基金模拟定投收益图'.format(fundCode), color='blue', fontweight=800, size=50)
37+
38+
profit_list = [round((i-100*j)/(100*j), 4) for i, j in zip(total_money, count)] # 到期后总收益率
39+
print(profit_list)
40+
# 生成画布
41+
name_list = ['周一', '周二', '周三', '周四', '周五']
42+
x = range(len(name_list))
43+
minytick=int(min(total_money))-1000
44+
maxytick=int(max(total_money))+1000
45+
plt.bar(x, [i for i in total_money], label='该日定投最终收益', width=0.4, color='y')
46+
# 参数 m、m2、r 用来调整高度比例
47+
m = sum(total_money) / 5
48+
m2 = min(profit_list)
49+
r = 50000
50+
plt.bar([i+0.4 for i in x], [(i-m2)*r + m for i in profit_list], label='该日定投收益率', width=0.4, color='r')
51+
plt.legend(loc="upper left") # 防止label和图像重合显示不出来
52+
plt.xticks(x, name_list, size=20) # x坐标
53+
plt.ylim(minytick, maxytick)
54+
plt.yticks(range(minytick, maxytick, 200), size=20) # y坐标
55+
ax = plt.gca();#获得坐标轴的句柄
56+
ax.spines['left'].set_linewidth(3) ; ####设置左边坐标轴的粗细
57+
ax.spines['bottom'].set_linewidth(3) ; ###设置底部坐标轴的粗细
58+
59+
for a, b, c in zip(x, total_money, count):
60+
plt.text(a, b+0.05, '%.1f' % b, ha='center', va='bottom', fontsize=15)
61+
plt.text(a, b+100, '定投{}次'.format(c), ha='center', va='bottom', fontsize=15, color='r')
62+
for a, b in zip(x, profit_list):
63+
plt.text(a+0.4, (b-m2)*r + m, '%.4f' % b, ha='center', va='bottom', fontsize=15)
64+
65+
plt.text(2, maxytick+300, '时间:{0}至{1}'.format(startDate, endDate), fontsize=20)
66+
plt.grid(axis="y") #生成网格'''
67+
plt.show()

0 commit comments

Comments
 (0)