|
| 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 | + |
0 commit comments