nofx icon indicating copy to clipboard operation
nofx copied to clipboard

[BUG] 🟡 [High] AI confidence always shows 0, preventing trade execution / AI 信心度一直為 0 導致無法交易

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

Issue #3: AI 信心度一直為 0 導致無法交易 / AI Confidence Always Shows 0, Preventing Trade Execution

🐛 問題描述 / Bug Description

中文

AI 分析經常返回信心度為 0,導致系統無法執行任何交易。用戶報告「信心度一直為 0」。

English

The AI analysis frequently returns confidence level of 0, which prevents the system from executing any trades. Users report "confidence is always 0".


📋 重現步驟 / Steps to Reproduce

中文

  1. 使用任何提示詞模板配置交易員
  2. 啟動自動交易
  3. 觀察 AI 決策日誌
  4. 結果:大多數週期顯示 confidence=0

English

  1. Configure trader with any prompt template
  2. Start automated trading
  3. Observe AI decision logs
  4. Result: Most cycles show confidence=0

🎯 預期行為 / Expected Behavior

中文

  • AI 應該返回不同的信心度級別(0-100)
  • 當市場條件有利時,信心度應該 ≥ 閾值(例如:75-85)
  • 當信心度滿足要求時應該執行交易

English

  • AI should return varying confidence levels (0-100)
  • When market conditions are favorable, confidence should be ≥ threshold (e.g., 75-85)
  • Trades should execute when confidence meets requirements

❌ 實際行為 / Actual Behavior

中文

從截圖顯示:

cooldown=allowed
trend_alignment=weak
confidence=0
Key insight: 所有币种均处于超买状态或趋势不明,无符合高胜率条件的交易机会

結果:因為信心度永遠無法達到最低閾值,所以不執行任何交易。

English

From screenshot:

cooldown=allowed
trend_alignment=weak
confidence=0
Key insight: 所有币种均处于超买状态或趋势不明,无符合高胜率条件的交易机会

Result: No trades are executed because confidence never meets minimum threshold.


🌍 環境信息 / Environment

  • 報告者 / Reporter: Musk Karl (Community user)
  • 提示詞模板 / Prompt Template: Not specified (possibly adaptive.txt or default.txt)

🔍 可能原因 / Possible Causes

中文

1. 提示詞 Schema 不匹配

最近對 action schema 的更改可能導致解析問題:

  • 從 0.0-1.0 float 改為 0-100 integer
  • PR #401 更改及其後續還原 (#522)

2. 過於嚴格的策略

如果使用 adaptive.txt:

  • 最低信心度閾值:85
  • 非常嚴格的進場條件
  • 可能需要 ≥3:1 的風險回報比

3. 市場條件過濾

AI 可能正確評估沒有機會符合條件:

  • "超买状态" (overbought)
  • "趋势不明" (unclear trend)

4. JSON 提取錯誤

信心度值可能沒有從 AI 響應中正確解析。

English

1. Prompt Schema Mismatch

Recent changes to action schema may cause parsing issues:

  • Changed from 0.0-1.0 float to 0-100 integer
  • PR #401 changes and subsequent revert (#522)

2. Overly Strict Strategy

If using adaptive.txt:

  • Minimum confidence threshold: 85
  • Very strict entry conditions
  • May require ≥3:1 risk-reward ratio

3. Market Condition Filtering

The AI may be correctly assessing that no opportunities meet criteria:

  • "超买状态" (overbought)
  • "趋势不明" (unclear trend)

4. JSON Extraction Bug

Confidence value not being parsed correctly from AI response.


🔧 需檢查的文件 / Files to Check

中文

  • decision/engine.go - 提示詞加載和 JSON 解析
  • prompts/adaptive.txt - 信心度閾值要求
  • prompts/default.txt - 信心度量表定義
  • 影響提示詞系統的最近提交:
    • 提交 0f98e06: Revert "fix: hard system prompt (#401)"
    • 提交 7dd669a: "fix: hard system prompt (#401)"

English

  • decision/engine.go - prompt loading and JSON parsing
  • prompts/adaptive.txt - confidence threshold requirements
  • prompts/default.txt - confidence scale definition
  • Recent commits affecting prompt system:
    • Commit 0f98e06: Revert "fix: hard system prompt (#401)"
    • Commit 7dd669a: "fix: hard system prompt (#401)"

💡 調試步驟 / Debugging Steps

中文

# 1. 啟用完整 AI 推理日誌
# 在 config.json 中設置:
"log_cot": true

# 2. 檢查信心度是否在 JSON 響應中但未被解析
# 查看日誌中的原始 AI 響應

# 3. 嘗試不同的提示詞模板
cp prompts/default.txt prompts/active_prompt.txt
# 重啟應用

# 4. 臨時降低信心度閾值測試
# 編輯提示詞文件,將閾值從 85 降到 60

# 5. 檢查 API 響應格式是否改變
# 查看完整的 Claude API 響應

English

# 1. Enable full AI reasoning logs
# Set in config.json:
"log_cot": true

# 2. Check if confidence is in JSON response but not parsed
# Look at raw AI response in logs

# 3. Try different prompt templates
cp prompts/default.txt prompts/active_prompt.txt
# Restart application

# 4. Temporarily lower confidence threshold to test
# Edit prompt file, change threshold from 85 to 60

# 5. Check if API response format changed
# Look at full Claude API response

🔬 臨時解決方法 / Temporary Workaround

中文

編輯提示詞文件以降低最低信心度要求:

# prompts/adaptive.txt 或 default.txt

- confidence ≥ 85  # 严格标准
+ confidence ≥ 60  # 测试用,放寬標準

# 或者
- 只在信心度 ≥75 時開倉
+ 只在信心度 ≥50 時開倉(臨時測試)

English

Edit prompt file to lower minimum confidence requirement:

# prompts/adaptive.txt or default.txt

- confidence ≥ 85  # Strict standard
+ confidence ≥ 60  # For testing, relaxed standard

# Or
- Only open positions when confidence ≥75
+ Only open positions when confidence ≥50 (temporary test)

📎 相關代碼 / Related Code

中文

// decision/engine.go - 檢查信心度解析
type DecisionOutput struct {
    Action     string  `json:"action"`
    Confidence int     `json:"confidence"` // 應該是 0-100
    Symbol     string  `json:"symbol"`
    // ...
}

// 確保解析邏輯正確
func parseDecision(jsonStr string) (*DecisionOutput, error) {
    var decision DecisionOutput
    err := json.Unmarshal([]byte(jsonStr), &decision)
    if err != nil {
        return nil, fmt.Errorf("解析失敗: %w", err)
    }

    // 添加調試日誌
    log.Printf("解析的信心度: %d", decision.Confidence)

    return &decision, nil
}

English

// decision/engine.go - Check confidence parsing
type DecisionOutput struct {
    Action     string  `json:"action"`
    Confidence int     `json:"confidence"` // Should be 0-100
    Symbol     string  `json:"symbol"`
    // ...
}

// Ensure parsing logic is correct
func parseDecision(jsonStr string) (*DecisionOutput, error) {
    var decision DecisionOutput
    err := json.Unmarshal([]byte(jsonStr), &decision)
    if err != nil {
        return nil, fmt.Errorf("parse failed: %w", err)
    }

    // Add debug logging
    log.Printf("Parsed confidence: %d", decision.Confidence)

    return &decision, nil
}

🔍 檢查清單 / Checklist

中文

  • [ ] 確認 confidence 字段在 AI JSON 響應中存在
  • [ ] 確認 confidence 值範圍是 0-100 而非 0.0-1.0
  • [ ] 檢查提示詞模板中的閾值設置(adaptive.txt: 85, default.txt: 75)
  • [ ] 嘗試不同的提示詞模板排除特定模板問題
  • [ ] 檢查市場條件是否確實不適合交易(可能是正確的 AI 判斷)
  • [ ] 查看是否有 JSON 解析錯誤日誌

English

  • [ ] Confirm confidence field exists in AI JSON response
  • [ ] Confirm confidence value range is 0-100 not 0.0-1.0
  • [ ] Check threshold settings in prompt templates (adaptive.txt: 85, default.txt: 75)
  • [ ] Try different prompt templates to rule out template-specific issues
  • [ ] Check if market conditions are genuinely unsuitable for trading (may be correct AI assessment)
  • [ ] Look for JSON parsing error logs

📊 示例日誌 / Example Logs

理想的日誌輸出 / Expected Log Output

2025-11-05 18:56:50 | 正在請求AI分析並決策... [模板: default]
2025-11-05 18:56:51 | AI返回決策:
{
  "action": "wait",
  "confidence": 45,
  "symbol": "BTCUSDT",
  "reasoning": "當前市場超買,等待更好的進場點"
}
2025-11-05 18:56:51 | 信心度 45 < 最低閾值 75,等待下一個週期

當前問題日誌 / Current Problematic Log

2025-11-05 18:56:50 | 正在請求AI分析並決策... [模板: adaptive]
2025-11-05 18:56:51 | AI返回決策:
{
  "action": "wait",
  "confidence": 0,   ← ❌ 總是 0
  "symbol": "",
  "reasoning": "所有币种均处于超买状态或趋势不明"
}

🏷️ 標籤 / Labels

bug, high-priority, ai-decision, prompt-system, needs-investigation


👥 報告者 / Reporter

社區用戶 / Community User: Musk Karl

報告時間 / Reported: 2025-11-05


📊 優先級 / Priority

🟡 High - Prevents trading execution, core functionality impacted

the-dev-z avatar Nov 05 '25 15:11 the-dev-z

Issue #557 狀態與處理建議

📊 當前狀態

項目 狀態 說明
Issue #557 🟢 OPEN 未自動關閉
PR #564 ✅ MERGED 2025-11-05 16:35
關聯方式 ⚠️ 非正式提及 PR 描述中提到但未用關鍵詞

🔍 為什麼沒有自動關閉?

GitHub 自動關閉機制

只有當 PR 描述或 commit 包含以下關鍵詞時,才會自動關閉 Issue:

✅ 會自動關閉:

  • Fixes #557
  • Closes #557
  • Resolves #557
  • Fix #557
  • Close #557
  • Resolve #557

❌ 不會自動關閉(我們的情況):

  • 與 Issue #557 的關係
  • 本 PR 修復了導致該問題的主要原因之一
  • 任何非關鍵詞的提及

PR #564 的實際用詞

### 與 Issue #557 的關係

Issue #557 報告 "AI confidence always shows 0"。
本 PR 修復了導致該問題的主要原因之一:
- Schema 不匹配導致解析失敗 → confidence 默認為 0

結論:提到了 Issue,但沒用關鍵詞,所以不會自動關閉。

🎯 Issue #557 應該怎麼處理?

選項 A: 在 Issue 下評論並手動關閉 ✅ 推薦

gh issue comment 557 --repo NoFxAiOS/nofx --body "$(cat <<'EOCOMMENT'
PR #564 已合併,修復了 confidence schema 不匹配問題(0-1 → 0-100)。

## 📊 修復內容

- ✅ 統一 prompt 文件使用 0-100 整數範圍
- ✅ 防止 AI 輸出 0.85 導致解析失敗 → confidence=0

## 💡 重要發現

經過深入分析,**confidence=0 本身不會阻止交易執行**。後端 `validateDecision()` 完全不檢查 confidence 值。

如果您的交易仍被阻止,請檢查:
1. 風險回報比是否 ≥ 1:3
2. 倉位大小是否足夠(BTC/ETH ≥60 USDT,其他 ≥12 USDT)
3. leverage, stop_loss, take_profit 是否合法

詳細分析見 PR #564 討論。

建議關閉此 Issue,如有其他問題請開新 Issue。
EOCOMMENT
)"

# 然後手動關閉
gh issue close 557 --repo NoFxAiOS/nofx --comment "Fixed by PR #564"

選項 B: 保持 Issue 開放,等待進一步反饋 🟡

如果你想等用戶確認問題是否真的解決,可以:

  • 在 Issue 下評論說明 PR #564 的修復
  • 詢問用戶問題是否仍存在
  • 等待用戶回應後再決定是否關閉

選項 C: 完全不處理 ⚪

讓上游維護者決定如何處理 Issue #557。

📝 推薦的評論內容(簡化版)

## ✅ 已通過 PR #564 修復

修復內容:
- 統一 confidence 範圍從 0-1 → 0-100
- 防止 JSON 解析失敗導致 confidence=0

## ⚠️ 重要說明

**Confidence=0 本身不影響交易執行**。後端不檢查 confidence 值。

如果交易被阻止,可能是其他驗證失敗:
- 風險回報比 < 1:3
- 倉位金額太小
- 其他參數不合法

請檢查完整錯誤日誌以確定真正原因。

---
Ref: PR #564, @zhouyongyou 

the-dev-z avatar Nov 05 '25 16:11 the-dev-z