fix: 指数K线数据获取
fetch_kline 修复两个bug: 1. _market_prefix 不认识 sh/sz/hk 开头指数代码 2. 指数code自带前缀,API key不要重复拼接 3. fqkline端点同样支持指数,不需要mkline 4. refresh_mtf_cache 加入6大指数自动缓存 上证指数(4163) MA5=4110 MA10=4053 MA20=4070 周线横盘 月线震荡上升
This commit is contained in:
+16
-3
@@ -57,6 +57,13 @@ def _write_klines_to_db(code: str, daily: list, weekly: list, monthly: list, fun
|
||||
def _market_prefix(code: str) -> str:
|
||||
"""根据代码确定市场前缀"""
|
||||
raw = str(code).split("_")[0]
|
||||
# 指数代码:sh/sz/hk开头
|
||||
if raw.startswith("sh"):
|
||||
return "sh"
|
||||
if raw.startswith("sz"):
|
||||
return "sz"
|
||||
if raw.startswith("hk"):
|
||||
return "hk"
|
||||
if len(raw) == 5 and raw.isdigit():
|
||||
return "hk"
|
||||
if raw.startswith("6") or raw.startswith("5"):
|
||||
@@ -138,8 +145,11 @@ def fetch_kline(code: str, period: str = "day", count: int = 120) -> list:
|
||||
return cached_klines
|
||||
|
||||
market = _market_prefix(code)
|
||||
url = KLINE_URL.format(market=market, code=code,
|
||||
period=period, count=count)
|
||||
is_index = any(code.startswith(p) for p in ["sh", "sz", "hk"])
|
||||
|
||||
# 指数代码已经自带前缀,API直接用code;普通股票需要加market前缀
|
||||
api_code = code if is_index else f"{market}{code}"
|
||||
url = f"http://web.ifzq.gtimg.cn/appstock/app/fqkline/get?param={api_code},{period},,,{count},qfq"
|
||||
|
||||
try:
|
||||
req = urllib.request.Request(url, headers=_user_agent())
|
||||
@@ -155,7 +165,10 @@ def fetch_kline(code: str, period: str = "day", count: int = 120) -> list:
|
||||
if not isinstance(api_data, dict):
|
||||
return {"error": f"data field is {type(api_data).__name__}", "raw": str(api_data)[:200]}
|
||||
|
||||
stock_key = f"{market}{code}"
|
||||
# 指数代码已经自带前缀(sh000001/sz399001),直接用
|
||||
# 普通股票代码需要加market前缀(sh600036/sz300750)
|
||||
is_index = any(code.startswith(p) for p in ["sh", "sz", "hk"])
|
||||
stock_key = code if is_index else f"{market}{code}"
|
||||
stock_data = api_data.get(stock_key, {})
|
||||
|
||||
# 腾讯API的K线字段名: qfqday, qfqweek, qfqmonth
|
||||
|
||||
Reference in New Issue
Block a user