Skip to content

Commit 62aa8ae

Browse files
committed
csv下載功能
1 parent d820896 commit 62aa8ae

File tree

3 files changed

+225
-95
lines changed

3 files changed

+225
-95
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
venv
22
__pycache__
3-
logs/*
3+
logs/*
4+
temp_files/*

algo_pyramid.py

Lines changed: 104 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import copy
22
import math
3+
from pathlib import Path
4+
import datetime
35

6+
PATH_TMP_DIR = Path('temp_files')
7+
# if PATH_TMP_DIR not exists, create one
8+
if not PATH_TMP_DIR.exists():
9+
PATH_TMP_DIR.mkdir()
410

511
交易方法 = {
612
"1":"等差",
@@ -52,31 +58,50 @@ def etl_percentage(dic_金字塔下單資料:dict,
5258
flt_累計百分比 = 0
5359
flt_累計下跌百分比 = 0
5460
flt_最終價格=0
61+
flt_累計數量=0
62+
5563

56-
for key,value in dic_金字塔下單資料.items():
64+
# for key,value in dic_金字塔下單資料.items():
65+
lst_results = []
66+
for dic_階資料 in dic_金字塔下單資料['各階資料']:
67+
dic_階資料_etl = copy.deepcopy(dic_階資料)
5768
flt_百分比 = 0
5869
# dic_金字塔下單資料_ret[key]["百分比"]= (dic_金字塔下單資料_ret[key]["金額"]/flt_一組金字塔的加總金額)*100
5970

60-
flt_百分比 = get本階投入資金百分比(flt_一組金字塔的加總金額,dic_金字塔下單資料_ret[key]["金額"])
61-
dic_金字塔下單資料_ret[key]["百分比"] = float("{:.2f}".format(flt_百分比))
71+
flt_百分比 = get本階投入資金百分比(flt_一組金字塔的加總金額,dic_階資料_etl["金額"])
72+
dic_階資料_etl["百分比"] = float("{:.2f}".format(flt_百分比))
6273

6374
flt_累計百分比 += flt_百分比
6475
# float("{:.2f}".format(13.949999999999999))
65-
dic_金字塔下單資料_ret[key]["累計百分比"]= float("{:.2f}".format(flt_累計百分比))
76+
dic_階資料_etl["累計百分比"]= float("{:.2f}".format(flt_累計百分比))
6677

6778
# 計算累計下跌百分比 : ((起始價格 - 本階價格)/起始價格)*100
68-
flt_累計下跌百分比 += get下跌百分比(flt_起始價格,dic_金字塔下單資料_ret[key]['價格'])
79+
flt_累計下跌百分比 += get下跌百分比(flt_起始價格,dic_階資料_etl['價格'])
6980
# flt_累計下跌百分比 += ((flt_起始價格 - dic_金字塔下單資料_ret[key]['價格'])/flt_起始價格)*100
70-
dic_金字塔下單資料_ret[key]["累計下跌百分比"]= float("{:.2f}".format(flt_累計下跌百分比))
71-
flt_最終價格=dic_金字塔下單資料_ret[key]['價格']
81+
dic_階資料_etl["累計下跌百分比"]= float("{:.2f}".format(flt_累計下跌百分比))
82+
83+
flt_累計數量 += dic_階資料_etl["單位數"]
84+
flt_最終價格 = dic_階資料_etl['價格']
85+
86+
lst_results.append(dic_階資料_etl)
87+
88+
dic_金字塔下單資料_ret['各階資料'] = lst_results
89+
flt_平均成本 = dic_金字塔下單資料_ret["總投入金額"] / flt_累計數量
90+
dic_金字塔下單資料_ret["平均成本"] = float("{:.2f}".format(flt_平均成本))
91+
dic_金字塔下單資料_ret["累計數量"]= float("{:.2f}".format(flt_累計數量))
7292

7393
dic_金字塔下單資料_ret["最終價格"] = flt_最終價格
94+
7495
dic_金字塔下單資料_ret["累計下跌百分比"] = flt_累計下跌百分比
7596

7697
return dic_金字塔下單資料_ret
7798

7899

79-
def get本階下單資料(lst_買入價格, flt_最小增加數量, round單位數, i, flt_本階單位數):
100+
def get本階下單資料(lst_買入價格:list,
101+
flt_最小增加數量:float,
102+
round單位數,
103+
i:int,
104+
flt_本階單位數:float):
80105
dic_本階下單資料={}
81106
if round單位數:
82107
if flt_本階單位數 < flt_最小增加數量:
@@ -86,10 +111,11 @@ def get本階下單資料(lst_買入價格, flt_最小增加數量, round單位
86111
flt_本階下單金額 = lst_買入價格[i] * flt_本階單位數
87112

88113
dic_本階下單資料 = {
89-
"價格":lst_買入價格[i],
90-
"單位數":flt_本階單位數,
91-
"金額":flt_本階下單金額,
92-
}
114+
"階": i+1,
115+
"價格":lst_買入價格[i],
116+
"單位數":flt_本階單位數,
117+
"金額":flt_本階下單金額,
118+
}
93119

94120
return dic_本階下單資料
95121

@@ -105,7 +131,7 @@ def get一組等差金字塔下單資料(flt_起始價格:float,
105131
round單位數:bool=False)->dict:
106132
flt_一組金字塔的加總金額 = 0
107133
dic_單位等差金字塔下單資料 ={}
108-
134+
lst_results = []
109135
for i in range(int_交易次數 ):
110136

111137
# 本階金額 = 本階買入價格 * 本階單位數
@@ -117,11 +143,13 @@ def get一組等差金字塔下單資料(flt_起始價格:float,
117143
i,
118144
flt_本階單位數)
119145

120-
階層數 = str(i + 1)
121-
dic_單位等差金字塔下單資料[階層數] = dic_本階下單資料
146+
# 階層數 = str(i + 1)
147+
# dic_單位等差金字塔下單資料[階層數] = dic_本階下單資料
148+
lst_results.append(dic_本階下單資料)
122149
flt_一組金字塔的加總金額 += dic_本階下單資料["金額"]
123150

124151
# 處理各階層佔比
152+
dic_單位等差金字塔下單資料['各階資料'] = lst_results
125153
dic_單位等差金字塔下單資料 = etl_percentage(dic_單位等差金字塔下單資料,
126154
flt_一組金字塔的加總金額,
127155
flt_起始價格)
@@ -178,19 +206,22 @@ def get一組等比金字塔下單資料(flt_起始價格:float,
178206
round單位數:bool=False)->dict:
179207
flt_一組金字塔的加總金額 = 0
180208
dic_單位等比金字塔下單資料 ={}
209+
lst_results = []
181210
for i in range(int_交易次數 ):
182211

183212
# 本階金額 = 本階買入價格 * 本階單位數
184213
flt_本階單位數 = (flt_起始單位數 * (flt_下單數量等比參數 ** i) ) * flt_金字塔放大倍數
185214

186215
dic_本階下單資料 = get本階下單資料(lst_買入價格, flt_最小增加數量, round單位數, i, flt_本階單位數)
187216

188-
階層數 = str(i + 1)
189-
dic_單位等比金字塔下單資料[階層數] = dic_本階下單資料
217+
# 階層數 = str(i + 1)
218+
# dic_單位等比金字塔下單資料[階層數] = dic_本階下單資料
219+
220+
lst_results.append(dic_本階下單資料)
190221
flt_一組金字塔的加總金額 += dic_本階下單資料["金額"]
191-
# 處理各階層佔比
192-
193222

223+
# 處理各階層佔比
224+
dic_單位等比金字塔下單資料['各階資料'] = lst_results
194225
dic_單位等比金字塔下單資料= etl_percentage(dic_單位等比金字塔下單資料,
195226
flt_一組金字塔的加總金額,
196227
flt_起始價格)
@@ -245,3 +276,57 @@ def get買入等比金字塔(flt_總預算:float,
245276
dic_等差金字塔下單資料 = get買入等差金字塔(flt_總預算,flt_起始價格, flt最終價格, int_交易次數, flt_最小增加數量, flt_下單數量等差參數, flt_起始單位數=1)
246277
dic_等比金字塔下單資料 = get買入等比金字塔(flt_總預算,flt_起始價格, flt最終價格, int_交易次數, flt_最小增加數量, flt_下單數量等比參數, flt_起始單位數=1)
247278

279+
def getLocalTimestamp()->str:
280+
return datetime.datetime.now().strftime("%Y%m%d%H%M%S")
281+
282+
def 金字塔資料轉csv(dic_data):
283+
"""
284+
"價格": 390,
285+
"單位數": 9,
286+
"金額": 3510,
287+
"百分比": 3.51,
288+
"累計百分比": 3.51,
289+
"累計下跌百分比": 2.5
290+
291+
"總投入金額": 99990,
292+
"平均成本": 43183.33,
293+
"累計數量": 45,
294+
"起始價格": 400,
295+
"最終價格": 350,
296+
"累計下跌百分比": 37.5
297+
"""
298+
import csv
299+
str_now = getLocalTimestamp()
300+
fname = '金字塔下單資料'+str_now+'.csv'
301+
path_file = PATH_TMP_DIR.joinpath(fname)
302+
# 將dic_ret 寫入 csv檔
303+
with open(path_file, 'w', newline='') as the_file:
304+
# writer = csv.writer(csvfile)
305+
# 寫入header summary
306+
307+
308+
str_header = '階,買入價格,單位數,金額,百分比,累計百分比,累計下跌百分比'
309+
the_file.write(str_header)
310+
the_file.write("\n")
311+
# the_file.write('\n'.join(tokens))
312+
lst_line_data = []
313+
for dic_階資料 in dic_data['各階資料']:
314+
# str_row = f"{dic_階資料['價格']},{dic_階資料['單位數']},{dic_階資料['金額']},{dic_階資料['百分比']},{dic_階資料['累計百分比']},{dic_階資料['累計下跌百分比']}"
315+
str_row = ",".join( [str(item) for item in dic_階資料.values()])
316+
lst_line_data.append(str_row)
317+
318+
the_file.write("\n".join(lst_line_data))
319+
the_file.write("\n")
320+
the_file.write("\n")
321+
str_summary = f"""
322+
,,,,,總投入金額:,{dic_data["總投入金額"]}
323+
,,,,,平均成本:,{dic_data["平均成本"]}
324+
,,,,,累計數量:,{dic_data["累計數量"]}
325+
,,,,,起始價格:,{dic_data["起始價格"]}
326+
,,,,,最終價格:,{dic_data["最終價格"]}
327+
,,,,,累計下跌百分比:,{dic_data["累計下跌百分比"]}
328+
"""
329+
the_file.write(str_summary)
330+
the_file.write("\n")
331+
332+
return path_file,fname

0 commit comments

Comments
 (0)