a293119a31
- market_watch.py: 新增 init_db() 建表 + write_snapshot() 双写 SQLite - market_snapshots: 每次采集的元信息(时间、来源、涨跌比、情绪) - sector_snapshots: 每个板块的涨跌幅、资金流向、领涨股等 - JSON 写入保留不变,SQLite 写入失败不影响 JSON 管道 - mofin_query.py: 通用查询工具 - 板块趋势查询:「半导体最近5次采集的涨跌幅」 - 资金流向排行:「净流入最多的5个板块」 - 连续净流入检测:「最近3天连续净流入的板块」 - 市场情绪趋势 + 数据库概览 - 支持直接 SQL 查询
266 lines
8.8 KiB
Python
266 lines
8.8 KiB
Python
#!/usr/bin/env python3
|
|
"""market_watch.py — 行業熱點數據採集,寫入 dashboard data/market.json
|
|
|
|
數據源優先級:
|
|
後端A:東方財富 push2 API(首選,有板塊代碼+實時指數)
|
|
後端B:同花順 THS / akshare(降級,有漲跌家數+資金流向)
|
|
|
|
注意:當前服務器無法連通東方財富API(已被封禁/域名不可達),
|
|
實際運行時自動降級到同花順 THS 後端。THS 提供90+行業板塊的
|
|
實時漲跌、上漲/下跌家數、淨流入資金等數據,足以滿足需求。
|
|
|
|
輸出:data/market.json → MoFin Dashboard 市場數據展示
|
|
"""
|
|
|
|
import json
|
|
import sqlite3
|
|
from datetime import datetime
|
|
from pathlib import Path
|
|
|
|
DATA_DIR = Path(__file__).parent / "data"
|
|
DB_PATH = DATA_DIR / "mofin.db"
|
|
|
|
# ── 数据库初始化 ──────────────────────────────────────
|
|
|
|
def init_db():
|
|
"""创建 mofin.db 及所有表(幂等,已存在则跳过)"""
|
|
DATA_DIR.mkdir(parents=True, exist_ok=True)
|
|
conn = sqlite3.connect(str(DB_PATH))
|
|
conn.execute("PRAGMA journal_mode=WAL")
|
|
conn.execute("PRAGMA foreign_keys=ON")
|
|
conn.executescript("""
|
|
CREATE TABLE IF NOT EXISTS market_snapshots (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
timestamp TEXT NOT NULL,
|
|
source TEXT NOT NULL DEFAULT 'ths',
|
|
up_ratio REAL,
|
|
mood TEXT,
|
|
created_at TEXT DEFAULT (datetime('now','localtime'))
|
|
);
|
|
CREATE INDEX IF NOT EXISTS idx_snapshots_time ON market_snapshots(timestamp);
|
|
|
|
CREATE TABLE IF NOT EXISTS sector_snapshots (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
snapshot_id INTEGER NOT NULL REFERENCES market_snapshots(id),
|
|
name TEXT NOT NULL,
|
|
change_pct REAL,
|
|
up_count INTEGER,
|
|
down_count INTEGER,
|
|
net_inflow REAL,
|
|
lead_stock TEXT,
|
|
lead_stock_change REAL,
|
|
volume REAL,
|
|
turnover REAL
|
|
);
|
|
CREATE INDEX IF NOT EXISTS idx_sector_name ON sector_snapshots(name);
|
|
CREATE INDEX IF NOT EXISTS idx_sector_snapshot ON sector_snapshots(snapshot_id);
|
|
CREATE INDEX IF NOT EXISTS idx_sector_name_time ON sector_snapshots(name, snapshot_id);
|
|
""")
|
|
conn.commit()
|
|
return conn
|
|
|
|
|
|
def write_snapshot(conn, market_data: dict):
|
|
"""将一次采集结果双写 SQLite(JSON 写入由 main 负责)"""
|
|
try:
|
|
# 1. INSERT market_snapshots
|
|
cur = conn.execute(
|
|
"""INSERT INTO market_snapshots (timestamp, source, up_ratio, mood)
|
|
VALUES (?, ?, ?, ?)""",
|
|
(
|
|
market_data["timestamp"],
|
|
market_data.get("source", "unknown"),
|
|
market_data.get("up_ratio", 0),
|
|
market_data.get("mood", "unknown"),
|
|
),
|
|
)
|
|
snapshot_id = cur.lastrowid
|
|
|
|
# 2. INSERT sector_snapshots(逐板块)
|
|
sectors = market_data.get("sectors", [])
|
|
rows = []
|
|
for s in sectors:
|
|
rows.append((
|
|
snapshot_id,
|
|
s.get("name", ""),
|
|
s.get("change", 0),
|
|
s.get("up_count"),
|
|
s.get("down_count"),
|
|
s.get("net_inflow"),
|
|
s.get("lead_stock"),
|
|
s.get("lead_stock_change"),
|
|
s.get("volume"),
|
|
s.get("turnover"),
|
|
))
|
|
if rows:
|
|
conn.executemany(
|
|
"""INSERT INTO sector_snapshots
|
|
(snapshot_id, name, change_pct, up_count, down_count,
|
|
net_inflow, lead_stock, lead_stock_change, volume, turnover)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""",
|
|
rows,
|
|
)
|
|
conn.commit()
|
|
return snapshot_id, len(rows)
|
|
except Exception as e:
|
|
print(f"[DB] SQLite 写入失败(JSON 不受影响): {e}", flush=True)
|
|
try:
|
|
conn.rollback()
|
|
except Exception:
|
|
pass
|
|
return None, 0
|
|
|
|
|
|
# ── 後端A:東方財富 push2 API(首選,有板塊代碼+實時指數) ──
|
|
|
|
def _fetch_em(url):
|
|
"""通用 EM API 請求"""
|
|
import urllib.request
|
|
req = urllib.request.Request(
|
|
url,
|
|
headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"}
|
|
)
|
|
resp = urllib.request.urlopen(req, timeout=10)
|
|
return json.loads(resp.read().decode("utf-8"))
|
|
|
|
|
|
def fetch_sector_em():
|
|
"""東方財富行業板塊"""
|
|
try:
|
|
data = _fetch_em(
|
|
"https://push2.eastmoney.com/api/qt/clist/get?"
|
|
"pn=1&pz=60&po=1&np=1&fields=f2,f3,f4,f12,f14&fs=m:90+t:2"
|
|
)
|
|
return [{
|
|
"name": i["f14"],
|
|
"code": i["f12"],
|
|
"price": i.get("f2", 0),
|
|
"change": i.get("f3", 0),
|
|
} for i in data.get("data", {}).get("diff", [])]
|
|
except Exception:
|
|
return None
|
|
|
|
|
|
def fetch_concept_em():
|
|
"""東方財富概念板塊"""
|
|
try:
|
|
data = _fetch_em(
|
|
"https://push2.eastmoney.com/api/qt/clist/get?"
|
|
"pn=1&pz=30&po=1&np=1&fields=f2,f3,f4,f12,f14&fs=m:90+t:3"
|
|
)
|
|
return [{
|
|
"name": i["f14"],
|
|
"code": i["f12"],
|
|
"change": i.get("f3", 0),
|
|
} for i in data.get("data", {}).get("diff", [])]
|
|
except Exception:
|
|
return None
|
|
|
|
|
|
# ── 後端B:同花順 THS / akshare(降級) ──
|
|
|
|
def fetch_sector_ths():
|
|
"""THS 行業板塊(含漲跌家數、資金流向、領漲股)"""
|
|
try:
|
|
import akshare as ak
|
|
df = ak.stock_board_industry_summary_ths()
|
|
return [{
|
|
"name": r["板块"],
|
|
"code": "",
|
|
"price": 0,
|
|
"change": float(r.get("涨跌幅", 0)),
|
|
"volume": float(r.get("总成交量", 0)),
|
|
"turnover": float(r.get("总成交额", 0)),
|
|
"net_inflow": float(r.get("净流入", 0)),
|
|
"up_count": int(r.get("上涨家数", 0)),
|
|
"down_count": int(r.get("下跌家数", 0)),
|
|
"avg_price": float(r.get("均价", 0)),
|
|
"lead_stock": r.get("领涨股", ""),
|
|
"lead_stock_change": float(r.get("领涨股-涨跌幅", 0)),
|
|
} for _, r in df.iterrows()]
|
|
except Exception as e:
|
|
print(f"THS行業失敗: {e}", flush=True)
|
|
return []
|
|
|
|
|
|
def fetch_concept_ths():
|
|
"""THS 概念板塊(僅名稱,無實時漲跌)"""
|
|
try:
|
|
import akshare as ak
|
|
df = ak.stock_board_concept_name_ths()
|
|
return [{
|
|
"name": r["name"],
|
|
"code": str(r.get("code", "")),
|
|
"change": 0,
|
|
} for _, r in df.iterrows()]
|
|
except Exception as e:
|
|
print(f"THS概念失敗: {e}", flush=True)
|
|
return []
|
|
|
|
|
|
# ── 輔助函數 ──
|
|
|
|
def get_market_mood(sectors):
|
|
if not sectors:
|
|
return "unknown"
|
|
ratio = sum(1 for s in sectors if s.get("change", 0) > 0) / len(sectors)
|
|
return "bullish" if ratio > 0.7 else "neutral" if ratio > 0.4 else "bearish"
|
|
|
|
|
|
# ── 主流程 ──
|
|
|
|
def main():
|
|
# 行業板塊:EM → THS → 兜底
|
|
sectors = fetch_sector_em()
|
|
source = "eastmoney"
|
|
if sectors is None:
|
|
sectors = fetch_sector_ths()
|
|
source = "ths"
|
|
|
|
# 概念板塊:EM → THS → 空
|
|
concepts = fetch_concept_em()
|
|
concept_source = "eastmoney"
|
|
if concepts is None:
|
|
concepts = fetch_concept_ths()
|
|
concept_source = "ths"
|
|
if not concepts:
|
|
concepts = []
|
|
concept_source = "unavailable"
|
|
|
|
# 排序
|
|
sorted_sectors = sorted(sectors, key=lambda s: s.get("change", 0), reverse=True)
|
|
top_gainers = [s for s in sorted_sectors if s.get("change", 0) > 0][:5]
|
|
top_losers = [s for s in reversed(sorted_sectors) if s.get("change", 0) < 0][:3]
|
|
|
|
market_data = {
|
|
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M"),
|
|
"source": source,
|
|
"concept_source": concept_source,
|
|
"total_sectors": len(sectors),
|
|
"up_ratio": round(
|
|
sum(1 for s in sectors if s.get("change", 0) > 0) / max(len(sectors), 1) * 100, 1
|
|
),
|
|
"mood": get_market_mood(sectors),
|
|
"top_gainers": top_gainers,
|
|
"top_losers": top_losers,
|
|
"sectors": sectors,
|
|
"concepts": concepts,
|
|
}
|
|
|
|
DATA_DIR.mkdir(parents=True, exist_ok=True)
|
|
with open(DATA_DIR / "market.json", "w", encoding="utf-8") as f:
|
|
json.dump(market_data, f, ensure_ascii=False, indent=2)
|
|
|
|
# ── SQLite 双写 ──
|
|
conn = init_db()
|
|
sid, count = write_snapshot(conn, market_data)
|
|
if sid:
|
|
print(f"[DB] snapshot_id={sid}, sectors={count}", flush=True)
|
|
conn.close()
|
|
|
|
# 靜默:只寫文件,不輸出到stdout,避免cron推送
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|