Files
nofx/config/config.go
nobody 0164ac6cc0 feat: Add Hyperliquid exchange support with unified trader interface
Major changes:
- Add full Hyperliquid trading support (long/short, leverage, SL/TP)
- Create unified Trader interface for multi-exchange support
- Implement automatic precision handling for orders and prices
- Fix balance calculation and unrealized P&L display
- Add comprehensive configuration guide in README
New features:
- Support for both Binance and Hyperliquid exchanges
- Automatic order size precision based on szDecimals
- Price formatting with 5 significant figures
- Non-custodial trading with Ethereum private key
- Seamless exchange switching via configuration
Technical details:
- Add trader/interface.go for unified trader interface
- Add trader/hyperliquid_trader.go for Hyperliquid implementation
- Update manager and auto_trader to support multiple exchanges
- Add go-hyperliquid SDK dependency
- Fix precision errors (float_to_wire, invalid price)
Fixes:
- Correct calculation of wallet balance and unrealized P&L
- Proper handling of AccountValue vs TotalRawUsd
- Frontend display issues for total equity and P&L
Documentation:
- Add Hyperliquid setup guide in README
- Update config.json.example with both exchanges
- Add troubleshooting section for common errors
Tested with live trading on Hyperliquid mainnet.
No breaking changes - backward compatible with existing configs.
2025-10-29 20:00:30 +08:00

139 lines
4.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package config
import (
"encoding/json"
"fmt"
"os"
"time"
)
// TraderConfig 单个trader的配置
type TraderConfig struct {
ID string `json:"id"`
Name string `json:"name"`
AIModel string `json:"ai_model"` // "qwen" or "deepseek"
// 交易平台选择(二选一)
Exchange string `json:"exchange"` // "binance" or "hyperliquid"
// 币安配置
BinanceAPIKey string `json:"binance_api_key,omitempty"`
BinanceSecretKey string `json:"binance_secret_key,omitempty"`
// Hyperliquid配置
HyperliquidPrivateKey string `json:"hyperliquid_private_key,omitempty"`
HyperliquidTestnet bool `json:"hyperliquid_testnet,omitempty"`
// AI配置
QwenKey string `json:"qwen_key,omitempty"`
DeepSeekKey string `json:"deepseek_key,omitempty"`
InitialBalance float64 `json:"initial_balance"`
ScanIntervalMinutes int `json:"scan_interval_minutes"`
}
// Config 总配置
type Config struct {
Traders []TraderConfig `json:"traders"`
UseDefaultCoins bool `json:"use_default_coins"` // 是否使用默认主流币种列表
CoinPoolAPIURL string `json:"coin_pool_api_url"`
OITopAPIURL string `json:"oi_top_api_url"`
APIServerPort int `json:"api_server_port"`
MaxDailyLoss float64 `json:"max_daily_loss"`
MaxDrawdown float64 `json:"max_drawdown"`
StopTradingMinutes int `json:"stop_trading_minutes"`
}
// LoadConfig 从文件加载配置
func LoadConfig(filename string) (*Config, error) {
data, err := os.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("读取配置文件失败: %w", err)
}
var config Config
if err := json.Unmarshal(data, &config); err != nil {
return nil, fmt.Errorf("解析配置文件失败: %w", err)
}
// 设置默认值如果use_default_coins未设置为false且没有配置coin_pool_api_url则默认使用默认币种列表
if !config.UseDefaultCoins && config.CoinPoolAPIURL == "" {
config.UseDefaultCoins = true
}
// 验证配置
if err := config.Validate(); err != nil {
return nil, fmt.Errorf("配置验证失败: %w", err)
}
return &config, nil
}
// Validate 验证配置有效性
func (c *Config) Validate() error {
if len(c.Traders) == 0 {
return fmt.Errorf("至少需要配置一个trader")
}
traderIDs := make(map[string]bool)
for i, trader := range c.Traders {
if trader.ID == "" {
return fmt.Errorf("trader[%d]: ID不能为空", i)
}
if traderIDs[trader.ID] {
return fmt.Errorf("trader[%d]: ID '%s' 重复", i, trader.ID)
}
traderIDs[trader.ID] = true
if trader.Name == "" {
return fmt.Errorf("trader[%d]: Name不能为空", i)
}
if trader.AIModel != "qwen" && trader.AIModel != "deepseek" {
return fmt.Errorf("trader[%d]: ai_model必须是 'qwen' 或 'deepseek'", i)
}
// 验证交易平台配置
if trader.Exchange == "" {
trader.Exchange = "binance" // 默认使用币安
}
if trader.Exchange != "binance" && trader.Exchange != "hyperliquid" {
return fmt.Errorf("trader[%d]: exchange必须是 'binance' 或 'hyperliquid'", i)
}
// 根据平台验证对应的密钥
if trader.Exchange == "binance" {
if trader.BinanceAPIKey == "" || trader.BinanceSecretKey == "" {
return fmt.Errorf("trader[%d]: 使用币安时必须配置binance_api_key和binance_secret_key", i)
}
} else if trader.Exchange == "hyperliquid" {
if trader.HyperliquidPrivateKey == "" {
return fmt.Errorf("trader[%d]: 使用Hyperliquid时必须配置hyperliquid_private_key", i)
}
}
if trader.AIModel == "qwen" && trader.QwenKey == "" {
return fmt.Errorf("trader[%d]: 使用Qwen时必须配置qwen_key", i)
}
if trader.AIModel == "deepseek" && trader.DeepSeekKey == "" {
return fmt.Errorf("trader[%d]: 使用DeepSeek时必须配置deepseek_key", i)
}
if trader.InitialBalance <= 0 {
return fmt.Errorf("trader[%d]: initial_balance必须大于0", i)
}
if trader.ScanIntervalMinutes <= 0 {
trader.ScanIntervalMinutes = 3 // 默认3分钟
}
}
if c.APIServerPort <= 0 {
c.APIServerPort = 8080 // 默认8080端口
}
return nil
}
// GetScanInterval 获取扫描间隔
func (tc *TraderConfig) GetScanInterval() time.Duration {
return time.Duration(tc.ScanIntervalMinutes) * time.Minute
}