nofx icon indicating copy to clipboard operation
nofx copied to clipboard

feat(decision): add open orders to AI context to prevent duplicate orders (#998)

Open the-dev-z opened this issue 1 year ago • 2 comments

問題描述

Issue #998: Aster 交易所重複止損/止盈掛單問題。

根本原因: AI 在決策時缺少未成交訂單信息,無法判斷止損/止盈單是否已存在,導致重複發送 update_stop_loss 指令,超出交易所訂單數量限制。

解決方案

將未成交訂單信息添加到 AI 決策上下文,使 AI 能夠:

  • 看到已存在的止損/止盈訂單
  • 避免發送重複的訂單指令
  • 識別缺少止損保護的持倉

主要變更

1. 定義 OpenOrderInfo 結構 (decision/engine.go)

  • 包含訂單詳細信息:symbol, orderID, type, side, quantity, stopPrice
  • 放置在 decision 包以避免循環導入

2. 實現 GetOpenOrders() 方法

  • AsterTrader: 完整實現,調用 /fapi/v3/openOrders API
  • BinanceTrader/HyperliquidTrader: 空實現,返回空列表(待後續補充)
  • Trader 接口: 添加方法簽名

3. 更新決策上下文

  • Context.OpenOrders: 添加未成交訂單列表
  • buildTradingContext(): 獲取訂單並添加到上下文
  • 錯誤處理: 獲取失敗時使用空列表,不阻塞主流程

4. 更新 AI Prompt

  • buildUserPrompt(): 在每個持倉下方顯示訂單狀態
    • 🛡️ 止損單
    • 🎯 止盈單
    • ⚠️ 警告(缺少止損保護時)
  • buildSystemPrompt(): 添加「未成交掛單提醒」章節
    • 說明訂單圖標含義
    • 提醒 AI 不要重複下單

技術細節

避免循環依賴

  • 將 OpenOrderInfo 定義在 decision 包(而非 trader 包)
  • 避免 trader ↔ decision 循環導入

錯誤容錯

  • GetOpenOrders 失敗時記錄警告但不中斷流程
  • 使用空列表確保 AI 仍能正常決策

日誌記錄

  • 顯示獲取到的訂單數量,方便調試

測試結果

  • ✅ decision 包測試通過 (0.941s)
  • ✅ trader 包測試通過 (16.649s)
  • ✅ 無循環依賴錯誤
  • ✅ 代碼已使用 gofmt 格式化

預期效果

  • ✅ AI 能看到已有止損/止盈單,避免重複下單
  • ✅ AI 能識別缺少止損的持倉,主動提醒設置
  • ✅ 更智能的交易決策(完整上下文)
  • ✅ 符合「給 AI 完整信息」的設計理念

後續工作

  • [ ] 為 Binance Futures 實現完整的 GetOpenOrders()
  • [ ] 為 Hyperliquid 實現完整的 GetOpenOrders()
  • [ ] 生產環境測試以驗證重複訂單防護效果

Fixes #998

🤖 Generated with Claude Code

Co-Authored-By: Claude [email protected]

the-dev-z avatar Nov 14 '25 07:11 the-dev-z

🤖 Advisory Check Results

These are advisory checks to help improve code quality. They won't block your PR from being merged.

📋 PR Information

Title Format: ✅ Good - Follows Conventional Commits PR Size: 🟢 Small (142 lines: +141 -1)

🔧 Backend Checks

Go Formatting: ✅ Good Go Vet: ✅ Good Tests: ✅ Passed

Fix locally:

go fmt ./...      # Format code
go vet ./...      # Check for issues
go test ./...     # Run tests

⚛️ Frontend Checks

Build & Type Check: ✅ Success

Fix locally:

cd web
npm run build  # Test build (includes type checking)

📖 Resources

Questions? Feel free to ask in the comments! 🙏


These checks are advisory and won't block your PR from being merged. This comment is automatically generated from pr-checks-run.yml.

github-actions[bot] avatar Nov 14 '25 07:11 github-actions[bot]

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 13 committers have signed the CLA.

:white_check_mark: tinkle-community
:x: xqliu
:x: the-dev-z
:x: CoderMageFox
:x: tangmengqiu
:x: ljh740
:x: Im-Sue
:x: deloz
:x: tinkle
:x: 0xEmberZz
:x: hzb1115
:x: zpng
:x: MarsDoge


tinkle seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

cla-assistant[bot] avatar Dec 22 '25 16:12 cla-assistant[bot]