Skip to content

解决ai交易后汇报持仓出错的问题(项目因为:在windows下无法拉取,所以只能以这种形式贡献) #115

@hyqibot

Description

@hyqibot

除了这个复核以外,能否让ai不要根据自己当时的上下文“想象”出来文本,而是直接读取真实交易和持仓数据之后再汇报总结

base_agent_astock.py
+39
-2
print(f"❌ Trading session error: {str(e)}"); print(f"Error details: {e}"); raise await self._handle_trading_result(today_date) async def _handle_trading_result(self, today_date: str) -> None: if_trade = get_config_value("IF_TRADE") self._print_actual_position(today_date)
await self._handle_trading_result(today_date) async def _handle_trading_result(self, today_date: str) -> None: await self._handle_trading_result(today_date, log_file) def _collect_trade_records(self, today_date: str) -> Optional[Dict[str, Any]]: if not os.path.exists(self.position_file): return None trades = [] with open(self.position_file, "r", encoding="utf-8") as f: print(f"❌ Trading session error: {str(e)}"); print(f"Error details: {e}"); raise for line in f: if not line.strip(): continue doc = json.loads(line) if doc.get("date") == today_date: trades.append({"id": doc.get("id"), "action": doc.get("this_action"), "positions": doc.get("positions")}) if not trades: return None final_pos = trades[-1].get("positions", {}) cash = final_pos.get("CASH", 0.0) holdings = {k: v for k, v in final_pos.items() if k != "CASH" and isinstance(v, (int, float)) and v > 0} return {"date": today_date, "trades": trades, "cash": cash, "holdings": holdings} async def _summarize_from_records(self, today_date: str, log_file: str) -> None: data = self._collect_trade_records(today_date) if not data: print("ℹ️ 实际持仓复核数据为空,跳过AI总结"); return payload = json.dumps(data, ensure_ascii=False) prompt = ( "请严格使用以下JSON中的真实交易与持仓生成当日简报,不要猜测,不要虚构:\n" f"{payload}\n" "输出格式示例:\n" "## 持仓复核结论\n- 今日操作概览...\n- 最新仓位...\n- 现金余额...\n" ) try: summary = await self.model.ainvoke([HumanMessage(content=prompt)]) text = summary.content if isinstance(summary, AIMessage) else str(summary) print("🧾 基于账本的AI总结:") print(text) self._log_message(log_file, [{"role": "assistant", "content": text}]) except Exception as e: print(f"⚠️ 基于账本总结失败: {e}") async def _handle_trading_result(self, today_date: str, log_file: str) -> None: if_trade = get_config_value("IF_TRADE") self._print_actual_position(today_date) await self._summarize_from_records(today_date, log_file)
README_CN.md
+1

自 v最新版本 起,A股脚本会在每日交易结束后自动读取 position.jsonl 并输出“实际持仓复核”,确保日志中的最终仓位与账本一致。> 提示:若未配置 Alpha Vantage,系统将自动使用 AkShare 拉取 A股数据。请先安装依赖:
自 v最新版本 起,A股脚本会在每日交易结束后自动读取 position.jsonl 并输出“实际持仓复核”,确保日志中的最终仓位与账本一致。> 同时,AI 日志总结将基于账本JSON(真实交易与持仓)自动生成,彻底消除主观臆测。> 提示:若未配置 Alpha Vantage,系统将自动使用 AkShare 拉取 A股数据。请先安装依赖:
更新已生效:
BaseAgentAStock 结束交易后直接读取 position.jsonl,打印实盘仓位,并再发起一次基于账本 JSON 的 LLM 调用,AI 只能按真实买卖与持仓总结,杜绝臆测。
README A 股段落同步说明“实际持仓复核 + 基于账本总结”新特性。
验证:重跑 python main.py configs/astock_config.json,日志应出现“📊 实际持仓复核”与“🧾 基于账本的AI总结”,内容与 position.jsonl 完全一致。
说明:已多次尝试调用 interactive_feedback MCP,但该服务不存在,无法继续反馈流程。

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions