Feature/okx trading (#1177)

* feat: add OKX exchange trading support
- Add OKX trader client with full trading API integration
- Support API Key, Secret Key, and Passphrase authentication
- Add OKX icon and frontend configuration modal
- Update exchange store and types for OKX fields
* fix: add passphrase column migration and fix exchange type mapping
* fix: show OKX input fields in exchange config modal
* fix: ensure all supported exchanges exist for user when listing
* fix: simplify exchange type check condition for OKX
* debug: add visible debug info for exchange id
* fix: remove debug info from exchange config modal
* fix: add OKX to exchange type condition in AITradersPage
* feat: complete OKX trading support and fix exchange config issues
- Add LIGHTER exchange UI support in AITradersPage
- Add passphrase field to UpdateExchangeConfigRequest type
- Fix OKX HTTP client to bypass proxy (disable system proxy)
- Auto-fetch initial balance from exchange when not set
- Support multiple balance field names for different exchanges
- Add detailed error messages when trader fails to load
- Add lighter_api_key_private_key field to exchange store
This commit is contained in:
tinkle-community
2025-12-06 18:04:59 +08:00
committed by GitHub
parent 5d1d0b6842
commit 1e5ece947c
11 changed files with 1489 additions and 68 deletions

View File

@@ -22,7 +22,7 @@ type AutoTraderConfig struct {
AIModel string // AI模型: "qwen" 或 "deepseek"
// 交易平台选择
Exchange string // "binance", "bybit", "hyperliquid", "aster" 或 "lighter"
Exchange string // "binance", "bybit", "okx", "hyperliquid", "aster" 或 "lighter"
// 币安API配置
BinanceAPIKey string
@@ -32,6 +32,11 @@ type AutoTraderConfig struct {
BybitAPIKey string
BybitSecretKey string
// OKX API配置
OKXAPIKey string
OKXSecretKey string
OKXPassphrase string
// Hyperliquid配置
HyperliquidPrivateKey string
HyperliquidWalletAddr string
@@ -174,6 +179,9 @@ func NewAutoTrader(config AutoTraderConfig, st *store.Store, userID string) (*Au
case "bybit":
logger.Infof("🏦 [%s] 使用Bybit合约交易", config.Name)
trader = NewBybitTrader(config.BybitAPIKey, config.BybitSecretKey)
case "okx":
logger.Infof("🏦 [%s] 使用OKX合约交易", config.Name)
trader = NewOKXTrader(config.OKXAPIKey, config.OKXSecretKey, config.OKXPassphrase)
case "hyperliquid":
logger.Infof("🏦 [%s] 使用Hyperliquid交易", config.Name)
trader, err = NewHyperliquidTrader(config.HyperliquidPrivateKey, config.HyperliquidWalletAddr, config.HyperliquidTestnet)
@@ -213,9 +221,28 @@ func NewAutoTrader(config AutoTraderConfig, st *store.Store, userID string) (*Au
return nil, fmt.Errorf("不支持的交易平台: %s", config.Exchange)
}
// 验证初始金额配置
// 验证初始金额配置如果为0则自动从交易所获取
if config.InitialBalance <= 0 {
return nil, fmt.Errorf("初始金额必须大于0请在配置中设置InitialBalance")
logger.Infof("📊 [%s] 初始金额未设置,尝试从交易所获取当前余额...", config.Name)
account, err := trader.GetBalance()
if err != nil {
return nil, fmt.Errorf("初始金额未设置且无法从交易所获取余额: %w", err)
}
// 尝试多种余额字段名(不同交易所返回格式不同)
balanceKeys := []string{"total_equity", "totalWalletBalance", "wallet_balance", "totalEq", "balance"}
var foundBalance float64
for _, key := range balanceKeys {
if balance, ok := account[key].(float64); ok && balance > 0 {
foundBalance = balance
break
}
}
if foundBalance > 0 {
config.InitialBalance = foundBalance
logger.Infof("✓ [%s] 自动获取初始金额: %.2f USDT", config.Name, foundBalance)
} else {
return nil, fmt.Errorf("初始金额必须大于0请在配置中设置InitialBalance或确保交易所账户有余额")
}
}
// 获取最后的周期编号(用于恢复)

1123
trader/okx_trader.go Normal file

File diff suppressed because it is too large Load Diff