mirror of
https://github.com/NoFxAiOS/nofx.git
synced 2026-07-02 18:41:01 +08:00
feat(config): 增加新闻源配置与数据库迁移支持
- 在config.json.example中添加新闻源相关配置示例,支持telegram频道订阅 - 扩展数据库表结构,新增traders表多字段支持信号源和杠杆参数 - 新增NewsConfig结构体及相关telegram新闻配置数据模型 - 在交易决策上下文结构Context中添加新闻数据news字段支持传递新闻 - 在交易决策构建用户提示信息时加入相关新闻内容 - 优化数据库操作代码,支持交易所和交易员配置的完整字段读取与更新 - 添加数据库exchanges表迁移逻辑,重建表结构和触发器以支持新字段 - 引入第三方库github.com/samber/lo用于集合操作 - 在go.mod添加新的依赖模块,并更新相关依赖版本
This commit is contained in:
@@ -19,5 +19,22 @@
|
||||
"max_daily_loss": 10.0,
|
||||
"max_drawdown": 20.0,
|
||||
"stop_trading_minutes": 60,
|
||||
"jwt_secret": "Qk0kAa+d0iIEzXVHXbNbm+UaN3RNabmWtH8rDWZ5OPf+4GX8pBflAHodfpbipVMyrw1fsDanHsNBjhgbDeK9Jg=="
|
||||
"jwt_secret": "Qk0kAa+d0iIEzXVHXbNbm+UaN3RNabmWtH8rDWZ5OPf+4GX8pBflAHodfpbipVMyrw1fsDanHsNBjhgbDeK9Jg==",
|
||||
// 建议使用时删除,目前新闻源功能还比较初级
|
||||
"news": [
|
||||
{
|
||||
"provider": "telegram",
|
||||
"telegram": {
|
||||
// 国外服务器无需配置
|
||||
"proxyurl": "http://127.0.0.1:18080"
|
||||
},
|
||||
"channels": [
|
||||
{
|
||||
// 如t.me/ChannelPANews,id为ChannelPANews
|
||||
"id": "ChannelPANews",
|
||||
"name": "PANews"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
type TraderConfig struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Enabled bool `json:"enabled"` // 是否启用该trader
|
||||
Enabled bool `json:"enabled"` // 是否启用该trader
|
||||
AIModel string `json:"ai_model"` // "qwen" or "deepseek"
|
||||
|
||||
// 交易平台选择(二选一)
|
||||
|
||||
@@ -177,17 +177,17 @@ func (d *Database) createTables() error {
|
||||
`ALTER TABLE exchanges ADD COLUMN aster_private_key TEXT DEFAULT ''`,
|
||||
`ALTER TABLE traders ADD COLUMN custom_prompt TEXT DEFAULT ''`,
|
||||
`ALTER TABLE traders ADD COLUMN override_base_prompt BOOLEAN DEFAULT 0`,
|
||||
`ALTER TABLE traders ADD COLUMN is_cross_margin BOOLEAN DEFAULT 1`, // 默认为全仓模式
|
||||
`ALTER TABLE traders ADD COLUMN use_default_coins BOOLEAN DEFAULT 1`, // 默认使用默认币种
|
||||
`ALTER TABLE traders ADD COLUMN custom_coins TEXT DEFAULT ''`, // 自定义币种列表(JSON格式)
|
||||
`ALTER TABLE traders ADD COLUMN btc_eth_leverage INTEGER DEFAULT 5`, // BTC/ETH杠杆倍数
|
||||
`ALTER TABLE traders ADD COLUMN altcoin_leverage INTEGER DEFAULT 5`, // 山寨币杠杆倍数
|
||||
`ALTER TABLE traders ADD COLUMN trading_symbols TEXT DEFAULT ''`, // 交易币种,逗号分隔
|
||||
`ALTER TABLE traders ADD COLUMN use_coin_pool BOOLEAN DEFAULT 0`, // 是否使用COIN POOL信号源
|
||||
`ALTER TABLE traders ADD COLUMN use_oi_top BOOLEAN DEFAULT 0`, // 是否使用OI TOP信号源
|
||||
`ALTER TABLE traders ADD COLUMN is_cross_margin BOOLEAN DEFAULT 1`, // 默认为全仓模式
|
||||
`ALTER TABLE traders ADD COLUMN use_default_coins BOOLEAN DEFAULT 1`, // 默认使用默认币种
|
||||
`ALTER TABLE traders ADD COLUMN custom_coins TEXT DEFAULT ''`, // 自定义币种列表(JSON格式)
|
||||
`ALTER TABLE traders ADD COLUMN btc_eth_leverage INTEGER DEFAULT 5`, // BTC/ETH杠杆倍数
|
||||
`ALTER TABLE traders ADD COLUMN altcoin_leverage INTEGER DEFAULT 5`, // 山寨币杠杆倍数
|
||||
`ALTER TABLE traders ADD COLUMN trading_symbols TEXT DEFAULT ''`, // 交易币种,逗号分隔
|
||||
`ALTER TABLE traders ADD COLUMN use_coin_pool BOOLEAN DEFAULT 0`, // 是否使用COIN POOL信号源
|
||||
`ALTER TABLE traders ADD COLUMN use_oi_top BOOLEAN DEFAULT 0`, // 是否使用OI TOP信号源
|
||||
`ALTER TABLE traders ADD COLUMN system_prompt_template TEXT DEFAULT 'default'`, // 系统提示词模板名称
|
||||
`ALTER TABLE ai_models ADD COLUMN custom_api_url TEXT DEFAULT ''`, // 自定义API地址
|
||||
`ALTER TABLE ai_models ADD COLUMN custom_model_name TEXT DEFAULT ''`, // 自定义模型名称
|
||||
`ALTER TABLE ai_models ADD COLUMN custom_api_url TEXT DEFAULT ''`, // 自定义API地址
|
||||
`ALTER TABLE ai_models ADD COLUMN custom_model_name TEXT DEFAULT ''`, // 自定义模型名称
|
||||
}
|
||||
|
||||
for _, query := range alterQueries {
|
||||
@@ -245,16 +245,16 @@ func (d *Database) initDefaultData() error {
|
||||
|
||||
// 初始化系统配置 - 创建所有字段,设置默认值,后续由config.json同步更新
|
||||
systemConfigs := map[string]string{
|
||||
"admin_mode": "true", // 默认开启管理员模式,便于首次使用
|
||||
"api_server_port": "8080", // 默认API端口
|
||||
"use_default_coins": "true", // 默认使用内置币种列表
|
||||
"default_coins": `["BTCUSDT","ETHUSDT","SOLUSDT","BNBUSDT","XRPUSDT","DOGEUSDT","ADAUSDT","HYPEUSDT"]`, // 默认币种列表(JSON格式)
|
||||
"max_daily_loss": "10.0", // 最大日损失百分比
|
||||
"max_drawdown": "20.0", // 最大回撤百分比
|
||||
"stop_trading_minutes": "60", // 停止交易时间(分钟)
|
||||
"btc_eth_leverage": "5", // BTC/ETH杠杆倍数
|
||||
"altcoin_leverage": "5", // 山寨币杠杆倍数
|
||||
"jwt_secret": "", // JWT密钥,默认为空,由config.json或系统生成
|
||||
"admin_mode": "true", // 默认开启管理员模式,便于首次使用
|
||||
"api_server_port": "8080", // 默认API端口
|
||||
"use_default_coins": "true", // 默认使用内置币种列表
|
||||
"default_coins": `["BTCUSDT","ETHUSDT","SOLUSDT","BNBUSDT","XRPUSDT","DOGEUSDT","ADAUSDT","HYPEUSDT"]`, // 默认币种列表(JSON格式)
|
||||
"max_daily_loss": "10.0", // 最大日损失百分比
|
||||
"max_drawdown": "20.0", // 最大回撤百分比
|
||||
"stop_trading_minutes": "60", // 停止交易时间(分钟)
|
||||
"btc_eth_leverage": "5", // BTC/ETH杠杆倍数
|
||||
"altcoin_leverage": "5", // 山寨币杠杆倍数
|
||||
"jwt_secret": "", // JWT密钥,默认为空,由config.json或系统生成
|
||||
}
|
||||
|
||||
for key, value := range systemConfigs {
|
||||
@@ -281,14 +281,14 @@ func (d *Database) migrateExchangesTable() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
// 如果已经迁移过,直接返回
|
||||
if count > 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
log.Printf("🔄 开始迁移exchanges表...")
|
||||
|
||||
|
||||
// 创建新的exchanges表,使用复合主键
|
||||
_, err = d.db.Exec(`
|
||||
CREATE TABLE exchanges_new (
|
||||
@@ -313,7 +313,7 @@ func (d *Database) migrateExchangesTable() error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("创建新exchanges表失败: %w", err)
|
||||
}
|
||||
|
||||
|
||||
// 复制数据到新表
|
||||
_, err = d.db.Exec(`
|
||||
INSERT INTO exchanges_new
|
||||
@@ -322,19 +322,19 @@ func (d *Database) migrateExchangesTable() error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("复制数据失败: %w", err)
|
||||
}
|
||||
|
||||
|
||||
// 删除旧表
|
||||
_, err = d.db.Exec(`DROP TABLE exchanges`)
|
||||
if err != nil {
|
||||
return fmt.Errorf("删除旧表失败: %w", err)
|
||||
}
|
||||
|
||||
|
||||
// 重命名新表
|
||||
_, err = d.db.Exec(`ALTER TABLE exchanges_new RENAME TO exchanges`)
|
||||
if err != nil {
|
||||
return fmt.Errorf("重命名表失败: %w", err)
|
||||
}
|
||||
|
||||
|
||||
// 重新创建触发器
|
||||
_, err = d.db.Exec(`
|
||||
CREATE TRIGGER IF NOT EXISTS update_exchanges_updated_at
|
||||
@@ -347,20 +347,20 @@ func (d *Database) migrateExchangesTable() error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("创建触发器失败: %w", err)
|
||||
}
|
||||
|
||||
|
||||
log.Printf("✅ exchanges表迁移完成")
|
||||
return nil
|
||||
}
|
||||
|
||||
// User 用户配置
|
||||
type User struct {
|
||||
ID string `json:"id"`
|
||||
Email string `json:"email"`
|
||||
PasswordHash string `json:"-"` // 不返回到前端
|
||||
OTPSecret string `json:"-"` // 不返回到前端
|
||||
OTPVerified bool `json:"otp_verified"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
ID string `json:"id"`
|
||||
Email string `json:"email"`
|
||||
PasswordHash string `json:"-"` // 不返回到前端
|
||||
OTPSecret string `json:"-"` // 不返回到前端
|
||||
OTPVerified bool `json:"otp_verified"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
// AIModelConfig AI模型配置
|
||||
@@ -379,36 +379,36 @@ type AIModelConfig struct {
|
||||
|
||||
// ExchangeConfig 交易所配置
|
||||
type ExchangeConfig struct {
|
||||
ID string `json:"id"`
|
||||
UserID string `json:"user_id"`
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
Enabled bool `json:"enabled"`
|
||||
APIKey string `json:"apiKey"`
|
||||
SecretKey string `json:"secretKey"`
|
||||
Testnet bool `json:"testnet"`
|
||||
ID string `json:"id"`
|
||||
UserID string `json:"user_id"`
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
Enabled bool `json:"enabled"`
|
||||
APIKey string `json:"apiKey"`
|
||||
SecretKey string `json:"secretKey"`
|
||||
Testnet bool `json:"testnet"`
|
||||
// Hyperliquid 特定字段
|
||||
HyperliquidWalletAddr string `json:"hyperliquidWalletAddr"`
|
||||
// Aster 特定字段
|
||||
AsterUser string `json:"asterUser"`
|
||||
AsterSigner string `json:"asterSigner"`
|
||||
AsterPrivateKey string `json:"asterPrivateKey"`
|
||||
AsterUser string `json:"asterUser"`
|
||||
AsterSigner string `json:"asterSigner"`
|
||||
AsterPrivateKey string `json:"asterPrivateKey"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
// TraderRecord 交易员配置(数据库实体)
|
||||
type TraderRecord struct {
|
||||
ID string `json:"id"`
|
||||
UserID string `json:"user_id"`
|
||||
Name string `json:"name"`
|
||||
AIModelID string `json:"ai_model_id"`
|
||||
ExchangeID string `json:"exchange_id"`
|
||||
InitialBalance float64 `json:"initial_balance"`
|
||||
ScanIntervalMinutes int `json:"scan_interval_minutes"`
|
||||
IsRunning bool `json:"is_running"`
|
||||
BTCETHLeverage int `json:"btc_eth_leverage"` // BTC/ETH杠杆倍数
|
||||
AltcoinLeverage int `json:"altcoin_leverage"` // 山寨币杠杆倍数
|
||||
ID string `json:"id"`
|
||||
UserID string `json:"user_id"`
|
||||
Name string `json:"name"`
|
||||
AIModelID string `json:"ai_model_id"`
|
||||
ExchangeID string `json:"exchange_id"`
|
||||
InitialBalance float64 `json:"initial_balance"`
|
||||
ScanIntervalMinutes int `json:"scan_interval_minutes"`
|
||||
IsRunning bool `json:"is_running"`
|
||||
BTCETHLeverage int `json:"btc_eth_leverage"` // BTC/ETH杠杆倍数
|
||||
AltcoinLeverage int `json:"altcoin_leverage"` // 山寨币杠杆倍数
|
||||
TradingSymbols string `json:"trading_symbols"` // 交易币种,逗号分隔
|
||||
UseCoinPool bool `json:"use_coin_pool"` // 是否使用COIN POOL信号源
|
||||
UseOITop bool `json:"use_oi_top"` // 是否使用OI TOP信号源
|
||||
@@ -422,12 +422,31 @@ type TraderRecord struct {
|
||||
|
||||
// UserSignalSource 用户信号源配置
|
||||
type UserSignalSource struct {
|
||||
ID int `json:"id"`
|
||||
UserID string `json:"user_id"`
|
||||
CoinPoolURL string `json:"coin_pool_url"`
|
||||
OITopURL string `json:"oi_top_url"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
ID int `json:"id"`
|
||||
UserID string `json:"user_id"`
|
||||
CoinPoolURL string `json:"coin_pool_url"`
|
||||
OITopURL string `json:"oi_top_url"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
// NewsConfig 新闻配置
|
||||
type NewsConfig struct {
|
||||
Provider string `json:"provider"` // 新闻搜索器名称: telegram
|
||||
Telegram NewsConfigTelegram `json:"telegram"` // telegram客户端配置
|
||||
TelegramChannel []NewsConfigTelegramChannel `json:"channels"` // telegram频道配置
|
||||
}
|
||||
|
||||
// NewsConfigTelegram telegram配置
|
||||
type NewsConfigTelegram struct {
|
||||
BaseURL string `json:"baseurl"` // 基础url
|
||||
ProxyURL string `json:"proxyurl"` // 代理url
|
||||
}
|
||||
|
||||
// NewsConfigTelegramChannel 电报频道配置
|
||||
type NewsConfigTelegramChannel struct {
|
||||
ID string `json:"id"` // 频道id
|
||||
Name string `json:"name"` // 频道名称
|
||||
}
|
||||
|
||||
// GenerateOTPSecret 生成OTP密钥
|
||||
@@ -457,12 +476,12 @@ func (d *Database) EnsureAdminUser() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
// 如果已存在,直接返回
|
||||
if count > 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
// 创建admin用户(密码为空,因为管理员模式下不需要密码)
|
||||
adminUser := &User{
|
||||
ID: "admin",
|
||||
@@ -471,7 +490,7 @@ func (d *Database) EnsureAdminUser() error {
|
||||
OTPSecret: "",
|
||||
OTPVerified: true,
|
||||
}
|
||||
|
||||
|
||||
return d.CreateUser(adminUser)
|
||||
}
|
||||
|
||||
@@ -482,7 +501,7 @@ func (d *Database) GetUserByEmail(email string) (*User, error) {
|
||||
SELECT id, email, password_hash, otp_secret, otp_verified, created_at, updated_at
|
||||
FROM users WHERE email = ?
|
||||
`, email).Scan(
|
||||
&user.ID, &user.Email, &user.PasswordHash, &user.OTPSecret,
|
||||
&user.ID, &user.Email, &user.PasswordHash, &user.OTPSecret,
|
||||
&user.OTPVerified, &user.CreatedAt, &user.UpdatedAt,
|
||||
)
|
||||
if err != nil {
|
||||
@@ -498,7 +517,7 @@ func (d *Database) GetUserByID(userID string) (*User, error) {
|
||||
SELECT id, email, password_hash, otp_secret, otp_verified, created_at, updated_at
|
||||
FROM users WHERE id = ?
|
||||
`, userID).Scan(
|
||||
&user.ID, &user.Email, &user.PasswordHash, &user.OTPSecret,
|
||||
&user.ID, &user.Email, &user.PasswordHash, &user.OTPSecret,
|
||||
&user.OTPVerified, &user.CreatedAt, &user.UpdatedAt,
|
||||
)
|
||||
if err != nil {
|
||||
@@ -668,7 +687,7 @@ func (d *Database) GetExchanges(userID string) ([]*ExchangeConfig, error) {
|
||||
err := rows.Scan(
|
||||
&exchange.ID, &exchange.UserID, &exchange.Name, &exchange.Type,
|
||||
&exchange.Enabled, &exchange.APIKey, &exchange.SecretKey, &exchange.Testnet,
|
||||
&exchange.HyperliquidWalletAddr, &exchange.AsterUser,
|
||||
&exchange.HyperliquidWalletAddr, &exchange.AsterUser,
|
||||
&exchange.AsterSigner, &exchange.AsterPrivateKey,
|
||||
&exchange.CreatedAt, &exchange.UpdatedAt,
|
||||
)
|
||||
@@ -684,7 +703,7 @@ func (d *Database) GetExchanges(userID string) ([]*ExchangeConfig, error) {
|
||||
// UpdateExchange 更新交易所配置,如果不存在则创建用户特定配置
|
||||
func (d *Database) UpdateExchange(userID, id string, enabled bool, apiKey, secretKey string, testnet bool, hyperliquidWalletAddr, asterUser, asterSigner, asterPrivateKey string) error {
|
||||
log.Printf("🔧 UpdateExchange: userID=%s, id=%s, enabled=%v", userID, id, enabled)
|
||||
|
||||
|
||||
// 首先尝试更新现有的用户配置
|
||||
result, err := d.db.Exec(`
|
||||
UPDATE exchanges SET enabled = ?, api_key = ?, secret_key = ?, testnet = ?,
|
||||
@@ -695,20 +714,20 @@ func (d *Database) UpdateExchange(userID, id string, enabled bool, apiKey, secre
|
||||
log.Printf("❌ UpdateExchange: 更新失败: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
// 检查是否有行被更新
|
||||
rowsAffected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
log.Printf("❌ UpdateExchange: 获取影响行数失败: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
log.Printf("📊 UpdateExchange: 影响行数 = %d", rowsAffected)
|
||||
|
||||
|
||||
// 如果没有行被更新,说明用户没有这个交易所的配置,需要创建
|
||||
if rowsAffected == 0 {
|
||||
log.Printf("💡 UpdateExchange: 没有现有记录,创建新记录")
|
||||
|
||||
|
||||
// 根据交易所ID确定基本信息
|
||||
var name, typ string
|
||||
if id == "binance" {
|
||||
@@ -724,16 +743,16 @@ func (d *Database) UpdateExchange(userID, id string, enabled bool, apiKey, secre
|
||||
name = id + " Exchange"
|
||||
typ = "cex"
|
||||
}
|
||||
|
||||
|
||||
log.Printf("🆕 UpdateExchange: 创建新记录 ID=%s, name=%s, type=%s", id, name, typ)
|
||||
|
||||
|
||||
// 创建用户特定的配置,使用原始的交易所ID
|
||||
_, err = d.db.Exec(`
|
||||
INSERT INTO exchanges (id, user_id, name, type, enabled, api_key, secret_key, testnet,
|
||||
hyperliquid_wallet_addr, aster_user, aster_signer, aster_private_key, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, datetime('now'), datetime('now'))
|
||||
`, id, userID, name, typ, enabled, apiKey, secretKey, testnet, hyperliquidWalletAddr, asterUser, asterSigner, asterPrivateKey)
|
||||
|
||||
|
||||
if err != nil {
|
||||
log.Printf("❌ UpdateExchange: 创建记录失败: %v", err)
|
||||
} else {
|
||||
@@ -741,7 +760,7 @@ func (d *Database) UpdateExchange(userID, id string, enabled bool, apiKey, secre
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
log.Printf("✅ UpdateExchange: 更新现有记录成功")
|
||||
return nil
|
||||
}
|
||||
@@ -790,9 +809,9 @@ func (d *Database) GetTraders(userID string) ([]*TraderRecord, error) {
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var traders []*TraderRecord
|
||||
var traders []*TraderRecord
|
||||
for rows.Next() {
|
||||
var trader TraderRecord
|
||||
var trader TraderRecord
|
||||
err := rows.Scan(
|
||||
&trader.ID, &trader.UserID, &trader.Name, &trader.AIModelID, &trader.ExchangeID,
|
||||
&trader.InitialBalance, &trader.ScanIntervalMinutes, &trader.IsRunning,
|
||||
@@ -847,7 +866,7 @@ func (d *Database) DeleteTrader(userID, id string) error {
|
||||
|
||||
// GetTraderConfig 获取交易员完整配置(包含AI模型和交易所信息)
|
||||
func (d *Database) GetTraderConfig(userID, traderID string) (*TraderRecord, *AIModelConfig, *ExchangeConfig, error) {
|
||||
var trader TraderRecord
|
||||
var trader TraderRecord
|
||||
var aiModel AIModelConfig
|
||||
var exchange ExchangeConfig
|
||||
|
||||
@@ -873,7 +892,7 @@ func (d *Database) GetTraderConfig(userID, traderID string) (*TraderRecord, *AIM
|
||||
`, traderID, userID).Scan(
|
||||
&trader.ID, &trader.UserID, &trader.Name, &trader.AIModelID, &trader.ExchangeID,
|
||||
&trader.InitialBalance, &trader.ScanIntervalMinutes, &trader.IsRunning,
|
||||
&trader.BTCETHLeverage, &trader.AltcoinLeverage, &trader.TradingSymbols, &trader.UseCoinPool,
|
||||
&trader.BTCETHLeverage, &trader.AltcoinLeverage, &trader.TradingSymbols, &trader.UseCoinPool,
|
||||
&trader.UseOITop, &trader.CustomPrompt, &trader.OverrideBasePrompt, &trader.IsCrossMargin,
|
||||
&trader.CreatedAt, &trader.UpdatedAt,
|
||||
&aiModel.ID, &aiModel.UserID, &aiModel.Name, &aiModel.Provider, &aiModel.Enabled, &aiModel.APIKey,
|
||||
@@ -943,4 +962,4 @@ func (d *Database) UpdateUserSignalSource(userID, coinPoolURL, oiTopURL string)
|
||||
// Close 关闭数据库连接
|
||||
func (d *Database) Close() error {
|
||||
return d.db.Close()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,9 +6,12 @@ import (
|
||||
"log"
|
||||
"nofx/market"
|
||||
"nofx/mcp"
|
||||
"nofx/news"
|
||||
"nofx/pool"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
// PositionInfo 持仓信息
|
||||
@@ -55,17 +58,18 @@ type OITopData struct {
|
||||
|
||||
// Context 交易上下文(传递给AI的完整信息)
|
||||
type Context struct {
|
||||
CurrentTime string `json:"current_time"`
|
||||
RuntimeMinutes int `json:"runtime_minutes"`
|
||||
CallCount int `json:"call_count"`
|
||||
Account AccountInfo `json:"account"`
|
||||
Positions []PositionInfo `json:"positions"`
|
||||
CandidateCoins []CandidateCoin `json:"candidate_coins"`
|
||||
MarketDataMap map[string]*market.Data `json:"-"` // 不序列化,但内部使用
|
||||
OITopDataMap map[string]*OITopData `json:"-"` // OI Top数据映射
|
||||
Performance interface{} `json:"-"` // 历史表现分析(logger.PerformanceAnalysis)
|
||||
BTCETHLeverage int `json:"-"` // BTC/ETH杠杆倍数(从配置读取)
|
||||
AltcoinLeverage int `json:"-"` // 山寨币杠杆倍数(从配置读取)
|
||||
CurrentTime string `json:"current_time"`
|
||||
RuntimeMinutes int `json:"runtime_minutes"`
|
||||
CallCount int `json:"call_count"`
|
||||
Account AccountInfo `json:"account"`
|
||||
Positions []PositionInfo `json:"positions"`
|
||||
CandidateCoins []CandidateCoin `json:"candidate_coins"`
|
||||
MarketDataMap map[string]*market.Data `json:"-"` // 不序列化,但内部使用
|
||||
OITopDataMap map[string]*OITopData `json:"-"` // OI Top数据映射
|
||||
Performance interface{} `json:"-"` // 历史表现分析(logger.PerformanceAnalysis)
|
||||
BTCETHLeverage int `json:"-"` // BTC/ETH杠杆倍数(从配置读取)
|
||||
AltcoinLeverage int `json:"-"` // 山寨币杠杆倍数(从配置读取)
|
||||
News map[string][]news.NewsItem `json:"news,omitempty"` // 新闻数据(可选)按symbol分组传给AI
|
||||
}
|
||||
|
||||
// Decision AI的交易决策
|
||||
@@ -380,6 +384,18 @@ func buildUserPrompt(ctx *Context) string {
|
||||
}
|
||||
}
|
||||
|
||||
// 新闻内容
|
||||
newsItem := make([]news.NewsItem, 0, 100)
|
||||
for _, symbol := range lo.Keys(ctx.News) {
|
||||
newsItem = append(newsItem, ctx.News[symbol]...)
|
||||
}
|
||||
if len(newsItem) > 0 {
|
||||
sb.WriteString("\n## 相关新闻\n")
|
||||
for _, item := range newsItem {
|
||||
sb.WriteString(item.String())
|
||||
}
|
||||
}
|
||||
|
||||
sb.WriteString("---\n\n")
|
||||
sb.WriteString("现在请分析并输出决策(思维链 + JSON)\n")
|
||||
|
||||
|
||||
3
go.mod
3
go.mod
@@ -3,6 +3,7 @@ module nofx
|
||||
go 1.25.0
|
||||
|
||||
require (
|
||||
github.com/PuerkitoBio/goquery v1.10.3
|
||||
github.com/adshao/go-binance/v2 v2.8.7
|
||||
github.com/ethereum/go-ethereum v1.16.5
|
||||
github.com/gin-gonic/gin v1.11.0
|
||||
@@ -10,11 +11,13 @@ require (
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/mattn/go-sqlite3 v1.14.32
|
||||
github.com/pquerna/otp v1.4.0
|
||||
github.com/samber/lo v1.52.0
|
||||
github.com/sonirico/go-hyperliquid v0.17.0
|
||||
golang.org/x/crypto v0.42.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/andybalholm/cascadia v1.3.3 // indirect
|
||||
github.com/armon/go-radix v1.0.0 // indirect
|
||||
github.com/bitly/go-simplejson v0.5.0 // indirect
|
||||
github.com/bits-and-blooms/bitset v1.24.0 // indirect
|
||||
|
||||
70
go.sum
70
go.sum
@@ -1,7 +1,11 @@
|
||||
github.com/PuerkitoBio/goquery v1.10.3 h1:pFYcNSqHxBD06Fpj/KsbStFRsgRATgnf3LeXiUkhzPo=
|
||||
github.com/PuerkitoBio/goquery v1.10.3/go.mod h1:tMUX0zDMHXYlAQk6p35XxQMqMweEKB7iK7iLNd4RH4Y=
|
||||
github.com/StackExchange/wmi v1.2.1 h1:VIkavFPXSjcnS+O8yTq7NI32k0R5Aj+v39y29VYDOSA=
|
||||
github.com/StackExchange/wmi v1.2.1/go.mod h1:rcmrprowKIVzvc+NUiLncP2uuArMWLCbu9SBzvHz7e8=
|
||||
github.com/adshao/go-binance/v2 v2.8.7 h1:n7jkhwIHMdtd/9ZU2gTqFV15XVSbUCjyFlOUAtTd8uU=
|
||||
github.com/adshao/go-binance/v2 v2.8.7/go.mod h1:XkkuecSyJKPolaCGf/q4ovJYB3t0P+7RUYTbGr+LMGM=
|
||||
github.com/andybalholm/cascadia v1.3.3 h1:AG2YHrzJIm4BZ19iwJ/DAua6Btl3IwJX+VI4kktS1LM=
|
||||
github.com/andybalholm/cascadia v1.3.3/go.mod h1:xNd9bqTn98Ln4DwST8/nG+H0yuB8Hmgu1YHNnWw0GeA=
|
||||
github.com/armon/go-radix v1.0.0 h1:F4z6KzEeeQIMeLFa97iZU6vupzoecKdU5TX24SNppXI=
|
||||
github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
|
||||
github.com/bitly/go-simplejson v0.5.0 h1:6IH+V8/tVMab511d5bn4M7EwGXZf9Hj6i2xSwkNEM+Y=
|
||||
@@ -75,6 +79,7 @@ github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
|
||||
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
|
||||
github.com/golang/snappy v1.0.0 h1:Oy607GVXHs7RtbggtPBnr2RmDArIsAefDwvrdWvRhGs=
|
||||
github.com/golang/snappy v1.0.0/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
|
||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
||||
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
@@ -151,6 +156,8 @@ github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7
|
||||
github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0=
|
||||
github.com/rs/zerolog v1.34.0 h1:k43nTLIwcTVQAncfCw4KZ2VY6ukYoZaBPNOE8txlOeY=
|
||||
github.com/rs/zerolog v1.34.0/go.mod h1:bJsvje4Z08ROH4Nhs5iH600c3IkWhwp44iRc54W6wYQ=
|
||||
github.com/samber/lo v1.52.0 h1:Rvi+3BFHES3A8meP33VPAxiBZX/Aws5RxrschYGjomw=
|
||||
github.com/samber/lo v1.52.0/go.mod h1:4+MXEGsJzbKGaUEQFKBq2xtfuznW9oz/WrgyzMzRoM0=
|
||||
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible h1:Bn1aCHHRnjv4Bl16T8rcaFjYSrGrIZvpiGO6P3Q4GpU=
|
||||
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
|
||||
github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k=
|
||||
@@ -188,6 +195,7 @@ github.com/vmihailenco/msgpack/v5 v5.4.1 h1:cQriyiUvjTwOHg8QZaPihLWeRAAVoCpE00IU
|
||||
github.com/vmihailenco/msgpack/v5 v5.4.1/go.mod h1:GaZTsDaehaPpQVyxrf5mtQlH+pc21PIudVV/E3rRQok=
|
||||
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
|
||||
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
go.elastic.co/apm/module/apmzerolog/v2 v2.7.1 h1:C9+KrlqS8F4SZFu+ct0Jmv2YLmzDhWsI8htK6exd3vg=
|
||||
go.elastic.co/apm/module/apmzerolog/v2 v2.7.1/go.mod h1:wXViB7paxMUrERgZrmUb+0FCqgb13Dull1JOOd8Hcj0=
|
||||
go.elastic.co/apm/v2 v2.7.1 h1:OFjARuESjBsxw7wHrEAnfSVNCHGBATXSI/kPvBARY/A=
|
||||
@@ -198,23 +206,85 @@ go.uber.org/mock v0.5.0 h1:KAMbZvZPyBPWgD14IrIQ38QCyjwpvVVV6K/bHl1IwQU=
|
||||
go.uber.org/mock v0.5.0/go.mod h1:ge71pBPLYDk7QIi1LupWxdAykm7KIEFchiOqd6z7qMM=
|
||||
golang.org/x/arch v0.20.0 h1:dx1zTU0MAE98U+TQ8BLl7XsJbgze2WnNKF/8tGp/Q6c=
|
||||
golang.org/x/arch v0.20.0/go.mod h1:bdwinDaKcfZUGpH09BB7ZmOfhalA8lQdzl62l8gGWsk=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc=
|
||||
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
|
||||
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
|
||||
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
|
||||
golang.org/x/crypto v0.42.0 h1:chiH31gIWm57EkTXpwnqf8qeuMUi0yekh6mT2AvFlqI=
|
||||
golang.org/x/crypto v0.42.0/go.mod h1:4+rDnOTJhQCx2q7/j6rAN5XDw8kPjeaXEUR2eL94ix8=
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||
golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
|
||||
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
|
||||
golang.org/x/mod v0.27.0 h1:kb+q2PyFnEADO2IEF935ehFUXlWiNjJWtRNgBLSfbxQ=
|
||||
golang.org/x/mod v0.27.0/go.mod h1:rWI627Fq0DEoudcK+MBkNkCe0EetEaDSwJJkCcjpazc=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
|
||||
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
|
||||
golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk=
|
||||
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
|
||||
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
|
||||
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
|
||||
golang.org/x/net v0.43.0 h1:lat02VYK2j4aLzMzecihNvTlJNQUq316m2Mr9rnM6YE=
|
||||
golang.org/x/net v0.43.0/go.mod h1:vhO1fvI4dGsIjh73sWfUVjj3N7CA9WkKJNQm2svM6Jg=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
|
||||
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sync v0.17.0 h1:l60nONMj9l5drqw6jlhIELNv9I0A4OFgRsG9k2oT9Ug=
|
||||
golang.org/x/sync v0.17.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/sys v0.36.0 h1:KVRy2GtZBrk1cBYA7MKu5bEZFxQk4NIDV6RLVcC8o0k=
|
||||
golang.org/x/sys v0.36.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
||||
golang.org/x/telemetry v0.0.0-20240228155512-f48c80bd79b2/go.mod h1:TeRTkGYfJXctD9OcfyVLyj2J3IxLnKwHJR8f4D8a3YE=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
|
||||
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
|
||||
golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU=
|
||||
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
|
||||
golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY=
|
||||
golang.org/x/term v0.27.0/go.mod h1:iMsnZpn0cago0GOrHO2+Y7u7JPn5AylBrcoWkElMTSM=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
|
||||
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
|
||||
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
|
||||
golang.org/x/text v0.29.0 h1:1neNs90w9YzJ9BocxfsQNHKuAT4pkghyXc4nhZ6sJvk=
|
||||
golang.org/x/text v0.29.0/go.mod h1:7MhJOA9CD2qZyOKYazxdYMF85OwPdEr9jTtBpO7ydH4=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
|
||||
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
|
||||
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
|
||||
golang.org/x/tools v0.36.0 h1:kWS0uv/zsvHEle1LbV5LE8QujrxB3wfQyxHfhOk0Qkg=
|
||||
golang.org/x/tools v0.36.0/go.mod h1:WBDiHKJK8YgLHlcQPYQzNCkUxUypCaa5ZegCVutKm+s=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
google.golang.org/protobuf v1.36.9 h1:w2gp2mA27hUeUzj9Ex9FBjsBm40zfaDtEWow293U7Iw=
|
||||
google.golang.org/protobuf v1.36.9/go.mod h1:fuxRtAxBytpl4zzqUh6/eyUujkJdNiuEkXntxiD/uRU=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
|
||||
61
main.go
61
main.go
@@ -24,17 +24,18 @@ type LeverageConfig struct {
|
||||
|
||||
// ConfigFile 配置文件结构,只包含需要同步到数据库的字段
|
||||
type ConfigFile struct {
|
||||
AdminMode bool `json:"admin_mode"`
|
||||
APIServerPort int `json:"api_server_port"`
|
||||
UseDefaultCoins bool `json:"use_default_coins"`
|
||||
DefaultCoins []string `json:"default_coins"`
|
||||
CoinPoolAPIURL string `json:"coin_pool_api_url"`
|
||||
OITopAPIURL string `json:"oi_top_api_url"`
|
||||
MaxDailyLoss float64 `json:"max_daily_loss"`
|
||||
MaxDrawdown float64 `json:"max_drawdown"`
|
||||
StopTradingMinutes int `json:"stop_trading_minutes"`
|
||||
Leverage LeverageConfig `json:"leverage"`
|
||||
JWTSecret string `json:"jwt_secret"`
|
||||
AdminMode bool `json:"admin_mode"`
|
||||
APIServerPort int `json:"api_server_port"`
|
||||
UseDefaultCoins bool `json:"use_default_coins"`
|
||||
DefaultCoins []string `json:"default_coins"`
|
||||
CoinPoolAPIURL string `json:"coin_pool_api_url"`
|
||||
OITopAPIURL string `json:"oi_top_api_url"`
|
||||
MaxDailyLoss float64 `json:"max_daily_loss"`
|
||||
MaxDrawdown float64 `json:"max_drawdown"`
|
||||
StopTradingMinutes int `json:"stop_trading_minutes"`
|
||||
Leverage LeverageConfig `json:"leverage"`
|
||||
JWTSecret string `json:"jwt_secret"`
|
||||
News []config.NewsConfig `json:"news"`
|
||||
}
|
||||
|
||||
// syncConfigToDatabase 从config.json读取配置并同步到数据库
|
||||
@@ -61,14 +62,14 @@ func syncConfigToDatabase(database *config.Database) error {
|
||||
|
||||
// 同步各配置项到数据库
|
||||
configs := map[string]string{
|
||||
"admin_mode": fmt.Sprintf("%t", configFile.AdminMode),
|
||||
"api_server_port": strconv.Itoa(configFile.APIServerPort),
|
||||
"use_default_coins": fmt.Sprintf("%t", configFile.UseDefaultCoins),
|
||||
"coin_pool_api_url": configFile.CoinPoolAPIURL,
|
||||
"oi_top_api_url": configFile.OITopAPIURL,
|
||||
"max_daily_loss": fmt.Sprintf("%.1f", configFile.MaxDailyLoss),
|
||||
"max_drawdown": fmt.Sprintf("%.1f", configFile.MaxDrawdown),
|
||||
"stop_trading_minutes": strconv.Itoa(configFile.StopTradingMinutes),
|
||||
"admin_mode": fmt.Sprintf("%t", configFile.AdminMode),
|
||||
"api_server_port": strconv.Itoa(configFile.APIServerPort),
|
||||
"use_default_coins": fmt.Sprintf("%t", configFile.UseDefaultCoins),
|
||||
"coin_pool_api_url": configFile.CoinPoolAPIURL,
|
||||
"oi_top_api_url": configFile.OITopAPIURL,
|
||||
"max_daily_loss": fmt.Sprintf("%.1f", configFile.MaxDailyLoss),
|
||||
"max_drawdown": fmt.Sprintf("%.1f", configFile.MaxDrawdown),
|
||||
"stop_trading_minutes": strconv.Itoa(configFile.StopTradingMinutes),
|
||||
}
|
||||
|
||||
// 同步default_coins(转换为JSON字符串存储)
|
||||
@@ -92,6 +93,14 @@ func syncConfigToDatabase(database *config.Database) error {
|
||||
configs["jwt_secret"] = configFile.JWTSecret
|
||||
}
|
||||
|
||||
// 新闻配置
|
||||
if len(configFile.News) != 0 {
|
||||
newsJSON, err := json.Marshal(configFile.News)
|
||||
if err == nil {
|
||||
configs["news_config"] = string(newsJSON)
|
||||
}
|
||||
}
|
||||
|
||||
// 更新数据库配置
|
||||
for key, value := range configs {
|
||||
if err := database.SetSystemConfig(key, value); err != nil {
|
||||
@@ -133,11 +142,11 @@ func main() {
|
||||
useDefaultCoinsStr, _ := database.GetSystemConfig("use_default_coins")
|
||||
useDefaultCoins := useDefaultCoinsStr == "true"
|
||||
apiPortStr, _ := database.GetSystemConfig("api_server_port")
|
||||
|
||||
|
||||
// 获取管理员模式配置
|
||||
adminModeStr, _ := database.GetSystemConfig("admin_mode")
|
||||
adminMode := adminModeStr != "false" // 默认为true
|
||||
|
||||
|
||||
// 设置JWT密钥
|
||||
jwtSecret, _ := database.GetSystemConfig("jwt_secret")
|
||||
if jwtSecret == "" {
|
||||
@@ -145,7 +154,7 @@ func main() {
|
||||
log.Printf("⚠️ 使用默认JWT密钥,建议在生产环境中配置")
|
||||
}
|
||||
auth.SetJWTSecret(jwtSecret)
|
||||
|
||||
|
||||
// 在管理员模式下,确保admin用户存在
|
||||
if adminMode {
|
||||
err := database.EnsureAdminUser()
|
||||
@@ -156,7 +165,7 @@ func main() {
|
||||
}
|
||||
auth.SetAdminMode(true)
|
||||
}
|
||||
|
||||
|
||||
log.Printf("✓ 配置数据库初始化成功")
|
||||
fmt.Println()
|
||||
|
||||
@@ -192,7 +201,7 @@ func main() {
|
||||
pool.SetCoinPoolAPI(coinPoolAPIURL)
|
||||
log.Printf("✓ 已配置AI500币种池API")
|
||||
}
|
||||
|
||||
|
||||
oiTopAPIURL, _ := database.GetSystemConfig("oi_top_api_url")
|
||||
if oiTopAPIURL != "" {
|
||||
pool.SetOITopAPI(oiTopAPIURL)
|
||||
@@ -237,7 +246,7 @@ func main() {
|
||||
status = "运行中"
|
||||
}
|
||||
fmt.Printf(" • %s (%s + %s) - 用户: %s - 初始资金: %.0f USDT [%s]\n",
|
||||
trader.Name, strings.ToUpper(trader.AIModelID), strings.ToUpper(trader.ExchangeID),
|
||||
trader.Name, strings.ToUpper(trader.AIModelID), strings.ToUpper(trader.ExchangeID),
|
||||
trader.UserID, trader.InitialBalance, status)
|
||||
}
|
||||
}
|
||||
@@ -256,7 +265,7 @@ func main() {
|
||||
fmt.Println()
|
||||
|
||||
// 获取API服务器端口
|
||||
apiPort := 8080 // 默认端口
|
||||
apiPort := 8080 // 默认端口
|
||||
if apiPortStr != "" {
|
||||
if port, err := strconv.Atoi(apiPortStr); err == nil {
|
||||
apiPort = port
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"nofx/config"
|
||||
"nofx/news"
|
||||
"nofx/trader"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -83,8 +84,31 @@ func (tm *TraderManager) LoadTradersFromDatabase(database *config.Database) erro
|
||||
}
|
||||
}
|
||||
|
||||
// 获取新闻源配置
|
||||
var newsCfg []config.NewsConfig
|
||||
newsConfStr, _ := database.GetSystemConfig("news_config")
|
||||
if newsConfStr != "" {
|
||||
if err := json.Unmarshal([]byte(newsConfStr), &newsCfg); err != nil {
|
||||
log.Printf("⚠️ 解析新闻源配置失败: %v,使用空列表", err)
|
||||
newsCfg = []config.NewsConfig{}
|
||||
}
|
||||
|
||||
// 新闻配置处理
|
||||
for index, newsConf := range newsCfg {
|
||||
switch newsConf.Provider {
|
||||
case news.ProviderTelegram:
|
||||
if newsConf.Telegram.BaseURL == "" {
|
||||
newsCfg[index].Telegram.BaseURL = "https://t.me/s"
|
||||
}
|
||||
if len(newsConf.TelegramChannel) == 0 {
|
||||
newsCfg[index].TelegramChannel = append(newsCfg[index].TelegramChannel, config.NewsConfigTelegramChannel{ID: "ChannelPANews", Name: "PANews"})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 为每个交易员获取AI模型和交易所配置
|
||||
for _, traderCfg := range allTraders {
|
||||
for _, traderCfg := range allTraders {
|
||||
// 获取AI模型配置(使用交易员所属的用户ID)
|
||||
aiModels, err := database.GetAIModels(traderCfg.UserID)
|
||||
if err != nil {
|
||||
@@ -157,7 +181,7 @@ func (tm *TraderManager) LoadTradersFromDatabase(database *config.Database) erro
|
||||
}
|
||||
|
||||
// 添加到TraderManager
|
||||
err = tm.addTraderFromDB(traderCfg, aiModelCfg, exchangeCfg, coinPoolURL, oiTopURL, maxDailyLoss, maxDrawdown, stopTradingMinutes, defaultCoins)
|
||||
err = tm.addTraderFromDB(traderCfg, aiModelCfg, exchangeCfg, newsCfg, coinPoolURL, oiTopURL, maxDailyLoss, maxDrawdown, stopTradingMinutes, defaultCoins)
|
||||
if err != nil {
|
||||
log.Printf("❌ 添加交易员 %s 失败: %v", traderCfg.Name, err)
|
||||
continue
|
||||
@@ -169,7 +193,7 @@ func (tm *TraderManager) LoadTradersFromDatabase(database *config.Database) erro
|
||||
}
|
||||
|
||||
// addTraderFromConfig 内部方法:从配置添加交易员(不加锁,因为调用方已加锁)
|
||||
func (tm *TraderManager) addTraderFromDB(traderCfg *config.TraderRecord, aiModelCfg *config.AIModelConfig, exchangeCfg *config.ExchangeConfig, coinPoolURL, oiTopURL string, maxDailyLoss, maxDrawdown float64, stopTradingMinutes int, defaultCoins []string) error {
|
||||
func (tm *TraderManager) addTraderFromDB(traderCfg *config.TraderRecord, aiModelCfg *config.AIModelConfig, exchangeCfg *config.ExchangeConfig, newsCfg []config.NewsConfig, coinPoolURL, oiTopURL string, maxDailyLoss, maxDrawdown float64, stopTradingMinutes int, defaultCoins []string) error {
|
||||
if _, exists := tm.traders[traderCfg.ID]; exists {
|
||||
return fmt.Errorf("trader ID '%s' 已存在", traderCfg.ID)
|
||||
}
|
||||
@@ -186,7 +210,7 @@ func (tm *TraderManager) addTraderFromDB(traderCfg *config.TraderRecord, aiModel
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 如果没有指定交易币种,使用默认币种
|
||||
if len(tradingCoins) == 0 {
|
||||
tradingCoins = defaultCoins
|
||||
@@ -200,7 +224,7 @@ func (tm *TraderManager) addTraderFromDB(traderCfg *config.TraderRecord, aiModel
|
||||
}
|
||||
|
||||
// 构建AutoTraderConfig
|
||||
traderConfig := trader.AutoTraderConfig{
|
||||
traderConfig := trader.AutoTraderConfig{
|
||||
ID: traderCfg.ID,
|
||||
Name: traderCfg.Name,
|
||||
AIModel: aiModelCfg.Provider, // 使用provider作为模型标识
|
||||
@@ -226,6 +250,7 @@ func (tm *TraderManager) addTraderFromDB(traderCfg *config.TraderRecord, aiModel
|
||||
DefaultCoins: defaultCoins,
|
||||
TradingCoins: tradingCoins,
|
||||
SystemPromptTemplate: traderCfg.SystemPromptTemplate, // 系统提示词模板
|
||||
NewsConfig: newsCfg, // 新闻源配置
|
||||
}
|
||||
|
||||
// 根据交易所类型设置API密钥
|
||||
@@ -253,7 +278,7 @@ func (tm *TraderManager) addTraderFromDB(traderCfg *config.TraderRecord, aiModel
|
||||
if err != nil {
|
||||
return fmt.Errorf("创建trader失败: %w", err)
|
||||
}
|
||||
|
||||
|
||||
// 设置自定义prompt(如果有)
|
||||
if traderCfg.CustomPrompt != "" {
|
||||
at.SetCustomPrompt(traderCfg.CustomPrompt)
|
||||
@@ -293,7 +318,7 @@ func (tm *TraderManager) AddTraderFromDB(traderCfg *config.TraderRecord, aiModel
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 如果没有指定交易币种,使用默认币种
|
||||
if len(tradingCoins) == 0 {
|
||||
tradingCoins = defaultCoins
|
||||
@@ -359,7 +384,7 @@ func (tm *TraderManager) AddTraderFromDB(traderCfg *config.TraderRecord, aiModel
|
||||
if err != nil {
|
||||
return fmt.Errorf("创建trader失败: %w", err)
|
||||
}
|
||||
|
||||
|
||||
// 设置自定义prompt(如果有)
|
||||
if traderCfg.CustomPrompt != "" {
|
||||
at.SetCustomPrompt(traderCfg.CustomPrompt)
|
||||
@@ -488,9 +513,9 @@ func (tm *TraderManager) GetCompetitionData() (map[string]interface{}, error) {
|
||||
for _, t := range tm.traders {
|
||||
account, err := t.GetAccountInfo()
|
||||
status := t.GetStatus()
|
||||
|
||||
|
||||
var traderData map[string]interface{}
|
||||
|
||||
|
||||
if err != nil {
|
||||
// 如果获取账户信息失败,使用默认值但仍然显示交易员
|
||||
log.Printf("⚠️ 获取交易员 %s 账户信息失败: %v", t.GetID(), err)
|
||||
@@ -522,7 +547,7 @@ func (tm *TraderManager) GetCompetitionData() (map[string]interface{}, error) {
|
||||
"is_running": status["is_running"],
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
traders = append(traders, traderData)
|
||||
}
|
||||
comparison["traders"] = traders
|
||||
@@ -708,7 +733,7 @@ func (tm *TraderManager) loadSingleTrader(traderCfg *config.TraderRecord, aiMode
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 如果没有指定交易币种,使用默认币种
|
||||
if len(tradingCoins) == 0 {
|
||||
tradingCoins = defaultCoins
|
||||
@@ -723,25 +748,25 @@ func (tm *TraderManager) loadSingleTrader(traderCfg *config.TraderRecord, aiMode
|
||||
|
||||
// 构建AutoTraderConfig
|
||||
traderConfig := trader.AutoTraderConfig{
|
||||
ID: traderCfg.ID,
|
||||
Name: traderCfg.Name,
|
||||
AIModel: aiModelCfg.Provider, // 使用provider作为模型标识
|
||||
Exchange: exchangeCfg.ID, // 使用exchange ID
|
||||
InitialBalance: traderCfg.InitialBalance,
|
||||
BTCETHLeverage: traderCfg.BTCETHLeverage,
|
||||
AltcoinLeverage: traderCfg.AltcoinLeverage,
|
||||
ScanInterval: time.Duration(traderCfg.ScanIntervalMinutes) * time.Minute,
|
||||
CoinPoolAPIURL: effectiveCoinPoolURL,
|
||||
CustomAPIURL: aiModelCfg.CustomAPIURL, // 自定义API URL
|
||||
CustomModelName: aiModelCfg.CustomModelName, // 自定义模型名称
|
||||
UseQwen: aiModelCfg.Provider == "qwen",
|
||||
MaxDailyLoss: maxDailyLoss,
|
||||
MaxDrawdown: maxDrawdown,
|
||||
StopTradingTime: time.Duration(stopTradingMinutes) * time.Minute,
|
||||
IsCrossMargin: traderCfg.IsCrossMargin,
|
||||
DefaultCoins: defaultCoins,
|
||||
TradingCoins: tradingCoins,
|
||||
SystemPromptTemplate: traderCfg.SystemPromptTemplate, // 系统提示词模板
|
||||
ID: traderCfg.ID,
|
||||
Name: traderCfg.Name,
|
||||
AIModel: aiModelCfg.Provider, // 使用provider作为模型标识
|
||||
Exchange: exchangeCfg.ID, // 使用exchange ID
|
||||
InitialBalance: traderCfg.InitialBalance,
|
||||
BTCETHLeverage: traderCfg.BTCETHLeverage,
|
||||
AltcoinLeverage: traderCfg.AltcoinLeverage,
|
||||
ScanInterval: time.Duration(traderCfg.ScanIntervalMinutes) * time.Minute,
|
||||
CoinPoolAPIURL: effectiveCoinPoolURL,
|
||||
CustomAPIURL: aiModelCfg.CustomAPIURL, // 自定义API URL
|
||||
CustomModelName: aiModelCfg.CustomModelName, // 自定义模型名称
|
||||
UseQwen: aiModelCfg.Provider == "qwen",
|
||||
MaxDailyLoss: maxDailyLoss,
|
||||
MaxDrawdown: maxDrawdown,
|
||||
StopTradingTime: time.Duration(stopTradingMinutes) * time.Minute,
|
||||
IsCrossMargin: traderCfg.IsCrossMargin,
|
||||
DefaultCoins: defaultCoins,
|
||||
TradingCoins: tradingCoins,
|
||||
SystemPromptTemplate: traderCfg.SystemPromptTemplate, // 系统提示词模板
|
||||
}
|
||||
|
||||
// 根据交易所类型设置API密钥
|
||||
@@ -769,7 +794,7 @@ func (tm *TraderManager) loadSingleTrader(traderCfg *config.TraderRecord, aiMode
|
||||
if err != nil {
|
||||
return fmt.Errorf("创建trader失败: %w", err)
|
||||
}
|
||||
|
||||
|
||||
// 设置自定义prompt(如果有)
|
||||
if traderCfg.CustomPrompt != "" {
|
||||
at.SetCustomPrompt(traderCfg.CustomPrompt)
|
||||
|
||||
73
news/news.go
Normal file
73
news/news.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package news
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
// ProviderTelegrame 电报
|
||||
ProviderTelegram = "telegram"
|
||||
)
|
||||
|
||||
// NewsItem 表示一条新闻条目的核心数据结构。
|
||||
// 该结构体通常用于金融资讯、舆情分析等场景,存储新闻的元数据、内容及情感分析结果。
|
||||
type NewsItem struct {
|
||||
// Symbol 代表与该新闻相关的金融产品代码(如股票代码 "AAPL")。
|
||||
// 该字段是新闻关联资产的关键标识符。
|
||||
Symbol string `json:"symbol"`
|
||||
|
||||
// Headline 是新闻的标题。
|
||||
// 此字段为必填项,应简洁概括新闻主要内容。
|
||||
Headline string `json:"headline"`
|
||||
|
||||
// Source 指明新闻的发布来源(例如:"路透社"、"彭博社")。
|
||||
// 该字段为可选字段,若为空则不会在 JSON 输出中体现。
|
||||
Source string `json:"source,omitempty"`
|
||||
|
||||
// URL 是新闻原文的完整链接。
|
||||
// 该字段为可选字段,若为空则不会在 JSON 输出中体现。
|
||||
URL string `json:"url,omitempty"`
|
||||
|
||||
// PublishedAt 记录新闻准确的发布时间。
|
||||
// 使用 time.Time 类型以确保时间数据的一致性和可序列化。
|
||||
PublishedAt time.Time `json:"published_at"`
|
||||
|
||||
// Sentiment 表示对新闻内容进行情感分析得出的分值。
|
||||
// 取值范围为 -100 到 100,其中 -100 代表极度负面,100 代表极度正面,0 为中性。
|
||||
// 该字段为可选字段,若未进行分析则不会在 JSON 输出中体现。
|
||||
Sentiment int `json:"sentiment,omitempty"`
|
||||
|
||||
// Impact 评估该新闻可能对市场产生的影响程度。
|
||||
// 取值范围为 0 到 100,数值越大表示潜在影响越大。
|
||||
// 该字段为可选字段,若未进行评估则不会在 JSON 输出中体现。
|
||||
Impact int `json:"impact,omitempty"`
|
||||
|
||||
// Summary 是新闻内容的简要摘要或关键要点。
|
||||
// 该字段为可选字段,若为空则不会在 JSON 输出中体现。
|
||||
Summary string `json:"summary,omitempty"`
|
||||
|
||||
// Tags 是与新闻内容相关的关键词标签列表(例如:["科技", "财报", "Apple"])。
|
||||
// 该字段为可选字段,若为空切片则不会在 JSON 输出中体现。
|
||||
Tags []string `json:"tags,omitempty"`
|
||||
}
|
||||
|
||||
func (ni NewsItem) String() string {
|
||||
return fmt.Sprintf("代币符号: %s\n标题: %s\n来源: %s\n发布时间: %s\n情感分数: %d\n影响指数: %d\n摘要: %s\n标签: %v\n%s\n",
|
||||
ni.Symbol,
|
||||
ni.Headline,
|
||||
ni.Source,
|
||||
ni.PublishedAt.Format("2006-01-02 15:04:05"),
|
||||
ni.Sentiment,
|
||||
ni.Impact,
|
||||
ni.Summary,
|
||||
ni.Tags,
|
||||
"-----------",
|
||||
)
|
||||
}
|
||||
|
||||
// Provider 新闻提供者接口(由调用方实现)
|
||||
type Provider interface {
|
||||
// FetchNews 按币种批量获取最新新闻;返回值按symbol分组
|
||||
FetchNews(symbols []string, limit int) (map[string][]NewsItem, error)
|
||||
}
|
||||
145
news/provider/default/default.go
Normal file
145
news/provider/default/default.go
Normal file
@@ -0,0 +1,145 @@
|
||||
package news
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"nofx/news"
|
||||
"time"
|
||||
)
|
||||
|
||||
// DefaultNewsProvider 默认新闻提供者实现(参考示例)
|
||||
// 展示如何实现 NewsProvider 接口
|
||||
type DefaultNewsProvider struct {
|
||||
// API配置(根据实际数据源调整)
|
||||
APIKey string
|
||||
BaseURL string
|
||||
Timeout time.Duration
|
||||
CacheTime time.Duration
|
||||
|
||||
// 可选:缓存机制
|
||||
cache map[string]cacheEntry
|
||||
}
|
||||
|
||||
type cacheEntry struct {
|
||||
news []news.NewsItem
|
||||
timestamp time.Time
|
||||
}
|
||||
|
||||
// NewDefaultNewsProvider 创建默认新闻提供者
|
||||
func NewDefaultNewsProvider(apiKey, baseURL string) *DefaultNewsProvider {
|
||||
return &DefaultNewsProvider{
|
||||
APIKey: apiKey,
|
||||
BaseURL: baseURL,
|
||||
Timeout: 30 * time.Second,
|
||||
CacheTime: 5 * time.Minute, // 5分钟缓存
|
||||
cache: make(map[string]cacheEntry),
|
||||
}
|
||||
}
|
||||
|
||||
// FetchNews 实现 NewsProvider 接口
|
||||
// 按币种批量获取最新新闻,返回值按symbol分组
|
||||
func (p *DefaultNewsProvider) FetchNews(symbols []string, limit int) (map[string][]news.NewsItem, error) {
|
||||
if len(symbols) == 0 {
|
||||
return make(map[string][]news.NewsItem), nil
|
||||
}
|
||||
|
||||
result := make(map[string][]news.NewsItem)
|
||||
|
||||
for _, symbol := range symbols {
|
||||
// 1. 检查缓存
|
||||
if cached, ok := p.getFromCache(symbol); ok {
|
||||
result[symbol] = cached
|
||||
continue
|
||||
}
|
||||
|
||||
// 2. 从数据源获取(具体实现留空,由你后续补充)
|
||||
news, err := p.fetchFromSource(symbol, limit)
|
||||
if err != nil {
|
||||
// 单个币种失败不影响其他币种
|
||||
continue
|
||||
}
|
||||
|
||||
// 3. 更新缓存
|
||||
p.updateCache(symbol, news)
|
||||
result[symbol] = news
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// getFromCache 从缓存获取新闻
|
||||
func (p *DefaultNewsProvider) getFromCache(symbol string) ([]news.NewsItem, bool) {
|
||||
entry, exists := p.cache[symbol]
|
||||
if !exists {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// 检查缓存是否过期
|
||||
if time.Since(entry.timestamp) > p.CacheTime {
|
||||
delete(p.cache, symbol)
|
||||
return nil, false
|
||||
}
|
||||
|
||||
return entry.news, true
|
||||
}
|
||||
|
||||
// updateCache 更新缓存
|
||||
func (p *DefaultNewsProvider) updateCache(symbol string, news []news.NewsItem) {
|
||||
p.cache[symbol] = cacheEntry{
|
||||
news: news,
|
||||
timestamp: time.Now(),
|
||||
}
|
||||
}
|
||||
|
||||
// fetchFromSource 从数据源获取新闻(具体实现由你补充)
|
||||
func (p *DefaultNewsProvider) fetchFromSource(symbol string, limit int) ([]news.NewsItem, error) {
|
||||
// TODO: 实现具体的新闻获取逻辑
|
||||
// 可选的数据源:
|
||||
// 1. CryptoPanic API: https://cryptopanic.com/developers/api/
|
||||
// 2. CoinGecko Events API
|
||||
// 3. 交易所公告(币安、OKX等)
|
||||
// 4. Twitter/X API
|
||||
// 5. Reddit API
|
||||
// 6. 自定义爬虫
|
||||
|
||||
// 示例返回结构(实际需要调用API)
|
||||
news := []news.NewsItem{
|
||||
{
|
||||
Symbol: symbol,
|
||||
Headline: fmt.Sprintf("%s 相关新闻标题", symbol),
|
||||
Source: "CryptoPanic", // 或其他来源
|
||||
URL: "https://example.com/news/123",
|
||||
PublishedAt: time.Now().Add(-1 * time.Hour),
|
||||
Sentiment: 0, // -100 到 100
|
||||
Impact: 50, // 0 到 100
|
||||
Summary: "新闻摘要内容...",
|
||||
Tags: []string{"breaking", "market"},
|
||||
},
|
||||
}
|
||||
|
||||
return news, nil
|
||||
}
|
||||
|
||||
// 可选:实现其他辅助方法
|
||||
|
||||
// FilterByImpact 按影响力过滤新闻
|
||||
func (p *DefaultNewsProvider) FilterByImpact(items []news.NewsItem, minImpact int) []news.NewsItem {
|
||||
filtered := make([]news.NewsItem, 0)
|
||||
for _, item := range items {
|
||||
if item.Impact >= minImpact {
|
||||
filtered = append(filtered, item)
|
||||
}
|
||||
}
|
||||
return filtered
|
||||
}
|
||||
|
||||
// FilterByTime 按时间过滤新闻(只保留最近N小时的)
|
||||
func (p *DefaultNewsProvider) FilterByTime(items []news.NewsItem, hours int) []news.NewsItem {
|
||||
cutoff := time.Now().Add(-time.Duration(hours) * time.Hour)
|
||||
filtered := make([]news.NewsItem, 0)
|
||||
for _, item := range items {
|
||||
if item.PublishedAt.After(cutoff) {
|
||||
filtered = append(filtered, item)
|
||||
}
|
||||
}
|
||||
return filtered
|
||||
}
|
||||
299
news/provider/telegram/telegram.go
Normal file
299
news/provider/telegram/telegram.go
Normal file
@@ -0,0 +1,299 @@
|
||||
package telegram
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"nofx/news"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
// Message 表示 Telegram 消息结构
|
||||
type Message struct {
|
||||
MessageID string `json:"messageId"`
|
||||
Title string `json:"title"`
|
||||
Content string `json:"content"`
|
||||
PubDate string `json:"pubDate"`
|
||||
Image string `json:"image"`
|
||||
Tags []string `json:"tags"`
|
||||
}
|
||||
|
||||
// ToNews 转新闻结构
|
||||
func (m Message) ToNews() news.NewsItem {
|
||||
return news.NewsItem{
|
||||
Symbol: "",
|
||||
Headline: m.Title,
|
||||
Summary: m.Content,
|
||||
PublishedAt: time.Now(),
|
||||
}
|
||||
}
|
||||
|
||||
// Channel 表示频道配置
|
||||
type Channel struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
// Searcher Telegram 搜索服务
|
||||
type Searcher struct {
|
||||
client *http.Client
|
||||
baseURL string
|
||||
channels []Channel // telegram频道
|
||||
keywords []string // 关键词列表,暂时忽略
|
||||
}
|
||||
|
||||
// NewSearcher 创建搜索服务实例
|
||||
func NewSearcher(baseURL string, proxyURL string, channels []Channel) (*Searcher, error) {
|
||||
client := &http.Client{
|
||||
Timeout: 30 * time.Second,
|
||||
}
|
||||
|
||||
// 配置代理(如果提供)
|
||||
if proxyURL != "" {
|
||||
proxy, err := url.Parse(proxyURL)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid proxy URL: %w", err)
|
||||
}
|
||||
client.Transport = &http.Transport{
|
||||
Proxy: http.ProxyURL(proxy),
|
||||
}
|
||||
}
|
||||
|
||||
return &Searcher{
|
||||
client: client,
|
||||
baseURL: baseURL,
|
||||
channels: channels,
|
||||
keywords: []string{""},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// FetchNews 按币种批量获取最新新闻;返回值按symbol分组
|
||||
//
|
||||
// TODO: 此处未全部实现,当前不分币种,将所有消息返回
|
||||
func (s *Searcher) FetchNews(symbols []string, limit int) (map[string][]news.NewsItem, error) {
|
||||
newsItem := make(map[string][]news.NewsItem)
|
||||
for _, keyword := range s.keywords {
|
||||
mapMessages := s.SearchAllChannels(s.channels, keyword)
|
||||
for symbol, mes := range mapMessages {
|
||||
newsItem[symbol] = append(newsItem[symbol], lo.Map(mes, func(item Message, _ int) news.NewsItem { return item.ToNews() })...)
|
||||
}
|
||||
}
|
||||
return newsItem, nil
|
||||
}
|
||||
|
||||
// SearchChannel 搜索单个频道
|
||||
func (s *Searcher) SearchChannel(channelID string, keyword string) ([]Message, string, error) {
|
||||
// 构造搜索 URL
|
||||
searchURL := fmt.Sprintf("%s/%s", s.baseURL, channelID)
|
||||
if keyword != "" {
|
||||
searchURL = fmt.Sprintf("%s?q=%s", searchURL, url.QueryEscape(keyword))
|
||||
}
|
||||
|
||||
// 创建 HTTP 请求
|
||||
req, err := http.NewRequest("GET", searchURL, nil)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
|
||||
// 设置请求头,模拟浏览器
|
||||
req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)")
|
||||
req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9")
|
||||
req.Header.Set("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8")
|
||||
|
||||
// 发送请求
|
||||
resp, err := s.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, "", fmt.Errorf("request failed: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, "", fmt.Errorf("unexpected status code: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
// 解析 HTML
|
||||
return s.parseHTML(resp.Body)
|
||||
}
|
||||
|
||||
// parseHTML 解析 HTML 内容
|
||||
func (s *Searcher) parseHTML(body io.Reader) ([]Message, string, error) {
|
||||
doc, err := goquery.NewDocumentFromReader(body)
|
||||
if err != nil {
|
||||
return nil, "", fmt.Errorf("failed to parse HTML: %w", err)
|
||||
}
|
||||
|
||||
var messages []Message
|
||||
var channelLogo string
|
||||
|
||||
// 提取频道 logo
|
||||
doc.Find(".tgme_header_link img").Each(func(i int, s *goquery.Selection) {
|
||||
if src, exists := s.Attr("src"); exists {
|
||||
channelLogo = src
|
||||
}
|
||||
})
|
||||
|
||||
// 遍历消息
|
||||
doc.Find(".tgme_widget_message_wrap").Each(func(i int, sel *goquery.Selection) {
|
||||
message := s.extractMessage(sel)
|
||||
messages = append(messages, message)
|
||||
})
|
||||
|
||||
return messages, channelLogo, nil
|
||||
}
|
||||
|
||||
// extractMessage 从 HTML 元素中提取消息信息
|
||||
func (s *Searcher) extractMessage(sel *goquery.Selection) Message {
|
||||
msg := Message{}
|
||||
|
||||
// 提取消息 ID
|
||||
if dataPost, exists := sel.Find(".tgme_widget_message").Attr("data-post"); exists {
|
||||
// data-post 格式: "channelId/messageId"
|
||||
if len(dataPost) > 0 {
|
||||
parts := splitLast(dataPost, "/")
|
||||
if len(parts) == 2 {
|
||||
msg.MessageID = parts[1]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 提取消息文本
|
||||
messageText := sel.Find(".js-message_text")
|
||||
if messageText.Length() > 0 {
|
||||
html, _ := messageText.Html()
|
||||
|
||||
// 提取标题(第一行)
|
||||
if html != "" {
|
||||
lines := splitFirst(html, "<br/>")
|
||||
if len(lines) > 0 {
|
||||
msg.Title = stripHTML(lines[0])
|
||||
}
|
||||
|
||||
// 提取内容(去除标题后的文本)
|
||||
if len(lines) > 1 {
|
||||
msg.Content = stripHTML(lines[1])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 提取发布时间
|
||||
if datetime, exists := sel.Find("time").Attr("datetime"); exists {
|
||||
msg.PubDate = datetime
|
||||
}
|
||||
|
||||
// 提取图片
|
||||
if style, exists := sel.Find(".tgme_widget_message_photo_wrap").Attr("style"); exists {
|
||||
msg.Image = extractImageURL(style)
|
||||
}
|
||||
|
||||
// 提取标签
|
||||
sel.Find(".tgme_widget_message_text a").Each(func(i int, s *goquery.Selection) {
|
||||
text := s.Text()
|
||||
if len(text) > 0 && text[0] == '#' {
|
||||
msg.Tags = append(msg.Tags, text)
|
||||
}
|
||||
})
|
||||
|
||||
return msg
|
||||
}
|
||||
|
||||
// SearchAllChannels 并行搜索多个频道
|
||||
func (s *Searcher) SearchAllChannels(channels []Channel, keyword string) map[string][]Message {
|
||||
type result struct {
|
||||
channelID string
|
||||
messages []Message
|
||||
logo string
|
||||
err error
|
||||
}
|
||||
|
||||
resultChan := make(chan result, len(channels))
|
||||
|
||||
// 并行搜索
|
||||
for _, channel := range channels {
|
||||
go func(ch Channel) {
|
||||
messages, logo, err := s.SearchChannel(ch.ID, keyword)
|
||||
resultChan <- result{
|
||||
channelID: ch.ID,
|
||||
messages: messages,
|
||||
logo: logo,
|
||||
err: err,
|
||||
}
|
||||
}(channel)
|
||||
}
|
||||
|
||||
// 收集结果
|
||||
results := make(map[string][]Message)
|
||||
for i := 0; i < len(channels); i++ {
|
||||
res := <-resultChan
|
||||
if res.err == nil {
|
||||
results[res.channelID] = res.messages
|
||||
}
|
||||
}
|
||||
|
||||
return results
|
||||
}
|
||||
|
||||
// splitFirst 在第一个分隔符处分割字符串,返回最多两个部分
|
||||
func splitFirst(s, sep string) []string {
|
||||
// 使用 SplitN 限制分割次数为 2
|
||||
// 当 n=2 时,返回的切片最多包含两个元素
|
||||
result := strings.SplitN(s, sep, 2)
|
||||
|
||||
// 如果字符串中不包含分隔符,SplitN 会返回包含原字符串的切片
|
||||
// 这符合预期行为,直接返回即可
|
||||
return result
|
||||
}
|
||||
|
||||
// splitLast 在最后一个分隔符处分割字符串,返回最多两个部分
|
||||
func splitLast(s, sep string) []string {
|
||||
// 查找最后一个分隔符的位置
|
||||
index := strings.LastIndex(s, sep)
|
||||
|
||||
if index < 0 {
|
||||
// 如果没有找到分隔符,返回包含原字符串的切片
|
||||
return []string{s}
|
||||
}
|
||||
|
||||
// 根据最后一个分隔符的位置分割字符串
|
||||
part1 := s[:index]
|
||||
part2 := s[index+len(sep):]
|
||||
|
||||
return []string{part1, part2}
|
||||
}
|
||||
|
||||
// stripHTML 移除字符串中的所有 HTML 标签,只保留纯文本
|
||||
func stripHTML(s string) string {
|
||||
// 将HTML标签全转换成小写(确保匹配大小写不敏感的标签)
|
||||
re := regexp.MustCompile(`\<[\S\s]+?\>`)
|
||||
s = re.ReplaceAllStringFunc(s, strings.ToLower)
|
||||
|
||||
// 去除 <style> 标签及其内容
|
||||
re = regexp.MustCompile(`\<style[\S\s]+?\</style\>`)
|
||||
s = re.ReplaceAllString(s, "")
|
||||
|
||||
// 去除 <script> 标签及其内容
|
||||
re = regexp.MustCompile(`\<script[\S\s]+?\</script\>`)
|
||||
s = re.ReplaceAllString(s, "")
|
||||
|
||||
// 去除所有尖括号内的 HTML 代码,并换成换行符
|
||||
re = regexp.MustCompile(`\<[\S\s]+?\>`)
|
||||
s = re.ReplaceAllString(s, "\n")
|
||||
|
||||
// 去除连续的换行符和空白字符
|
||||
re = regexp.MustCompile(`\s{2,}`)
|
||||
s = re.ReplaceAllString(s, "\n")
|
||||
|
||||
// 去除首尾的空白字符
|
||||
return strings.TrimSpace(s)
|
||||
}
|
||||
|
||||
func extractImageURL(style string) string {
|
||||
// 从 style 属性中提取图片 URL
|
||||
// 格式: background-image:url('...')
|
||||
return ""
|
||||
}
|
||||
@@ -4,13 +4,18 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"nofx/config"
|
||||
"nofx/decision"
|
||||
"nofx/logger"
|
||||
"nofx/market"
|
||||
"nofx/mcp"
|
||||
"nofx/news"
|
||||
"nofx/news/provider/telegram"
|
||||
"nofx/pool"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
// AutoTraderConfig 自动交易配置(简化版 - AI全权决策)
|
||||
@@ -68,11 +73,14 @@ type AutoTraderConfig struct {
|
||||
IsCrossMargin bool // true=全仓模式, false=逐仓模式
|
||||
|
||||
// 币种配置
|
||||
DefaultCoins []string // 默认币种列表(从数据库获取)
|
||||
TradingCoins []string // 实际交易币种列表
|
||||
DefaultCoins []string // 默认币种列表(从数据库获取)
|
||||
TradingCoins []string // 实际交易币种列表
|
||||
|
||||
// 系统提示词模板
|
||||
SystemPromptTemplate string // 系统提示词模板名称(如 "default", "aggressive")
|
||||
|
||||
// 新闻源配置
|
||||
NewsConfig []config.NewsConfig
|
||||
}
|
||||
|
||||
// AutoTrader 自动交易器
|
||||
@@ -87,9 +95,9 @@ type AutoTrader struct {
|
||||
decisionLogger *logger.DecisionLogger // 决策日志记录器
|
||||
initialBalance float64
|
||||
dailyPnL float64
|
||||
customPrompt string // 自定义交易策略prompt
|
||||
overrideBasePrompt bool // 是否覆盖基础prompt
|
||||
systemPromptTemplate string // 系统提示词模板名称
|
||||
customPrompt string // 自定义交易策略prompt
|
||||
overrideBasePrompt bool // 是否覆盖基础prompt
|
||||
systemPromptTemplate string // 系统提示词模板名称
|
||||
defaultCoins []string // 默认币种列表(从数据库获取)
|
||||
tradingCoins []string // 实际交易币种列表
|
||||
lastResetTime time.Time
|
||||
@@ -98,58 +106,81 @@ type AutoTrader struct {
|
||||
startTime time.Time // 系统启动时间
|
||||
callCount int // AI调用次数
|
||||
positionFirstSeenTime map[string]int64 // 持仓首次出现时间 (symbol_side -> timestamp毫秒)
|
||||
newsProcessor []news.Provider // 新闻
|
||||
}
|
||||
|
||||
// NewAutoTrader 创建自动交易器
|
||||
func NewAutoTrader(config AutoTraderConfig) (*AutoTrader, error) {
|
||||
func NewAutoTrader(traderConfig AutoTraderConfig) (*AutoTrader, error) {
|
||||
// 设置默认值
|
||||
if config.ID == "" {
|
||||
config.ID = "default_trader"
|
||||
if traderConfig.ID == "" {
|
||||
traderConfig.ID = "default_trader"
|
||||
}
|
||||
if config.Name == "" {
|
||||
config.Name = "Default Trader"
|
||||
if traderConfig.Name == "" {
|
||||
traderConfig.Name = "Default Trader"
|
||||
}
|
||||
if config.AIModel == "" {
|
||||
if config.UseQwen {
|
||||
config.AIModel = "qwen"
|
||||
if traderConfig.AIModel == "" {
|
||||
if traderConfig.UseQwen {
|
||||
traderConfig.AIModel = "qwen"
|
||||
} else {
|
||||
config.AIModel = "deepseek"
|
||||
traderConfig.AIModel = "deepseek"
|
||||
}
|
||||
}
|
||||
|
||||
mcpClient := mcp.New()
|
||||
|
||||
// 初始化AI
|
||||
if config.AIModel == "custom" {
|
||||
if traderConfig.AIModel == "custom" {
|
||||
// 使用自定义API
|
||||
mcpClient.SetCustomAPI(config.CustomAPIURL, config.CustomAPIKey, config.CustomModelName)
|
||||
log.Printf("🤖 [%s] 使用自定义AI API: %s (模型: %s)", config.Name, config.CustomAPIURL, config.CustomModelName)
|
||||
} else if config.UseQwen || config.AIModel == "qwen" {
|
||||
mcpClient.SetCustomAPI(traderConfig.CustomAPIURL, traderConfig.CustomAPIKey, traderConfig.CustomModelName)
|
||||
log.Printf("🤖 [%s] 使用自定义AI API: %s (模型: %s)", traderConfig.Name, traderConfig.CustomAPIURL, traderConfig.CustomModelName)
|
||||
} else if traderConfig.UseQwen || traderConfig.AIModel == "qwen" {
|
||||
// 使用Qwen (支持自定义URL和Model)
|
||||
mcpClient.SetQwenAPIKey(config.QwenKey, config.CustomAPIURL, config.CustomModelName)
|
||||
if config.CustomAPIURL != "" || config.CustomModelName != "" {
|
||||
log.Printf("🤖 [%s] 使用阿里云Qwen AI (自定义URL: %s, 模型: %s)", config.Name, config.CustomAPIURL, config.CustomModelName)
|
||||
mcpClient.SetQwenAPIKey(traderConfig.QwenKey, traderConfig.CustomAPIURL, traderConfig.CustomModelName)
|
||||
if traderConfig.CustomAPIURL != "" || traderConfig.CustomModelName != "" {
|
||||
log.Printf("🤖 [%s] 使用阿里云Qwen AI (自定义URL: %s, 模型: %s)", traderConfig.Name, traderConfig.CustomAPIURL, traderConfig.CustomModelName)
|
||||
} else {
|
||||
log.Printf("🤖 [%s] 使用阿里云Qwen AI", config.Name)
|
||||
log.Printf("🤖 [%s] 使用阿里云Qwen AI", traderConfig.Name)
|
||||
}
|
||||
} else {
|
||||
// 默认使用DeepSeek (支持自定义URL和Model)
|
||||
mcpClient.SetDeepSeekAPIKey(config.DeepSeekKey, config.CustomAPIURL, config.CustomModelName)
|
||||
if config.CustomAPIURL != "" || config.CustomModelName != "" {
|
||||
log.Printf("🤖 [%s] 使用DeepSeek AI (自定义URL: %s, 模型: %s)", config.Name, config.CustomAPIURL, config.CustomModelName)
|
||||
mcpClient.SetDeepSeekAPIKey(traderConfig.DeepSeekKey, traderConfig.CustomAPIURL, traderConfig.CustomModelName)
|
||||
if traderConfig.CustomAPIURL != "" || traderConfig.CustomModelName != "" {
|
||||
log.Printf("🤖 [%s] 使用DeepSeek AI (自定义URL: %s, 模型: %s)", traderConfig.Name, traderConfig.CustomAPIURL, traderConfig.CustomModelName)
|
||||
} else {
|
||||
log.Printf("🤖 [%s] 使用DeepSeek AI", config.Name)
|
||||
log.Printf("🤖 [%s] 使用DeepSeek AI", traderConfig.Name)
|
||||
}
|
||||
}
|
||||
|
||||
// 初始化新闻提取器
|
||||
var newsProcessor []news.Provider
|
||||
for _, newsCfg := range traderConfig.NewsConfig {
|
||||
switch newsCfg.Provider {
|
||||
case news.ProviderTelegram:
|
||||
newsProvider, err := telegram.NewSearcher(newsCfg.Telegram.BaseURL,
|
||||
newsCfg.Telegram.ProxyURL,
|
||||
lo.Map(newsCfg.TelegramChannel, func(item config.NewsConfigTelegramChannel, _ int) telegram.Channel {
|
||||
return telegram.Channel{
|
||||
ID: item.ID,
|
||||
Name: item.Name,
|
||||
}
|
||||
}),
|
||||
)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
newsProcessor = append(newsProcessor, newsProvider)
|
||||
}
|
||||
}
|
||||
|
||||
// 初始化币种池API
|
||||
if config.CoinPoolAPIURL != "" {
|
||||
pool.SetCoinPoolAPI(config.CoinPoolAPIURL)
|
||||
if traderConfig.CoinPoolAPIURL != "" {
|
||||
pool.SetCoinPoolAPI(traderConfig.CoinPoolAPIURL)
|
||||
}
|
||||
|
||||
// 设置默认交易平台
|
||||
if config.Exchange == "" {
|
||||
config.Exchange = "binance"
|
||||
if traderConfig.Exchange == "" {
|
||||
traderConfig.Exchange = "binance"
|
||||
}
|
||||
|
||||
// 根据配置创建对应的交易器
|
||||
@@ -158,64 +189,65 @@ func NewAutoTrader(config AutoTraderConfig) (*AutoTrader, error) {
|
||||
|
||||
// 记录仓位模式(通用)
|
||||
marginModeStr := "全仓"
|
||||
if !config.IsCrossMargin {
|
||||
if !traderConfig.IsCrossMargin {
|
||||
marginModeStr = "逐仓"
|
||||
}
|
||||
log.Printf("📊 [%s] 仓位模式: %s", config.Name, marginModeStr)
|
||||
log.Printf("📊 [%s] 仓位模式: %s", traderConfig.Name, marginModeStr)
|
||||
|
||||
switch config.Exchange {
|
||||
switch traderConfig.Exchange {
|
||||
case "binance":
|
||||
log.Printf("🏦 [%s] 使用币安合约交易", config.Name)
|
||||
trader = NewFuturesTrader(config.BinanceAPIKey, config.BinanceSecretKey)
|
||||
log.Printf("🏦 [%s] 使用币安合约交易", traderConfig.Name)
|
||||
trader = NewFuturesTrader(traderConfig.BinanceAPIKey, traderConfig.BinanceSecretKey)
|
||||
case "hyperliquid":
|
||||
log.Printf("🏦 [%s] 使用Hyperliquid交易", config.Name)
|
||||
trader, err = NewHyperliquidTrader(config.HyperliquidPrivateKey, config.HyperliquidWalletAddr, config.HyperliquidTestnet)
|
||||
log.Printf("🏦 [%s] 使用Hyperliquid交易", traderConfig.Name)
|
||||
trader, err = NewHyperliquidTrader(traderConfig.HyperliquidPrivateKey, traderConfig.HyperliquidWalletAddr, traderConfig.HyperliquidTestnet)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("初始化Hyperliquid交易器失败: %w", err)
|
||||
}
|
||||
case "aster":
|
||||
log.Printf("🏦 [%s] 使用Aster交易", config.Name)
|
||||
trader, err = NewAsterTrader(config.AsterUser, config.AsterSigner, config.AsterPrivateKey)
|
||||
log.Printf("🏦 [%s] 使用Aster交易", traderConfig.Name)
|
||||
trader, err = NewAsterTrader(traderConfig.AsterUser, traderConfig.AsterSigner, traderConfig.AsterPrivateKey)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("初始化Aster交易器失败: %w", err)
|
||||
}
|
||||
default:
|
||||
return nil, fmt.Errorf("不支持的交易平台: %s", config.Exchange)
|
||||
return nil, fmt.Errorf("不支持的交易平台: %s", traderConfig.Exchange)
|
||||
}
|
||||
|
||||
// 验证初始金额配置
|
||||
if config.InitialBalance <= 0 {
|
||||
if traderConfig.InitialBalance <= 0 {
|
||||
return nil, fmt.Errorf("初始金额必须大于0,请在配置中设置InitialBalance")
|
||||
}
|
||||
|
||||
// 初始化决策日志记录器(使用trader ID创建独立目录)
|
||||
logDir := fmt.Sprintf("decision_logs/%s", config.ID)
|
||||
logDir := fmt.Sprintf("decision_logs/%s", traderConfig.ID)
|
||||
decisionLogger := logger.NewDecisionLogger(logDir)
|
||||
|
||||
// 设置默认系统提示词模板
|
||||
systemPromptTemplate := config.SystemPromptTemplate
|
||||
systemPromptTemplate := traderConfig.SystemPromptTemplate
|
||||
if systemPromptTemplate == "" {
|
||||
systemPromptTemplate = "default" // 默认使用 default 模板
|
||||
}
|
||||
|
||||
return &AutoTrader{
|
||||
id: config.ID,
|
||||
name: config.Name,
|
||||
aiModel: config.AIModel,
|
||||
exchange: config.Exchange,
|
||||
config: config,
|
||||
id: traderConfig.ID,
|
||||
name: traderConfig.Name,
|
||||
aiModel: traderConfig.AIModel,
|
||||
exchange: traderConfig.Exchange,
|
||||
config: traderConfig,
|
||||
trader: trader,
|
||||
mcpClient: mcpClient,
|
||||
decisionLogger: decisionLogger,
|
||||
initialBalance: config.InitialBalance,
|
||||
initialBalance: traderConfig.InitialBalance,
|
||||
systemPromptTemplate: systemPromptTemplate,
|
||||
defaultCoins: config.DefaultCoins,
|
||||
tradingCoins: config.TradingCoins,
|
||||
defaultCoins: traderConfig.DefaultCoins,
|
||||
tradingCoins: traderConfig.TradingCoins,
|
||||
lastResetTime: time.Now(),
|
||||
startTime: time.Now(),
|
||||
callCount: 0,
|
||||
isRunning: false,
|
||||
positionFirstSeenTime: make(map[string]int64),
|
||||
newsProcessor: newsProcessor,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -558,7 +590,23 @@ func (at *AutoTrader) buildTradingContext() (*decision.Context, error) {
|
||||
performance = nil
|
||||
}
|
||||
|
||||
// 6. 构建上下文
|
||||
// 6.提取新闻内容
|
||||
newsItem := make(map[string][]news.NewsItem)
|
||||
for _, newspro := range at.newsProcessor {
|
||||
// TODO: 此出是为后续扩展考虑,当前随意给了个值占位
|
||||
newsMap, err := newspro.FetchNews([]string{"btc"}, 100)
|
||||
if err != nil {
|
||||
log.Printf("⚠️ 获取新闻内容失败: %v", err)
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
for symbol, value := range newsMap {
|
||||
newsItem[symbol] = append(newsItem[symbol], value...)
|
||||
}
|
||||
}
|
||||
|
||||
// 7. 构建上下文
|
||||
ctx := &decision.Context{
|
||||
CurrentTime: time.Now().Format("2006-01-02 15:04:05"),
|
||||
RuntimeMinutes: int(time.Since(at.startTime).Minutes()),
|
||||
@@ -577,6 +625,7 @@ func (at *AutoTrader) buildTradingContext() (*decision.Context, error) {
|
||||
Positions: positionInfos,
|
||||
CandidateCoins: candidateCoins,
|
||||
Performance: performance, // 添加历史表现分析
|
||||
News: newsItem,
|
||||
}
|
||||
|
||||
return ctx, nil
|
||||
@@ -1016,7 +1065,7 @@ func (at *AutoTrader) getCandidateCoins() ([]decision.CandidateCoin, error) {
|
||||
if len(at.tradingCoins) == 0 {
|
||||
// 使用数据库配置的默认币种列表
|
||||
var candidateCoins []decision.CandidateCoin
|
||||
|
||||
|
||||
if len(at.defaultCoins) > 0 {
|
||||
// 使用数据库中配置的默认币种
|
||||
for _, coin := range at.defaultCoins {
|
||||
@@ -1032,7 +1081,7 @@ func (at *AutoTrader) getCandidateCoins() ([]decision.CandidateCoin, error) {
|
||||
} else {
|
||||
// 如果数据库中没有配置默认币种,则使用AI500+OI Top作为fallback
|
||||
const ai500Limit = 20 // AI500取前20个评分最高的币种
|
||||
|
||||
|
||||
mergedPool, err := pool.GetMergedCoinPool(ai500Limit)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("获取合并币种池失败: %w", err)
|
||||
@@ -1073,11 +1122,11 @@ func (at *AutoTrader) getCandidateCoins() ([]decision.CandidateCoin, error) {
|
||||
func normalizeSymbol(symbol string) string {
|
||||
// 转为大写
|
||||
symbol = strings.ToUpper(strings.TrimSpace(symbol))
|
||||
|
||||
|
||||
// 确保以USDT结尾
|
||||
if !strings.HasSuffix(symbol, "USDT") {
|
||||
symbol = symbol + "USDT"
|
||||
}
|
||||
|
||||
|
||||
return symbol
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user