Commit Graph

15 Commits

Author SHA1 Message Date
ZhouYongyou
bb2edfc293 feat(market): Add WebSocket stream limit protection with auto-downgrade
## Problem
Binance WebSocket has a hard limit of 1024 streams per connection.
Without protection:
- 300 coins × 4 timeframes = 1200 streams → Connection REJECTED
- System fails to start with "全市場掃描" mode

## Solution
Implement intelligent auto-downgrade mechanism with transparent logging.

### Changes to market/monitor.go (+42 lines)

**1. Add Constants**:
```go
const (
    MaxStreamsPerConnection = 1024  // Binance hard limit
    SafeMaxSymbols = 250            // Safe limit (97.7% usage, 2.3% buffer)
)
```

**2. Add Stream Count Check in Initialize()**:
- Calculate total streams: `len(symbols) × len(subKlineTime)`
- If exceeds 250 coins → Auto-downgrade to first 250
- Display detailed adjustment logs

**3. Add Usage Rate Display**:
```
✓ WebSocket 訂閱: X 個幣種 × 4 時間週期 = Y 流 (Z% 用量)
```

**4. Add High Usage Warning (>90%)**:
```
⚠️ 警告: 訂閱流使用率較高 (93.8%),建議減少幣種數量以確保穩定性
```

## Behavior by Scenario

| Coins | Original Streams | Adjusted Streams | Behavior |
|-------|-----------------|------------------|----------|
| 8 | 32 | 32 |  Normal |
| 100 | 400 | 400 |  Normal |
| 150 | 600 | 600 |  Normal |
| 240 | 960 | 960 | ⚠️ Warning |
| 300 | 1200 | **1000** | 🔄 Auto-downgrade |
| 500 | 2000 | **1000** | 🔄 Auto-downgrade |

## Example Logs

### Normal Case (8 coins):
```
找到 8 个交易对
✓ WebSocket 訂閱: 8 個幣種 × 4 時間週期 = 32 流 (3.1% 用量)
```

### Auto-downgrade Case (300 coins):
```
找到 300 个交易对
⚠️  幣種數量過多,自動調整:
   - 原始數量: 300 個幣種 (1200 流)
   - Binance 限制: 1024 流/連接
   - 時間週期: 4 ([3m 15m 1h 4h])
   - 調整後: 250 個幣種 (1000 流)
   - 已過濾: 前 250 個幣種保留,其餘忽略
✓ WebSocket 訂閱: 250 個幣種 × 4 時間週期 = 1000 流 (97.7% 用量)
⚠️  警告: 訂閱流使用率較高 (97.7%),建議減少幣種數量以確保穩定性
```

## Benefits
-  System always starts successfully (no crashes)
-  Transparent logging (users know what happened)
-  Safe buffer (2.3% headroom for reconnections)
-  No breaking changes (8-250 coins unchanged)
-  Covers all realistic use cases (AI500 + OI Top = ~150 coins)

## Why 250, not 256?
- 256 × 4 = 1024 (no buffer, risky during reconnections)
- 250 × 4 = 1000 (24 stream buffer = 2.3% safety margin)
- Future-proof for potential 5th timeframe

Related: 15m/1h timeframe addition, WebSocket architecture merge
2025-11-03 19:27:01 +08:00
ZhouYongyou
8d12514498 feat(market): Add 15m/1h timeframes for comprehensive trend analysis
## Problem
Merged WebSocket architecture (nofxaios/dev) only supported 3m/4h intervals,
but adaptive.txt v5.5.6.1 requires 15m/1h data for:
- BTC state evaluation (line 105-107)
- Multi-confirmation checklist (line 146, 159)
- False breakout detection (line 176, 182)
- Confidence scoring (line 197)

## Solution
Add 15m and 1h timeframe support across WebSocket pipeline:

### 1. market/monitor.go (+64/-40 lines)
- Add klineDataMap15m and klineDataMap1h to WSMonitor struct
- Update subKlineTime: ["3m", "4h"] → ["3m", "15m", "1h", "4h"]
- Extend initializeHistoricalData() to load 15m/1h historical klines
- Update getKlineDataMap() switch to handle all 4 timeframes
- Fix bug: line 109 used wrong variable (klines vs klines4h)

### 2. market/types.go (+26/-1 lines)
- Add MidTermData15m struct (15-minute short-term trend filtering)
- Add MidTermData1h struct (1-hour mid-term trend confirmation)
- Update Data struct to include:
  - MidTermSeries15m *MidTermData15m
  - MidTermSeries1h *MidTermData1h

### 3. market/data.go (+171/-2 lines)
- Update Get() to fetch klines15m and klines1h via WebSocket
- Implement calculateMidTermSeries15m() - computes EMA20, MACD, RSI7/14 for 15m
- Implement calculateMidTermSeries1h() - computes EMA20, MACD, RSI7/14 for 1h
- Update Format() to output 15m/1h series data for AI prompt context

## Impact Assessment
### WebSocket Load (Binance limit: 1024 streams/connection)
- 8 coins × 4 timeframes = 32 streams (3% usage) 
- 100 coins × 4 timeframes = 400 streams (39% usage) 
- 250 coins × 4 timeframes = 1000 streams (98% usage) ⚠️

### Benefits
- Enables adaptive.txt standard mode: 15m/1h/4h multi-timeframe confirmation
- Restores false breakout detection: 15m RSI vs 1h RSI divergence checks
- Improves confidence scoring: 15m/1h/4h MACD alignment validation
- Zero REST API calls (WebSocket cache, no rate limit risk)

## Testing Notes
- Monitor initial subscription logs for "已加载 X 的历史K线数据-15m/1h"
- Verify AI prompts contain "Mid-term series (15-minute intervals)" section
- Check decision logs reference 15m/1h indicators in reasoning

Related: adaptive.txt v5.5.6.1 requirements, NoFxAiOS/dev WebSocket merge
2025-11-03 19:17:12 +08:00
yuanshi2016
95c32fcb2e 修改Kline获取方式为Websocket缓存。 2025-11-02 17:59:19 +08:00
yuanshi2016
3b1db6f64f K线获取方式改为websocket组合流. 带重拨机制
流程为下:
1. 启动时使用所有交易员设置的币种(去重) 如果交易员未配置,则使用系统默认
2. 在决策获取K线时 如果没有缓存 则先实时获取后再添加订阅. ps: 适用于Api方式的币种列表
2025-11-02 14:03:13 +08:00
yuanshi2016
2c08e1f10b 新增内置AI评分
修改market/data.go Get函数获取K线为流式获取(可以解决传入币种比较多的情况下耗时问题)
market目录下新增文件
main.go 新增运行入口
通过inside_coins=true控制
该评分默认初始化大约需要2分钟左右(因为币种列表比较多,api有限速)
使用时应该注意engine.go下的流动性过滤问题
2025-11-01 15:58:54 +08:00
tinkle
4846e4c4ef chore: Remove obsolete market and scanner files
Remove deprecated files that don't exist in nofx internal version:
- market/ai_decision_engine.go
- market/ai_signal.go
- market/market_data.go
- scanner/ai_scanner.go

Keep only market/data.go to align with internal version structure.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-29 10:50:09 +08:00
tinkle
f3a87b5a7a Refactor: Modularize codebase with separate decision and MCP packages
Architecture improvements:
- Extract AI decision engine to dedicated `decision` package
- Create `mcp` package for Model Context Protocol client
- Separate market data structures into `market/data.go`
- Update trader to use new modular structure

New packages:
- `decision/engine.go` - AI decision logic and prompt building
- `mcp/client.go` - Unified AI API client (DeepSeek/Qwen)
- `market/data.go` - Market data type definitions

Benefits:
- Better separation of concerns
- Improved code organization and maintainability
- Easier to test individual components
- More flexible AI provider integration
- Cleaner dependency management

Updated imports:
- trader/auto_trader.go now uses decision and mcp packages
- Consistent API across different AI providers

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-29 06:14:57 +08:00
tinkle
102d9f438d Refactor: Improve AI decision system and Sharpe ratio calculation
Major improvements:
- Use period-level Sharpe ratio (range -2 to +2) instead of annualized
- Save full user prompt in decision logs for debugging
- Format complete market data (3m + 4h candles) for AI analysis
- Prevent position stacking with duplicate position checks
- Update Sharpe ratio interpretation thresholds

Market data enhancements:
- Display full technical indicators in user prompt
- Include 3-minute and 4-hour timeframe data
- Add OI (Open Interest) change and funding rate signals

Risk control:
- Block opening duplicate positions (same symbol + direction)
- Suggest close action first before opening new position
- Prevent margin usage from exceeding limits

UI improvements:
- Update multi-language translations
- Refine AI learning dashboard display

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-29 04:44:17 +08:00
tinkle
c2aae0b652 Fix: Ensure Sharpe ratio is always visible to AI
Problem: When TotalTrades == 0 (no completed trades), formatPerformanceFeedback
would return early, hiding Sharpe ratio and adaptive behavior recommendations
from AI's prompt. This caused AI to say "no historical data" even though Sharpe
ratio was calculated and displayed on frontend.

Solution:
- Display Sharpe ratio BEFORE checking TotalTrades
- Don't return early when TotalTrades == 0
- Always show adaptive behavior recommendations if Sharpe ratio exists
- Sharpe ratio is calculated from account equity changes, not just closed trades

Impact: AI can now properly use Sharpe ratio for self-evolution, even in early
stages before any trades are closed.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-29 02:36:37 +08:00
tinkle
35c7a1d037 Feature: Add Sharpe ratio for AI self-evolution
- Implement Sharpe ratio calculation in decision logger
- Add adaptive behavior recommendations based on Sharpe ratio
- Display Sharpe ratio in AI learning dashboard with visual indicators
- Enable AI to adjust trading strategy based on risk-adjusted returns
- Color-coded performance levels (red/yellow/green) for easy monitoring

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-29 02:31:15 +08:00
tinkle
8b0b0c76a6 Refactor: Separate system and user prompts for AI API
- Split AI prompts into system (cacheable) and user (dynamic) messages
- Add confidence and risk_usd fields to trading decisions
- Reduce temperature to 0.5 for more stable JSON output
- Add retry mechanism for AI API calls
- Simplify candidate display (show top 10 only)
- Improve prompt structure for better AI decision quality
2025-10-28 23:51:55 +08:00
tinkle
5522e33ec6 Update: Simplify JSON parsing and add contact info
- Replace regex-based quote fixing with simple character replacement
- Add Twitter/X contact link to all README versions
- Remove complex regex pattern for better performance
- Fix Chinese quote mark handling in AI responses
2025-10-28 22:58:48 +08:00
tinkle
17868cf0e9 Improve: Optimize AI decision engine and position management
- Add focused position strategy (max 3 concurrent positions)
- Increase position sizes for better capital efficiency
- Add historical reflection prompts for AI learning
- Fix JSON parsing with missing quotes in reasoning field
- Update position limits and leverage recommendations
- Enhance risk management with concentrated positions
2025-10-28 22:40:34 +08:00
tinkle
14ffb0593a 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
2025-10-28 21:45:28 +08:00
tinkle
7e8a494ed3 Initial commit: NOFX AI Trading System
- Multi-AI competition mode (Qwen vs DeepSeek)
- Binance Futures integration
- AI self-learning mechanism
- Professional web dashboard
- Complete risk management system
2025-10-28 15:47:34 +08:00