Skip to content

Commit bf0c34f

Browse files
committed
添加代码
1 parent a4e1ef7 commit bf0c34f

File tree

6 files changed

+755
-0
lines changed

6 files changed

+755
-0
lines changed
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
import turtle as t
2+
3+
t.speed(5)
4+
t.pensize(1)
5+
t.screensize(500, 500)
6+
t.bgcolor('white')
7+
8+
head_color = '#029FEC'
9+
scarf_color = '#E20030'
10+
nose_color = '#EB0134'
11+
bell_color = '#FEE241'
12+
color_white = '#FFFFFF'
13+
14+
15+
def go_to(x=0, y=0):
16+
t.up()
17+
t.goto(x, y)
18+
t.down()
19+
20+
21+
# 头部
22+
def head():
23+
t.up()
24+
t.circle(150, 45)
25+
t.down()
26+
t.fillcolor(head_color)
27+
t.begin_fill()
28+
t.circle(150, 270)
29+
t.end_fill()
30+
31+
32+
# 围巾
33+
def scarf():
34+
t.fillcolor(scarf_color)
35+
t.begin_fill()
36+
t.seth(0)
37+
t.fd(216)
38+
t.circle(-5, 90)
39+
t.fd(10)
40+
t.circle(-5, 90)
41+
t.fd(220)
42+
t.circle(-5, 90)
43+
t.fd(10)
44+
t.circle(-5, 90)
45+
t.end_fill()
46+
47+
48+
def face():
49+
t.fd(186)
50+
t.lt(45)
51+
t.fillcolor(color_white)
52+
t.begin_fill()
53+
t.circle(120, 100)
54+
t.seth(180)
55+
t.fd(120)
56+
t.seth(215)
57+
t.circle(120, 100)
58+
t.end_fill()
59+
60+
61+
def draw_eyes():
62+
t.fillcolor(color_white)
63+
t.begin_fill()
64+
a = 2.5
65+
for i in range(120):
66+
if 0 <= i < 30 or 60 <= i < 90:
67+
a -= 0.05
68+
else:
69+
a += 0.05
70+
t.lt(3)
71+
t.fd(a)
72+
t.end_fill()
73+
74+
75+
# 鼻子
76+
def nose():
77+
go_to(-13, 166)
78+
t.seth(315)
79+
t.fillcolor(nose_color)
80+
t.begin_fill()
81+
t.circle(20)
82+
t.end_fill()
83+
84+
85+
# 嘴巴
86+
def mouth():
87+
go_to(0, 156)
88+
t.seth(270)
89+
t.fd(100)
90+
pos = t.pos()
91+
t.seth(0)
92+
t.circle(110, 60)
93+
go_to(pos[0], pos[1])
94+
t.seth(180)
95+
t.circle(-110, 60)
96+
97+
98+
# 胡须
99+
def mustache():
100+
h = 70
101+
go_to(30, 140)
102+
t.seth(15)
103+
t.fd(h)
104+
105+
go_to(30, 130)
106+
t.seth(0)
107+
t.fd(h)
108+
109+
go_to(30, 120)
110+
t.seth(-15)
111+
t.fd(h)
112+
113+
go_to(-30, 140)
114+
t.seth(150)
115+
t.fd(h)
116+
117+
go_to(-30, 130)
118+
t.seth(180)
119+
t.fd(h)
120+
121+
go_to(-30, 120)
122+
t.seth(195)
123+
t.fd(h)
124+
125+
126+
def eyes():
127+
go_to(0, 227)
128+
t.seth(90)
129+
draw_eyes()
130+
go_to(0, 227)
131+
t.seth(270)
132+
draw_eyes()
133+
134+
135+
def fill_eyes():
136+
# 填充眼睛
137+
go_to(-15, 220)
138+
t.pensize(12)
139+
t.color('black')
140+
for i in range(30):
141+
t.forward(2)
142+
t.right(12)
143+
go_to(15, 220)
144+
for i in range(30):
145+
t.forward(2)
146+
t.left(12)
147+
t.pensize(1)
148+
149+
150+
# 铃铛
151+
def bell():
152+
# 大圆
153+
go_to(0, 33)
154+
t.pensize(1)
155+
t.fillcolor("#FCE341")
156+
t.begin_fill()
157+
t.circle(25)
158+
t.end_fill()
159+
160+
# 横条纹
161+
go_to(-15, 22)
162+
t.seth(0)
163+
t.forward(42)
164+
go_to(-18, 17)
165+
t.seth(0)
166+
t.forward(47)
167+
168+
# 小圆
169+
go_to(5, 0)
170+
t.pensize(1)
171+
t.color("black", '#79675d')
172+
t.begin_fill()
173+
t.circle(5)
174+
t.end_fill()
175+
t.seth(270)
176+
t.pensize(1)
177+
t.forward(15)
178+
179+
180+
if __name__ == '__main__':
181+
head()
182+
scarf()
183+
face()
184+
eyes()
185+
fill_eyes()
186+
nose()
187+
mouth()
188+
mustache()
189+
bell()
190+
go_to()
191+
t.hideturtle()
192+
t.done()

doudou/2020-03-27-found/found_510300.txt

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import requests
2+
3+
startDate = '2012-05-04'
4+
endDate = '2020-03-01'
5+
foundCode = '510300'
6+
pageSize = 3000
7+
header = {
8+
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36',
9+
'Referer': f'http://fundf10.eastmoney.com/jjjz_{foundCode}.html'
10+
}
11+
12+
url = f'http://api.fund.eastmoney.com/f10/lsjz?&fundCode={foundCode}&pageIndex=1&pageSize={pageSize}&startDate={startDate}&endDate={endDate}&_=1585302987423'
13+
response = requests.get(url, headers=header)
14+
15+
16+
def write_file(content):
17+
filename = f'found_{foundCode}.txt'
18+
with open(filename, 'a') as f:
19+
f.write(content + '\n')
20+
21+
22+
write_file(response.text)
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
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

Comments
 (0)