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 |
|