fix: feed today-only data to AI daily summary to reduce hallucination
Shrink summary context and prompts to today's trades and positions only, and tighten anti-fabrication rules. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -517,6 +517,97 @@ def format_context_text(payload: dict) -> str:
|
||||
return "\n".join(lines).strip()
|
||||
|
||||
|
||||
def format_summary_context_text(payload: dict) -> str:
|
||||
"""今日总结专用:仅当日平仓/持仓/监控,不含昨日明细与资金走势。"""
|
||||
lines = []
|
||||
totals = payload.get("totals") or {}
|
||||
day = totals.get("trading_day")
|
||||
lines.append(
|
||||
f"【合计·今日 {day}】平仓盈亏 {totals.get('total_pnl_u')}U | "
|
||||
f"笔数 {totals.get('closed_count')}(胜{totals.get('win_count')}/负{totals.get('loss_count')})| "
|
||||
f"实盘持仓 {totals.get('open_position_count', 0)} 仓 | "
|
||||
f"浮盈亏 {totals.get('float_pnl_u')}U | "
|
||||
f"资金账户合计 {_fmt_fund(totals.get('total_funding_usdt'))} | "
|
||||
f"交易账户合计 {_fmt_fund(totals.get('total_trading_usdt'))}"
|
||||
)
|
||||
lines.append(
|
||||
f"【说明】交易日={day}。"
|
||||
"「持仓」= 交易所 Agent 实盘;「趋势/关键位/监控单/加仓」= 本地计划,不等于已开仓。"
|
||||
)
|
||||
lines.append("")
|
||||
for ac in payload.get("accounts") or []:
|
||||
st = ac.get("trade_stats") or {}
|
||||
lines.append(f"--- 账户:{ac.get('name')} ({ac.get('key')}) ---")
|
||||
lines.append(f"状态:{ac.get('status')}")
|
||||
if ac.get("status") == "未监控":
|
||||
lines.append("")
|
||||
continue
|
||||
lines.append(
|
||||
f"资金账户 {_fmt_fund(ac.get('funding_usdt'))} | "
|
||||
f"交易账户 {_fmt_fund(ac.get('trading_usdt'))} | "
|
||||
f"可用 {_fmt_fund(ac.get('available_trading_usdt'))}"
|
||||
)
|
||||
lines.append(
|
||||
f"今日({day})平仓:{st.get('closed_count')} 笔,盈亏 {st.get('total_pnl_u')}U "
|
||||
f"(胜{st.get('win_count')}/负{st.get('loss_count')})"
|
||||
)
|
||||
open_n = int(ac.get("open_position_count") or _account_open_position_count(ac))
|
||||
if open_n <= 0:
|
||||
lines.append("当前交易所持仓:无(空仓)")
|
||||
else:
|
||||
lines.append(
|
||||
f"当前交易所持仓:{open_n} 仓 | 浮盈亏合计 {ac.get('float_pnl_u')}U"
|
||||
)
|
||||
mon = ac.get("monitor_lines") or {}
|
||||
if mon.get("trends"):
|
||||
lines.append("趋势回调计划(本地,非持仓):")
|
||||
for row in mon["trends"][:8]:
|
||||
lines.append(f" - {row}")
|
||||
if mon.get("rolls"):
|
||||
lines.append("顺势加仓(本地,非持仓):")
|
||||
for row in mon["rolls"][:8]:
|
||||
lines.append(f" - {row}")
|
||||
if mon.get("keys"):
|
||||
lines.append("关键位监控(本地,非持仓):")
|
||||
for row in mon["keys"][:8]:
|
||||
lines.append(f" - {row}")
|
||||
if mon.get("orders"):
|
||||
lines.append("进行中的下单监控(本地,非持仓):")
|
||||
for row in mon["orders"][:8]:
|
||||
lines.append(f" - {row}")
|
||||
positions = ac.get("positions") or []
|
||||
if positions:
|
||||
lines.append("持仓明细(交易所实盘):")
|
||||
for p in positions[:8]:
|
||||
if not isinstance(p, dict):
|
||||
continue
|
||||
sym = p.get("symbol") or "?"
|
||||
side = p.get("side") or "?"
|
||||
contracts = p.get("contracts") or p.get("size") or "?"
|
||||
upnl = _position_float_pnl(p)
|
||||
lines.append(f" - {sym} {side} 张数{contracts} 浮盈亏{upnl:.4f}U")
|
||||
lines.append(
|
||||
f"Agent合约余额:{ac.get('balance_usdt') if ac.get('balance_usdt') is not None else '未知'} USDT"
|
||||
)
|
||||
trades_today = ac.get("trades") or []
|
||||
if trades_today:
|
||||
lines.append("今日平仓明细:")
|
||||
for t in trades_today[:15]:
|
||||
lines.append(f" - {_format_trade_line(t)}")
|
||||
else:
|
||||
lines.append("今日平仓明细:无")
|
||||
issues = ac.get("issues") or []
|
||||
if issues:
|
||||
lines.append("关注点:" + ";".join(issues))
|
||||
lines.append("")
|
||||
return "\n".join(lines).strip()
|
||||
|
||||
|
||||
def summary_context_hash(payload: dict) -> str:
|
||||
text = format_summary_context_text(payload)
|
||||
return hashlib.sha256(text.encode("utf-8")).hexdigest()[:16]
|
||||
|
||||
|
||||
def format_account_remark(ac: dict) -> str:
|
||||
"""分户表格备注:监控摘要 + 持仓。"""
|
||||
parts: list[str] = []
|
||||
@@ -548,14 +639,20 @@ def format_account_remark(ac: dict) -> str:
|
||||
return ";".join(parts)
|
||||
|
||||
|
||||
def collect_closed_trades_snapshot(accounts: list[dict], *, today: str, yesterday: str) -> list[dict]:
|
||||
def collect_closed_trades_snapshot(
|
||||
accounts: list[dict],
|
||||
*,
|
||||
today: str,
|
||||
yesterday: str | None = None,
|
||||
) -> list[dict]:
|
||||
rows: list[dict] = []
|
||||
for ac in accounts or []:
|
||||
name = ac.get("name") or ac.get("key")
|
||||
for t in ac.get("trades_yesterday") or []:
|
||||
if not isinstance(t, dict):
|
||||
continue
|
||||
rows.append({**t, "account_name": name, "trading_day": yesterday})
|
||||
if yesterday:
|
||||
for t in ac.get("trades_yesterday") or []:
|
||||
if not isinstance(t, dict):
|
||||
continue
|
||||
rows.append({**t, "account_name": name, "trading_day": yesterday})
|
||||
for t in ac.get("trades") or []:
|
||||
if not isinstance(t, dict):
|
||||
continue
|
||||
|
||||
Reference in New Issue
Block a user