feat: AI日报核心股追踪章节增加连续上榜股票(近3日+)
This commit is contained in:
@@ -42,6 +42,20 @@ TOOLS = [
|
||||
"parameters": {"type": "object", "properties": {}, "required": []}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "get_consecutive_core_stocks",
|
||||
"description": "获取连续上榜的核心股列表(近3日及以上连续上榜),用于识别持续活跃的热点股",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"min_days": {"type": "integer", "description": "最小连续天数,默认3"}
|
||||
},
|
||||
"required": []
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
@@ -98,6 +112,8 @@ async def execute_tool(tool_name: str, arguments: dict) -> str:
|
||||
return await _get_theme_history(arguments.get("date", ""))
|
||||
elif tool_name == "get_active_core_stocks":
|
||||
return await _get_active_core_stocks()
|
||||
elif tool_name == "get_consecutive_core_stocks":
|
||||
return await _get_consecutive_core_stocks(arguments.get("min_days", 3))
|
||||
elif tool_name == "get_stock_quote":
|
||||
return await _get_stock_quote(arguments.get("code", ""))
|
||||
elif tool_name == "get_fund_flow":
|
||||
@@ -219,6 +235,84 @@ async def _get_active_core_stocks() -> str:
|
||||
conn.close()
|
||||
|
||||
|
||||
async def _get_consecutive_core_stocks(min_days: int = 3) -> str:
|
||||
"""获取连续上榜的核心股列表"""
|
||||
from database import get_connection
|
||||
conn = get_connection()
|
||||
try:
|
||||
# 最近30个有数据的交易日
|
||||
rows = conn.execute(
|
||||
"SELECT DISTINCT trade_date FROM daily_core_stocks ORDER BY trade_date DESC LIMIT 30"
|
||||
).fetchall()
|
||||
all_dates = [r["trade_date"] for r in reversed(rows)]
|
||||
|
||||
if not all_dates:
|
||||
return json.dumps({"dates": [], "stocks": []}, ensure_ascii=False)
|
||||
|
||||
# 查询所有上榜记录
|
||||
placeholders = ",".join("?" * len(all_dates))
|
||||
rows = conn.execute(
|
||||
f"""SELECT trade_date, stock_code, stock_name, f3, cover_count
|
||||
FROM daily_core_stocks
|
||||
WHERE trade_date IN ({placeholders})
|
||||
ORDER BY stock_code, trade_date""",
|
||||
all_dates,
|
||||
).fetchall()
|
||||
|
||||
# 按股票分组,记录上榜日期
|
||||
stock_dates: dict[str, dict] = {}
|
||||
for r in rows:
|
||||
code = r["stock_code"]
|
||||
s = stock_dates.setdefault(code, {
|
||||
"stockCode": code,
|
||||
"stockName": r["stock_name"],
|
||||
"dates": [],
|
||||
"dailyGains": {},
|
||||
})
|
||||
s["dates"].append(r["trade_date"])
|
||||
s["dailyGains"][r["trade_date"]] = r["f3"]
|
||||
|
||||
# 计算连续上榜天数(从最新交易日往回数)
|
||||
results = []
|
||||
for code, s in stock_dates.items():
|
||||
dates_list = sorted(s["dates"])
|
||||
# 从最新的日期往回数连续天数
|
||||
consecutive = 0
|
||||
last_n = []
|
||||
for i in range(len(dates_list) - 1, -1, -1):
|
||||
if i == len(dates_list) - 1:
|
||||
consecutive = 1
|
||||
last_n.append(dates_list[i])
|
||||
else:
|
||||
# 检查是否连续(允许间隔1天非交易日)
|
||||
prev_idx = all_dates.index(dates_list[i + 1]) if dates_list[i + 1] in all_dates else -1
|
||||
curr_idx = all_dates.index(dates_list[i]) if dates_list[i] in all_dates else -1
|
||||
if prev_idx - curr_idx <= 2: # 允许间隔1天
|
||||
consecutive += 1
|
||||
last_n.append(dates_list[i])
|
||||
else:
|
||||
break
|
||||
|
||||
if consecutive >= min_days:
|
||||
results.append({
|
||||
"stockCode": code,
|
||||
"stockName": s["stockName"],
|
||||
"consecutiveDays": consecutive,
|
||||
"recentDates": last_n,
|
||||
"dailyGains": {d: s["dailyGains"].get(d) for d in last_n},
|
||||
})
|
||||
|
||||
# 按连续天数降序排列
|
||||
results.sort(key=lambda x: -x["consecutiveDays"])
|
||||
|
||||
return json.dumps({
|
||||
"minDays": min_days,
|
||||
"stocks": results[:30],
|
||||
}, ensure_ascii=False)
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
async def _get_stock_quote(code: str) -> str:
|
||||
"""获取个股实时行情"""
|
||||
from services.tencent import fetch_quote
|
||||
|
||||
Reference in New Issue
Block a user