feat: Add Telegram news integration for market sentiment analysis (PR #277)

- Add Telegram channel monitoring for market news
- Integrate news sentiment into AI decision making
- Improve context awareness for trading decisions
- Fix missing InsideCoins field in ConfigFile structure
- Merge with existing partial_close and position tracking features
This commit is contained in:
ZhouYongyou
2025-11-03 22:19:38 +08:00
11 changed files with 799 additions and 74 deletions

View File

@@ -21,5 +21,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"
}
]
}
]
}

View File

@@ -435,6 +435,25 @@ type UserSignalSource struct {
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密钥
func GenerateOTPSecret() (string, error) {
secret := make([]byte, 20)

View File

@@ -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的交易决策
@@ -398,6 +402,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
View File

@@ -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
@@ -11,11 +12,13 @@ require (
github.com/gorilla/websocket v1.5.3
github.com/mattn/go-sqlite3 v1.14.16
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
View File

@@ -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=
@@ -76,6 +80,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=
@@ -155,6 +160,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=
@@ -192,6 +199,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=
@@ -202,23 +210,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=

35
main.go
View File

@@ -25,19 +25,20 @@ 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"`
InsideCoins bool `json:"inside_coins"`
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"`
DataKLineTime string `json:"data_k_line_time"`
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"`
InsideCoins bool `json:"inside_coins"`
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"`
DataKLineTime string `json:"data_k_line_time"`
News []config.NewsConfig `json:"news"`
}
// syncConfigToDatabase 从config.json读取配置并同步到数据库
@@ -96,6 +97,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 {

View File

@@ -5,6 +5,7 @@ import (
"fmt"
"log"
"nofx/config"
"nofx/news"
"nofx/trader"
"strconv"
"strings"
@@ -83,6 +84,29 @@ 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 {
// 获取AI模型配置使用交易员所属的用户ID
@@ -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)
}
@@ -226,6 +250,7 @@ func (tm *TraderManager) addTraderFromDB(traderCfg *config.TraderRecord, aiModel
DefaultCoins: defaultCoins,
TradingCoins: tradingCoins,
SystemPromptTemplate: traderCfg.SystemPromptTemplate, // 系统提示词模板
NewsConfig: newsCfg, // 新闻源配置
}
// 根据交易所类型设置API密钥

73
news/news.go Normal file
View 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)
}

View 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
}

View 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 ""
}

View File

@@ -5,13 +5,18 @@ import (
"fmt"
"log"
"math"
"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全权决策
@@ -74,6 +79,9 @@ type AutoTraderConfig struct {
// 系统提示词模板
SystemPromptTemplate string // 系统提示词模板名称(如 "default", "aggressive"
// 新闻源配置
NewsConfig []config.NewsConfig
}
// PositionSnapshot 持仓快照(用于检测自动平仓)
@@ -109,58 +117,81 @@ type AutoTrader struct {
callCount int // AI调用次数
positionFirstSeenTime map[string]int64 // 持仓首次出现时间 (symbol_side -> timestamp毫秒)
lastPositions map[string]*PositionSnapshot // 上一个周期的持仓快照 (symbol_side -> snapshot)
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"
}
// 根据配置创建对应的交易器
@@ -169,66 +200,67 @@ 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 == "" {
// feature/partial-close-dynamic-tpsl 分支默认使用 adaptive支持动态止盈止损
systemPromptTemplate = "adaptive"
}
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),
lastPositions: make(map[string]*PositionSnapshot),
newsProcessor: newsProcessor,
}, nil
}
@@ -640,7 +672,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()),
@@ -659,6 +707,7 @@ func (at *AutoTrader) buildTradingContext() (*decision.Context, error) {
Positions: positionInfos,
CandidateCoins: candidateCoins,
Performance: performance, // 添加历史表现分析
News: newsItem,
}
return ctx, nil