-
Notifications
You must be signed in to change notification settings - Fork 2k
fix(decision): 添加槓桿超限 fallback 機制並澄清盈虧計算說明 #716
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(decision): 添加槓桿超限 fallback 機制並澄清盈虧計算說明 #716
Conversation
🤖 Advisory Check ResultsThese are advisory checks to help improve code quality. They won't block your PR from being merged. 📋 PR InformationTitle Format: ✅ Good - Follows Conventional Commits 🔧 Backend ChecksGo Formatting: ✅ Good Fix locally: go fmt ./... # Format code
go vet ./... # Check for issues
go test ./... # Run tests⚛️ Frontend ChecksBuild & Type Check: ✅ Success Fix locally: cd web
npm run build # Test build (includes type checking)📖 ResourcesQuestions? 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. |
| if d.Leverage > maxLeverage { | ||
| log.Printf("⚠️ [Leverage Fallback] %s 杠杆超限 (%dx > %dx),自动调整为上限值 %dx", | ||
| d.Symbol, d.Leverage, maxLeverage, maxLeverage) | ||
| d.Leverage = maxLeverage // 自动修正为上限值 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this cause user confusion?
prompts/adaptive.txt
Outdated
| - 信心度 90-95 → 杠杆 2-4x,风险预算 2% | ||
| - 信心度 >95 → 杠杆 4-5x,风险预算 2.5%(谨慎) | ||
|
|
||
| ⚠️ **实际杠杆上限由配置文件决定**(BTC/ETH 和山寨币可能不同),系统会在"硬约束"部分明确告知当前配置的上限值 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this will be sent to AI engine but how can it understand this ?
代码审查报告 - PR #716审查结果:✅ 通过(有建议改进)业务层面审查✅ 需求验证
✅ 功能完整性
✅ Edge Case 处理
技术层面审查✅ 架构合理性
✅ KISS 原则
✅ 测试完备性
🟡 改进建议(非 BLOCKING)建议1:记录 fallback 到决策日志当前:只记录到标准日志 ( // decision/engine.go
if d.Leverage > maxLeverage {
originalLeverage := d.Leverage
d.Leverage = maxLeverage
log.Printf("⚠️ [Leverage Fallback] %s 杠杆超限 (%dx > %dx),自动调整为上限值 %dx",
d.Symbol, originalLeverage, maxLeverage, maxLeverage)
// 建议:记录到决策日志
// d.Warnings = append(d.Warnings, fmt.Sprintf(
// "杠杆超限(%dx -> %dx)", originalLeverage, maxLeverage))
}理由:
建议2:添加指标监控// 建议添加 fallback 计数器
var (
leverageFallbackCounter = promauto.NewCounterVec(
prometheus.CounterOpts{
Name: "leverage_fallback_total",
Help: "Number of leverage fallback events",
},
[]string{"symbol", "original_leverage"},
)
)
// 在 fallback 时增加计数
leverageFallbackCounter.WithLabelValues(d.Symbol, fmt.Sprintf("%d", originalLeverage)).Inc()理由:
建议3:考虑渐进式修正当前:直接修正为上限值 // 可选:修正为上限的 80%(更保守)
d.Leverage = int(float64(maxLeverage) * 0.8)
if d.Leverage < 1 {
d.Leverage = maxLeverage // 避免过小
}权衡:
建议4:Prompt 中添加示例当前:只有文字说明 ⚠️ **实际杠杆上限由配置文件决定**(BTC/ETH 和山寨币可能不同),系统会在"硬约束"部分明确告知当前配置的上限值
**示例**:
- 配置上限 5x(山寨币):即使信心度 >95,最高也只能使用 5x 杠杆
- 配置上限 10x(BTC/ETH):信心度 >95 时,可使用 8-10x 杠杆
- ⚠️ 如果输出的杠杆超过配置上限,系统会自动修正为上限值(并记录警告日志)安全审查(Web3 交易系统)✅ 资金保护
✅ 限额控制
|
f643ab9 to
3b27133
Compare
🤖 Advisory Check ResultsThese are advisory checks to help improve code quality. They won't block your PR from being merged. 📋 PR InformationTitle Format: ✅ Good - Follows Conventional Commits 🔧 Backend ChecksGo Formatting: Files needing formattingGo Vet: ✅ Good Fix locally: go fmt ./... # Format code
go vet ./... # Check for issues
go test ./... # Run tests⚛️ Frontend ChecksBuild & Type Check: ✅ Success Fix locally: cd web
npm run build # Test build (includes type checking)📖 ResourcesQuestions? 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. |
|
此 PR 已獲批准 ✅ 修復內容:
可以合併了嗎?謝謝!🙏 |
3b27133 to
00721a3
Compare
🤖 Advisory Check ResultsThese are advisory checks to help improve code quality. They won't block your PR from being merged. 📋 PR InformationTitle Format: ✅ Good - Follows Conventional Commits 🔧 Backend ChecksGo Formatting: Files needing formattingGo Vet: ✅ Good Fix locally: go fmt ./... # Format code
go vet ./... # Check for issues
go test ./... # Run tests⚛️ Frontend ChecksBuild & Type Check: ✅ Success Fix locally: cd web
npm run build # Test build (includes type checking)📖 ResourcesQuestions? 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. |
1. AI 決策輸出超限槓桿時(如 20x),驗證直接拒絕導致整個交易週期失敗 2. Prompt 未明確說明盈虧百分比已包含槓桿效應,導致 AI 思維鏈中誤用價格變動% - **Before**: 超限直接報錯 → 決策失敗 - **After**: 自動降級為配置上限 → 決策繼續執行 - **效果**: SOLUSDT 20x → 自動修正為 5x(配置上限) - 明確告知 AI:系統提供的「盈虧%」已包含槓桿效應 - 公式: 盈虧% = (未實現盈虧 / 保證金) × 100 - 示例: 5x 槓桿,價格漲 2% = 實際盈利 10% - 測試山寨幣超限修正(20x → 5x) - 測試 BTC/ETH 超限修正(20x → 10x) - 測試正常範圍不修正 - 測試無效槓桿拒絕 ``` PASS: TestLeverageFallback/山寨币杠杆超限_自动修正为上限 PASS: TestLeverageFallback/BTC杠杆超限_自动修正为上限 PASS: TestLeverageFallback/杠杆在上限内_不修正 PASS: TestLeverageFallback/杠杆为0_应该报错 ``` - ✅ 向後兼容:正常槓桿範圍不受影響 - ✅ 容錯性增強:AI 輸出超限時系統自動修正 - ✅ 決策質量提升:AI 對槓桿收益有正確認知 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
00721a3 to
aaef135
Compare
🤖 Advisory Check ResultsThese are advisory checks to help improve code quality. They won't block your PR from being merged. 📋 PR InformationTitle Format: ✅ Good - Follows Conventional Commits 🔧 Backend ChecksGo Formatting: ✅ Good Fix locally: go fmt ./... # Format code
go vet ./... # Check for issues
go test ./... # Run tests⚛️ Frontend ChecksBuild & Type Check: ✅ Success Fix locally: cd web
npm run build # Test build (includes type checking)📖 ResourcesQuestions? 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. |
* fix(decision): 添加槓桿超限 fallback 機制並澄清盈虧計算說明 1. AI 決策輸出超限槓桿時(如 20x),驗證直接拒絕導致整個交易週期失敗 2. Prompt 未明確說明盈虧百分比已包含槓桿效應,導致 AI 思維鏈中誤用價格變動% - **Before**: 超限直接報錯 → 決策失敗 - **After**: 自動降級為配置上限 → 決策繼續執行 - **效果**: SOLUSDT 20x → 自動修正為 5x(配置上限) - 明確告知 AI:系統提供的「盈虧%」已包含槓桿效應 - 公式: 盈虧% = (未實現盈虧 / 保證金) × 100 - 示例: 5x 槓桿,價格漲 2% = 實際盈利 10% - 測試山寨幣超限修正(20x → 5x) - 測試 BTC/ETH 超限修正(20x → 10x) - 測試正常範圍不修正 - 測試無效槓桿拒絕 ``` PASS: TestLeverageFallback/山寨币杠杆超限_自动修正为上限 PASS: TestLeverageFallback/BTC杠杆超限_自动修正为上限 PASS: TestLeverageFallback/杠杆在上限内_不修正 PASS: TestLeverageFallback/杠杆为0_应该报错 ``` - ✅ 向後兼容:正常槓桿範圍不受影響 - ✅ 容錯性增強:AI 輸出超限時系統自動修正 - ✅ 決策質量提升:AI 對槓桿收益有正確認知 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]> * style: apply go fmt after rebase 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]> --------- Co-authored-by: ZhouYongyou <[email protected]> Co-authored-by: Claude <[email protected]>
Pull Request - Backend
🎯 問題描述
槓桿超限導致決策失敗
現象:
根本原因:
config.json配置altcoin_leverage: 5(子帳戶限制)✅ 解決方案
槓桿超限自動修正 (decision/engine.go)
Before(舊邏輯):
After(新邏輯):
效果:
單元測試覆蓋 (decision/validate_test.go)
新增
TestLeverageFallback,覆蓋 4 個場景:🧪 測試結果
📊 修復效果對比
[Leverage Fallback]📦 變更文件
decision/engine.go- 智能修正邏輯(+8 lines, -2 lines)decision/validate_test.go- 單元測試(+100 lines, new file)總計: +108 lines, -2 lines
🔍 技術細節
向後兼容性
多層防禦架構
🚀 下次交易週期預期行為
📝 Note
原本 PR 包含
prompts/adaptive.txt的修改,但該文件已在 PR #712 和後續 commit 中被重構/刪除。本 PR 現在專注於後端槓桿驗證邏輯的改進,與 prompt 系統無關。
🤖 Generated with Claude Code
Co-Authored-By: Claude [email protected]
🎯 Type of Change | 變更類型
🔗 Related Issues | 相關 Issue
🧪 Testing | 測試
Test Environment | 測試環境
Manual Testing | 手動測試
TestLeverageFallback)🔒 Security Considerations | 安全考慮
⚡ Performance Impact | 性能影響
If impacted, explain | 如果受影響,請說明:
無顯著影響。Fallback 機制僅在槓桿超限時觸發,不影響正常流程性能。
✅ Checklist | 檢查清單
Code Quality | 代碼質量
go build)go fmt| 已運行go fmtDocumentation | 文檔
Git
devbranch | 已 rebase 到最新dev分支By submitting this PR, I confirm | 提交此 PR,我確認:
🤖 Generated with Claude Code
Co-Authored-By: Claude [email protected]