feat: unify NofxOS data provider and fix language consistency

- Add unified NofxOS API key configuration in IndicatorEditor
- Add language field to StrategyConfig for consistent prompt generation
- Auto-update prompt sections when interface language changes
- Remove scattered URL inputs from CoinSourceEditor and IndicatorEditor
- Create nofxos provider package with formatted data output
- Update kernel engine to use config-based language setting
This commit is contained in:
tinkle-community
2026-01-04 00:59:07 +08:00
parent 13fda47151
commit 0275e23b7e
32 changed files with 3029 additions and 1767 deletions
+183 -70
View File
@@ -8,7 +8,7 @@ import (
"nofx/logger"
"nofx/market"
"nofx/mcp"
"nofx/provider"
"nofx/provider/nofxos"
"nofx/security"
"nofx/store"
"regexp"
@@ -119,8 +119,10 @@ type Context struct {
MultiTFMarket map[string]map[string]*market.Data `json:"-"`
OITopDataMap map[string]*OITopData `json:"-"`
QuantDataMap map[string]*QuantData `json:"-"`
OIRankingData *provider.OIRankingData `json:"-"` // Market-wide OI ranking data
BTCETHLeverage int `json:"-"`
OIRankingData *nofxos.OIRankingData `json:"-"` // Market-wide OI ranking data
NetFlowRankingData *nofxos.NetFlowRankingData `json:"-"` // Market-wide fund flow ranking data
PriceRankingData *nofxos.PriceRankingData `json:"-"` // Market-wide price gainers/losers
BTCETHLeverage int `json:"-"`
AltcoinLeverage int `json:"-"`
Timeframes []string `json:"-"`
}
@@ -189,12 +191,23 @@ type OIDeltaData struct {
// StrategyEngine strategy execution engine
type StrategyEngine struct {
config *store.StrategyConfig
config *store.StrategyConfig
nofxosClient *nofxos.Client
}
// NewStrategyEngine creates strategy execution engine
func NewStrategyEngine(config *store.StrategyConfig) *StrategyEngine {
return &StrategyEngine{config: config}
// Create NofxOS client with API key from config
apiKey := config.Indicators.NofxOSAPIKey
if apiKey == "" {
apiKey = nofxos.DefaultAuthKey
}
client := nofxos.NewClient(nofxos.DefaultBaseURL, apiKey)
return &StrategyEngine{
config: config,
nofxosClient: client,
}
}
// GetRiskControlConfig gets risk control configuration
@@ -202,6 +215,19 @@ func (e *StrategyEngine) GetRiskControlConfig() store.RiskControlConfig {
return e.config.RiskControl
}
// GetLanguage returns the language from config or falls back to auto-detection
func (e *StrategyEngine) GetLanguage() Language {
switch e.config.Language {
case "zh":
return LangChinese
case "en":
return LangEnglish
default:
// Fall back to auto-detection from prompt content for backward compatibility
return detectLanguage(e.config.PromptSections.RoleDefinition)
}
}
// GetConfig gets complete strategy configuration
func (e *StrategyEngine) GetConfig() *store.StrategyConfig {
return e.config
@@ -239,7 +265,7 @@ func GetFullDecisionWithStrategy(ctx *Context, mcpClient mcp.AIClient, engine *S
// Ensure OITopDataMap is initialized
if ctx.OITopDataMap == nil {
ctx.OITopDataMap = make(map[string]*OITopData)
oiPositions, err := provider.GetOITopPositions()
oiPositions, err := engine.nofxosClient.GetOITopPositions()
if err == nil {
for _, pos := range oiPositions {
ctx.OITopDataMap[pos.Symbol] = &OITopData{
@@ -385,13 +411,6 @@ func (e *StrategyEngine) GetCandidateCoins() ([]CandidateCoin, error) {
coinSource := e.config.CoinSource
if coinSource.CoinPoolAPIURL != "" {
provider.SetCoinPoolAPI(coinSource.CoinPoolAPIURL)
}
if coinSource.OITopAPIURL != "" {
provider.SetOITopAPI(coinSource.OITopAPIURL)
}
switch coinSource.SourceType {
case "static":
for _, symbol := range coinSource.StaticCoins {
@@ -404,10 +423,10 @@ func (e *StrategyEngine) GetCandidateCoins() ([]CandidateCoin, error) {
return e.filterExcludedCoins(candidates), nil
case "coinpool":
// 检查 use_coin_pool 标志,如果为 false 则回退到静态币种
if !coinSource.UseCoinPool {
logger.Infof("⚠️ source_type is 'coinpool' but use_coin_pool is false, falling back to static coins")
case "ai500":
// 检查 use_ai500 标志,如果为 false 则回退到静态币种
if !coinSource.UseAI500 {
logger.Infof("⚠️ source_type is 'ai500' but use_ai500 is false, falling back to static coins")
for _, symbol := range coinSource.StaticCoins {
symbol = market.Normalize(symbol)
candidates = append(candidates, CandidateCoin{
@@ -417,7 +436,7 @@ func (e *StrategyEngine) GetCandidateCoins() ([]CandidateCoin, error) {
}
return e.filterExcludedCoins(candidates), nil
}
coins, err := e.getCoinPoolCoins(coinSource.CoinPoolLimit)
coins, err := e.getAI500Coins(coinSource.AI500Limit)
if err != nil {
return nil, err
}
@@ -443,10 +462,10 @@ func (e *StrategyEngine) GetCandidateCoins() ([]CandidateCoin, error) {
return e.filterExcludedCoins(coins), nil
case "mixed":
if coinSource.UseCoinPool {
poolCoins, err := e.getCoinPoolCoins(coinSource.CoinPoolLimit)
if coinSource.UseAI500 {
poolCoins, err := e.getAI500Coins(coinSource.AI500Limit)
if err != nil {
logger.Infof("⚠️ Failed to get AI500 coin pool: %v", err)
logger.Infof("⚠️ Failed to get AI500 coins: %v", err)
} else {
for _, coin := range poolCoins {
symbolSources[coin.Symbol] = append(symbolSources[coin.Symbol], "ai500")
@@ -513,12 +532,12 @@ func (e *StrategyEngine) filterExcludedCoins(candidates []CandidateCoin) []Candi
return filtered
}
func (e *StrategyEngine) getCoinPoolCoins(limit int) ([]CandidateCoin, error) {
func (e *StrategyEngine) getAI500Coins(limit int) ([]CandidateCoin, error) {
if limit <= 0 {
limit = 30
}
symbols, err := provider.GetTopRatedCoins(limit)
symbols, err := e.nofxosClient.GetTopRatedCoins(limit)
if err != nil {
return nil, err
}
@@ -538,7 +557,7 @@ func (e *StrategyEngine) getOITopCoins(limit int) ([]CandidateCoin, error) {
limit = 20
}
positions, err := provider.GetOITopPositions()
positions, err := e.nofxosClient.GetOITopPositions()
if err != nil {
return nil, err
}
@@ -645,50 +664,82 @@ func extractJSONPath(data interface{}, path string) interface{} {
// FetchQuantData fetches quantitative data for a single coin
func (e *StrategyEngine) FetchQuantData(symbol string) (*QuantData, error) {
if !e.config.Indicators.EnableQuantData || e.config.Indicators.QuantDataAPIURL == "" {
if !e.config.Indicators.EnableQuantData {
return nil, nil
}
apiURL := e.config.Indicators.QuantDataAPIURL
url := strings.Replace(apiURL, "{symbol}", symbol, -1)
// Use nofxos client with unified API key
include := "oi,price"
if e.config.Indicators.EnableQuantNetflow {
include = "netflow,oi,price"
}
// SSRF Protection: Validate URL before making request
resp, err := security.SafeGet(url, 10*time.Second)
nofxosData, err := e.nofxosClient.GetCoinData(symbol, include)
if err != nil {
return nil, fmt.Errorf("request failed: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("HTTP status code: %d", resp.StatusCode)
return nil, fmt.Errorf("failed to fetch quant data: %w", err)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response: %w", err)
if nofxosData == nil {
return nil, nil
}
var apiResp struct {
Code int `json:"code"`
Data *QuantData `json:"data"`
// Convert nofxos.QuantData to kernel.QuantData
quantData := &QuantData{
Symbol: nofxosData.Symbol,
Price: nofxosData.Price,
PriceChange: nofxosData.PriceChange,
}
if err := json.Unmarshal(body, &apiResp); err != nil {
return nil, fmt.Errorf("failed to parse JSON: %w", err)
// Convert OI data
if nofxosData.OI != nil {
quantData.OI = make(map[string]*OIData)
for exchange, oiData := range nofxosData.OI {
if oiData != nil {
kData := &OIData{
CurrentOI: oiData.CurrentOI,
}
if oiData.Delta != nil {
kData.Delta = make(map[string]*OIDeltaData)
for dur, delta := range oiData.Delta {
if delta != nil {
kData.Delta[dur] = &OIDeltaData{
OIDelta: delta.OIDelta,
OIDeltaValue: delta.OIDeltaValue,
OIDeltaPercent: delta.OIDeltaPercent,
}
}
}
}
quantData.OI[exchange] = kData
}
}
}
if apiResp.Code != 0 {
return nil, fmt.Errorf("API returned error code: %d", apiResp.Code)
// Convert Netflow data
if nofxosData.Netflow != nil {
quantData.Netflow = &NetflowData{}
if nofxosData.Netflow.Institution != nil {
quantData.Netflow.Institution = &FlowTypeData{
Future: nofxosData.Netflow.Institution.Future,
Spot: nofxosData.Netflow.Institution.Spot,
}
}
if nofxosData.Netflow.Personal != nil {
quantData.Netflow.Personal = &FlowTypeData{
Future: nofxosData.Netflow.Personal.Future,
Spot: nofxosData.Netflow.Personal.Spot,
}
}
}
return apiResp.Data, nil
return quantData, nil
}
// FetchQuantDataBatch batch fetches quantitative data
func (e *StrategyEngine) FetchQuantDataBatch(symbols []string) map[string]*QuantData {
result := make(map[string]*QuantData)
if !e.config.Indicators.EnableQuantData || e.config.Indicators.QuantDataAPIURL == "" {
if !e.config.Indicators.EnableQuantData {
return result
}
@@ -707,28 +758,12 @@ func (e *StrategyEngine) FetchQuantDataBatch(symbols []string) map[string]*Quant
}
// FetchOIRankingData fetches market-wide OI ranking data
func (e *StrategyEngine) FetchOIRankingData() *provider.OIRankingData {
func (e *StrategyEngine) FetchOIRankingData() *nofxos.OIRankingData {
indicators := e.config.Indicators
if !indicators.EnableOIRanking {
return nil
}
baseURL := indicators.OIRankingAPIURL
if baseURL == "" {
baseURL = "http://nofxaios.com:30006"
}
// Get auth key from existing API URL or use default
authKey := "cm_568c67eae410d912c54c"
if indicators.QuantDataAPIURL != "" {
if idx := strings.Index(indicators.QuantDataAPIURL, "auth="); idx != -1 {
authKey = indicators.QuantDataAPIURL[idx+5:]
if ampIdx := strings.Index(authKey, "&"); ampIdx != -1 {
authKey = authKey[:ampIdx]
}
}
}
duration := indicators.OIRankingDuration
if duration == "" {
duration = "1h"
@@ -741,7 +776,7 @@ func (e *StrategyEngine) FetchOIRankingData() *provider.OIRankingData {
logger.Infof("📊 Fetching OI ranking data (duration: %s, limit: %d)", duration, limit)
data, err := provider.GetOIRankingData(baseURL, authKey, duration, limit)
data, err := e.nofxosClient.GetOIRanking(duration, limit)
if err != nil {
logger.Warnf("⚠️ Failed to fetch OI ranking data: %v", err)
return nil
@@ -753,6 +788,68 @@ func (e *StrategyEngine) FetchOIRankingData() *provider.OIRankingData {
return data
}
// FetchNetFlowRankingData fetches market-wide NetFlow ranking data
func (e *StrategyEngine) FetchNetFlowRankingData() *nofxos.NetFlowRankingData {
indicators := e.config.Indicators
if !indicators.EnableNetFlowRanking {
return nil
}
duration := indicators.NetFlowRankingDuration
if duration == "" {
duration = "1h"
}
limit := indicators.NetFlowRankingLimit
if limit <= 0 {
limit = 10
}
logger.Infof("💰 Fetching NetFlow ranking data (duration: %s, limit: %d)", duration, limit)
data, err := e.nofxosClient.GetNetFlowRanking(duration, limit)
if err != nil {
logger.Warnf("⚠️ Failed to fetch NetFlow ranking data: %v", err)
return nil
}
logger.Infof("✓ NetFlow ranking data ready: inst_in=%d, inst_out=%d, retail_in=%d, retail_out=%d",
len(data.InstitutionFutureTop), len(data.InstitutionFutureLow),
len(data.PersonalFutureTop), len(data.PersonalFutureLow))
return data
}
// FetchPriceRankingData fetches market-wide price ranking data (gainers/losers)
func (e *StrategyEngine) FetchPriceRankingData() *nofxos.PriceRankingData {
indicators := e.config.Indicators
if !indicators.EnablePriceRanking {
return nil
}
durations := indicators.PriceRankingDuration
if durations == "" {
durations = "1h"
}
limit := indicators.PriceRankingLimit
if limit <= 0 {
limit = 10
}
logger.Infof("📈 Fetching Price ranking data (durations: %s, limit: %d)", durations, limit)
data, err := e.nofxosClient.GetPriceRanking(durations, limit)
if err != nil {
logger.Warnf("⚠️ Failed to fetch Price ranking data: %v", err)
return nil
}
logger.Infof("✓ Price ranking data ready for %d durations", len(data.Durations))
return data
}
// ============================================================================
// Prompt Building - System Prompt
// ============================================================================
@@ -764,7 +861,7 @@ func (e *StrategyEngine) BuildSystemPrompt(accountEquity float64, variant string
promptSections := e.config.PromptSections
// 0. Data Dictionary & Schema (ensure AI understands all fields)
lang := detectLanguage(promptSections.RoleDefinition)
lang := e.GetLanguage()
schemaPrompt := GetSchemaPrompt(lang)
sb.WriteString(schemaPrompt)
sb.WriteString("\n\n")
@@ -955,7 +1052,7 @@ func (e *StrategyEngine) writeAvailableIndicators(sb *strings.Builder) {
sb.WriteString("- Funding rate\n")
}
if len(e.config.CoinSource.StaticCoins) > 0 || e.config.CoinSource.UseCoinPool || e.config.CoinSource.UseOITop {
if len(e.config.CoinSource.StaticCoins) > 0 || e.config.CoinSource.UseAI500 || e.config.CoinSource.UseOITop {
sb.WriteString("- AI500 / OI_Top filter tags (if available)\n")
}
@@ -1011,8 +1108,8 @@ func (e *StrategyEngine) BuildUserPrompt(ctx *Context) string {
// Historical trading statistics (helps AI understand past performance)
if ctx.TradingStats != nil && ctx.TradingStats.TotalTrades > 0 {
// Detect language from strategy config
lang := detectLanguage(e.config.PromptSections.RoleDefinition)
// Get language from strategy config
lang := e.GetLanguage()
// Win/Loss ratio
var winLossRatio float64
@@ -1116,9 +1213,25 @@ func (e *StrategyEngine) BuildUserPrompt(ctx *Context) string {
}
sb.WriteString("\n")
// Get language for market data formatting
nofxosLang := nofxos.LangEnglish
if e.GetLanguage() == LangChinese {
nofxosLang = nofxos.LangChinese
}
// OI Ranking data (market-wide open interest changes)
if ctx.OIRankingData != nil {
sb.WriteString(provider.FormatOIRankingForAI(ctx.OIRankingData))
sb.WriteString(nofxos.FormatOIRankingForAI(ctx.OIRankingData, nofxosLang))
}
// NetFlow Ranking data (market-wide fund flow)
if ctx.NetFlowRankingData != nil {
sb.WriteString(nofxos.FormatNetFlowRankingForAI(ctx.NetFlowRankingData, nofxosLang))
}
// Price Ranking data (market-wide gainers/losers)
if ctx.PriceRankingData != nil {
sb.WriteString(nofxos.FormatPriceRankingForAI(ctx.PriceRankingData, nofxosLang))
}
sb.WriteString("---\n\n")
+4 -12
View File
@@ -3,6 +3,7 @@ package kernel
import (
"fmt"
"nofx/market"
"nofx/provider/nofxos"
"sort"
"strings"
"time"
@@ -89,11 +90,11 @@ func formatContextData(ctx *Context, lang Language) string {
// 7. OI排名数据(如果有)
if ctx.OIRankingData != nil {
nofxosLang := nofxos.LangEnglish
if lang == LangChinese {
sb.WriteString(formatOIRankingZH(ctx.OIRankingData))
} else {
sb.WriteString(formatOIRankingEN(ctx.OIRankingData))
nofxosLang = nofxos.LangChinese
}
sb.WriteString(nofxos.FormatOIRankingForAI(ctx.OIRankingData, nofxosLang))
}
return sb.String()
@@ -354,11 +355,6 @@ func formatKlineDataZH(symbol string, tfData map[string]*market.TimeframeSeriesD
return sb.String()
}
// formatOIRankingZH 格式化OI排名数据(中文)
func formatOIRankingZH(oiData interface{}) string {
// TODO: 根据实际OIRankingData结构实现
return "## 市场持仓量排名\n\n(数据加载中...)\n\n"
}
// getOIInterpretationZH 获取OI变化解读(中文)
func getOIInterpretationZH(oiChange, priceChange string) string {
@@ -624,10 +620,6 @@ func formatKlineDataEN(symbol string, tfData map[string]*market.TimeframeSeriesD
return sb.String()
}
// formatOIRankingEN 格式化OI排名数据(英文)
func formatOIRankingEN(oiData interface{}) string {
return "## Market-wide OI Ranking\n\n(Loading data...)\n\n"
}
// getOIInterpretationEN 获取OI变化解读(英文)
func getOIInterpretationEN(oiChange, priceChange string) string {