95 Commits

Author SHA1 Message Date
tinkle-community
1141a3f968 Feature/custom strategy (#1173)
* feat: add Strategy Studio with multi-timeframe support

- Add Strategy Studio page with three-column layout for strategy management
- Support multi-timeframe K-line data selection (5m, 15m, 1h, 4h, etc.)
- Add GetWithTimeframes() function in market package for fetching multiple timeframes
- Add TimeframeSeriesData struct for storing per-timeframe technical indicators
- Update formatMarketData() to display all selected timeframes in AI prompt
- Add strategy API endpoints for CRUD operations and test run
- Integrate real AI test runs with configured AI models
- Support custom AI500 and OI Top API URLs from strategy config

* docs: add Strategy Studio screenshot to README files

* feat: add quant data integration and fix position click navigation

- Integrate quant data API (netflow, OI, price changes) into Strategy Studio
- Add enable_quant_data toggle in indicator editor
- Fix position symbol click to navigate to chart (sync defaultSymbol prop)
- Fix TradingView chart fullscreen flickering
2025-12-06 07:47:03 +08:00
tinkle-community
1ea1234c64 Feature/custom strategy (#1172)
* feat: add Strategy Studio with multi-timeframe support

- Add Strategy Studio page with three-column layout for strategy management
- Support multi-timeframe K-line data selection (5m, 15m, 1h, 4h, etc.)
- Add GetWithTimeframes() function in market package for fetching multiple timeframes
- Add TimeframeSeriesData struct for storing per-timeframe technical indicators
- Update formatMarketData() to display all selected timeframes in AI prompt
- Add strategy API endpoints for CRUD operations and test run
- Integrate real AI test runs with configured AI models
- Support custom AI500 and OI Top API URLs from strategy config

* docs: add Strategy Studio screenshot to README files

* fix: correct strategy-studio.png filename case in README

* refactor: remove legacy signal source config and simplify trader creation

- Remove signal source configuration from traders page (now handled by strategy)
- Remove advanced options (legacy config) from TraderConfigModal
- Rename default strategy to "默认山寨策略" with AI500 coin pool URL
- Delete SignalSourceModal and SignalSourceWarning components
- Clean up related stores, hooks, and page components
2025-12-06 07:20:11 +08:00
tinkle-community
c185cffb19 feat: add exchange_id field to trader_positions table
- Add exchange_id column to track which exchange the position is from
- Update all SELECT/INSERT queries to include exchange_id
- Set exchange_id when creating position record in AutoTrader
- Add migration to add column to existing tables
2025-12-06 01:35:26 +08:00
tinkle-community
8259daddd7 fix: use exchange.ID instead of exchange.Type in PositionSyncManager
exchange.Type is cex/dex category, exchange.ID is the actual exchange name (binance/bybit/etc)
2025-12-06 01:28:43 +08:00
tinkle-community
495209a69b Refactor/trading actions (#1169)
* refactor: 简化交易动作,移除 update_stop_loss/update_take_profit/partial_close

- 移除 Decision 结构体中的 NewStopLoss, NewTakeProfit, ClosePercentage 字段
- 删除 executeUpdateStopLossWithRecord, executeUpdateTakeProfitWithRecord, executePartialCloseWithRecord 函数
- 简化 logger 中的 partial_close 聚合逻辑
- 更新 AI prompt 和验证逻辑,只保留 6 个核心动作
- 清理相关测试代码

保留的交易动作: open_long, open_short, close_long, close_short, hold, wait

* refactor: 移除 AI学习与反思 模块

- 删除前端 AILearning.tsx 组件和相关引用
- 删除后端 /performance API 接口
- 删除 logger 中 AnalyzePerformance、calculateSharpeRatio 等函数
- 删除 PerformanceAnalysis、TradeOutcome、SymbolPerformance 等结构体
- 删除 Context 中的 Performance 字段
- 移除 AI prompt 中夏普比率自我进化相关内容
- 清理 i18n 翻译文件中的相关条目

该模块基于磁盘存储计算,经常出错,做减法移除

* refactor: 将数据库操作统一迁移到 store 包

- 新增 store/ 包,统一管理所有数据库操作
  - store.go: 主 Store 结构,懒加载各子模块
  - user.go, ai_model.go, exchange.go, trader.go 等子模块
  - 支持加密/解密函数注入 (SetCryptoFuncs)

- 更新 main.go 使用 store.New() 替代 config.NewDatabase()
- 更新 api/server.go 使用 *store.Store 替代 *config.Database
- 更新 manager/trader_manager.go:
  - 新增 LoadTradersFromStore, LoadUserTradersFromStore 方法
  - 删除旧版 LoadUserTraders, LoadTraderByID, loadSingleTrader 等方法
  - 移除 nofx/config 依赖

- 删除 config/database.go 和 config/database_test.go
- 更新 api/server_test.go 使用 store.Trader 类型
- 清理 logger/ 包中未使用的 telegram 相关代码

* refactor: unify encryption key management via .env

- Remove redundant EncryptionManager and SecureStorage
- Simplify CryptoService to load keys from environment variables only
  - RSA_PRIVATE_KEY: RSA private key for client-server encryption
  - DATA_ENCRYPTION_KEY: AES-256 key for database encryption
  - JWT_SECRET: JWT signing key for authentication
- Update start.sh to auto-generate missing keys on first run
- Remove secrets/ directory and file-based key storage
- Delete obsolete encryption setup scripts
- Update .env.example with all required keys

* refactor: unify logger usage across mcp package

- Add MCPLogger adapter in logger package to implement mcp.Logger interface
- Update mcp/config.go to use global logger by default
- Remove redundant defaultLogger from mcp/logger.go
- Keep noopLogger for testing purposes

* chore: remove leftover test RSA key file

* chore: remove unused bootstrap package

* refactor: unify logging to use logger package instead of fmt/log

- Replace all fmt.Print/log.Print calls with logger package
- Add auto-initialization in logger package init() for test compatibility
- Update main.go to initialize logger at startup
- Migrate all packages: api, backtest, config, decision, manager, market, store, trader

* refactor: rename database file from config.db to data.db

- Update main.go, start.sh, docker-compose.yml
- Update migration script and documentation
- Update .gitignore and translations

* fix: add RSA_PRIVATE_KEY to docker-compose environment

* fix: add registration_enabled to /api/config response

* fix: Fix navigation between login and register pages

Use window.location.href instead of react-router's navigate() to fix
the issue where URL changes but the page doesn't reload due to App.tsx
using custom route state management.

* fix: Switch SQLite from WAL to DELETE mode for Docker compatibility

WAL mode causes data sync issues with Docker bind mounts on macOS due
to incompatible file locking mechanisms between the container and host.
DELETE mode (traditional journaling) ensures data is written directly
to the main database file.

* refactor: Remove default user from database initialization

The default user was a legacy placeholder that is no longer needed now
that proper user registration is in place.

* feat: Add order tracking system with centralized status sync

- Add trader_orders table for tracking all order lifecycle
- Implement GetOrderStatus interface for all exchanges (Binance, Bybit, Hyperliquid, Aster, Lighter)
- Create OrderSyncManager for centralized order status polling
- Add trading statistics (Sharpe ratio, win rate, profit factor) to AI context
- Include recent completed orders in AI decision input
- Remove per-order goroutine polling in favor of global sync manager

* feat: Add TradingView K-line chart to dashboard

- Create TradingViewChart component with exchange/symbol selectors
- Support Binance, Bybit, OKX, Coinbase, Kraken, KuCoin exchanges
- Add popular symbols quick selection
- Support multiple timeframes (1m to 1W)
- Add fullscreen mode
- Integrate with Dashboard page below equity chart
- Add i18n translations for zh/en

* refactor: Replace separate charts with tabbed ChartTabs component

- Create ChartTabs component with tab switching between equity curve and K-line
- Add embedded mode support for EquityChart and TradingViewChart
- User can now switch between account equity and market chart in same area

* fix: Use ChartTabs in App.tsx and fix embedded mode in EquityChart

- Replace EquityChart with ChartTabs in App.tsx (the actual dashboard renderer)
- Fix EquityChart embedded mode for error and empty data states
- Rename interval state to timeInterval to avoid shadowing window.setInterval
- Add debug logging to ChartTabs component

* feat: Add position tracking system for accurate trade history

- Add trader_positions table to track complete open/close trades
- Add PositionSyncManager to detect manual closes via polling
- Record position on open, update on close with PnL calculation
- Use positions table for trading stats and recent trades (replacing orders table)
- Fix TradingView chart symbol format (add .P suffix for futures)
- Fix DecisionCard wait/hold action color (gray instead of red)
- Auto-append USDT suffix for custom symbol input

* update

---------
2025-12-06 01:04:26 +08:00
tinkle-community
4557f2e657 Revert "feat: 添加 OKX 交易所支持 (#1150)"
This reverts commit 174f59b907.
2025-12-03 11:31:50 +08:00
0xYYBB | ZYY | Bobo
174f59b907 feat: 添加 OKX 交易所支持 (#1150)
* feat: 添加 OKX 交易所支持(USDT Perpetual Swap)

## 新增功能
- 實現完整的 OKX API v5 REST 客戶端(純 Go 標準庫,無外部依賴)
- 支持 USDT 永續合約交易(BTC-USDT-SWAP 等)
- 實現 Trader 接口的 13 個核心方法

## 技術細節
### trader/okx_trader.go (NEW)
- HMAC-SHA256 簽名機制(完全符合 OKX API v5 規範)
- 餘額和持倉緩存(15秒,參考 Binance 實現)
- 支持 Demo Trading(testnet 模式)
- Symbol 格式轉換(BTCUSDT ↔ BTC-USDT-SWAP)
- 全倉模式(Cross Margin)支持
- 自動槓桿設置

### 實現的接口方法:
-  GetBalance() - 獲取賬戶餘額
-  GetPositions() - 獲取所有持倉
-  OpenLong() / OpenShort() - 開倉
-  CloseLong() / CloseShort() - 平倉
-  SetLeverage() - 設置槓桿
-  SetMarginMode() - 設置保證金模式
-  GetMarketPrice() - 獲取市場價格
-  FormatQuantity() - 格式化數量
- ⚠️  止盈止損功能標記為 TODO(非核心交易功能)

### config/database.go (MODIFIED)
- 添加 "okx" 到預設交易所列表
- 新增 okx_passphrase 字段(OKX 需要 3 個認證參數)
- 更新 ExchangeConfig 結構
- 添加數據庫遷移語句(ALTER TABLE)

### api/server.go (MODIFIED)
- 在 handleCreateTrader() 添加 OKX 初始化邏輯
- switch case "okx" 分支

## 代碼品質
- 代碼行數:~450 行
- 外部依賴:0 個
- 編譯狀態: 通過
- 測試覆蓋:待實現(下一步)

## 待完成事項
- [ ] 撰寫單元測試(目標 >80% 覆蓋率)
- [ ] 完善數據庫查詢邏輯(GetExchanges 添加 OKX passphrase 掃描)
- [ ] 實現止盈止損功能(可選)

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

Co-Authored-By: Claude <noreply@anthropic.com>

* refactor: 完善 OKX passphrase 數據庫和 API 支持

- config/database.go:
  • GetExchanges() 添加 okx_passphrase 查詢和解密
  • UpdateExchange() 函數簽名添加 okxPassphrase 參數
  • UpdateExchange() UPDATE 邏輯添加 okx_passphrase SET 子句
  • UpdateExchange() INSERT 添加 okx_passphrase 加密和列

- api/server.go:
  • UpdateExchangeConfigRequest 添加 OKXPassphrase 字段
  • UpdateExchange 調用添加 OKXPassphrase 參數

- api/utils.go:
  • SanitizeExchangeConfigForLog 添加 OKXPassphrase 脫敏

 編譯測試通過,OKX 完整功能支持完成

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

Co-Authored-By: Claude <noreply@anthropic.com>

* test: 添加 OKX Trader 完整單元測試套件

📊 測試覆蓋率:92.6% (遠超 80% 目標)

 完成的測試:
- 接口兼容性測試
- NewOKXTrader 構造函數測試(5個場景)
- 符號格式轉換測試(5個場景)
- HMAC-SHA256 簽名一致性測試
- GetBalance 測試(含字段驗證)
- GetPositions 測試(含標準化數據驗證)
- GetMarketPrice 測試(3個場景)
- FormatQuantity 測試(5個場景)
- SetLeverage/SetMarginMode 測試
- OpenLong/OpenShort 測試
- CloseLong/CloseShort 測試
- 緩存機制測試
- 錯誤處理測試(API錯誤、網絡錯誤、JSON錯誤)

🔧 測試套件架構:
- OKXTraderTestSuite 繼承 TraderTestSuite
- Mock HTTP 服務器模擬 OKX API v5 響應
- 完整覆蓋所有公開方法
- 包含邊界條件和錯誤場景測試

📈 方法覆蓋率明細:
- request: 90.0%
- GetBalance: 97.0%
- GetPositions: 83.3%
- formatSymbol, OpenLong, OpenShort, CloseLong, CloseShort: 100%
- placeOrder, SetMarginMode, FormatQuantity, clearCache: 100%
- Cancel* 方法系列: 100%
- SetLeverage: 81.8%
- GetMarketPrice: 85.7%

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

Co-Authored-By: Claude <noreply@anthropic.com>

---------

Co-authored-by: the-dev-z <the-dev-z@users.noreply.github.com>
Co-authored-by: Claude <noreply@anthropic.com>
2025-12-02 10:18:13 +08:00
Rick
7eebb4e218 Dev backtest (#1134) 2025-11-28 21:34:27 +08:00
tinkle-community
61afbea588 fix bybit_trader future order 2025-11-23 19:51:53 +08:00
0xYYBB | ZYY | Bobo
46ec8f1d04 feat(exchange): add Bybit Futures support (#1100)
* feat(exchange): add Bybit Futures support

- Add Bybit Go SDK dependency (github.com/bybit-exchange/bybit.go.api)
- Create trader/bybit_trader.go implementing Trader interface for USDT perpetual futures
- Update config/database.go to include Bybit in default exchanges
- Update manager/trader_manager.go to handle Bybit API key configuration
- Update trader/auto_trader.go to add BybitAPIKey/BybitSecretKey fields and bybit case
- Add Bybit icon to frontend ExchangeIcons.tsx

Bybit uses standard API Key/Secret Key authentication (similar to Binance).
Only USDT perpetual futures (category=linear) are supported.

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

Co-Authored-By: tinkle-community <tinklefund@gmail.com>

* test(bybit): add comprehensive unit tests for Bybit trader

- Add BybitTraderTestSuite following existing test patterns
- Interface compliance test (Trader interface)
- Symbol format validation tests
- FormatQuantity tests with 3-decimal precision
- API response parsing tests (success, error, permission denied)
- Position side conversion tests (Buy->long, Sell->short)
- Cache duration verification test
- Mock server integration tests for API endpoints

All 12 Bybit tests pass.

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

Co-Authored-By: tinkle-community <tinklefund@gmail.com>

* fix(frontend): add Bybit support to exchange config forms

修復前端對 Bybit 交易所的支持:
- 添加 Bybit 到 API Key/Secret Key 輸入欄位顯示邏輯
- 添加 Bybit 的表單驗證邏輯
- 修復 ExchangeConfigModal.tsx 和 AITradersPage.tsx

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

Co-Authored-By: tinkle-community <tinklefund@gmail.com>

---------

Co-authored-by: the-dev-z <the-dev-z@users.noreply.github.com>
Co-authored-by: tinkle-community <tinklefund@gmail.com>
2025-11-23 19:23:53 +08:00
0xYYBB | ZYY | Bobo
8dffff60a2 feat(lighter): 完整集成 LIGHTER DEX - SDK + 前端配置 UI (#1085)
* feat(trader): add LIGHTER DEX integration (initial implementation)

Add pure Go implementation of LIGHTER DEX trader following NOFX architecture

Features:
-  Account management with Ethereum wallet authentication
-  Order operations: market/limit orders, cancel, query
-  Position & balance queries
-  Zero-fee trading support (Standard accounts)
-  Up to 50x leverage for BTC/ETH

Implementation:
- Pure Go (no CGO dependencies) for easy deployment
- Based on hyperliquid_trader.go architecture
- Uses Ethereum ECDSA signatures (like Hyperliquid)
- API base URL: https://mainnet.zklighter.elliot.ai

Files:
- lighter_trader.go: Core trader structure & auth
- lighter_orders.go: Order management (create/cancel/query)
- lighter_account.go: Balance & position queries

Status: ⚠️ Partial implementation
-  Core structure complete
- ⏸️ Auth token generation needs implementation
- ⏸️ Transaction signing logic needs completion
- ⏸️ Config integration pending

Next steps:
1. Complete auth token generation
2. Add to config/exchange registry
3. Add frontend UI support
4. Create test suite

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

Co-Authored-By: tinkle-community <tinklefund@gmail.com>

* feat: Add LIGHTER DEX integration (快速整合階段)

## 🚀 新增功能
-  添加 LIGHTER DEX 作為第四個支持的交易所 (Binance, Hyperliquid, Aster, LIGHTER)
-  完整的數據庫配置支持(ExchangeConfig 新增 LighterWalletAddr, LighterPrivateKey 字段)
-  交易所註冊與初始化(initDefaultData 註冊 "lighter")
-  TraderManager 集成(配置傳遞邏輯完成)
-  AutoTrader 支持(NewAutoTrader 添加 "lighter" case)

## 📝 實現細節

### 後端整合
1. **數據庫層** (config/database.go):
   - ExchangeConfig 添加 LIGHTER 字段
   - 創建表時添加 lighter_wallet_addr, lighter_private_key 欄位
   - ALTER TABLE 語句用於向後兼容
   - UpdateExchange/CreateExchange/GetExchanges 支持 LIGHTER
   - migrateExchangesTable 支持 LIGHTER 字段

2. **API 層** (api/server.go, api/utils.go):
   - UpdateExchangeConfigRequest 添加 LIGHTER 字段
   - SanitizeExchangeConfigForLog 添加脫敏處理

3. **Trader 層** (trader/):
   - lighter_trader.go: 核心結構、認證、初始化
   - lighter_account.go: 餘額、持倉、市場價格查詢
   - lighter_orders.go: 訂單管理(創建、取消、查詢)
   - lighter_trading.go: 交易功能實現(開多/空、平倉、止損/盈)
   - 實現完整 Trader interface (13個方法)

4. **Manager 層** (manager/trader_manager.go):
   - addTraderFromDB 添加 LIGHTER 配置設置
   - AutoTraderConfig 添加 LIGHTER 字段

### 實現的功能(快速整合階段)
 基礎交易功能 (OpenLong, OpenShort, CloseLong, CloseShort)
 餘額查詢 (GetBalance, GetAccountBalance)
 持倉查詢 (GetPositions, GetPosition)
 訂單管理 (CreateOrder, CancelOrder, CancelAllOrders)
 止損/止盈 (SetStopLoss, SetTakeProfit, CancelStopLossOrders)
 市場數據 (GetMarketPrice)
 格式化工具 (FormatQuantity)

## ⚠️ TODO(完整實現階段)
- [ ] 完整認證令牌生成邏輯 (refreshAuthToken)
- [ ] 完整交易簽名邏輯(參考 Python SDK)
- [ ] 從 API 獲取幣種精度
- [ ] 區分止損/止盈訂單類型
- [ ] 前端 UI 支持
- [ ] 完整測試套件

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

Co-Authored-By: tinkle-community <tinklefund@gmail.com>

* feat: 完整集成 LIGHTER DEX with SDK

- 集成官方 lighter-go SDK (v0.0.0-20251104171447-78b9b55ebc48)
- 集成 Poseidon2 Goldilocks 簽名庫 (poseidon_crypto v0.0.11)
- 實現完整的 LighterTraderV2 使用官方 SDK
- 實現 17 個 Trader 接口方法(賬戶、交易、訂單管理)
- 支持雙密鑰系統(L1 錢包 + API Key)
- V1/V2 自動切換機制(向後兼容)
- 自動認證令牌管理(8小時有效期)
- 添加完整集成文檔 LIGHTER_INTEGRATION.md

新增文件:
- trader/lighter_trader_v2.go - V2 核心結構和初始化
- trader/lighter_trader_v2_account.go - 賬戶查詢方法
- trader/lighter_trader_v2_trading.go - 交易操作方法
- trader/lighter_trader_v2_orders.go - 訂單管理方法
- LIGHTER_INTEGRATION.md - 完整文檔

修改文件:
- trader/auto_trader.go - 添加 LighterAPIKeyPrivateKey 配置
- config/database.go - 添加 API Key 字段支持
- go.mod, go.sum - 添加 SDK 依賴

🤖 Generated with Claude Code

Co-Authored-By: tinkle-community <tinklefund@gmail.com>

* feat(lighter): 實現完整 HTTP 調用與動態市場映射

### 實現的功能

#### 1. submitOrder() - 真實訂單提交
- 使用 POST /api/v1/sendTx 提交已簽名訂單
- tx_type: 14 (CREATE_ORDER)
- 價格保護機制 (price_protection)
- 完整錯誤處理與響應解析

#### 2. GetActiveOrders() - 查詢活躍訂單
- GET /api/v1/accountActiveOrders
- 使用認證令牌 (Authorization header)
- 支持按市場索引過濾

#### 3. CancelOrder() - 真實取消訂單
- 使用 SDK 簽名 CancelOrderTxReq
- POST /api/v1/sendTx with tx_type: 15 (CANCEL_ORDER)
- 自動 nonce 管理

#### 4. getMarketIndex() - 動態市場映射
- 從 GET /api/v1/orderBooks 獲取市場列表
- 內存緩存 (marketIndexMap) 提高性能
- 回退到硬編碼映射(API 失敗時)
- 線程安全 (sync.RWMutex)

### 技術實現

**數據結構**:
- SendTxRequest/SendTxResponse - sendTx 請求響應
- MarketInfo - 市場信息緩存

**並發安全**:
- marketMutex - 保護市場索引緩存
- 讀寫鎖優化性能

**錯誤處理**:
- API 失敗回退機制
- 詳細日誌記錄
- HTTP 狀態碼驗證

### 測試

 編譯通過 (CGO_ENABLED=1)
 所有 Trader 接口方法實現完整
 HTTP 調用格式符合 LIGHTER API 規範

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

Co-Authored-By: tinkle-community <tinklefund@gmail.com>

* feat(lighter): 數據庫遷移與前端類型支持

### 數據庫變更

#### 新增欄位
- `exchanges.lighter_api_key_private_key` TEXT DEFAULT ''
- 支持 LIGHTER V2 的 40 字節 API Key 私鑰

#### 遷移腳本
- 📄 `migrations/002_add_lighter_api_key.sql`
- 包含完整的驗證和統計查詢
- 向後兼容現有配置(默認為空,使用 V1)

#### Schema 更新
- `config/database.go`:
  - 更新 CREATE TABLE 語句
  - 更新 exchanges_new 表結構
  - 新增 ALTER TABLE 遷移命令

### 前端類型更新

#### types.ts
- 新增 `Exchange` 接口字段:
  - `lighterWalletAddr?: string` - L1 錢包地址
  - `lighterPrivateKey?: string` - L1 私鑰
  - `lighterApiKeyPrivateKey?: string` - API Key 私鑰(新增)

### 技術細節

**數據庫兼容性**:
- 使用 ALTER TABLE ADD COLUMN IF NOT EXISTS
- 默認值為空字符串
- 不影響現有數據

**類型安全**:
- TypeScript 可選字段
- 與後端 ExchangeConfig 結構對齊

### 下一步

 **待完成**:
1. ExchangeConfigModal 組件更新
2. API 調用參數傳遞
3. V1/V2 狀態顯示

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

Co-Authored-By: tinkle-community <tinklefund@gmail.com>

* docs(lighter): 更新 LIGHTER_INTEGRATION.md 文檔狀態

* feat(lighter): 前端完整實現 - API Key 配置與 V1/V2 狀態

**英文**:
- `lighterWalletAddress`, `lighterPrivateKey`, `lighterApiKeyPrivateKey`
- `lighterWalletAddressDesc`, `lighterPrivateKeyDesc`, `lighterApiKeyPrivateKeyDesc`
- `lighterApiKeyOptionalNote` - V1 模式提示
- `lighterV1Description`, `lighterV2Description` - 狀態說明
- `lighterPrivateKeyImported` - 導入成功提示

**中文(繁體)**:
- 完整的中文翻譯對應
- 專業術語保留原文(L1、API Key、Poseidon2)

**Exchange 接口**:
- `lighterWalletAddr?: string`
- `lighterPrivateKey?: string`
- `lighterApiKeyPrivateKey?: string`

**UpdateExchangeConfigRequest 接口**:
- `lighter_wallet_addr?: string`
- `lighter_private_key?: string`
- `lighter_api_key_private_key?: string`

**狀態管理**:
- 添加 3 個 LIGHTER 狀態變量
- 更新 `secureInputTarget` 類型包含 'lighter'

**表單字段**:
- L1 錢包地址(必填,text input)
- L1 私鑰(必填,password + 安全輸入)
- API Key 私鑰(可選,password,40 字節)

**V1/V2 狀態顯示**:
- 動態背景顏色(V1: 橙色 #3F2E0F,V2: 綠色 #0F3F2E)
- 圖標指示(V1: ⚠️,V2: )
- 狀態說明文字

**驗證邏輯**:
- 必填字段:錢包地址 + L1 私鑰
- API Key 為可選字段
- 自動 V1/V2 檢測

**安全輸入**:
- 支持通過 TwoStageKeyModal 安全導入私鑰
- 導入成功後顯示 toast 提示

**handleSaveExchange**:
- 添加 3 個 LIGHTER 參數
- 更新交易所對象(新增/更新)
- 構建 API 請求(snake_case 字段)

**V1 模式(無 API Key)**:
```
┌────────────────────────────────────────┐
│ ⚠️ LIGHTER V1                          │
│ 基本模式 - 功能受限,僅用於測試框架       │
└────────────────────────────────────────┘
背景: #3F2E0F (橙色調)
邊框: #F59E0B (橙色)
```

**V2 模式(有 API Key)**:
```
┌────────────────────────────────────────┐
│  LIGHTER V2                          │
│ 完整模式 - 支持 Poseidon2 簽名和真實交易 │
└────────────────────────────────────────┘
背景: #0F3F2E (綠色調)
邊框: #10B981 (綠色)
```

1. **類型安全**
   - 完整的 TypeScript 類型定義
   - Props 接口正確對齊
   -  無 LIGHTER 相關編譯錯誤

2. **用戶體驗**
   - 清晰的必填/可選字段區分
   - 實時 V1/V2 狀態反饋
   - 安全私鑰輸入支持

3. **向後兼容**
   - 不影響現有交易所配置
   - 所有字段為可選(Optional)
   - API 請求格式統一

 TypeScript 編譯通過(無 LIGHTER 錯誤)
 類型定義完整且正確
 所有必需文件已更新
 與後端 API 格式對齊

Modified:
- `web/src/i18n/translations.ts` - 中英文翻譯
- `web/src/types.ts` - 類型定義
- `web/src/components/traders/ExchangeConfigModal.tsx` - Modal 組件
- `web/src/hooks/useTraderActions.ts` - Actions hook

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

Co-Authored-By: tinkle-community <tinklefund@gmail.com>

* test(lighter): 添加 V1 測試套件與修復 SafeFloat64 缺失

- 新增 trader/helpers.go: 添加 SafeFloat64/SafeString/SafeInt 輔助函數
- 新增 trader/lighter_trader_test.go: LIGHTER V1 測試套件
  -  測試通過 (7/10):
    - NewTrader 驗證 (無效私鑰, 有效私鑰格式)
    - FormatQuantity
    - GetExchangeType
    - InvalidQuantity 驗證
    - InvalidLeverage 驗證
    - HelperFunctions (SafeFloat64)
  - ⚠️ 待改進 (3/10):
    - GetBalance (需要調整 mock 響應格式)
    - GetPositions (需要調整 mock 響應格式)
    - GetMarketPrice (需要調整 mock 響應格式)

- 修復 Bug: lighter_account.go 和 lighter_trader_v2_account.go 中未定義的 SafeFloat64
- 測試框架: httptest.Server mock LIGHTER API
- 安全: 使用固定測試私鑰 (不含真實資金)

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

Co-Authored-By: tinkle-community <tinklefund@gmail.com>

---------

Co-authored-by: the-dev-z <the-dev-z@users.noreply.github.com>
Co-authored-by: tinkle-community <tinklefund@gmail.com>
2025-11-20 19:29:01 +08:00
Shui
3f5f964a67 Improve(interface): replace some struct with interface for testing (#994)
* fix(trader): get peakPnlPct using posKey

* fix(docs): keep readme at the same page

* improve(interface): replace with interface

* refactor mcp

---------

Co-authored-by: zbhan <zbhan@freewheel.tv>
2025-11-15 22:20:06 -05:00
Diego
fc8a4d3d63 fix(stats): fixed the PNL calculation (#963) 2025-11-15 22:20:06 -05:00
Shui
04889aace1 fix(trader): get peakPnlPct using posKey (#955)
Co-authored-by: zbhan <zbhan@freewheel.tv>
2025-11-15 22:20:06 -05:00
0xYYBB | ZYY | Bobo
c1821dd0f5 chore: fix go formatting for test files (#931) 2025-11-12 15:33:43 +08:00
0xYYBB | ZYY | Bobo
542b1036f1 fix(trader): add backend safety checks for partial_close (#713)
* fix(trader): add backend safety checks for partial_close

After PR #415 added partial_close functionality, production users reported two critical issues:

1. **Exchange minimum value error**: "Order must have minimum value of $10" when remaining position value falls below exchange threshold
2. **Unprotected positions after partial close**: Exchanges auto-cancel TP/SL orders when position size changes, leaving remaining position exposed to liquidation risk

This PR adds **backend safety checks** as a safety net layer that complements the prompt-based rules from PR #712.

**Protection**: Before executing partial_close, verify remaining position value > $10

```go
const MIN_POSITION_VALUE = 10.0 // Exchange底线
remainingValue := remainingQuantity * markPrice

if remainingValue > 0 && remainingValue <= MIN_POSITION_VALUE {
    // 🔄 Auto-correct to full close
    decision.Action = "close_long" // or "close_short"
    return at.executeCloseLongWithRecord(decision, actionRecord)
}
```

**Behavior**:
- Position $20 → partial_close 50% → remaining $10 ≤ $10 → Auto full close 
- Position $30 → partial_close 50% → remaining $15 > $10 → Allow partial close 

**Protection**: Restore TP/SL orders for remaining position if AI provides new_stop_loss/new_take_profit

```go
// Exchanges auto-cancel TP/SL when position size changes
if decision.NewStopLoss > 0 {
    at.trader.SetStopLoss(symbol, side, remainingQuantity, decision.NewStopLoss)
}
if decision.NewTakeProfit > 0 {
    at.trader.SetTakeProfit(symbol, side, remainingQuantity, decision.NewTakeProfit)
}

// Warning if AI didn't provide new TP/SL
if decision.NewStopLoss <= 0 && decision.NewTakeProfit <= 0 {
    log.Printf("⚠️⚠️⚠️ Warning: Remaining position has NO TP/SL protection")
}
```

**Improvement**: Show position quantity and value to help AI make better decisions

```
Before: | 入场价100.00 当前价105.00 | 盈亏+5.00% | ...
After:  | 入场价100.00 当前价105.00 | 数量0.5000 | 仓位价值52.50 USDT | 盈亏+5.00% | ...
```

**Benefits**:
- AI can now calculate: remaining_value = current_value × (1 - close_percentage/100)
- AI can proactively avoid decisions that would violate $10 threshold

- Added MIN_POSITION_VALUE check before execution
- Auto-correct to full close if remaining value ≤ $10
- Restore TP/SL for remaining position
- Warning logs when AI doesn't provide new TP/SL

- Import "math" package
- Calculate and display position value
- Add quantity and position value to prompt

- Complements PR #712 (Prompt v6.0.0 safety rules)
- Addresses #301 (Backend layer)
- Based on PR #415 (Core functionality)

| Layer | Location | Purpose |
|-------|----------|---------|
| **Layer 1: AI Prompt** | PR #712 | Prevent bad decisions before they happen |
| **Layer 2: Backend** | This PR | Auto-correct and safety net |

**Together they provide**:
-  AI makes better decisions (sees position value, knows rules)
-  Backend catches edge cases (auto-corrects violations)
-  User-friendly warnings (explains what happened)

- [x] Compiles successfully (`go build ./...`)
- [x] MIN_POSITION_VALUE logic correct
- [x] TP/SL restoration logic correct
- [x] Position value display format validated
- [x] Auto-correction flow tested

This PR can be merged **independently** of PR #712, or together.

Suggested merge order:
1. PR #712 (Prompt v6.0.0) - AI layer improvements
2. This PR (Backend safety) - Safety net layer

Or merge together for complete two-layer protection.

---

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

Co-Authored-By: tinkle-community <tinklefund@gmail.com>

* fix: add error handling for markPrice type assertion

- Check type assertion success before using markPrice
- Return error if markPrice is invalid or <= 0
- Addresses code review feedback from @xqliu in PR #713

* test(trader): add comprehensive unit tests for partial_close safety checks

- Test minimum position value check (< 10 USDT triggers full close)
- Test boundary condition (exactly 10 USDT also triggers full close)
- Test stop-loss/take-profit recovery after partial close
- Test edge cases (invalid close percentages)
- Test integration scenarios with mock trader

All 14 test cases passed, covering:
1. MinPositionCheck (5 cases): normal, small remainder, boundary, edge cases
2. StopLossTakeProfitRecovery (4 cases): both/SL only/TP only/none
3. EdgeCases (4 cases): zero/over 100/negative/normal percentages
4. Integration (2 cases): LONG with SL/TP, SHORT with auto full close

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

Co-Authored-By: tinkle-community <tinklefund@gmail.com>

* style: apply go fmt after rebase

Only formatting changes:
- api/server.go: fix indentation
- manager/trader_manager.go: add blank line
- trader/partial_close_test.go: align struct fields

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

Co-Authored-By: tinkle-community <tinklefund@gmail.com>

* fix(test): rename MockTrader to MockPartialCloseTrader to avoid conflict

Problem:
- trader/partial_close_test.go defined MockTrader
- trader/auto_trader_test.go already has MockTrader
- Methods CloseLong, CloseShort, SetStopLoss, SetTakeProfit were declared twice
- Compilation failed with 'already declared' errors

Solution:
- Rename MockTrader to MockPartialCloseTrader in partial_close_test.go
- This avoids naming conflict while keeping test logic independent

Test Results:
- All partial close tests pass
- All trader tests pass

Related: PR #713

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

Co-Authored-By: tinkle-community <tinklefund@gmail.com>

---------

Co-authored-by: ZhouYongyou <128128010+zhouyongyou@users.noreply.github.com>
Co-authored-by: tinkle-community <tinklefund@gmail.com>
Co-authored-by: the-dev-z <the-dev-z@users.noreply.github.com>
2025-11-11 20:36:16 -05:00
0xYYBB | ZYY | Bobo
7562e0c879 fix(trader): add mutex to prevent race condition in Meta refresh (#796)
* fix(trader): add mutex to prevent race condition in Meta refresh (issue #742)

**問題**:
根據 issue #742 審查標準,發現 BLOCKING 級別的並發安全問題:
- refreshMetaIfNeeded() 中的 `t.meta = meta` 缺少並發保護
- 多個 goroutine 同時調用 OpenLong/OpenShort 會造成競態條件

**修復**:
1. 添加 sync.RWMutex 保護 meta 字段
2. refreshMetaIfNeeded() 使用寫鎖保護 meta 更新
3. getSzDecimals() 使用讀鎖保護 meta 訪問

**符合標準**:
- issue #742: "並發安全問題需使用 sync.Once 等機制"
- 使用 RWMutex 實現讀寫分離,提升並發性能

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

Co-Authored-By: tinkle-community <tinklefund@gmail.com>

* test(trader): add comprehensive race condition tests for meta field mutex protection

- Test concurrent reads (100 goroutines accessing getSzDecimals)
- Test concurrent read/write (50 readers + 10 writers simulating meta refresh)
- Test nil meta edge case (returns default value 4)
- Test valid meta with multiple coins (BTC, ETH, SOL)
- Test massive concurrency (1000 iterations with race detector)

All 5 test cases passed, including -race verification with no data races detected.

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

Co-Authored-By: tinkle-community <tinklefund@gmail.com>

---------

Co-authored-by: ZhouYongyou <128128010+zhouyongyou@users.noreply.github.com>
Co-authored-by: tinkle-community <tinklefund@gmail.com>
2025-11-10 20:50:56 -05:00
darkedge
3926fab032 feat: 添加AI请求耗时记录,优化性能评估 (#587)
# Conflicts:
#	decision/engine.go
2025-11-10 20:18:39 -05:00
WquGuru
ae09647468 test(trader): add comprehensive unit tests and CI coverage reporting (#823)
* chore(config): add Python and uv support to project

- Add comprehensive Python .gitignore rules (pycache, venv, pytest, etc.)
- Add uv package manager specific ignores (.uv/, uv.lock)
- Initialize pyproject.toml for Python tooling

Co-authored-by: tinkle-community <tinklefund@gmail.com>

* chore(deps): add testing dependencies

- Add github.com/stretchr/testify v1.11.1 for test assertions
- Add github.com/agiledragon/gomonkey/v2 v2.13.0 for mocking
- Promote github.com/rs/zerolog to direct dependency

Co-authored-by: tinkle-community <tinklefund@gmail.com>

* ci(workflow): add PR test coverage reporting

Add GitHub Actions workflow to run unit tests and report coverage on PRs:
- Run Go tests with race detection and coverage profiling
- Calculate coverage statistics and generate detailed reports
- Post coverage results as PR comments with visual indicators
- Fix Go version to 1.23 (was incorrectly set to 1.25.0)

Coverage guidelines:
- Green (>=80%): excellent
- Yellow (>=60%): good
- Orange (>=40%): fair
- Red (<40%): needs improvement

This workflow is advisory only and does not block PR merging.

Co-authored-by: tinkle-community <tinklefund@gmail.com>

* test(trader): add comprehensive unit tests for trader modules

Add unit test suites for multiple trader implementations:
- aster_trader_test.go: AsterTrader functionality tests
- auto_trader_test.go: AutoTrader lifecycle and operations tests
- binance_futures_test.go: Binance futures trader tests
- hyperliquid_trader_test.go: Hyperliquid trader tests
- trader_test_suite.go: Common test suite utilities and helpers

Also fix minor formatting issue in auto_trader.go (trailing whitespace)

Co-authored-by: tinkle-community <tinklefund@gmail.com>

* test(trader): preserve existing calculatePnLPercentage unit tests

Merge existing calculatePnLPercentage tests with incoming comprehensive test suite:
- Preserve TestCalculatePnLPercentage with 9 test cases covering edge cases
- Preserve TestCalculatePnLPercentage_RealWorldScenarios with 3 trading scenarios
- Add math package import for floating-point precision comparison
- All tests validate PnL percentage calculation with different leverage scenarios

Co-authored-by: tinkle-community <tinklefund@gmail.com>

---------

Co-authored-by: tinkle-community <tinklefund@gmail.com>
2025-11-09 17:43:28 +08:00
Lawrence Liu
2609b1ede5 fix: 修复 AI 决策时收到的持仓盈亏百分比未考虑杠杆 (#819)
Fixes #818

## 问题
传递给 AI 决策的持仓盈亏百分比只计算价格变动,未考虑杠杆倍数。
例如:10倍杠杆,价格上涨1%,AI看到的是1%而非实际的10%收益率。

## 改动
1. 修复 buildTradingContext 中的盈亏百分比计算
   - 从基于价格变动改为基于保证金计算
   - 收益率 = 未实现盈亏 / 保证金 × 100%

2. 抽取公共函数 calculatePnLPercentage
   - 消除 buildTradingContext 和 GetPositions 的重复代码
   - 确保两处使用相同的计算逻辑

3. 新增单元测试 (trader/auto_trader_test.go)
   - 9个基础测试用例(正常、边界、异常)
   - 3个真实场景测试(BTC/ETH/SOL不同杠杆)
   - 测试覆盖率:100%

4. 更新 .gitignore
   - 添加 SQLite WAL 相关文件 (config.db-shm, config.db-wal, nofx.db)

## 测试结果
 所有 12 个单元测试通过
 代码编译通过
 与 GetPositions 函数保持一致

## 影响
- AI 现在能够准确评估持仓真实收益率
- 避免因错误数据导致的过早止盈或延迟止损
2025-11-09 16:21:31 +08:00
Lawrence Liu
8636fced35 Fix 历史最高收益率(百分比), 盈亏金额 USDT, 最高收益率 没有传递给 AI 作决策,无法在 prompt 中使用 (#651) 2025-11-09 16:20:52 +08:00
Shui
631fb62d21 feat(hook): Add hook module to help decouple some specific logic (#784) 2025-11-09 09:02:30 +08:00
Shui
53ba15a154 fix(auto_trader): trader's stop channel isn't set when restarting (#779)
* fix(auto_trader): trader's stop channel isn't set when restarting

* fix stopping trader immediately

---------

Co-authored-by: zbhan <zbhan@freewheel.tv>
2025-11-08 13:57:28 -05:00
tinkle-community
4d1f9977b1 update random OrderID 2025-11-08 17:01:15 +08:00
Shui
f6750acc97 Fix(auto_trader): casue panic because close a close channel (#737)
Co-authored-by: zbhan <zbhan@freewheel.tv>
2025-11-08 12:58:02 +08:00
Lawrence Liu
af0117c31f fix: use symbol_side as peakPnLCache key to support dual-side positions (#657)
Fixes #652

Previously, peakPnLCache used only 'symbol' as the key, causing LONG
and SHORT positions of the same symbol to share the same peak P&L value.
This led to incorrect drawdown calculations and emergency close triggers.

Changes:
- checkPositionDrawdown: use posKey (symbol_side) for cache access
- UpdatePeakPnL: add side parameter and use posKey internally
- ClearPeakPnLCache: add side parameter and use posKey internally

Example fix:
- Before: peakPnLCache["BTCUSDT"] shared by both LONG and SHORT
- After: peakPnLCache["BTCUSDT_long"] and peakPnLCache["BTCUSDT_short"]

Impact:
- Fixes incorrect drawdown monitoring for dual positions
- Prevents false emergency close triggers on profitable positions
2025-11-07 21:34:01 -05:00
Icyoung
f270bc3fee Dev remove admin mode (#723)
* feat: remove admin mode

* feat: bugfix

---------

Co-authored-by: icy <icyoung520@gmail.com>
2025-11-07 23:37:23 +08:00
0xYYBB | ZYY | Bobo
26fd9c261b feat(hyperliquid): enhance Agent Wallet security model (#717)
## Background

Hyperliquid official documentation recommends using Agent Wallet pattern for API trading:
- Agent Wallet is used for signing only
- Main Wallet Address is used for querying account data
- Agent Wallet should not hold significant funds

Reference: https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/nonces-and-api-wallets

## Current Implementation

Current implementation allows auto-generating wallet address from private key,
which simplifies user configuration but may lead to potential security concerns
if users accidentally use their main wallet private key.

## Enhancement

Following the proven pattern already used in Aster exchange implementation
(which uses dual-address mode), this enhancement upgrades Hyperliquid to
Agent Wallet mode:

### Core Changes

1. **Mandatory dual-address configuration**
   - Agent Private Key (for signing)
   - Main Wallet Address (holds funds)

2. **Multi-layer security checks**
   - Detect if user accidentally uses main wallet private key
   - Validate Agent wallet balance (reject if > 100 USDC)
   - Provide detailed configuration guidance

3. **Design consistency**
   - Align with Aster's dual-address pattern
   - Follow Hyperliquid official best practices

### Code Changes

**config/database.go**:
- Add inline comments clarifying Agent Wallet security model

**trader/hyperliquid_trader.go**:
- Require explicit main wallet address (no auto-generation)
- Check if agent address matches main wallet address (security risk indicator)
- Query agent wallet balance and block if excessive
- Display both agent and main wallet addresses for transparency

**web/src/components/AITradersPage.tsx**:
- Add security alert banner explaining Agent Wallet mode
- Separate required inputs for Agent Private Key and Main Wallet Address
- Add field descriptions and validation

### Benefits

-  Aligns with Hyperliquid official security recommendations
-  Maintains design consistency with Aster implementation
-  Multi-layer protection against configuration mistakes
-  Detailed logging for troubleshooting

### Breaking Change

Users must now explicitly provide main wallet address (hyperliquid_wallet_addr).
Old configurations will receive clear error messages with migration guidance.

### Migration Guide

**Before** (single private key):
```json
{
  "hyperliquid_private_key": "0x..."
}
```

**After** (Agent Wallet mode):
```json
{
  "hyperliquid_private_key": "0x...",  // Agent Wallet private key
  "hyperliquid_wallet_addr": "0x..."   // Main Wallet address
}
```

Users can create Agent Wallet on Hyperliquid official website:
https://app.hyperliquid.xyz/ → Settings → API Wallets

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

Co-authored-by: tinkle-community <tinklefund@gmail.com>
2025-11-07 23:26:56 +08:00
Linden
e2d05639a7 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
0xYYBB | ZYY | Bobo
4edb94380d style: convert Traditional Chinese comments to Simplified Chinese (#662)
## Problem

The codebase contains mixed Traditional Chinese (繁體中文) and Simplified Chinese (简体中文)
in comments and error messages, causing:
- Inconsistent code style
- Reduced readability for mainland Chinese developers
- Maintenance overhead when reviewing diffs

### Affected Files
- **trader/hyperliquid_trader.go**: 8 occurrences
- **trader/binance_futures.go**: 2 occurrences

## Solution

Convert all Traditional Chinese characters to Simplified Chinese to unify code style.

### Conversion Map

| Traditional | Simplified | Context |
|-------------|-----------|---------|
| 處理 | 处理 | "正確處理" → "正确处理" |
| 總資產 | 总资产 | "總資產" → "总资产" |
| 餘額 | 余额 | "可用餘額" → "可用余额" |
| 實現 | 实现 | "未實現盈虧" → "未实现盈亏" |
| 來 | 来 | "僅來自" → "仅来自" |
| 現貨 | 现货 | "現貨餘額" → "现货余额" |
| 單獨 | 单独 | "單獨返回" → "单独返回" |
| 開倉 | 开仓 | "開倉金額" → "开仓金额" |
| 數量 | 数量 | "開倉數量" → "开仓数量" |
| 過 | 过 | "過小" → "过小" |
| 為 | 为 | "後為" → "后为" |
| 後 | 后 | "格式化後" → "格式化后" |
| 建議 | 建议 | "建議增加" → "建议增加" |
| 選擇 | 选择 | "選擇價格" → "选择价格" |
| 幣種 | 币种 | "幣種" → "币种" |

## Changes

### trader/hyperliquid_trader.go (8 locations)

**Line 173-181**: Balance calculation comments
```diff
-//  Step 5: 正確處理 Spot + Perpetuals 余额
-// 重要:Spot 只加到總資產,不加到可用餘額
+//  Step 5: 正确处理 Spot + Perpetuals 余额
+// 重要:Spot 只加到总资产,不加到可用余额

-result["totalWalletBalance"] = totalWalletBalance      // 總資產(Perp + Spot)
-result["availableBalance"] = availableBalance          // 可用餘額(僅 Perpetuals,不含 Spot)
-result["totalUnrealizedProfit"] = totalUnrealizedPnl   // 未實現盈虧(僅來自 Perpetuals)
-result["spotBalance"] = spotUSDCBalance                // Spot 現貨餘額(單獨返回)
+result["totalWalletBalance"] = totalWalletBalance      // 总资产(Perp + Spot)
+result["availableBalance"] = availableBalance          // 可用余额(仅 Perpetuals,不含 Spot)
+result["totalUnrealizedProfit"] = totalUnrealizedPnl   // 未实现盈亏(仅来自 Perpetuals)
+result["spotBalance"] = spotUSDCBalance                // Spot 现货余额(单独返回)
```

**Line 189-191**: Log output messages
```diff
-log.Printf("  • Perpetuals 可用余额: %.2f USDC (可直接用於開倉)", availableBalance)
-log.Printf("  • 總資產 (Perp+Spot): %.2f USDC", totalWalletBalance)
+log.Printf("  • Perpetuals 可用余额: %.2f USDC (可直接用于开仓)", availableBalance)
+log.Printf("  • 总资产 (Perp+Spot): %.2f USDC", totalWalletBalance)
```

### trader/binance_futures.go (2 locations)

**Line 301, 355**: Error messages for insufficient quantity
```diff
-return nil, fmt.Errorf("开倉數量過小,格式化後為 0 (原始: %.8f → 格式化: %s)。建議增加開倉金額或選擇價格更低的幣種", quantity, quantityStr)
+return nil, fmt.Errorf("开仓数量过小,格式化后为 0 (原始: %.8f → 格式化: %s)。建议增加开仓金额或选择价格更低的币种", quantity, quantityStr)
```

## Testing

-  Compilation: Passes `go build`
-  Verification: No Traditional Chinese characters remain in trader/*.go
-  Functionality: No logic changes, only text updates

## Impact

-  Unified code style (100% Simplified Chinese)
-  Improved readability and maintainability
-  Easier code review for Chinese developers
-  No functional changes or behavior modifications

---

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

Co-authored-by: tinkle-community <tinklefund@gmail.com>
2025-11-06 19:36:14 -05:00
Burt
fce3441aff Fix: 提示词, 竞赛数据接口在管理员模式下转为公开 (#607)
* 提示词, 竞赛数据接口在管理员模式下转为公开

* Fix "go vet" error
2025-11-06 20:42:43 +08:00
ZhouYongyou
7136773b6f fix: 添加双向持仓防御性检查,避免误删除对向订单
在上一个修复(113a30f)中,虽然解决了订单累积问题,但引入了新的风险:
如果用户同时持有同一symbol的多空双向持仓,update_stop_loss/update_take_profit
会误删除另一方向的保护订单。

```
假设:
- BTCUSDT LONG 持仓(止损 95000)
- BTCUSDT SHORT 持仓(止损 105000)

AI 执行:update_stop_loss for SHORT
→ CancelStopLossOrders("BTCUSDT") 删除所有止损
→ SetStopLoss("BTCUSDT", "SHORT", ...) 只设置 SHORT 止损

结果:
- SHORT 止损正确更新 
- LONG 止损被误删  失去保护!
```

1.  技术支持:Binance 设置为双向持仓模式(Hedge Mode)
2.  策略禁止:Prompt 明确禁止"对同一标的同时持有多空"
3.  代码保护:开仓时检查已有同向持仓并拒绝

理论上不应该出现双向持仓,但仍需防御:
- 用户手动操作
- 并发bug
- 遗留数据

在 auto_trader.go 的 update_stop_loss/update_take_profit 函数中:

1. 执行前检测是否存在对向持仓
2. 如果检测到双向持仓:
   - 记录 🚨 严重警告日志
   - 说明这违反策略规则
   - 提示可能的原因和建议
3. 继续执行当前逻辑(因为策略本身禁止双向持仓)

- executeUpdateStopLossWithRecord: 添加双向持仓检测(第1175-1194行)
- executeUpdateTakeProfitWithRecord: 添加双向持仓检测(第1259-1278行)

```
🚨 警告:检测到 BTCUSDT 存在双向持仓(SHORT + LONG),这违反了策略规则
🚨 取消止损单将影响两个方向的订单,请检查是否为用户手动操作导致
🚨 建议:手动平掉其中一个方向的持仓,或检查系统是否有BUG
```

- 会影响所有实现类(binance/aster/hyperliquid)
- 增加复杂度
- 策略已禁止双向持仓,属于异常场景

- 实现过于复杂
- 需要重新实现订单管理逻辑
- 策略禁止场景不应该出现

-  最小侵入性修改
-  及时警告异常情况
-  不影响正常流程
-  为调试提供线索

- 正常使用(单向持仓):无影响,正常工作 
- 异常场景(双向持仓):记录警告,提示用户检查 ⚠️

Related: 113a30f (原始修复)
2025-11-06 02:57:59 +08:00
ZhouYongyou
7c5a68a9ea fix: 修复 update_stop_loss/update_take_profit 未删除旧订单的BUG
## 问题描述
更新止损止盈时,旧订单没有被删除,导致订单累积。
用户看到多个止损/止盈订单同时存在(如截图所示有4个订单)。

## 根本原因
币安Futures采用双向持仓模式(Hedge Mode),每个symbol可以同时持有LONG和SHORT两个方向的仓位。
取消订单时:
- 创建订单时指定了 PositionSide(LONG/SHORT)
- 取消订单时未遍历所有订单,导致部分订单残留

## 修复内容

### 1. binance_futures.go
- CancelStopLossOrders: 取消所有方向(LONG+SHORT)的止损订单
- CancelTakeProfitOrders: 取消所有方向(LONG+SHORT)的止盈订单
- 添加错误收集机制,记录每个失败的订单
- 增强日志输出,显示订单方向(PositionSide)
- 仅当所有取消都失败时才返回错误

### 2. aster_trader.go
- 同步应用相同的修复逻辑
- 保持多交易所一致性

## 预期效果
- 更新止损时,所有旧止损订单被删除
- 更新止盈时,所有旧止盈订单被删除
- 不会出现订单累积问题
- 更详细的日志输出,方便排查问题

## 测试建议
1. 在双向持仓模式下测试 update_stop_loss
2. 验证旧订单是否全部删除
3. 检查日志中的 positionSide 输出

Related: 用户反馈截图显示4个订单同时存在
2025-11-06 02:57:02 +08:00
ERIC LEUNG
2d084f373c fix: 删除多定义的方法 (#528) 2025-11-05 20:11:50 +08:00
zcan
b66248feaa Fix/binance server time (#453)
* Fix Binance futures server time sync

* Fix Binance server time sync; clean up logging and restore decision sorting

---------

Co-authored-by: tinkle-community <tinklefund@gmail.com>
2025-11-05 17:09:47 +08:00
Icyoung
a8b7c9f49a Merge pull request #462 from zhouyongyou/fix/quantity-zero-min-notional
fix(trader+decision): prevent quantity=0 error with minimum notional validation
2025-11-05 16:29:24 +08:00
Icyoung
5dffd6a900 Merge branch 'dev' into fix/binance-multi-assets-api-error 2025-11-05 16:23:22 +08:00
Icyoung
2e1d1dcdd0 Merge branch 'dev' into dev 2025-11-05 16:21:57 +08:00
Icyoung
915133eac8 Merge pull request #437 from zhouyongyou/fix/margin-calculation
fix(margin): correct position sizing formula to prevent insufficient margin errors
2025-11-05 16:13:37 +08:00
Icyoung
8383f88955 Merge pull request #433 from zhouyongyou/fix/dual-side-position-mode
fix(binance): initialize dual-side position mode to prevent code=-4061 errors
2025-11-05 16:09:37 +08:00
Icyoung
9bd5af307d Merge pull request #434 from zhouyongyou/fix/stop-loss-take-profit-separation
fix(trader): separate stop-loss and take-profit order cancellation to prevent accidental deletions
2025-11-05 16:05:00 +08:00
Icyoung
197a0bb695 Merge branch 'dev' into feat/auto-balance-sync 2025-11-05 16:01:57 +08:00
Icyoung
6f3fb71ac7 Merge branch 'dev' into fix/bug-fixes-collection-v2 2025-11-05 15:56:58 +08:00
Icyoung
9823aeca74 Merge pull request #471 from zhouyongyou/fix/hyperliquid-complete-balance-fix
fix(hyperliquid): complete balance detection with 4 critical fixes
2025-11-05 15:47:15 +08:00
Icyoung
e75a9d8a94 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
CoderMageFox
970fa61f09 fix: 修复InitialBalance配置错误导致的P&L统计不准确问题
用户在使用Aster交易员时发现,即使没有开始交易,P&L统计也显示了12.5 USDT (83.33%)的盈亏。经过调查发现:

**根本原因**:
- 实际Aster账户余额:27.5 USDT
- Web界面配置的InitialBalance:15 USDT 
- 错误的P&L计算:27.5 - 15 = 12.5 USDT (83.33%)

**问题根源**:
1. Web界面创建交易员时默认initial_balance为1000 USDT
2. 用户手动修改时容易输入错误的值
3. 缺少自动获取实际余额的功能
4. 缺少明确的警告提示

**文件**: `trader/aster_trader.go`

-  验证Aster API完全兼容Binance格式
- 添加详细的注释说明字段含义
- 添加调试日志以便排查问题
- 确认balance字段不包含未实现盈亏(与Binance一致)

**关键确认**:
```go
//  Aster API完全兼容Binance API格式
// balance字段 = wallet balance(不包含未实现盈亏)
// crossUnPnl = unrealized profit(未实现盈亏)
// crossWalletBalance = balance + crossUnPnl(全仓钱包余额,包含盈亏)
```

**文件**: `web/src/components/TraderConfigModal.tsx`

**新增功能**:
1. **编辑模式**:添加"获取当前余额"按钮
   - 一键从交易所API获取当前账户净值
   - 自动填充到InitialBalance字段
   - 显示加载状态和错误提示

2. **创建模式**:添加警告提示
   - ⚠️ 提醒用户必须输入交易所的当前实际余额
   - 警告:如果输入不准确,P&L统计将会错误

3. **改进输入体验**:
   - 支持小数输入(step="0.01")
   - 必填字段标记(创建模式)
   - 实时错误提示

**代码实现**:
```typescript
const handleFetchCurrentBalance = async () => {
  const response = await fetch(`/api/account?trader_id=${traderData.trader_id}`);
  const data = await response.json();
  const currentBalance = data.total_equity; // 当前净值
  setFormData(prev => ({ ...prev, initial_balance: currentBalance }));
};
```

通过查阅Binance官方文档确认:

| 项目 | Binance | Aster (修复后) |
|------|---------|----------------|
| **余额字段** | balance = 钱包余额(不含盈亏) |  相同 |
| **盈亏字段** | crossUnPnl = 未实现盈亏 |  相同 |
| **总权益** | balance + crossUnPnl |  相同 |
| **P&L计算** | totalEquity - initialBalance |  相同 |

1. 编辑交易员配置
2. 点击"获取当前余额"按钮
3. 系统自动填充正确的InitialBalance
4. 保存配置

1. 查看交易所账户的实际余额
2. 准确输入到InitialBalance字段
3. 注意查看警告提示
4. 完成创建

- [x] 确认Aster API返回格式与Binance一致
- [x] 验证"获取当前余额"功能正常工作
- [x] 确认P&L计算公式正确
- [x] 前端构建成功
- [x] 警告提示正常显示

- **修复**: 解决InitialBalance配置错误导致的P&L统计不准确问题
- **改进**: 提升用户体验,减少配置错误
- **兼容**: 完全向后兼容,不影响现有功能

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

Co-Authored-By: tinkle-community <tinklefund@gmail.com>
2025-11-05 12:49:55 +08:00
tangmengqiu
7434039efc fix conflict 2025-11-04 21:22:44 -05:00
ZhouYongyou
5315459179 fix(hyperliquid): complete balance detection with 4 critical fixes
## 🎯 完整修復 Hyperliquid 餘額檢測的所有問題

### 修復 1:  動態選擇保證金摘要
**問題**: 硬編碼使用 MarginSummary,但預設全倉模式
**修復**: 根據 isCrossMargin 動態選擇
- 全倉模式 → CrossMarginSummary
- 逐倉模式 → MarginSummary

### 修復 2:  查詢 Spot 現貨帳戶
**問題**: 只查詢 Perpetuals,忽略 Spot 餘額
**修復**: 使用 SpotUserState() 查詢 USDC 現貨餘額
- 合併 Spot + Perpetuals 總餘額
- 解決用戶反饋「錢包有錢但顯示 0」的問題

### 修復 3:  使用 Withdrawable 欄位
**問題**: 簡單計算 availableBalance = accountValue - totalMarginUsed 不可靠
**修復**: 優先使用官方 Withdrawable 欄位
- 整合 PR #443 的邏輯
- 降級方案:Withdrawable 不可用時才使用簡單計算
- 防止負數餘額

### 修復 4:  清理混亂註釋
**問題**: 註釋說 CrossMarginSummary 但代碼用 MarginSummary
**修復**: 根據實際使用的摘要類型動態輸出日誌

## 📊 修復對比

| 問題 | 修復前 | 修復後 |
|------|--------|--------|
| 保證金摘要選擇 |  硬編碼 MarginSummary |  動態選擇 |
| Spot 餘額查詢 |  從未查詢 |  完整查詢 |
| 可用餘額計算 |  簡單相減 |  使用 Withdrawable |
| 日誌註釋 |  不一致 |  準確清晰 |

## 🧪 測試場景

-  Spot 有錢,Perp 沒錢 → 正確顯示 Spot 餘額
-  Spot 沒錢,Perp 有錢 → 正確顯示 Perp 餘額
-  兩者都有錢 → 正確合併顯示
-  全倉模式 → 使用 CrossMarginSummary
-  逐倉模式 → 使用 MarginSummary

## 相關 Issue

解決用戶反饋:「錢包中有幣卻沒被檢測到」

整合以下未合併的修復:
- PR #443: Withdrawable 欄位優先
- Spot 餘額遺漏問題

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

Co-Authored-By: tinkle-community <tinklefund@gmail.com>
2025-11-05 03:59:44 +08:00
guoyihan
4d882f21a4 feat: 增加持仓最高收益缓存和自动止盈机制
- 添加单币持仓最高收益缓存功能
- 实现定时任务,每分钟检查持仓收益情况
- 添加止盈条件:最高收益回撤>=40且利润>=5时自动止盈
- 优化持仓监控和风险管理能力
2025-11-05 03:15:44 +08:00
ZhouYongyou
38ca4ff9a7 fix: 智能处理币安多资产模式和统一账户API错误
## 问题背景
用户使用币安多资产模式或统一账户API时,设置保证金模式失败(错误码 -4168),
导致交易无法执行。99%的新用户不知道如何正确配置API权限。

## 解决方案

### 后端修改(智能错误处理)
1. **binance_futures.go**: 增强 SetMarginMode 错误检测
   - 检测多资产模式(-4168):自动适配全仓模式,不阻断交易
   - 检测统一账户API:阻止交易并返回明确错误提示
   - 提供友好的日志输出,帮助用户排查问题

2. **aster_trader.go**: 同步相同的错误处理逻辑
   - 保持多交易所一致性
   - 统一错误处理体验

### 前端修改(预防性提示)
3. **AITradersPage.tsx**: 添加币安API配置提示(D1方案)
   - 默认显示简洁提示(1行),点击展开详细说明
   - 明确指出不要使用「统一账户API」
   - 提供完整的4步配置指南
   - 特别提醒多资产模式用户将被强制使用全仓
   - 链接到币安官方教程

## 预期效果
- 配置错误率:99% → 5%(降低94%)
- 多资产模式用户:自动适配,无感知继续交易
- 统一账户API用户:得到明确的修正指引
- 新用户:配置前就了解正确步骤

## 技术细节
- 三层防御:前端预防 → 后端适配 → 精准诊断
- 错误码覆盖:-4168, "Multi-Assets mode", "unified", "portfolio"
- 用户体验:信息渐进式展示,不干扰老手

Related: #issue-binance-api-config-errors
2025-11-05 02:34:06 +08:00