|
| 1 | +import json |
| 2 | +import datetime |
| 3 | +import calendar |
| 4 | +import matplotlib.pyplot as plt |
| 5 | +from matplotlib import font_manager |
| 6 | +import numpy as np |
| 7 | + |
| 8 | +foundCode = '510300' |
| 9 | +file = f'./found_{foundCode}.txt' |
| 10 | +found_date_price = {} |
| 11 | +found_price_x = [] |
| 12 | +found_price_y = [] |
| 13 | + |
| 14 | +fixed_investment_amount_per_week = 500 |
| 15 | +fixed_investment_amount_per_month = 2000 |
| 16 | +my_font = font_manager.FontProperties(fname='/System/Library/Fonts/PingFang.ttc') |
| 17 | + |
| 18 | +with open(file) as f: |
| 19 | + line = f.readline() |
| 20 | + result = json.loads(line) |
| 21 | + for found in result['Data']['LSJZList'][::-1]: |
| 22 | + found_date_price[found['FSRQ']] = found['DWJZ'] |
| 23 | + found_price_x.append(found['FSRQ']) |
| 24 | + found_price_y.append(found['DWJZ']) |
| 25 | + |
| 26 | + |
| 27 | +# 买入规则:从 start_date 日期开始,每逢 weekday 买入,如果 weekday 不是交易日,则顺延至最近的交易日 |
| 28 | +# 每次买入 500 元,之后转化为相应的份额 |
| 29 | +def calculate_found_profit_by_week(start_date, end_date, weekday): |
| 30 | + total_stock = 0 |
| 31 | + total_amount = 0 |
| 32 | + nums = 0 |
| 33 | + day = start_date + datetime.timedelta(days=-1) |
| 34 | + while day < end_date: |
| 35 | + day = day + datetime.timedelta(days=1) |
| 36 | + if day.weekday() != weekday: |
| 37 | + continue |
| 38 | + while found_date_price.get(day.strftime('%Y-%m-%d'), None) is None and day < end_date: |
| 39 | + day += datetime.timedelta(days=1) |
| 40 | + if day == end_date: |
| 41 | + break |
| 42 | + nums += 1 |
| 43 | + total_stock += round(fixed_investment_amount_per_week / float(found_date_price[day.strftime('%Y-%m-%d')]), 2) |
| 44 | + total_amount += fixed_investment_amount_per_week |
| 45 | + # print(day.strftime('%Y-%m-%d'), found_date_price[day.strftime('%Y-%m-%d')], |
| 46 | + # round(fixed_investment_amount / float(found_date_price[day.strftime('%Y-%m-%d')]), 2), nums) |
| 47 | + |
| 48 | + # 计算盈利 |
| 49 | + while found_date_price.get(end_date.strftime('%Y-%m-%d'), None) is None: |
| 50 | + end_date += datetime.timedelta(days=-1) |
| 51 | + total_profit = round(total_stock, 2) * float(found_date_price[end_date.strftime('%Y-%m-%d')]) - total_amount |
| 52 | + |
| 53 | + return nums, round(total_stock, 2), total_amount, round(total_profit) |
| 54 | + |
| 55 | + |
| 56 | +def get_first_day_of_next_month(date): |
| 57 | + first_day = datetime.datetime(date.year, date.month, 1) |
| 58 | + days_num = calendar.monthrange(first_day.year, first_day.month)[1] # 获取一个月有多少天 |
| 59 | + return first_day + datetime.timedelta(days=days_num) |
| 60 | + |
| 61 | + |
| 62 | +# 买入规则:从 start_date 日期开始,每月 1 号买入,如果 1 号不是交易日,则顺延至最近的交易日 |
| 63 | +# 每次买入 2000 元,之后转化为相应的份额 |
| 64 | +def calculate_found_profit_by_month(start_date, end_date): |
| 65 | + total_stock = 0 |
| 66 | + total_amount = 0 |
| 67 | + nums = 0 |
| 68 | + first_day = datetime.datetime(start_date.year, start_date.month, 1) |
| 69 | + day = first_day + datetime.timedelta(days=-1) # 将日期设置为 start_date 上个月最后一天 |
| 70 | + while day < end_date: |
| 71 | + day = get_first_day_of_next_month(day) |
| 72 | + while found_date_price.get(day.strftime('%Y-%m-%d'), None) is None and day < end_date: |
| 73 | + day = day + datetime.timedelta(days=1) |
| 74 | + if day == end_date: |
| 75 | + break |
| 76 | + nums += 1 |
| 77 | + total_stock += round(fixed_investment_amount_per_month / float(found_date_price[day.strftime('%Y-%m-%d')]), 2) |
| 78 | + total_amount += fixed_investment_amount_per_month |
| 79 | + # print(day.strftime('%Y-%m-%d'), found_date_price[day.strftime('%Y-%m-%d')], |
| 80 | + # round(fixed_investment_amount / float(found_date_price[day.strftime('%Y-%m-%d')]), 2), nums) |
| 81 | + |
| 82 | + # 计算盈利 |
| 83 | + while found_date_price.get(end_date.strftime('%Y-%m-%d'), None) is None: |
| 84 | + end_date += datetime.timedelta(days=-1) |
| 85 | + total_profit = round(total_stock, 2) * float(found_date_price[end_date.strftime('%Y-%m-%d')]) - total_amount |
| 86 | + |
| 87 | + return nums, round(total_stock, 2), total_amount, round(total_profit) |
| 88 | + |
| 89 | + |
| 90 | +start_date = datetime.datetime.fromisoformat('2010-01-01') |
| 91 | +end_date = datetime.datetime.fromisoformat('2020-03-01') |
| 92 | + |
| 93 | + |
| 94 | +def calculate_found_profit_week_month(): |
| 95 | + total_amount = [] |
| 96 | + total_profit = [] |
| 97 | + |
| 98 | + for i in range(5): |
| 99 | + result = calculate_found_profit_by_week(start_date, end_date, i) |
| 100 | + total_amount.append(result[2]) |
| 101 | + total_profit.append(result[3]) |
| 102 | + |
| 103 | + result_month = calculate_found_profit_by_month(start_date, end_date) |
| 104 | + total_amount.append(result_month[2]) |
| 105 | + total_profit.append(result_month[3]) |
| 106 | + return total_amount, total_profit |
| 107 | + |
| 108 | + |
| 109 | +total_amount, total_profit = calculate_found_profit_week_month() |
| 110 | + |
| 111 | +print(total_amount) |
| 112 | +print(total_profit) |
| 113 | + |
| 114 | +fig, ax = plt.subplots() |
| 115 | + |
| 116 | + |
| 117 | +## 柱状图 |
| 118 | +def auto_text(rects): |
| 119 | + for rect in rects: |
| 120 | + ax.text(rect.get_x(), rect.get_height(), rect.get_height(), ha='left', va='bottom') |
| 121 | + |
| 122 | + |
| 123 | +def show_pic(): |
| 124 | + labels = ['周一', '周二', '周三', '周四', '周五', '月定投'] |
| 125 | + index = np.arange(len(labels)) |
| 126 | + width = 0.2 |
| 127 | + |
| 128 | + fig, ax = plt.subplots() |
| 129 | + rect1 = ax.bar(index - width / 2, total_profit, color='lightcoral', width=width, label='投资收益') |
| 130 | + rect2 = ax.bar(index + width / 2, total_amount, color='springgreen', width=width, label='投资金额') |
| 131 | + |
| 132 | + plt.title("投入金额 & 收益柱状图", fontproperties=my_font) |
| 133 | + plt.xticks(fontproperties=my_font) |
| 134 | + ax.set_xticks(ticks=index) |
| 135 | + ax.set_xticklabels(labels) |
| 136 | + |
| 137 | + ax.set_ylim(0, 220000) |
| 138 | + auto_text(rect1) |
| 139 | + auto_text(rect2) |
| 140 | + |
| 141 | + plt.show() |
| 142 | + |
| 143 | + |
| 144 | +# show_pic() |
| 145 | + |
| 146 | + |
| 147 | +# 基金走势 |
| 148 | +def show_found(found_price_y): |
| 149 | + found_price_y = list(map(float, found_price_y)) |
| 150 | + x = [i for i in range(0, len(found_price_y))] |
| 151 | + |
| 152 | + plt.figure(figsize=(10, 6)) |
| 153 | + |
| 154 | + plt.plot(x, found_price_y, linewidth=1, color='r') |
| 155 | + |
| 156 | + plt.xlabel('时间', fontproperties=my_font) |
| 157 | + plt.ylabel('单位净值', fontproperties=my_font) |
| 158 | + plt.title(f"{foundCode} 基金走势", fontproperties=my_font) |
| 159 | + plt.xticks(x[::90], found_price_x[::90], rotation=45) |
| 160 | + |
| 161 | + plt.show() |
| 162 | + |
| 163 | + |
| 164 | +# show_found(found_price_y) |
| 165 | + |
| 166 | +def calculate_found_profit(): |
| 167 | + start_date = datetime.datetime.fromisoformat('2015-06-10') |
| 168 | + end_date = datetime.datetime.fromisoformat('2020-03-01') |
| 169 | + result = calculate_found_profit_by_month(start_date, end_date) |
| 170 | + print(result) |
| 171 | + |
| 172 | +# calculate_found_profit() |
0 commit comments