1
1
import copy
2
2
import math
3
+ from pathlib import Path
4
+ import datetime
3
5
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 ()
4
10
5
11
交易方法 = {
6
12
"1" :"等差" ,
@@ -52,31 +58,50 @@ def etl_percentage(dic_金字塔下單資料:dict,
52
58
flt_累計百分比 = 0
53
59
flt_累計下跌百分比 = 0
54
60
flt_最終價格 = 0
61
+ flt_累計數量 = 0
62
+
55
63
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_階資料 )
57
68
flt_百分比 = 0
58
69
# dic_金字塔下單資料_ret[key]["百分比"]= (dic_金字塔下單資料_ret[key]["金額"]/flt_一組金字塔的加總金額)*100
59
70
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_百分比 ))
62
73
63
74
flt_累計百分比 += flt_百分比
64
75
# float("{:.2f}".format(13.949999999999999))
65
- dic_金字塔下單資料_ret [ key ] ["累計百分比" ]= float ("{:.2f}" .format (flt_累計百分比 ))
76
+ dic_階資料_etl ["累計百分比" ]= float ("{:.2f}" .format (flt_累計百分比 ))
66
77
67
78
# 計算累計下跌百分比 : ((起始價格 - 本階價格)/起始價格)*100
68
- flt_累計下跌百分比 += get下跌百分比 (flt_起始價格 ,dic_金字塔下單資料_ret [ key ] ['價格' ])
79
+ flt_累計下跌百分比 += get下跌百分比 (flt_起始價格 ,dic_階資料_etl ['價格' ])
69
80
# 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_累計數量 ))
72
92
73
93
dic_金字塔下單資料_ret ["最終價格" ] = flt_最終價格
94
+
74
95
dic_金字塔下單資料_ret ["累計下跌百分比" ] = flt_累計下跌百分比
75
96
76
97
return dic_金字塔下單資料_ret
77
98
78
99
79
- def get本階下單資料 (lst_買入價格 , flt_最小增加數量 , round單位數 , i , flt_本階單位數 ):
100
+ def get本階下單資料 (lst_買入價格 :list ,
101
+ flt_最小增加數量 :float ,
102
+ round單位數 ,
103
+ i :int ,
104
+ flt_本階單位數 :float ):
80
105
dic_本階下單資料 = {}
81
106
if round單位數 :
82
107
if flt_本階單位數 < flt_最小增加數量 :
@@ -86,10 +111,11 @@ def get本階下單資料(lst_買入價格, flt_最小增加數量, round單位
86
111
flt_本階下單金額 = lst_買入價格 [i ] * flt_本階單位數
87
112
88
113
dic_本階下單資料 = {
89
- "價格" :lst_買入價格 [i ],
90
- "單位數" :flt_本階單位數 ,
91
- "金額" :flt_本階下單金額 ,
92
- }
114
+ "階" : i + 1 ,
115
+ "價格" :lst_買入價格 [i ],
116
+ "單位數" :flt_本階單位數 ,
117
+ "金額" :flt_本階下單金額 ,
118
+ }
93
119
94
120
return dic_本階下單資料
95
121
@@ -105,7 +131,7 @@ def get一組等差金字塔下單資料(flt_起始價格:float,
105
131
round單位數 :bool = False )-> dict :
106
132
flt_一組金字塔的加總金額 = 0
107
133
dic_單位等差金字塔下單資料 = {}
108
-
134
+ lst_results = []
109
135
for i in range (int_交易次數 ):
110
136
111
137
# 本階金額 = 本階買入價格 * 本階單位數
@@ -117,11 +143,13 @@ def get一組等差金字塔下單資料(flt_起始價格:float,
117
143
i ,
118
144
flt_本階單位數 )
119
145
120
- 階層數 = str (i + 1 )
121
- dic_單位等差金字塔下單資料 [階層數 ] = dic_本階下單資料
146
+ # 階層數 = str(i + 1)
147
+ # dic_單位等差金字塔下單資料[階層數] = dic_本階下單資料
148
+ lst_results .append (dic_本階下單資料 )
122
149
flt_一組金字塔的加總金額 += dic_本階下單資料 ["金額" ]
123
150
124
151
# 處理各階層佔比
152
+ dic_單位等差金字塔下單資料 ['各階資料' ] = lst_results
125
153
dic_單位等差金字塔下單資料 = etl_percentage (dic_單位等差金字塔下單資料 ,
126
154
flt_一組金字塔的加總金額 ,
127
155
flt_起始價格 )
@@ -178,19 +206,22 @@ def get一組等比金字塔下單資料(flt_起始價格:float,
178
206
round單位數 :bool = False )-> dict :
179
207
flt_一組金字塔的加總金額 = 0
180
208
dic_單位等比金字塔下單資料 = {}
209
+ lst_results = []
181
210
for i in range (int_交易次數 ):
182
211
183
212
# 本階金額 = 本階買入價格 * 本階單位數
184
213
flt_本階單位數 = (flt_起始單位數 * (flt_下單數量等比參數 ** i ) ) * flt_金字塔放大倍數
185
214
186
215
dic_本階下單資料 = get本階下單資料 (lst_買入價格 , flt_最小增加數量 , round單位數 , i , flt_本階單位數 )
187
216
188
- 階層數 = str (i + 1 )
189
- dic_單位等比金字塔下單資料 [階層數 ] = dic_本階下單資料
217
+ # 階層數 = str(i + 1)
218
+ # dic_單位等比金字塔下單資料[階層數] = dic_本階下單資料
219
+
220
+ lst_results .append (dic_本階下單資料 )
190
221
flt_一組金字塔的加總金額 += dic_本階下單資料 ["金額" ]
191
- # 處理各階層佔比
192
-
193
222
223
+ # 處理各階層佔比
224
+ dic_單位等比金字塔下單資料 ['各階資料' ] = lst_results
194
225
dic_單位等比金字塔下單資料 = etl_percentage (dic_單位等比金字塔下單資料 ,
195
226
flt_一組金字塔的加總金額 ,
196
227
flt_起始價格 )
@@ -245,3 +276,57 @@ def get買入等比金字塔(flt_總預算:float,
245
276
dic_等差金字塔下單資料 = get買入等差金字塔 (flt_總預算 ,flt_起始價格 , flt最終價格 , int_交易次數 , flt_最小增加數量 , flt_下單數量等差參數 , flt_起始單位數 = 1 )
246
277
dic_等比金字塔下單資料 = get買入等比金字塔 (flt_總預算 ,flt_起始價格 , flt最終價格 , int_交易次數 , flt_最小增加數量 , flt_下單數量等比參數 , flt_起始單位數 = 1 )
247
278
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