- 新增 TrendFilter 信号记录(enter/exit),按完整信号和高分信号两档分类 - 信号持久化到 data/trend_signals_cache.json,开机自动恢复 - 新增 /api/trend-signals API + SSE trend_signal 实时广播 - 前端新增完整信号卡片和高分信号卡片,移除旧趋势检测卡片 - 评分加入 1h 涨跌方向和实时 drift 惩罚,下跌币不触发信号 - OKX 交易所支持(累积变动、动量、趋势检测) - 修复 trend_filter.go 编译错误 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
65 lines
1.8 KiB
Go
65 lines
1.8 KiB
Go
package db
|
|
|
|
import "time"
|
|
|
|
// TrendSignalRecord mirrors the trend_signals table row.
|
|
type TrendSignalRecord struct {
|
|
ID int64
|
|
Coin string
|
|
Type string // "enter" or "exit"
|
|
SignalScore *float64
|
|
Price *float64
|
|
EMA52 *float64
|
|
EMASlope *float64
|
|
VolumeRatio *float64
|
|
Range24h *float64
|
|
VolBaseline *float64
|
|
PriceAboveEMA bool
|
|
State *string
|
|
CreatedAt time.Time
|
|
}
|
|
|
|
// SaveTrendSignal inserts a new trend signal record.
|
|
func (d *DB) SaveTrendSignal(s *TrendSignalRecord) (int64, error) {
|
|
pa := 0
|
|
if s.PriceAboveEMA {
|
|
pa = 1
|
|
}
|
|
res, err := d.Exec(`INSERT INTO trend_signals
|
|
(coin, type, signal_score, price, ema_52, ema_slope, volume_ratio,
|
|
range_24h, vol_baseline, price_above_ema, state, created_at)
|
|
VALUES (?,?,?,?,?,?,?,?,?,?,?,?)`,
|
|
s.Coin, s.Type, s.SignalScore, s.Price, s.EMA52, s.EMASlope, s.VolumeRatio,
|
|
s.Range24h, s.VolBaseline, pa, s.State, s.CreatedAt,
|
|
)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return res.LastInsertId()
|
|
}
|
|
|
|
// GetTrendSignals returns the most recent N trend signal records.
|
|
func (d *DB) GetTrendSignals(limit int) ([]TrendSignalRecord, error) {
|
|
rows, err := d.Query(`SELECT id, coin, type, signal_score, price, ema_52, ema_slope,
|
|
volume_ratio, range_24h, vol_baseline, price_above_ema, state, created_at
|
|
FROM trend_signals ORDER BY id DESC LIMIT ?`, limit)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
var signals []TrendSignalRecord
|
|
for rows.Next() {
|
|
var s TrendSignalRecord
|
|
var pa int
|
|
if err := rows.Scan(&s.ID, &s.Coin, &s.Type, &s.SignalScore, &s.Price,
|
|
&s.EMA52, &s.EMASlope, &s.VolumeRatio, &s.Range24h, &s.VolBaseline,
|
|
&pa, &s.State, &s.CreatedAt); err != nil {
|
|
return nil, err
|
|
}
|
|
s.PriceAboveEMA = pa == 1
|
|
signals = append(signals, s)
|
|
}
|
|
return signals, rows.Err()
|
|
}
|