forked from SebastienZh/StockTradebyZ
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_kline.py
More file actions
298 lines (247 loc) · 10.9 KB
/
fetch_kline.py
File metadata and controls
298 lines (247 loc) · 10.9 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
from __future__ import annotations
import datetime as dt
import logging
import random
import sys
import time
import warnings
from concurrent.futures import ThreadPoolExecutor, as_completed
from pathlib import Path
from typing import List, Optional
import os
import pandas as pd
import tushare as ts
import yaml
from tqdm import tqdm
warnings.filterwarnings("ignore")
# --------------------------- pandas 兼容补丁 --------------------------- #
# tushare 内部使用了 fillna(method='ffill'/'bfill'),在 pandas 2.2+ 中已移除该参数。
# 此补丁将旧式调用自动转发到 ffill()/bfill(),无需降级 pandas。
import pandas as _pd
_orig_fillna = _pd.DataFrame.fillna
def _patched_fillna(self, value=None, *, method=None, axis=None, inplace=False, limit=None, **kwargs):
if method is not None:
if method == "ffill":
result = self.ffill(axis=axis, inplace=inplace, limit=limit)
elif method == "bfill":
result = self.bfill(axis=axis, inplace=inplace, limit=limit)
else:
raise ValueError(f"Unsupported fillna method: {method}")
return result
return _orig_fillna(self, value, axis=axis, inplace=inplace, limit=limit, **kwargs)
_pd.DataFrame.fillna = _patched_fillna # type: ignore[method-assign]
_orig_series_fillna = _pd.Series.fillna
def _patched_series_fillna(self, value=None, *, method=None, axis=None, inplace=False, limit=None, **kwargs):
if method is not None:
if method == "ffill":
result = self.ffill(axis=axis, inplace=inplace, limit=limit)
elif method == "bfill":
result = self.bfill(axis=axis, inplace=inplace, limit=limit)
else:
raise ValueError(f"Unsupported fillna method: {method}")
return result
return _orig_series_fillna(self, value, axis=axis, inplace=inplace, limit=limit, **kwargs)
_pd.Series.fillna = _patched_series_fillna # type: ignore[method-assign]
# --------------------------- 全局日志配置 --------------------------- #
_PROJECT_ROOT = Path(__file__).resolve().parent.parent
_DEFAULT_LOG_DIR = _PROJECT_ROOT / "data" / "logs"
def _resolve_cfg_path(path_like: str | Path, base_dir: Path = _PROJECT_ROOT) -> Path:
"""将配置中的路径统一解析为绝对路径:相对路径基于项目根目录。"""
p = Path(path_like)
return p if p.is_absolute() else (base_dir / p)
def _default_log_path() -> Path:
today = dt.date.today().strftime("%Y-%m-%d")
return _DEFAULT_LOG_DIR / f"fetch_{today}.log"
def setup_logging(log_path: Optional[Path] = None) -> None:
"""初始化日志:同时输出到 stdout 和指定文件。"""
if log_path is None:
log_path = _default_log_path()
log_path = Path(log_path)
log_path.parent.mkdir(parents=True, exist_ok=True)
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(filename)s:%(lineno)d %(message)s",
handlers=[
logging.StreamHandler(sys.stdout),
logging.FileHandler(log_path, mode="a", encoding="utf-8"),
],
)
logger = logging.getLogger("fetch_from_stocklist")
# --------------------------- 限流/封禁处理配置 --------------------------- #
COOLDOWN_SECS = 600
BAN_PATTERNS = (
"访问频繁", "请稍后", "超过频率", "频繁访问",
"too many requests", "429",
"forbidden", "403",
"max retries exceeded"
)
def _looks_like_ip_ban(exc: Exception) -> bool:
msg = (str(exc) or "").lower()
return any(pat in msg for pat in BAN_PATTERNS)
class RateLimitError(RuntimeError):
"""表示命中限流/封禁,需要长时间冷却后重试。"""
pass
def _cool_sleep(base_seconds: int) -> None:
jitter = random.uniform(0.9, 1.2)
sleep_s = max(1, int(base_seconds * jitter))
logger.warning("疑似被限流/封禁,进入冷却期 %d 秒...", sleep_s)
time.sleep(sleep_s)
# --------------------------- 历史K线(Tushare 日线,固定qfq) --------------------------- #
pro: Optional[ts.pro_api] = None # 模块级会话
def set_api(session) -> None:
"""由外部(比如GUI)注入已创建好的 ts.pro_api() 会话"""
global pro
pro = session
def _to_ts_code(code: str) -> str:
"""把6位code映射到标准 ts_code 后缀。"""
code = str(code).zfill(6)
if code.startswith(("60", "68", "9")):
return f"{code}.SH"
elif code.startswith(("4", "8")):
return f"{code}.BJ"
else:
return f"{code}.SZ"
def _get_kline_tushare(code: str, start: str, end: str) -> pd.DataFrame:
ts_code = _to_ts_code(code)
try:
df = ts.pro_bar(
ts_code=ts_code,
adj="qfq",
start_date=start,
end_date=end,
freq="D",
api=pro
)
except Exception as e:
if _looks_like_ip_ban(e):
raise RateLimitError(str(e)) from e
raise
if df is None or df.empty:
return pd.DataFrame()
df = df.rename(columns={"trade_date": "date", "vol": "volume"})[
["date", "open", "close", "high", "low", "volume"]
].copy()
df["date"] = pd.to_datetime(df["date"])
for c in ["open", "close", "high", "low", "volume"]:
df[c] = pd.to_numeric(df[c], errors="coerce")
return df.sort_values("date").reset_index(drop=True)
def validate(df: pd.DataFrame) -> pd.DataFrame:
if df is None or df.empty:
return df
df = df.drop_duplicates(subset="date").sort_values("date").reset_index(drop=True)
if df["date"].isna().any():
raise ValueError("存在缺失日期!")
if (df["date"] > pd.Timestamp.today()).any():
raise ValueError("数据包含未来日期,可能抓取错误!")
return df
# --------------------------- 读取 stocklist.csv & 过滤板块 --------------------------- #
def _filter_by_boards_stocklist(df: pd.DataFrame, exclude_boards: set[str]) -> pd.DataFrame:
ts = df["ts_code"].astype(str).str.upper()
num = ts.str.extract(r"(\d{6})", expand=False).str.zfill(6)
mask = pd.Series(True, index=df.index)
if "gem" in exclude_boards:
mask &= ~((ts.str.endswith(".SZ")) & num.str.startswith(("300", "301")))
if "star" in exclude_boards:
mask &= ~((ts.str.endswith(".SH")) & num.str.startswith(("688",)))
if "bj" in exclude_boards:
mask &= ~((ts.str.endswith(".BJ")) | num.str.startswith(("4", "8")))
return df[mask].copy()
def load_codes_from_stocklist(stocklist_csv: Path, exclude_boards: set[str]) -> List[str]:
df = pd.read_csv(stocklist_csv)
df = _filter_by_boards_stocklist(df, exclude_boards)
codes = df["symbol"].astype(str).str.zfill(6).tolist()
codes = list(dict.fromkeys(codes)) # 去重保持顺序
logger.info("从 %s 读取到 %d 只股票(排除板块:%s)",
stocklist_csv, len(codes), ",".join(sorted(exclude_boards)) or "无")
return codes
# --------------------------- 单只抓取(全量覆盖保存) --------------------------- #
def fetch_one(
code: str,
start: str,
end: str,
out_dir: Path,
):
csv_path = out_dir / f"{code}.csv"
for attempt in range(1, 4):
try:
new_df = _get_kline_tushare(code, start, end)
if new_df.empty:
logger.debug("%s 无数据,生成空表。", code)
new_df = pd.DataFrame(columns=["date", "open", "close", "high", "low", "volume"])
new_df = validate(new_df)
new_df.to_csv(csv_path, index=False) # 直接覆盖保存
break
except Exception as e:
if _looks_like_ip_ban(e):
logger.error(f"{code} 第 {attempt} 次抓取疑似被封禁,沉睡 {COOLDOWN_SECS} 秒")
_cool_sleep(COOLDOWN_SECS)
else:
silent_seconds = 30 * attempt
logger.info(f"{code} 第 {attempt} 次抓取失败,{silent_seconds} 秒后重试:{e}")
time.sleep(silent_seconds)
else:
logger.error("%s 三次抓取均失败,已跳过!", code)
# --------------------------- 配置加载 --------------------------- #
_CONFIG_PATH = Path(__file__).parent.parent / "config" / "fetch_kline.yaml"
def _load_config(config_path: Path = _CONFIG_PATH) -> dict:
if not config_path.exists():
raise FileNotFoundError(f"找不到配置文件:{config_path}")
with open(config_path, "r", encoding="utf-8") as f:
cfg = yaml.safe_load(f)
logger.info("已加载配置文件:%s", config_path.resolve())
return cfg
# --------------------------- 主入口 --------------------------- #
def main(log_path: Optional[Path] = None):
# ---------- 读取 YAML 配置 ---------- #
cfg = _load_config()
# ---------- 日志路径(优先参数,其次 YAML,最后默认值) ---------- #
if log_path is None:
cfg_log = cfg.get("log")
log_path = _resolve_cfg_path(cfg_log) if cfg_log else _default_log_path()
setup_logging(log_path)
logger.info("日志文件:%s", Path(log_path).resolve())
# ---------- Tushare Token ---------- #
os.environ["NO_PROXY"] = "api.waditu.com,.waditu.com,waditu.com"
os.environ["no_proxy"] = os.environ["NO_PROXY"]
ts_token = os.environ.get("TUSHARE_TOKEN")
if not ts_token:
raise ValueError("请先设置环境变量 TUSHARE_TOKEN,例如:export TUSHARE_TOKEN=你的token")
ts.set_token(ts_token)
global pro
pro = ts.pro_api()
# ---------- 日期解析 ---------- #
raw_start = str(cfg.get("start", "20190101"))
raw_end = str(cfg.get("end", "today"))
start = dt.date.today().strftime("%Y%m%d") if raw_start.lower() == "today" else raw_start
end = dt.date.today().strftime("%Y%m%d") if raw_end.lower() == "today" else raw_end
out_dir = _resolve_cfg_path(cfg.get("out", "./data"))
out_dir.mkdir(parents=True, exist_ok=True)
# ---------- 从 stocklist.csv 读取股票池 ---------- #
stocklist_path = _resolve_cfg_path(cfg.get("stocklist", "./pipeline/stocklist.csv"))
exclude_boards = set(cfg.get("exclude_boards") or [])
codes = load_codes_from_stocklist(stocklist_path, exclude_boards)
if not codes:
logger.error("stocklist 为空或被过滤后无代码,请检查。")
sys.exit(1)
logger.info(
"开始抓取 %d 支股票 | 数据源:Tushare(日线,qfq) | 日期:%s → %s | 排除:%s",
len(codes), start, end, ",".join(sorted(exclude_boards)) or "无",
)
# ---------- 多线程抓取(全量覆盖) ---------- #
workers = int(cfg.get("workers", 8))
with ThreadPoolExecutor(max_workers=workers) as executor:
futures = [
executor.submit(
fetch_one,
code,
start,
end,
out_dir,
)
for code in codes
]
for _ in tqdm(as_completed(futures), total=len(futures), desc="下载进度"):
pass
logger.info("全部任务完成,数据已保存至 %s", out_dir.resolve())
if __name__ == "__main__":
main()