使用push2 数据接口,优化代码
This commit is contained in:
@@ -192,10 +192,13 @@ async def fetch_quote(code: str) -> Optional[dict]:
|
||||
|
||||
|
||||
async def fetch_history(code: str, days: int = 90) -> List[dict]:
|
||||
"""获取历史K线(前复权日K),主数据源"""
|
||||
"""获取历史K线(前复权日K),含涨跌幅
|
||||
|
||||
多请求1天以计算第一条的涨跌幅,最终只返回 days 条。
|
||||
"""
|
||||
market = get_market_prefix(code)
|
||||
stock_code = f"{market}{code}"
|
||||
url = f"https://web.ifzq.gtimg.cn/appstock/app/fqkline/get?param={stock_code},day,,,{days},qfq"
|
||||
url = f"https://web.ifzq.gtimg.cn/appstock/app/fqkline/get?param={stock_code},day,,,{days + 1},qfq"
|
||||
headers = {
|
||||
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36",
|
||||
"Referer": "https://stockapp.finance.qq.com/",
|
||||
@@ -212,17 +215,31 @@ async def fetch_history(code: str, days: int = 90) -> List[dict]:
|
||||
if not isinstance(raw, list):
|
||||
return []
|
||||
result = []
|
||||
prev_close = 0
|
||||
for record in raw:
|
||||
if not isinstance(record, list) or len(record) < 6:
|
||||
continue
|
||||
close = float(record[2]) if record[2] else 0
|
||||
change_pct = 0
|
||||
if prev_close > 0:
|
||||
change_pct = (close - prev_close) / prev_close * 100
|
||||
result.append({
|
||||
"date": record[0],
|
||||
"open": float(record[1]) if record[1] else 0,
|
||||
"close": float(record[2]) if record[2] else 0,
|
||||
"close": close,
|
||||
"high": float(record[3]) if record[3] else 0,
|
||||
"low": float(record[4]) if record[4] else 0,
|
||||
"volume": int(record[5]) if record[5] else 0,
|
||||
"volume": int(float(record[5])) if record[5] else 0,
|
||||
"changePercent": round(change_pct, 2),
|
||||
})
|
||||
prev_close = close
|
||||
|
||||
# 丢弃最旧1条(它的 prev_close=0 导致涨跌幅为0),只保留最新 days 条
|
||||
if len(result) > 1:
|
||||
result = result[1:]
|
||||
if len(result) > days:
|
||||
result = result[-days:]
|
||||
|
||||
return result
|
||||
except Exception:
|
||||
return []
|
||||
|
||||
Reference in New Issue
Block a user