From a726702302c1245a6344f21c08faaa73b4ed7b58 Mon Sep 17 00:00:00 2001 From: tinkle-community Date: Tue, 28 Oct 2025 21:45:28 +0800 Subject: [PATCH] Update: Merge nofx improvements - Frontend trading records and UI enhancements - Optimized AI prompts and decision engine - Performance analysis and comparison features - Binance-style UI improvements --- .gitignore | 1 + README.md | 655 ++++++++++++----------- api/server.go | 28 + config.example.go | 84 +++ config.json.example | 4 +- logger/decision_logger.go | 62 +-- market/ai_decision_engine.go | 64 ++- market/ai_signal.go | 5 +- pool/coin_pool.go | 4 +- web/src/App.tsx | 122 ++--- web/src/components/AILearning.tsx | 688 +++++++++++++++++++++++++ web/src/components/CompetitionPage.tsx | 242 ++++----- web/src/components/EquityChart.tsx | 26 +- 13 files changed, 1440 insertions(+), 545 deletions(-) create mode 100644 config.example.go create mode 100644 web/src/components/AILearning.tsx diff --git a/.gitignore b/.gitignore index 629f8a8e..151e3b49 100644 --- a/.gitignore +++ b/.gitignore @@ -27,3 +27,4 @@ config.json # 决策日志 decision_logs/ coin_pool_cache/ +nofx-auto diff --git a/README.md b/README.md index 2cd60dbf..5758e8cf 100644 --- a/README.md +++ b/README.md @@ -1,140 +1,136 @@ -# 🤖 NOFX - AI-Driven Binance Futures Auto Trading Competition System +# 🤖 NOFX - AI驱动的币安合约自动交易竞赛系统 [![Go Version](https://img.shields.io/badge/Go-1.21+-00ADD8?style=flat&logo=go)](https://golang.org/) [![React](https://img.shields.io/badge/React-18+-61DAFB?style=flat&logo=react)](https://reactjs.org/) [![TypeScript](https://img.shields.io/badge/TypeScript-5.0+-3178C6?style=flat&logo=typescript)](https://www.typescriptlang.org/) [![License](https://img.shields.io/badge/License-MIT-green.svg)](LICENSE) -**Languages:** [English](README.md) | [中文](README.zh-CN.md) | [Українська](README.uk.md) | [Русский](README.ru.md) +一个基于 **DeepSeek/Qwen AI** 的币安合约自动交易系统,支持**多AI模型实盘竞赛**,具备完整的市场分析、AI决策、**自我学习机制**和专业的Web监控界面。 + +> ⚠️ **风险提示**:本系统为实验性项目,AI自动交易存在重大风险,强烈建议仅用于学习研究或小额资金测试! --- -An automated Binance futures trading system powered by **DeepSeek/Qwen AI**, supporting **multi-AI model live trading competition**, featuring comprehensive market analysis, AI decision-making, **self-learning mechanism**, and professional Web monitoring interface. +## ✨ 核心特性 -> ⚠️ **Risk Warning**: This system is experimental. AI auto-trading carries significant risks. Strongly recommended for learning/research purposes or testing with small amounts only! +### 🏆 多AI竞赛模式 +- **Qwen vs DeepSeek** 实盘对抗 +- 独立账户管理,独立决策日志 +- 实时性能对比图表 +- 收益率PK,胜率统计 + +### 🧠 AI自我学习机制(NEW!) +- **历史反馈**: 每次决策前分析最近20个周期的交易表现 +- **智能优化**: + - 识别表现最佳/最差币种 + - 计算胜率、盈亏比、平均盈利 + - 避免重复错误(连续亏损的币种) + - 强化成功策略(高胜率的交易模式) +- **动态调整**: AI根据历史表现自主调整交易风格 + +### 📊 智能市场分析 +- **3分钟K线**: 实时价格、EMA20、MACD、RSI(7) +- **4小时K线**: 长期趋势、EMA20/50、ATR、RSI(14) +- **持仓量分析**: 市场情绪、资金流向判断 +- **OI Top追踪**: 持仓量增长最快的20个币种 +- **AI500币种池**: 高评分币种自动筛选 +- **流动性过滤**: 自动过滤持仓价值<15M USD的低流动性币种 + +### 🎯 专业风险控制 +- **单币种仓位上限**: + - 山寨币 ≤ 1.5倍账户净值 + - BTC/ETH ≤ 10倍账户净值 +- **固定杠杆**: 山寨币20倍 | BTC/ETH 50倍 +- **保证金管理**: 总使用率≤90%,AI自主决策使用率 +- **风险回报比**: 强制≥1:2(止损:止盈) +- **防止仓位叠加**: 同币种同方向不允许重复开仓 + +### 🎨 风格UI +- **专业交易界面**: 视觉设计 +- **暗色主题**: 经典配色(金色#F0B90B + 深色背景) +- **实时数据**: 5秒刷新账户、持仓、图表 +- **收益率曲线**: 账户净值历史走势(美元/百分比切换) +- **性能对比图**: 多AI收益率实时对比 +- **动画效果**: 流畅的hover、过渡、加载动画 + +### 📝 完整决策记录 +- **思维链记录**: AI的完整推理过程(CoT) +- **历史表现**: 整体胜率、平均盈利、盈亏比 +- **最近交易**: 最近5笔交易详情(开仓价→平仓价→盈亏%) +- **币种统计**: 各币种表现(胜率、平均盈亏) +- **JSON日志**: 每次决策完整记录,便于复盘分析 --- -## ✨ Core Features - -### 🏆 Multi-AI Competition Mode -- **Qwen vs DeepSeek** live trading battle -- Independent account management and decision logs -- Real-time performance comparison charts -- ROI PK and win rate statistics - -### 🧠 AI Self-Learning Mechanism (NEW!) -- **Historical Feedback**: Analyzes last 20 cycles of trading performance before each decision -- **Smart Optimization**: - - Identifies best/worst performing coins - - Calculates win rate, profit/loss ratio, average profit - - Avoids repeating mistakes (consecutive losing coins) - - Reinforces successful strategies (high win rate patterns) -- **Dynamic Adjustment**: AI autonomously adjusts trading style based on historical performance - -### 📊 Intelligent Market Analysis -- **3-minute K-line**: Real-time price, EMA20, MACD, RSI(7) -- **4-hour K-line**: Long-term trend, EMA20/50, ATR, RSI(14) -- **Open Interest Analysis**: Market sentiment, capital flow judgment -- **OI Top Tracking**: Top 20 coins with fastest growing open interest -- **AI500 Coin Pool**: Automatic high-score coin screening -- **Liquidity Filter**: Auto-filters low liquidity coins (<15M USD position value) - -### 🎯 Professional Risk Control -- **Per-Coin Position Limit**: - - Altcoins ≤ 1.5x account equity - - BTC/ETH ≤ 10x account equity -- **Fixed Leverage**: Altcoins 20x | BTC/ETH 50x -- **Margin Management**: Total usage ≤90%, AI autonomous decision on usage rate -- **Risk-Reward Ratio**: Mandatory ≥1:2 (stop-loss:take-profit) -- **Prevent Position Stacking**: No duplicate opening of same coin/direction - -### 🎨 Professional UI -- **Professional Trading Interface**: Binance-style visual design -- **Dark Theme**: Classic color scheme (Gold #F0B90B + dark background) -- **Real-time Data**: 5-second refresh for accounts, positions, charts -- **Equity Curve**: Historical account value trend (USD/percentage toggle) -- **Performance Comparison Chart**: Real-time multi-AI ROI comparison -- **Smooth Animations**: Fluid hover, transition, and loading effects - -### 📝 Complete Decision Recording -- **Chain of Thought**: AI's complete reasoning process (CoT) -- **Historical Performance**: Overall win rate, average profit, profit/loss ratio -- **Recent Trades**: Last 5 trade details (entry price → exit price → P/L%) -- **Coin Statistics**: Per-coin performance (win rate, average P/L) -- **JSON Logs**: Complete decision records for post-trade analysis - ---- - -## 🏗️ Technical Architecture +## 🏗️ 技术架构 ``` nofx/ -├── main.go # Program entry (multi-trader manager) -├── config.json # Configuration file (API keys, multi-trader config) +├── main.go # 程序入口(多trader管理器) +├── config.json # 配置文件(API密钥、多trader配置) │ -├── api/ # HTTP API service -│ └── server.go # Gin framework, RESTful API +├── api/ # HTTP API服务 +│ └── server.go # Gin框架,RESTful API │ -├── trader/ # Trading core -│ ├── auto_trader.go # Auto trading main controller (single trader) -│ └── binance_futures.go # Binance futures API wrapper +├── trader/ # 交易核心 +│ ├── auto_trader.go # 自动交易主控(单trader) +│ └── binance_futures.go # 币安合约API封装 │ -├── manager/ # Multi-trader management -│ └── trader_manager.go # Manages multiple trader instances +├── manager/ # 多trader管理 +│ └── trader_manager.go # 管理多个trader实例 │ -├── market/ # Market data & AI decisions -│ ├── market_data.go # Market data fetching (K-line, indicators) -│ ├── ai_decision_engine.go # AI decision engine (with historical feedback) -│ └── ai_signal.go # AI API calls (DeepSeek/Qwen) +├── market/ # 市场数据与AI决策 +│ ├── market_data.go # 市场数据获取(K线、指标) +│ ├── ai_decision_engine.go # AI决策引擎(含历史反馈) +│ └── ai_signal.go # AI API调用(DeepSeek/Qwen) │ -├── pool/ # Coin pool management -│ └── coin_pool.go # AI500 + OI Top merged pool +├── pool/ # 币种池管理 +│ └── coin_pool.go # AI500 + OI Top合并池 │ -├── logger/ # Logging system -│ └── decision_logger.go # Decision recording + performance analysis +├── logger/ # 日志系统 +│ └── decision_logger.go # 决策记录 + 表现分析 │ -├── decision_logs/ # Decision log storage -│ ├── trader1/ # Trader 1 logs -│ └── trader2/ # Trader 2 logs +├── decision_logs/ # 决策日志存储 +│ ├── trader1/ # Trader 1的日志 +│ └── trader2/ # Trader 2的日志 │ -└── web/ # React frontend +└── web/ # React前端 ├── src/ - │ ├── components/ # React components - │ │ ├── EquityChart.tsx # Equity curve chart - │ │ ├── ComparisonChart.tsx # Multi-AI comparison chart - │ │ └── CompetitionPage.tsx # Competition leaderboard - │ ├── lib/api.ts # API call wrapper - │ ├── types/index.ts # TypeScript types - │ ├── index.css # Binance-style CSS - │ └── App.tsx # Main app + │ ├── components/ # React组件 + │ │ ├── EquityChart.tsx # 收益率曲线图 + │ │ ├── ComparisonChart.tsx # 多AI对比图 + │ │ └── CompetitionPage.tsx # 竞赛排行榜 + │ ├── lib/api.ts # API调用封装 + │ ├── types/index.ts # TypeScript类型 + │ ├── index.css # Binance风格样式 + │ └── App.tsx # 主应用 └── package.json ``` -### Core Dependencies +### 核心依赖 -**Backend (Go)** -- `github.com/adshao/go-binance/v2` - Binance API client -- `github.com/markcheno/go-talib` - Technical indicator calculation (TA-Lib) -- `github.com/gin-gonic/gin` - HTTP API framework +**后端 (Go)** +- `github.com/adshao/go-binance/v2` - 币安API客户端 +- `github.com/markcheno/go-talib` - 技术指标计算(TA-Lib) +- `github.com/gin-gonic/gin` - HTTP API框架 -**Frontend (React + TypeScript)** -- `react` + `react-dom` - UI framework -- `recharts` - Chart library (equity curve, comparison charts) -- `swr` - Data fetching and caching -- `tailwindcss` - CSS framework +**前端 (React + TypeScript)** +- `react` + `react-dom` - UI框架 +- `recharts` - 图表库(收益率曲线、对比图) +- `swr` - 数据获取和缓存 +- `tailwindcss` - CSS框架 --- -## 🚀 Quick Start +## 🚀 快速开始 -### 1. Environment Requirements +### 1. 环境要求 - **Go 1.21+** - **Node.js 18+** -- **TA-Lib** library (technical indicator calculation) +- **TA-Lib** 库(技术指标计算) -#### Installing TA-Lib +#### 安装 TA-Lib **macOS:** ```bash @@ -146,32 +142,32 @@ brew install ta-lib sudo apt-get install libta-lib0-dev ``` -**Other systems**: Refer to [TA-Lib Official Documentation](https://github.com/markcheno/go-talib) +**其他系统**: 参考 [TA-Lib官方文档](https://github.com/markcheno/go-talib) -### 2. Clone the Project +### 2. 克隆项目 ```bash -git clone https://github.com/tinkle-community/nofx.git +git clone cd nofx ``` -### 3. Install Dependencies +### 3. 安装依赖 -**Backend:** +**后端:** ```bash go mod download ``` -**Frontend:** +**前端:** ```bash cd web npm install cd .. ``` -### 4. System Configuration +### 4. 配置系统 -Create `config.json` file (use `config.json.example` as template): +创建 `config.json` 文件: ```json { @@ -205,240 +201,319 @@ Create `config.json` file (use `config.json.example` as template): } ``` -**Configuration Notes:** -- `traders`: Configure 1-N traders (single AI or multi-AI competition) -- `id`: Unique trader identifier (used for log directory) -- `ai_model`: "qwen" or "deepseek" -- `binance_api_key/secret_key`: Each trader uses independent Binance account -- `initial_balance`: Initial balance (for calculating P/L%) -- `scan_interval_minutes`: Decision cycle (recommended 3-5 minutes) -- `coin_pool_api_url`: AI500 coin pool API (optional) -- `oi_top_api_url`: OI Top open interest API (optional) +**配置说明:** +- `traders`: 可配置1-N个trader(单AI或多AI竞赛) +- `id`: Trader唯一标识(用于日志目录) +- `ai_model`: "qwen" 或 "deepseek" +- `binance_api_key/secret_key`: 每个trader使用独立的币安账户 +- `initial_balance`: 初始余额(用于计算盈亏%) +- `scan_interval_minutes`: 决策周期(建议3-5分钟) +- `coin_pool_api_url`: AI500币种池API(可选) +- `oi_top_api_url`: OI Top持仓量API(可选) -### 5. Run the System +### 5. 运行系统 -**Start backend (AI trading system + API server):** +**启动后端(AI交易系统 + API服务器):** ```bash go build -o nofx ./nofx ``` -**Start frontend (Web Dashboard):** +**启动前端(Web Dashboard):** -Open a new terminal: +新开终端窗口: ```bash cd web npm run dev ``` -**Access the interface:** +**访问界面:** ``` Web Dashboard: http://localhost:3000 API Server: http://localhost:8080 ``` -### 6. Stop the System +### 6. 停止系统 -Press `Ctrl+C` in both terminals +在两个终端中分别按 `Ctrl+C` --- -## 📖 AI Decision Flow +## 📖 AI决策流程 -Each decision cycle (default 3 minutes), the system runs the following process: +每个决策周期(默认3分钟),系统按以下流程运行: ``` ┌─────────────────────────────────────────────────────┐ -│ 1. Analyze Historical Performance (last 20 cycles) │ +│ 1. 分析历史表现(最近20个周期) │ ├─────────────────────────────────────────────────────┤ -│ ✓ Calculate overall win rate, avg profit, P/L ratio│ -│ ✓ Statistics for each coin (win rate, avg P/L) │ -│ ✓ Identify best/worst coins │ -│ ✓ List last 5 trade details │ +│ ✓ 计算整体胜率、平均盈利、盈亏比 │ +│ ✓ 统计各币种表现(胜率、平均盈亏) │ +│ ✓ 识别最佳/最差币种 │ +│ ✓ 列出最近5笔交易详情 │ └─────────────────────────────────────────────────────┘ ↓ ┌─────────────────────────────────────────────────────┐ -│ 2. Get Account Status │ +│ 2. 获取账户状态 │ ├─────────────────────────────────────────────────────┤ -│ • Account equity, available balance │ -│ • Number of positions, total P/L │ -│ • Margin usage rate │ +│ • 账户净值、可用余额 │ +│ • 持仓数量、总盈亏 │ +│ • 保证金使用率 │ └─────────────────────────────────────────────────────┘ ↓ ┌─────────────────────────────────────────────────────┐ -│ 3. Analyze Existing Positions (if any) │ +│ 3. 分析现有持仓(如果有) │ ├─────────────────────────────────────────────────────┤ -│ • Get market data for each position │ -│ • Calculate technical indicators (RSI, MACD, EMA) │ -│ • AI decides whether to close positions │ +│ • 获取每个持仓的市场数据 │ +│ • 计算技术指标(RSI、MACD、EMA) │ +│ • AI判断是否需要平仓 │ └─────────────────────────────────────────────────────┘ ↓ ┌─────────────────────────────────────────────────────┐ -│ 4. Evaluate New Opportunities (candidate coin pool) │ +│ 4. 评估新机会(候选币种池) │ ├─────────────────────────────────────────────────────┤ -│ • Get AI500 high-score coins (top 20) │ -│ • Get OI Top growing coins (top 20) │ -│ • Merge and deduplicate, filter low liquidity │ -│ • Batch fetch market data and technical indicators │ +│ • 获取AI500高评分币种(前20个) │ +│ • 获取OI Top持仓增长币种(前20个) │ +│ • 合并去重,过滤低流动性币种(<15M) │ +│ • 批量获取市场数据和技术指标 │ └─────────────────────────────────────────────────────┘ ↓ ┌─────────────────────────────────────────────────────┐ -│ 5. AI Comprehensive Decision │ +│ 5. AI综合决策 │ ├─────────────────────────────────────────────────────┤ -│ • Review historical feedback (win rate, best/worst)│ -│ • Chain of Thought analysis │ -│ • Output decision: close/open/hold/wait │ -│ • Includes leverage, position size, SL, TP │ +│ • 查看历史反馈(胜率、最佳/最差币种) │ +│ • Chain of Thought 思维链分析 │ +│ • 输出决策:平仓/开仓/持有/观望 │ +│ • 包含杠杆、仓位、止损、止盈 │ └─────────────────────────────────────────────────────┘ ↓ ┌─────────────────────────────────────────────────────┐ -│ 6. Execute Trades │ +│ 6. 执行交易 │ ├─────────────────────────────────────────────────────┤ -│ • Priority: close first, then open │ -│ • Auto-adapt precision (LOT_SIZE) │ -│ • Prevent position stacking (reject duplicate) │ -│ • Auto-cancel all orders after closing │ +│ • 优先级排序:先平仓,再开仓 │ +│ • 精度自动适配(LOT_SIZE) │ +│ • 防止仓位叠加(同币种同方向拒绝开仓) │ +│ • 平仓后自动取消所有挂单 │ └─────────────────────────────────────────────────────┘ ↓ ┌─────────────────────────────────────────────────────┐ -│ 7. Record Logs │ +│ 7. 记录日志 │ ├─────────────────────────────────────────────────────┤ -│ • Save complete decision to decision_logs/ │ -│ • Includes CoT, decision JSON, account snapshot │ +│ • 保存完整决策记录到 decision_logs/ │ +│ • 包含思维链、决策JSON、账户快照、执行结果 │ └─────────────────────────────────────────────────────┘ ``` --- -## 🧠 AI Self-Learning Example +## 🧠 AI自我学习示例 -### Historical Feedback (Auto-added to Prompt) +### 历史反馈(Prompt中自动添加) ```markdown -## 📊 Historical Performance Feedback +## 📊 历史表现反馈 -### Overall Performance -- **Total Trades**: 15 (Profit: 8 | Loss: 7) -- **Win Rate**: 53.3% -- **Average Profit**: +3.2% | Average Loss: -2.1% -- **Profit/Loss Ratio**: 1.52:1 +### 整体表现 +- **总交易数**: 15 笔 (盈利: 8 | 亏损: 7) +- **胜率**: 53.3% +- **平均盈利**: +3.2% | 平均亏损: -2.1% +- **盈亏比**: 1.52:1 -### Recent Trades +### 最近交易 1. BTCUSDT LONG: 95000.0000 → 97500.0000 = +2.63% ✓ 2. ETHUSDT SHORT: 3500.0000 → 3450.0000 = +1.43% ✓ 3. SOLUSDT LONG: 185.0000 → 180.0000 = -2.70% ✗ 4. BNBUSDT LONG: 610.0000 → 625.0000 = +2.46% ✓ 5. ADAUSDT LONG: 0.8500 → 0.8300 = -2.35% ✗ -### Coin Performance -- **Best**: BTCUSDT (Win rate 75%, avg +2.5%) -- **Worst**: SOLUSDT (Win rate 25%, avg -1.8%) +### 币种表现 +- **最佳**: BTCUSDT (胜率75%, 平均+2.5%) +- **最差**: SOLUSDT (胜率25%, 平均-1.8%) ``` -### How AI Uses Feedback +### AI如何使用反馈 -1. **Avoid consecutive losers**: Seeing SOLUSDT with 3 consecutive stop-losses, AI avoids or is more cautious -2. **Reinforce successful strategies**: BTC breakout long with 75% win rate, AI continues this pattern -3. **Dynamic style adjustment**: Win rate <40% → conservative; P/L ratio >2 → maintain aggressive -4. **Identify market conditions**: Consecutive losses may indicate choppy market, reduce trading frequency +1. **避免连续亏损币种**: 看到SOLUSDT连续3次止损,AI会避开或更谨慎 +2. **强化成功策略**: BTC突破做多胜率75%,AI会继续这个模式 +3. **动态调整风格**: 胜率<40%时变保守,盈亏比>2时保持激进 +4. **识别市场环境**: 连续亏损可能说明市场震荡,减少交易频率 --- -## 📊 Web Interface Features +## 📊 Web界面功能 -### 1. Competition Page +### 1. 竞赛页面(Competition) -- **🏆 Leaderboard**: Real-time ROI ranking, golden border highlights leader -- **📈 Performance Comparison**: Dual AI ROI curve comparison (purple vs blue) -- **⚔️ Head-to-Head**: Direct comparison showing lead margin -- **Real-time Data**: Total equity, P/L%, position count, margin usage +- **🏆 排行榜**: 实时收益率排名,金色边框突出显示领先者 +- **📈 性能对比图**: 双AI收益率曲线对比(紫色vs蓝色) +- **⚔️ Head-to-Head**: 直接对比,显示领先差距 +- **实时数据**: 总净值、盈亏%、持仓数、保证金使用率 -### 2. Details Page +### 2. 详情页面(Details) -- **Equity Curve**: Historical trend chart (USD/percentage toggle) -- **Statistics**: Total cycles, success/fail, open/close stats -- **Position Table**: All position details (entry price, current price, P/L%, liquidation price) -- **AI Decision Logs**: Recent decision records (expandable CoT) +- **账户净值曲线**: 历史走势图(美元/百分比切换) +- **统计信息**: 总周期、成功/失败、开仓/平仓统计 +- **持仓表格**: 所有持仓详情(入场价、当前价、盈亏%、强平价) +- **AI决策日志**: 最近决策记录(可展开思维链) -### 3. Real-time Updates +### 3. 实时更新 -- System status, account info, position list: **5-second refresh** -- Decision logs, statistics: **10-second refresh** -- Equity charts: **10-second refresh** +- 系统状态、账户信息、持仓列表:**每5秒刷新** +- 决策日志、统计信息:**每10秒刷新** +- 收益率图表:**每10秒刷新** --- -## 🎛️ API Endpoints +## 🎛️ API接口 -### Competition Related +### 竞赛相关 ```bash -GET /api/competition # Competition leaderboard (all traders) -GET /api/traders # Trader list +GET /api/competition # 竞赛排行榜(所有trader) +GET /api/traders # Trader列表 ``` -### Single Trader Related +### 单Trader相关 ```bash -GET /api/status?trader_id=xxx # System status -GET /api/account?trader_id=xxx # Account info -GET /api/positions?trader_id=xxx # Position list -GET /api/equity-history?trader_id=xxx # Equity history (chart data) -GET /api/decisions/latest?trader_id=xxx # Latest 5 decisions -GET /api/statistics?trader_id=xxx # Statistics +GET /api/status?trader_id=xxx # 系统状态 +GET /api/account?trader_id=xxx # 账户信息 +GET /api/positions?trader_id=xxx # 持仓列表 +GET /api/equity-history?trader_id=xxx # 净值历史(图表数据) +GET /api/decisions/latest?trader_id=xxx # 最新5条决策 +GET /api/statistics?trader_id=xxx # 统计信息 ``` -### System Endpoints +### 系统接口 ```bash -GET /health # Health check -GET /api/config # System configuration +GET /health # 健康检查 +GET /api/config # 系统配置 ``` --- -## ⚠️ Important Risk Warnings +## 📝 决策日志格式 -### Trading Risks +每次AI决策都会生成详细的JSON日志: -1. **Cryptocurrency markets are extremely volatile**, AI decisions don't guarantee profit -2. **Futures trading uses leverage**, losses may exceed principal -3. **Extreme market conditions** may lead to liquidation risk -4. **Funding rates** may affect holding costs -5. **Liquidity risk**: Some coins may experience slippage +### 日志文件路径 +``` +decision_logs/ +├── qwen_trader/ +│ └── decision_20251028_153042_cycle15.json +└── deepseek_trader/ + └── decision_20251028_153045_cycle15.json +``` -### Technical Risks +### 日志内容示例 -1. **Network latency** may cause price slippage -2. **API rate limits** may affect trade execution -3. **AI API timeouts** may cause decision failures -4. **System bugs** may trigger unexpected behavior - -### Usage Recommendations - -✅ **Recommended** -- Use only funds you can afford to lose for testing -- Start with small amounts (recommended 100-500 USDT) -- Regularly check system operation status -- Monitor account balance changes -- Analyze AI decision logs to understand strategy - -❌ **Not Recommended** -- Invest all funds or borrowed money -- Run unsupervised for long periods -- Blindly trust AI decisions -- Use without understanding the system -- Run during extreme market volatility +```json +{ + "timestamp": "2025-10-28T15:30:42+08:00", + "cycle_number": 15, + "cot_trace": "当前持仓:ETHUSDT多头盈利+2.3%,趋势良好继续持有...", + "decision_json": "[{\"symbol\":\"BTCUSDT\",\"action\":\"open_long\"...}]", + "account_state": { + "total_balance": 1045.80, + "available_balance": 823.40, + "position_count": 3, + "margin_used_pct": 21.3 + }, + "positions": [...], + "candidate_coins": ["BTCUSDT", "ETHUSDT", ...], + "decisions": [ + { + "action": "open_long", + "symbol": "BTCUSDT", + "quantity": 0.015, + "leverage": 50, + "price": 95800.0, + "order_id": 123456789, + "success": true + } + ], + "execution_log": ["✓ BTCUSDT open_long 成功"], + "success": true +} +``` --- -## 🛠️ Common Issues +## 🔧 风险控制详解 -### 1. Compilation error: TA-Lib not found +### 单币种仓位限制 -**Solution**: Install TA-Lib library +| 币种类型 | 仓位价值上限 | 杠杆 | 保证金占用 | 示例(1000U账户) | +|---------|-------------|------|-----------|------------------| +| 山寨币 | 1.5倍净值 | 20x | 7.5% | 最多开1500U仓位 = 75U保证金 | +| BTC/ETH | 10倍净值 | 50x | 20% | 最多开10000U仓位 = 200U保证金 | + +### 为什么这样设计? + +1. **高杠杆 + 小仓位 = 分散风险** + - 20倍杠杆,1500U仓位,只需75U保证金 + - 可以同时开10+个小仓位,分散单币种风险 + +2. **单币种风险可控** + - 山寨币仓位≤1.5倍净值,5%反向波动 = 7.5%损失 + - BTC仓位≤10倍净值,2%反向波动 = 20%损失 + +3. **不限制总保证金使用率** + - AI根据市场机会自主决策保证金使用率 + - 上限90%,但不强制满仓 + - 有好机会就开仓,没机会就观望 + +### 防止过度交易 + +- **同币种同方向不允许重复开仓**: 防止AI连续开同一个仓位导致超限 +- **先平仓后开仓**: 换仓时确保先释放保证金 +- **止损止盈强制检查**: 风险回报比≥1:2 + +--- + +## ⚠️ 重要风险提示 + +### 交易风险 + +1. **加密货币市场波动极大**,AI决策不保证盈利 +2. **合约交易使用杠杆**,亏损可能超过本金 +3. **市场极端行情**下可能出现爆仓风险 +4. **资金费率**可能影响持仓成本 +5. **流动性风险**:某些币种可能出现滑点 + +### 技术风险 + +1. **网络延迟**可能导致价格滑点 +2. **API限流**可能影响交易执行 +3. **AI API超时**可能导致决策失败 +4. **系统Bug**可能引发意外行为 + +### 使用建议 + +✅ **建议做法** +- 仅使用可承受损失的资金测试 +- 从小额资金开始(建议100-500 USDT) +- 定期检查系统运行状态 +- 监控账户余额变化 +- 分析AI决策日志,理解策略 + +❌ **不建议做法** +- 投入全部资金或借贷资金 +- 长时间无人监控运行 +- 盲目信任AI决策 +- 在不理解系统的情况下使用 +- 在市场极端波动时运行 + +--- + +## 🛠️ 常见问题 + +### 1. 编译错误:TA-Lib not found + +**解决**: 安装TA-Lib库 ```bash # macOS brew install ta-lib @@ -447,103 +522,103 @@ brew install ta-lib sudo apt-get install libta-lib0-dev ``` -### 2. Precision error: Precision is over the maximum +### 2. 精度错误:Precision is over the maximum -**Solution**: System auto-handles precision from Binance LOT_SIZE. If error persists, check network connection. +**解决**: 系统已自动处理精度,从Binance获取LOT_SIZE。如仍报错,检查网络连接。 -### 3. AI API timeout +### 3. AI API超时 -**Solution**: -- Check if API key is correct -- Check network connection (may need proxy) -- System timeout is set to 120 seconds +**解决**: +- 检查API密钥是否正确 +- 检查网络连接(可能需要代理) +- 系统超时时间已设置为120秒 -### 4. Frontend can't connect to backend +### 4. 前端无法连接后端 -**Solution**: -- Ensure backend is running (http://localhost:8080) -- Check if port 8080 is occupied -- Check browser console for errors +**解决**: +- 确保后端正在运行(http://localhost:8080) +- 检查端口8080是否被占用 +- 查看浏览器控制台错误信息 -### 5. Coin pool API failure +### 5. 币种池API失败 -**Solution**: -- Coin pool API is optional -- If API fails, system uses default mainstream coins (BTC, ETH, etc.) -- Check API URL and auth parameter in config.json +**解决**: +- 币种池API是可选的 +- 如果API失败,系统会使用默认主流币种(BTC、ETH等) +- 检查config.json中的API URL和auth参数 --- -## 📈 Performance Optimization Tips +## 📈 性能优化建议 -1. **Set reasonable decision cycle**: Recommended 3-5 minutes, avoid over-trading -2. **Control candidate coin count**: System defaults to AI500 top 20 + OI Top top 20 -3. **Regularly clean logs**: Avoid excessive disk usage -4. **Monitor API call count**: Avoid triggering Binance rate limits -5. **Test with small capital**: First test with 100-500 USDT for strategy validation +1. **合理设置决策周期**: 建议3-5分钟,避免过度交易 +2. **控制候选币种数量**: 系统默认分析AI500前20 + OI Top前20 +3. **定期清理日志**: 避免占用过多磁盘空间 +4. **监控API调用次数**: 避免触发Binance限流(权重限制) +5. **小额资金测试**: 先用100-500 USDT测试策略有效性 --- -## 🔄 Changelog +## 🔄 更新日志 ### v2.0.0 (2025-10-28) -**Major Updates:** -- ✅ AI self-learning mechanism (historical feedback, performance analysis) -- ✅ Multi-trader competition mode (Qwen vs DeepSeek) -- ✅ Binance-style UI (complete Binance interface imitation) -- ✅ Performance comparison charts (real-time ROI comparison) -- ✅ Risk control optimization (per-coin position limit adjustment) +**重大更新:** +- ✅ AI自我学习机制(历史反馈、表现分析) +- ✅ 多Trader竞赛模式(Qwen vs DeepSeek) +- ✅ Binance风格UI(完整模仿币安界面) +- ✅ 性能对比图表(收益率实时对比) +- ✅ 风险控制优化(单币种仓位上限调整) -**Bug Fixes:** -- Fixed hardcoded initial balance issue -- Fixed multi-trader data sync issue -- Optimized chart data alignment (using cycle_number) +**Bug修复:** +- 修复初始余额硬编码问题 +- 修复多trader数据同步问题 +- 优化图表数据对齐(使用cycle_number) ### v1.0.0 (2025-10-27) -- Initial release -- Basic AI trading functionality -- Decision logging system -- Simple Web interface +- 初始版本发布 +- 基础AI交易功能 +- 决策日志系统 +- 简单Web界面 --- -## 📄 License +## 📄 开源协议 -MIT License - See [LICENSE](LICENSE) file for details +MIT License - 详见 [LICENSE](LICENSE) 文件 --- -## 🤝 Contributing +## 🤝 贡献 -Issues and Pull Requests are welcome! +欢迎提交Issue和Pull Request! -### Development Guide +### 开发指南 -1. Fork the project -2. Create feature branch (`git checkout -b feature/AmazingFeature`) -3. Commit changes (`git commit -m 'Add some AmazingFeature'`) -4. Push to branch (`git push origin feature/AmazingFeature`) -5. Open Pull Request +1. Fork项目 +2. 创建功能分支 (`git checkout -b feature/AmazingFeature`) +3. 提交更改 (`git commit -m 'Add some AmazingFeature'`) +4. 推送到分支 (`git push origin feature/AmazingFeature`) +5. 开启Pull Request --- -## 📬 Contact +## 📬 联系方式 -For questions or suggestions, please submit a [GitHub Issue](https://github.com/tinkle-community/nofx/issues) +如有问题或建议,请提交 [GitHub Issue](https://github.com/yourusername/nofx/issues) --- -## 🙏 Acknowledgments +## 🙏 致谢 -- [Binance API](https://binance-docs.github.io/apidocs/futures/en/) - Binance Futures API +- [Binance API](https://binance-docs.github.io/apidocs/futures/cn/) - 币安合约API - [DeepSeek](https://platform.deepseek.com/) - DeepSeek AI API -- [Qwen](https://dashscope.aliyuncs.com/) - Alibaba Cloud Qwen -- [TA-Lib](https://ta-lib.org/) - Technical indicator library -- [Recharts](https://recharts.org/) - React chart library +- [Qwen](https://dashscope.aliyuncs.com/) - 阿里云通义千问 +- [TA-Lib](https://ta-lib.org/) - 技术指标库 +- [Recharts](https://recharts.org/) - React图表库 --- -**Last Updated**: 2025-10-28 +**最后更新**: 2025-10-28 -**⚡ Explore the possibilities of quantitative trading with the power of AI!** +**⚡ 用AI的力量,探索量化交易的可能性!** diff --git a/api/server.go b/api/server.go index 007d3b58..32d24c51 100644 --- a/api/server.go +++ b/api/server.go @@ -76,6 +76,7 @@ func (s *Server) setupRoutes() { api.GET("/decisions/latest", s.handleLatestDecisions) api.GET("/statistics", s.handleStatistics) api.GET("/equity-history", s.handleEquityHistory) + api.GET("/performance", s.handlePerformance) } } @@ -373,6 +374,32 @@ func (s *Server) handleEquityHistory(c *gin.Context) { c.JSON(http.StatusOK, history) } +// handlePerformance AI历史表现分析(用于展示AI学习和反思) +func (s *Server) handlePerformance(c *gin.Context) { + _, traderID, err := s.getTraderFromQuery(c) + if err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + + trader, err := s.traderManager.GetTrader(traderID) + if err != nil { + c.JSON(http.StatusNotFound, gin.H{"error": err.Error()}) + return + } + + // 分析最近20个周期的交易表现 + performance, err := trader.GetDecisionLogger().AnalyzePerformance(20) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{ + "error": fmt.Sprintf("分析历史表现失败: %v", err), + }) + return + } + + c.JSON(http.StatusOK, performance) +} + // Start 启动服务器 func (s *Server) Start() error { addr := fmt.Sprintf(":%d", s.port) @@ -387,6 +414,7 @@ func (s *Server) Start() error { log.Printf(" • GET /api/decisions/latest?trader_id=xxx - 指定trader的最新决策") log.Printf(" • GET /api/statistics?trader_id=xxx - 指定trader的统计信息") log.Printf(" • GET /api/equity-history?trader_id=xxx - 指定trader的收益率历史数据") + log.Printf(" • GET /api/performance?trader_id=xxx - 指定trader的AI学习表现分析") log.Printf(" • GET /health - 健康检查") log.Println() diff --git a/config.example.go b/config.example.go new file mode 100644 index 00000000..84f6f9db --- /dev/null +++ b/config.example.go @@ -0,0 +1,84 @@ +package main + +import ( + "nofx/trader" + "time" +) + +// 配置示例 - 复制到 main.go 中使用 + +func exampleConfig() trader.AutoTraderConfig { + return trader.AutoTraderConfig{ + // ========== API密钥配置 ========== + // 在币安官网申请: https://www.binance.com/zh-CN/my/settings/api-management + BinanceAPIKey: "YOUR_BINANCE_API_KEY", // 必填 + BinanceSecretKey: "YOUR_BINANCE_SECRET_KEY", // 必填 + + // 币种池API(可选,不填则使用默认池) + CoinPoolAPIURL: "", // 留空,将从config.json读取 + + // ========== AI配置 ========== + // 选择一个AI服务商(二选一) + UseQwen: true, // true=使用阿里云Qwen, false=使用DeepSeek + + // DeepSeek配置 + // 申请地址: https://platform.deepseek.com/ + DeepSeekKey: "sk-your-deepseek-api-key", + + // 阿里云Qwen配置 + // 申请地址: https://dashscope.aliyun.com/ + QwenKey: "sk-your-qwen-api-key", + + // ========== 交易周期 ========== + // AI决策频率(建议3-5分钟) + ScanInterval: 3 * time.Minute, + + // ========== 风险控制 ========== + // 注意:这些仅作为提示,AI可以自主决定实际参数 + MaxDailyLoss: 5.0, // 最大日亏损5%(触发后暂停) + MaxDrawdown: 10.0, // 最大回撤10%(触发后暂停) + StopTradingTime: 30 * time.Minute, // 触发风控后暂停30分钟 + } +} + +// ========== AI决策原则(内置在系统中) ========== +// +// 以下参数由AI根据市场情况自主决定,无需配置: +// +// 1. 杠杆倍数: 1-20倍 +// - AI会根据波动率和信心度选择 +// - 高波动 → 低杠杆 +// - 低波动 → 高杠杆 +// +// 2. 仓位大小: USD金额 +// - AI会根据账户净值和风险评估决定 +// - 建议单笔风险2-5% +// +// 3. 止损止盈价格: +// - AI会根据技术指标动态设置 +// - 风险回报比建议 ≥ 1:2 +// +// 4. 开仓时机: +// - 趋势明确、技术指标一致 +// - RSI、MACD、EMA多重确认 +// +// 5. 平仓时机: +// - 到达止损/止盈 +// - 趋势反转信号 +// - 大额亏损保护 +// +// ========== 快速开始 ========== +// +// 1. 配置API密钥(main.go中) +// 2. 选择AI服务商(DeepSeek或Qwen) +// 3. 编译: go build -o nofx-auto +// 4. 运行: ./nofx-auto +// 5. 观察AI的思维链分析和决策 +// +// ========== 风险提示 ========== +// +// ⚠️ 建议先用小额资金(100-1000 USDT)测试 +// ⚠️ 密切监控系统运行,特别是初期 +// ⚠️ 设置币安API的IP白名单 +// ⚠️ 定期检查持仓和盈亏 +// ⚠️ 加密货币交易有风险,投资需谨慎 diff --git a/config.json.example b/config.json.example index c78e943a..4b29cbbf 100644 --- a/config.json.example +++ b/config.json.example @@ -21,8 +21,8 @@ "scan_interval_minutes": 3 } ], - "coin_pool_api_url": "http://x.x.x.x:x/api/ai500/list?auth=YOUR_AUTH_TOKEN", - "oi_top_api_url": "http://x.x.x.x:x/api/oi/top?auth=YOUR_AUTH_TOKEN", + "coin_pool_api_url": "http://x.x.x.x:x/api/ai500/list?auth=", + "oi_top_api_url": "http://x.x.x.x:x/api/oi/top?auth=", "api_server_port": 8080, "max_daily_loss": 5.0, "max_drawdown": 10.0, diff --git a/logger/decision_logger.go b/logger/decision_logger.go index 525c11d3..b3d91759 100644 --- a/logger/decision_logger.go +++ b/logger/decision_logger.go @@ -267,42 +267,42 @@ type Statistics struct { // TradeOutcome 单笔交易结果 type TradeOutcome struct { - Symbol string // 币种 - Side string // long/short - OpenPrice float64 // 开仓价 - ClosePrice float64 // 平仓价 - PnL float64 // 盈亏(USDT) - PnLPct float64 // 盈亏百分比 - Duration string // 持仓时长 - OpenTime time.Time // 开仓时间 - CloseTime time.Time // 平仓时间 - WasStopLoss bool // 是否止损 + Symbol string `json:"symbol"` // 币种 + Side string `json:"side"` // long/short + OpenPrice float64 `json:"open_price"` // 开仓价 + ClosePrice float64 `json:"close_price"` // 平仓价 + PnL float64 `json:"pn_l"` // 盈亏(USDT) + PnLPct float64 `json:"pn_l_pct"` // 盈亏百分比 + Duration string `json:"duration"` // 持仓时长 + OpenTime time.Time `json:"open_time"` // 开仓时间 + CloseTime time.Time `json:"close_time"` // 平仓时间 + WasStopLoss bool `json:"was_stop_loss"` // 是否止损 } // PerformanceAnalysis 交易表现分析 type PerformanceAnalysis struct { - TotalTrades int // 总交易数 - WinningTrades int // 盈利交易数 - LosingTrades int // 亏损交易数 - WinRate float64 // 胜率 - AvgWin float64 // 平均盈利 - AvgLoss float64 // 平均亏损 - ProfitFactor float64 // 盈亏比 - RecentTrades []TradeOutcome // 最近N笔交易 - SymbolStats map[string]*SymbolPerformance // 各币种表现 - BestSymbol string // 表现最好的币种 - WorstSymbol string // 表现最差的币种 + TotalTrades int `json:"total_trades"` // 总交易数 + WinningTrades int `json:"winning_trades"` // 盈利交易数 + LosingTrades int `json:"losing_trades"` // 亏损交易数 + WinRate float64 `json:"win_rate"` // 胜率 + AvgWin float64 `json:"avg_win"` // 平均盈利 + AvgLoss float64 `json:"avg_loss"` // 平均亏损 + ProfitFactor float64 `json:"profit_factor"` // 盈亏比 + RecentTrades []TradeOutcome `json:"recent_trades"` // 最近N笔交易 + SymbolStats map[string]*SymbolPerformance `json:"symbol_stats"` // 各币种表现 + BestSymbol string `json:"best_symbol"` // 表现最好的币种 + WorstSymbol string `json:"worst_symbol"` // 表现最差的币种 } // SymbolPerformance 币种表现统计 type SymbolPerformance struct { - Symbol string // 币种 - TotalTrades int // 交易次数 - WinningTrades int // 盈利次数 - LosingTrades int // 亏损次数 - WinRate float64 // 胜率 - TotalPnL float64 // 总盈亏 - AvgPnL float64 // 平均盈亏 + Symbol string `json:"symbol"` // 币种 + TotalTrades int `json:"total_trades"` // 交易次数 + WinningTrades int `json:"winning_trades"` // 盈利次数 + LosingTrades int `json:"losing_trades"` // 亏损次数 + WinRate float64 `json:"win_rate"` // 胜率 + TotalPnL float64 `json:"total_pn_l"` // 总盈亏 + AvgPnL float64 `json:"avg_pn_l"` // 平均盈亏 } // AnalyzePerformance 分析最近N个周期的交易表现 @@ -314,12 +314,14 @@ func (l *DecisionLogger) AnalyzePerformance(lookbackCycles int) (*PerformanceAna if len(records) == 0 { return &PerformanceAnalysis{ - SymbolStats: make(map[string]*SymbolPerformance), + RecentTrades: []TradeOutcome{}, + SymbolStats: make(map[string]*SymbolPerformance), }, nil } analysis := &PerformanceAnalysis{ - SymbolStats: make(map[string]*SymbolPerformance), + RecentTrades: []TradeOutcome{}, + SymbolStats: make(map[string]*SymbolPerformance), } // 追踪持仓状态:symbol -> {side, openPrice, openTime} diff --git a/market/ai_decision_engine.go b/market/ai_decision_engine.go index 9e2bd1ec..be7f40a0 100644 --- a/market/ai_decision_engine.go +++ b/market/ai_decision_engine.go @@ -5,6 +5,7 @@ import ( "fmt" "log" "nofx/pool" + "regexp" "strings" "time" ) @@ -304,9 +305,10 @@ func buildFullDecisionPrompt(ctx *TradingContext) string { // AI决策要求 sb.WriteString("## 🎯 任务\n\n") sb.WriteString("分析市场数据,自主决策:\n") - sb.WriteString("1. 评估现有持仓 → 持有或平仓\n") - sb.WriteString(fmt.Sprintf("2. 从%d个候选币种中找交易机会\n", len(ctx.MarketDataMap))) - sb.WriteString("3. 开新仓(如果有机会)\n\n") + sb.WriteString("1. **如有历史数据,先进行自我反思**:回顾之前的交易,总结经验教训\n") + sb.WriteString("2. 评估现有持仓 → 持有或平仓\n") + sb.WriteString(fmt.Sprintf("3. 从%d个候选币种中找交易机会\n", len(ctx.MarketDataMap))) + sb.WriteString("4. 开新仓(如果有机会)\n\n") sb.WriteString("## 📋 规则\n\n") sb.WriteString(fmt.Sprintf("1. **单币种仓位上限**: 山寨币≤%.0f USDT | BTC/ETH≤%.0f USDT\n", ctx.Account.TotalEquity*1.5, ctx.Account.TotalEquity*10)) @@ -315,36 +317,41 @@ func buildFullDecisionPrompt(ctx *TradingContext) string { sb.WriteString("4. **风险回报比**: ≥1:2\n\n") sb.WriteString("### 📤 输出格式\n\n") - sb.WriteString("**思维链分析** (纯文本)\n") - sb.WriteString("- 分析持仓 → 找新机会 → 账户检查\n") - sb.WriteString("- **最后必须列出最终决策摘要**(例如:持有XX,平仓XX,开多XX,开空XX)\n\n") + sb.WriteString("**重要:严格按照JSON格式输出,所有字符串值必须用双引号包裹!**\n\n") + sb.WriteString("先输出思维链分析(纯文本),然后输出JSON数组:\n\n") + sb.WriteString("**思维链分析**:\n") + sb.WriteString("1. **历史经验反思**(如有历史数据): 回顾表现,总结教训\n") + sb.WriteString("2. **市场分析**: 分析BTC趋势和当前持仓\n") + sb.WriteString("3. **机会识别**: 从候选币种中找交易机会\n") + sb.WriteString("4. **风险控制**: 检查账户保证金和仓位限制\n") + sb.WriteString("5. **最终决策摘要**: 列出所有决策\n\n") sb.WriteString("---\n\n") - sb.WriteString("**决策JSON** (不要用```标记)\n") + sb.WriteString("**JSON决策数组** (不要加```标记,所有字符串必须用双引号):\n") sb.WriteString("[\n") sb.WriteString(" {\"symbol\": \"BTCUSDT\", \"action\": \"open_long\", \"leverage\": 50, \"position_size_usd\": 15000, \"stop_loss\": 92000, \"take_profit\": 98000, \"reasoning\": \"突破做多\"},\n") sb.WriteString(" {\"symbol\": \"ETHUSDT\", \"action\": \"hold\", \"reasoning\": \"持续观察\"}\n") sb.WriteString("]\n\n") sb.WriteString("**action类型**: open_long | open_short | close_long | close_short | hold | wait\n") sb.WriteString("**开仓必填**: leverage, position_size_usd, stop_loss, take_profit\n") - sb.WriteString("**position_size_usd**: 仓位价值(非保证金),保证金=position_size_usd/leverage\n\n") + sb.WriteString("**注意**: reasoning字段必须用双引号包裹!例如:\"reasoning\": \"这里是理由\"\n\n") sb.WriteString("### 📝 完整示例\n\n") // 简化示例仓位(使用新的仓位上限) btcSize := ctx.Account.TotalEquity * 8 // BTC示例:8倍净值(不超过10倍上限) - altSize := ctx.Account.TotalEquity * 1 // 山寨币示例:1倍净值(不超过1.5倍上限) - sb.WriteString("**思维链**:\n") - sb.WriteString("当前持仓:ETHUSDT多头盈利+2.3%,趋势良好继续持有。\n") - sb.WriteString("新机会:BTC突破上涨,MACD金叉,资金费率低,做多信号强。\n") - sb.WriteString(" SOLUSDT回调至支撑位,出现反弹信号,可小仓位做多。\n") - sb.WriteString("账户:可用余额充足,保证金使用率32%,可分散开仓。\n") - sb.WriteString("**最终决策**:持有ETHUSDT,开多BTCUSDT(8倍净值),开多SOLUSDT(1倍净值)。\n\n") + sb.WriteString("【历史经验反思】\n") + sb.WriteString("回顾最近10笔交易:胜率40%,盈亏比0.8:1,表现欠佳。\n") + sb.WriteString("SOLUSDT做多3次全部止损,该币种波动大,暂时避开。\n") + sb.WriteString("BTCUSDT做多2次,1胜1负,可继续关注。\n\n") + sb.WriteString("【市场分析】\n") + sb.WriteString("BTC突破上涨,MACD金叉,趋势向上。\n") + sb.WriteString("当前持仓:ETHUSDT多头盈利+2.3%,继续持有。\n\n") + sb.WriteString("【最终决策】持有ETHUSDT,开多BTCUSDT。\n\n") sb.WriteString("---\n\n") sb.WriteString("[\n") sb.WriteString(" {\"symbol\": \"ETHUSDT\", \"action\": \"hold\", \"reasoning\": \"盈利良好,趋势延续\"},\n") - sb.WriteString(fmt.Sprintf(" {\"symbol\": \"BTCUSDT\", \"action\": \"open_long\", \"leverage\": 50, \"position_size_usd\": %.0f, \"stop_loss\": 92000, \"take_profit\": 98000, \"reasoning\": \"突破做多\"},\n", btcSize)) - sb.WriteString(fmt.Sprintf(" {\"symbol\": \"SOLUSDT\", \"action\": \"open_long\", \"leverage\": 20, \"position_size_usd\": %.0f, \"stop_loss\": 180, \"take_profit\": 210, \"reasoning\": \"支撑位反弹\"}\n", altSize)) + sb.WriteString(fmt.Sprintf(" {\"symbol\": \"BTCUSDT\", \"action\": \"open_long\", \"leverage\": 50, \"position_size_usd\": %.0f, \"stop_loss\": 92000, \"take_profit\": 98000, \"reasoning\": \"突破确认,历史表现佳\"}\n", btcSize)) sb.WriteString("]\n\n") sb.WriteString("现在请开始分析并给出你的决策!\n") @@ -481,22 +488,20 @@ func formatMarketDataBrief(data *MarketData) string { // parseFullDecisionResponse 解析AI的完整决策响应 func parseFullDecisionResponse(aiResponse string, accountEquity float64) (*AIFullDecision, error) { - // 1. 提取 cot_trace(思维链) + // 1. 提取思维链 cotTrace := extractCoTTrace(aiResponse) - // 2. 提取 JSON 决策列表 + // 2. 提取JSON决策列表 decisions, err := extractDecisions(aiResponse) if err != nil { - // 即使JSON解析失败,也返回思维链 return &AIFullDecision{ CoTTrace: cotTrace, Decisions: []TradingDecision{}, }, fmt.Errorf("提取决策失败: %w\n\n=== AI思维链分析 ===\n%s", err, cotTrace) } - // 3. 验证决策(包含仓位价值上限检查) + // 3. 验证决策 if err := validateDecisions(decisions, accountEquity); err != nil { - // 验证失败时,也返回思维链和决策,但标记为错误 return &AIFullDecision{ CoTTrace: cotTrace, Decisions: decisions, @@ -539,6 +544,12 @@ func extractDecisions(response string) ([]TradingDecision, error) { jsonContent := strings.TrimSpace(response[arrayStart : arrayEnd+1]) + // 🔧 修复常见的JSON格式错误:缺少引号的字段值 + // 匹配: "reasoning": 内容"} 或 "reasoning": 内容} (没有引号) + // 修复为: "reasoning": "内容"} + // 使用简单的字符串扫描而不是正则表达式 + jsonContent = fixMissingQuotes(jsonContent) + // 解析JSON var decisions []TradingDecision if err := json.Unmarshal([]byte(jsonContent), &decisions); err != nil { @@ -548,6 +559,15 @@ func extractDecisions(response string) ([]TradingDecision, error) { return decisions, nil } +// fixMissingQuotes 修复缺少引号的reasoning字段 +func fixMissingQuotes(jsonStr string) string { + // 匹配: "reasoning": 内容"} 或 "reasoning": 内容} + // 不匹配: "reasoning": "已经有引号" + // 替换为: "reasoning": "内容"} + re := regexp.MustCompile(`"reasoning":\s*([^"}\n,][^}\n,]*?)([}\n,])`) + return re.ReplaceAllString(jsonStr, `"reasoning": "$1"$2`) +} + // validateDecisions 验证所有决策(需要账户信息) func validateDecisions(decisions []TradingDecision, accountEquity float64) error { for i, decision := range decisions { diff --git a/market/ai_signal.go b/market/ai_signal.go index 069a5534..96708dbf 100644 --- a/market/ai_signal.go +++ b/market/ai_signal.go @@ -262,10 +262,13 @@ func callDeepSeekAPIOnce(prompt string) (string, error) { "content": prompt, }, }, - "temperature": 0.7, + "temperature": 0.5, // 降低temperature以提高JSON格式稳定性 "max_tokens": 2000, } + // 注意:response_format 参数仅 OpenAI 支持,DeepSeek/Qwen 不支持 + // 我们通过强化 prompt 和后处理来确保 JSON 格式正确 + jsonData, err := json.Marshal(requestBody) if err != nil { return "", fmt.Errorf("序列化请求失败: %w", err) diff --git a/pool/coin_pool.go b/pool/coin_pool.go index 67dbfcd5..b88b865b 100644 --- a/pool/coin_pool.go +++ b/pool/coin_pool.go @@ -19,7 +19,7 @@ type CoinPoolConfig struct { } var coinPoolConfig = CoinPoolConfig{ - APIURL: "", // 将通过config.json配置或SetCoinPoolAPI()设置 + APIURL: "", Timeout: 30 * time.Second, // 增加到30秒 CacheDir: "coin_pool_cache", } @@ -359,7 +359,7 @@ var oiTopConfig = struct { Timeout time.Duration CacheDir string }{ - APIURL: "", // 将通过config.json配置或SetOITopAPI()设置 + APIURL: "", Timeout: 30 * time.Second, CacheDir: "coin_pool_cache", } diff --git a/web/src/App.tsx b/web/src/App.tsx index fb61bd80..f31dd891 100644 --- a/web/src/App.tsx +++ b/web/src/App.tsx @@ -3,6 +3,7 @@ import useSWR from 'swr'; import { api } from './lib/api'; import { EquityChart } from './components/EquityChart'; import { CompetitionPage } from './components/CompetitionPage'; +import AILearning from './components/AILearning'; import type { SystemStatus, AccountInfo, @@ -97,7 +98,7 @@ function App() {
{/* Header - Binance Style */}
-
+
@@ -179,7 +180,7 @@ function App() {
{/* Main Content */} -
+
{currentPage === 'competition' ? ( ) : ( @@ -197,7 +198,7 @@ function App() { {/* Footer */}