Commit Graph

20 Commits

Author SHA1 Message Date
darkedge 463a092a64 feat: 添加AI请求耗时记录,优化性能评估 (#587)
# Conflicts:
#	decision/engine.go
2025-11-10 20:18:39 -05:00
Lawrence Liu 9d41d5ab39 Fix(security): 增强日志文件安全权限和管理 (#757)
## 问题
决策日志包含敏感的交易数据(API密钥、仓位、PnL等),但使用了不安全的默认权限:
- 目录权限 0755(所有用户可读可执行)
- 文件权限 0644(所有用户可读)
这可能导致同一系统其他用户访问敏感交易数据。
## 解决方案
### 1. 代码层面安全加固(Secure by Default)
**logger/decision_logger.go**:
- 目录创建权限:0755 → 0700(只有所有者可访问)
- 文件创建权限:0644 → 0600(只有所有者可读写)
- **新增:强制修复已存在目录/文件权限**(修复升级场景)
  - `NewDecisionLogger` 启动时强制设置目录权限
  - 遍历并修复已存在的 *.json 文件权限
  - 覆盖 PM2/手动部署场景
### 2. 运行时安全检查(Defense in Depth)
**start.sh**:
- 新增 `setup_secure_permissions()` 函数
- 启动时自动修正敏感文件/目录权限:
  - 目录 (.secrets, secrets, logs, decision_logs): 700
  - 文件 (.env, config.db, 密钥文件, 日志): 600
- **修复:使用 `install -m MODE` 创建文件/目录**
  - `touch config.db` → `install -m 600 /dev/null config.db`
  - `mkdir -p decision_logs` → `install -m 700 -d decision_logs`
  - 确保首次启动就是安全权限(修复 fresh install 漏洞)
### 3. 日志轮转和清理
**scripts/cleanup_logs.sh**:
- 提供日志清理功能(默认保留7天)
- 支持 dry-run 模式预览
- 可通过 crontab 定期执行
## 测试要点
1.  验证新创建的日志文件权限为 0600
2.  验证新创建的日志目录权限为 0700
3.  验证 start.sh 正确设置所有敏感文件权限
4.  验证 Docker 部署(root)可正常访问
5.  验证 PM2 部署(owner)可正常访问
6.  验证清理脚本正确删除旧日志
7.  验证首次安装时 config.db 权限为 600(不是 644)
8.  验证升级后已存在的日志文件权限被修正为 600
## 安全影响
- 防止同一系统其他用户读取敏感交易数据
- 采用深度防御策略:代码默认安全 + 运行时检查 + 升级修复
- 对 Docker(root)和 PM2(owner)部署均兼容
- 修复首次安装和升级场景的权限漏洞
2025-11-09 09:47:24 +08:00
Linden f1f24ad1fa fix:完善aster账户净值和盈亏计算|Improve the calculation of the net value and profit/loss of the aster account (#695)
Co-authored-by: LindenWang <linden@Lindens-MacBookPro-2.local>
2025-11-07 13:38:39 +08:00
Icyoung 7bfefbb356 Merge branch 'dev' into fix/partial-close-stats 2025-11-05 16:11:53 +08:00
Icyoung 2c122f0c5c Merge pull request #273 from zhouyongyou/feat/logger-dynamic-tpsl
feat(logger): 添加動態 TP/SL 日誌支持 [依賴 #415]
2025-11-05 15:42:33 +08:00
Icyoung af1fc4189a Merge pull request #415 from zhouyongyou/feat/partial-close-core-v2
feat: 部分平倉和動態止盈止損核心實現 / Partial Close & Dynamic TP/SL Core
2025-11-05 15:41:20 +08:00
tangmengqiu 9658785c6b log: add logrus log lib and add telegram notification push as an option 2025-11-04 23:48:32 -05:00
ZhouYongyou 21ae77e7cc fix(stats): aggregate partial closes into single trade for accurate statistics
## Problem
Multiple partial_close actions on the same position were being counted as separate trades, inflating TotalTrades count and distorting win rate/profit factor statistics.
**Example of bug:**
- Open 1 BTC @ $100,000
- Partial close 30% @ $101,000 → Counted as trade #1 
- Partial close 50% @ $102,000 → Counted as trade #2 
- Close remaining 20% @ $103,000 → Counted as trade #3 
- **Result:** 3 trades instead of 1 
## Solution
### 1. Added tracking fields to openPositions map
- `remainingQuantity`: Tracks remaining position size
- `accumulatedPnL`: Accumulates PnL from all partial closes
- `partialCloseCount`: Counts number of partial close operations
- `partialCloseVolume`: Total volume closed partially
### 2. Modified partial_close handling logic
- Each partial_close:
  - Accumulates PnL into `accumulatedPnL`
  - Reduces `remainingQuantity`
  - **Does NOT increment TotalTrades++**
  - Keeps position in openPositions map
- Only when `remainingQuantity <= 0.0001`:
  - Records ONE TradeOutcome with aggregated PnL
  - Increments TotalTrades++ once
  - Removes from openPositions map
### 3. Updated full close handling
- If position had prior partial closes:
  - Adds `accumulatedPnL` to final close PnL
  - Reports total PnL in TradeOutcome
### 4. Fixed GetStatistics()
- Removed `partial_close` from TotalClosePositions count
- Only `close_long/close_short/auto_close` count as close operations
## Impact
-  Statistics now accurate: multiple partial closes = 1 trade
-  Win rate calculated correctly
-  Profit factor reflects true performance
-  Backward compatible: handles positions without tracking fields
## Testing
-  Compiles successfully
- ⚠️ Requires validation with live partial_close scenarios
## Code Changes
```
logger/decision_logger.go:
- Lines 420-430: Add tracking fields to openPositions
- Lines 441-534: Implement partial_close aggregation logic
- Lines 536-593: Update full close to include accumulated PnL
- Lines 246-250: Fix GetStatistics() to exclude partial_close
```
2025-11-04 18:58:20 +08:00
ZhouYongyou c0c0688805 更新 logger:支持新增的三個動作類型
更新內容:
1. DecisionAction 註釋:添加 update_stop_loss, update_take_profit, partial_close
2. GetStatistics:partial_close 計入 TotalClosePositions
3. AnalyzePerformance 預填充邏輯:處理 partial_close(不刪除持倉記錄)
4. AnalyzePerformance 分析邏輯:
   - partial_close 正確判斷持倉方向
   - 記錄部分平倉的盈虧統計
   - 保留持倉記錄(因為還有剩餘倉位)
說明:partial_close 會記錄盈虧,但不刪除 openPositions,
      因為還有剩餘倉位可能繼續交易
2025-11-04 17:19:58 +08:00
ZhouYongyou b9a4bfceca fix: 修复部分平仓盈利计算错误
问题:部分平仓时,历史记录显示的是全仓位盈利,而非实际平仓部分的盈利
根本原因:
- AnalyzePerformance 使用开仓总数量计算部分平仓的盈利
- 应该使用 action.Quantity(实际平仓数量)而非 openPos["quantity"](总数量)
修复:
- 添加 actualQuantity 变量区分完整平仓和部分平仓
- partial_close 使用 action.Quantity
- 所有相关计算(PnL、PositionValue、MarginUsed)都使用 actualQuantity
影响范围:logger/decision_logger.go:428-465
Co-Authored-By: tinkle-community <tinklefund@gmail.com>
2025-11-04 16:40:28 +08:00
SkywalkerJi 5ad135310f Supports custom system prompts and custom models. 2025-11-01 19:45:54 +08:00
tinkle-community beb9561742 Feature: Add position details to Trade History
Add missing fields to TradeOutcome:
- Quantity: Position size
- Leverage: Leverage multiplier
- PositionValue: Total position value (quantity × openPrice)
- MarginUsed: Margin required (positionValue / leverage)
This provides complete trade information for analysis and display.
Co-Authored-By: tinkle-community <tinklefund@gmail.com>
2025-10-30 18:09:16 +08:00
tinkle-community fd8b1477e7 Fix: Resolve Trade History data loss and P&L calculation errors
Major fixes:
1. Trade History data loss issue
   - Root cause: Open records outside analysis window caused close matching failures
   - Solution: Pre-populate position state by reading 3x window of historical records
   - Ensures long-term positions (>5 hours) generate correct trade records
2. P&L calculation errors
   - Remove incorrect leverage multiplication from absolute P&L
   - Correct calculation: Futures P&L = quantity × price difference
   - Leverage only affects P&L percentage (relative to margin)
3. Other fixes
   - Break-even trades (pnl=0) no longer misclassified as losses
   - Perfect strategy shows Profit Factor as 999.0 instead of 0.0
   - Expand analysis window from 20 to 100 cycles (5 hours)
Files changed:
- logger/decision_logger.go: Core matching and calculation logic
- api/server.go: API analysis window
- trader/auto_trader.go: AI decision analysis window
Co-Authored-By: tinkle-community <tinklefund@gmail.com>
2025-10-30 17:58:25 +08:00
tinkle-community 683ae58563 Fix: Correct Sharpe Ratio calculation by using proper equity values
Critical bug fix in Sharpe Ratio calculation logic:
Problem:
- Previously calculated equity as TotalBalance + TotalUnrealizedProfit
- This was incorrect because TotalBalance already stores TotalEquity
- TotalUnrealizedProfit actually stores TotalPnL (not unrealized profit)
- This caused: equity = 2 * TotalEquity - InitialBalance (wrong!)
Root cause:
- Field naming mismatch between AccountSnapshot and actual stored values
- TotalBalance field actually contains TotalEquity (wallet + unrealized)
- TotalUnrealizedProfit field actually contains TotalPnL (equity - initial)
Solution:
- Use TotalBalance directly as it already represents complete account equity
- Added clear comments explaining the field name vs content mismatch
- Sharpe Ratio now correctly calculates risk-adjusted returns
Impact:
- Sharpe Ratio values are now mathematically accurate
- AI performance assessment is now reliable
- No changes needed to data storage or API layer
Co-Authored-By: tinkle-community <tinklefund@gmail.com>
2025-10-29 18:08:36 +08:00
tinkle-community b9ea3f68ea Fix: Correct Profit Factor calculation and display units in AI Learning
Backend changes (logger/decision_logger.go):
- Fixed Profit Factor to use standard formula (total profit / total loss)
- Previously used average values which was incorrect when win/loss counts differ
- Now saves total amounts before calculating averages for accurate ratio
Frontend changes (web/src/components/AILearning.tsx):
- Fixed display units: changed USDT amounts from "%" to "USDT"
- Updated avg_win and avg_loss to show "USDT Average" instead of "%"
- Updated best/worst performer displays to show "USDT" instead of "%"
- Added "(USDT)" labels to table headers for clarity
- Removed "%" from all table data cells showing monetary amounts
This ensures accurate performance metrics and eliminates user confusion
between percentage values and absolute USDT amounts.
Co-Authored-By: tinkle-community <tinklefund@gmail.com>
2025-10-29 17:59:19 +08:00
tinkle-community 938926254f Fix: Correct PnL calculation in trade history analysis
Fixed critical issues in historical trade record and performance analysis:
1. PnL Calculation: Changed from percentage-only to actual USDT amount
   - Now correctly calculates: positionValue × priceChange% × leverage
   - Previously: 100U@5% and 1000U@5% both showed 5.0
   - Now: Properly reflects different position sizes and leverage
2. Position Tracking: Added quantity and leverage to open position records
   - Store complete trade data for accurate PnL calculation
   - Previously only stored: side, openPrice, openTime
   - Now includes: quantity, leverage for proper accounting
3. Position Key: Fixed to distinguish long/short positions
   - Changed from symbol to symbol_side (e.g., BTCUSDT_long)
   - Prevents conflicts when holding both long and short positions
4. Sharpe Ratio: Replaced custom Newton's method with math.Sqrt
   - Simplified standard deviation calculation
   - More reliable and maintainable
Impact: Win rate, profit factor, and Sharpe ratio now based on accurate USDT amounts
Co-Authored-By: tinkle-community <tinklefund@gmail.com>
2025-10-29 15:30:32 +08:00
tinkle-community ceaedca253 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
Co-Authored-By: tinkle-community <tinklefund@gmail.com>
2025-10-29 04:44:17 +08:00
tinkle-community 0dca506cfc 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
Co-Authored-By: tinkle-community <tinklefund@gmail.com>
2025-10-29 02:31:15 +08:00
tinkle-community a726702302 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-community 5aa50d35d7 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