-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathget_extraData.py
More file actions
169 lines (130 loc) · 5.75 KB
/
get_extraData.py
File metadata and controls
169 lines (130 loc) · 5.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import pandas as pd
import io
import requests
import os
import time
from datetime import datetime
from dateutil.relativedelta import relativedelta
from utils import gen_iter_date_by_month
def transform_date(date: str) -> str: # 民國轉西元
y, m, d = date.split("/")
return str(int(y) + 1911) + "/" + m + "/" + d
def process_data(data: str) -> float | None:
"""清理原始字串並轉為 float,空值或無效值回傳 None。"""
data = data.replace(",", "").replace("--", "").strip()
return float(data) if data else None
def getDatas(path: str) -> dict:
datas = {}
for dirPath, dirNames, fileNames in os.walk(path):
if len(fileNames) == 0:
continue
fileNames.sort()
# 移除當月資料,以免資料不全
current = f"{(datetime.now() + relativedelta(day=1)).strftime('%Y%m%d')}.csv"
if fileNames[-1] == current:
removed = fileNames[-1] # 先記錄再刪除,避免 del 後索引位移
os.remove(os.path.join(dirPath, removed))
del fileNames[-1]
print("remove", removed)
for f in fileNames:
datas[os.path.splitext(f)[0]] = True
return datas
def save_twse_ftse_index(
s: requests.Session, symbol: str, url_symbol: str, start: datetime
) -> None:
savePath = os.path.join("./extraData", symbol)
os.makedirs(savePath, exist_ok=True)
datas = getDatas(savePath)
end = datetime.now() + relativedelta(day=1) # 設定為當月的 1 號
for day in gen_iter_date_by_month(start, end):
d = day.strftime("%Y%m%d")
if datas.get(d, False):
print(symbol, d, "already exists")
continue
url = f"https://www.twse.com.tw/rwd/zh/FTSE/{url_symbol}?response=csv&date={d}"
print(symbol, url_symbol, d, "saving...", f"from {url}")
c = s.get(url).content
try:
df = pd.read_csv(io.StringIO(c.decode("big5")), header=1)
print("raw data")
print(df)
except pd.errors.EmptyDataError:
print(symbol, d, "is empty")
return
print("after drop")
df = df.dropna(axis=1)
df = df[df[symbol.replace("指數", "報酬指數")] != "--"]
print(df)
df.loc[:, "Date"] = pd.to_datetime(df["日期"].apply(transform_date), format="%Y/%m/%d")
df.loc[:, "Close"] = df[symbol].apply(process_data).astype(float)
df.loc[:, "Adj Close"] = (
df[symbol.replace("指數", "報酬指數")].apply(process_data).astype(float)
)
df.loc[:, "Dividends"] = 0
df.loc[:, "Stock Splits"] = 0
time.sleep(5)
df = df[df["Adj Close"] != 0]
df.to_csv(os.path.join(savePath, f"{d}.csv"), index=False, lineterminator="\n")
def save_TAI50I_index(s):
save_twse_ftse_index(s, "臺灣50指數", "TAI50I", start=datetime(2002, 10, 1))
def save_TAI100I_index(s):
save_twse_ftse_index(s, "臺灣中型100指數", "TAI100I", start=datetime(2004, 11, 1))
def save_TAIDIVIDI_index(s):
save_twse_ftse_index(s, "臺灣高股息指數", "TAIDIVIDI", start=datetime(2007, 1, 1))
def save_TAIEX_index(s: requests.Session) -> None:
symbol = "臺灣加權股價指數"
savePath = os.path.join("./extraData", symbol)
os.makedirs(savePath, exist_ok=True)
datas = getDatas(savePath)
start = datetime(2003, 1, 1)
end = datetime.now() + relativedelta(day=1) # 設定為當月的 1 號
for day in gen_iter_date_by_month(start, end):
d = day.strftime("%Y%m%d")
if datas.get(d, False):
print(symbol, d, "already exists")
continue
histURL = f"https://www.twse.com.tw/rwd/zh/TAIEX/MI_5MINS_HIST?response=csv&date={d}"
totalReturnURL = f"https://www.twse.com.tw/rwd/zh/TAIEX/MFI94U?response=csv&date={d}"
print(symbol, d, "get history...", f"from {histURL}")
c = s.get(histURL).content
try:
hist = pd.read_csv(io.StringIO(c.decode("big5")), header=1)
print("hist raw data")
print(hist)
print("after drop")
hist = hist.dropna(axis=1)
print(hist)
except pd.errors.EmptyDataError:
print(symbol, d, "is empty")
return
print(symbol, d, "get total return...", f"from {totalReturnURL}")
c = s.get(totalReturnURL).content
totalReturn = pd.read_csv(io.StringIO(c.decode("big5")), header=1)
print("totalReturn raw data")
print(totalReturn)
print("after drop")
totalReturn = totalReturn.dropna(axis=1)
print(totalReturn)
df = hist.join(totalReturn)
if not df[df["日期"] != df["日 期"]].empty:
raise ValueError(
f"{symbol} {d}: hist 與 totalReturn 的日期欄位不對齊,"
f"請確認來源資料格式是否改變"
)
df.loc[:, "Date"] = pd.to_datetime(df["日期"].apply(transform_date), format="%Y/%m/%d")
df.loc[:, "Open"] = df["開盤指數"].apply(process_data).astype(float)
df.loc[:, "High"] = df["最高指數"].apply(process_data).astype(float)
df.loc[:, "Low"] = df["最低指數"].apply(process_data).astype(float)
df.loc[:, "Close"] = df["收盤指數"].apply(process_data).astype(float)
df.loc[:, "Adj Close"] = df["發行量加權股價報酬指數"].apply(process_data).astype(float)
df.loc[:, "Dividends"] = 0
df.loc[:, "Stock Splits"] = 0
time.sleep(5)
df = df[df["Adj Close"] != 0]
df.to_csv(os.path.join(savePath, f"{d}.csv"), index=False, lineterminator="\n")
if __name__ == "__main__":
with requests.Session() as s:
save_TAI50I_index(s)
save_TAI100I_index(s)
save_TAIEX_index(s)
save_TAIDIVIDI_index(s)