fix: 修复6个数据和代码问题
1. 修复 _generate_plain_summary position key 不匹配 (20d→d20, pct→range_pct) 2. 修复 mairui_api.py 日期解析不一致及 float(None) 崩溃风险 3. 修复 fund_flow_analyzer.py 麦蕊回退数据 close_price=0 导致量价背离误判 4. 修复 db_get_fund_flow_history 缺少完整字段和日期过滤 5. 修复 scheduler.py 连接池泄漏 (conn.close() → put_db(conn)) 6. 修复多处北交所股票代码映射缺失 (8/9开头→bj)
This commit is contained in:
@@ -155,6 +155,39 @@ def _get_fund_flow_from_mairui(stock_code, days=10):
|
||||
'small_net_inflow_pct': small_net_pct,
|
||||
})
|
||||
|
||||
# 从本地DB补充 close_price 和 change_pct(避免全为0导致量价背离误判)
|
||||
from db import get_db, put_db
|
||||
conn = get_db()
|
||||
if conn:
|
||||
try:
|
||||
cur = conn.cursor()
|
||||
date_list = [r['date'] for r in records if r['date']]
|
||||
if date_list:
|
||||
cur.execute("""
|
||||
SELECT k.trade_date::text, k.close,
|
||||
CASE WHEN prev.close > 0
|
||||
THEN ROUND((k.close - prev.close) / prev.close * 100, 2)
|
||||
ELSE 0 END AS change_pct
|
||||
FROM stock_kline_daily k
|
||||
LEFT JOIN LATERAL (
|
||||
SELECT close FROM stock_kline_daily
|
||||
WHERE code = k.code AND trade_date < k.trade_date
|
||||
ORDER BY trade_date DESC LIMIT 1
|
||||
) prev ON true
|
||||
WHERE k.code = %s AND k.trade_date::text = ANY(%s)
|
||||
""", (stock_code, date_list))
|
||||
price_map = {r[0]: {'close': float(r[1] or 0), 'change_pct': float(r[2] or 0)}
|
||||
for r in cur.fetchall()}
|
||||
for r in records:
|
||||
info = price_map.get(r['date'])
|
||||
if info:
|
||||
r['close_price'] = info['close']
|
||||
r['change_pct'] = info['change_pct']
|
||||
except Exception as e:
|
||||
logger.warning(f"补充K线价格失败({stock_code}): {e}")
|
||||
finally:
|
||||
put_db(conn)
|
||||
|
||||
# 按日期升序排列
|
||||
records.sort(key=lambda x: x['date'])
|
||||
logger.info(f"麦蕊API获取{stock_code}资金流向{len(records)}条")
|
||||
@@ -245,17 +278,20 @@ def analyze_fund_flow(stock_code, days=5):
|
||||
# 出货:主力净流出但价格不跌(跌幅<2%)
|
||||
accumulation = False
|
||||
distribution = False
|
||||
if total_main_inflow > 0:
|
||||
price_changes = [r['change_pct'] for r in recent]
|
||||
avg_price_change = sum(price_changes) / len(price_changes) if price_changes else 0
|
||||
if avg_price_change < 2:
|
||||
accumulation = True
|
||||
# 检查是否有有效的价格数据(close_price 全为0说明数据不完整,跳过背离检测)
|
||||
has_valid_price = any(r.get('close_price', 0) > 0 for r in recent)
|
||||
if has_valid_price:
|
||||
if total_main_inflow > 0:
|
||||
price_changes = [r['change_pct'] for r in recent]
|
||||
avg_price_change = sum(price_changes) / len(price_changes) if price_changes else 0
|
||||
if avg_price_change < 2:
|
||||
accumulation = True
|
||||
|
||||
if total_main_inflow < 0:
|
||||
price_changes = [r['change_pct'] for r in recent]
|
||||
avg_price_change = sum(price_changes) / len(price_changes) if price_changes else 0
|
||||
if avg_price_change > -2:
|
||||
distribution = True
|
||||
if total_main_inflow < 0:
|
||||
price_changes = [r['change_pct'] for r in recent]
|
||||
avg_price_change = sum(price_changes) / len(price_changes) if price_changes else 0
|
||||
if avg_price_change > -2:
|
||||
distribution = True
|
||||
|
||||
# 单日超大单突击
|
||||
big_surge = False
|
||||
|
||||
Reference in New Issue
Block a user