Files
coin/internal/models/spot_position.go
2026-05-07 09:56:21 +08:00

58 lines
2.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Package models 中的现货持仓表模型,由 logic 包读写;表名 spot_positions。
package models
import (
"time"
"git.apinb.com/bsm-sdk/core/database"
)
// SpotPosition 现货策略在库中的一行:按 BaseAsset 唯一,对应配置 SpotWatchList 里每个基础资产。
type SpotPosition struct {
ID uint `gorm:"primarykey"`
CreatedAt time.Time
UpdatedAt time.Time
// BaseAsset 基础资产代码,如 BTC唯一索引用于 upsert 与内存 map 的键
BaseAsset string `gorm:"uniqueIndex:uk_spot_base_asset;size:32;not null"`
// Symbol 交易对,如 BTCUSDT
Symbol string `gorm:"size:32;not null"`
// AvgCostUSDT 加权平均持仓成本USDT/枚)
AvgCostUSDT float64 `gorm:"column:avg_cost_usdt;not null;default:0"`
// Quantity 策略侧同步的持仓数量(枚)
Quantity float64 `gorm:"not null;default:0"`
// DipAddsDone 本轮持仓已完成的超跌加仓次数(最多 4空仓时清零
DipAddsDone int `gorm:"column:dip_adds_done;not null;default:0"`
// DipLegLocked 超跌加仓波段锁
DipLegLocked bool `gorm:"column:dip_leg_locked;not null;default:false"`
// RallyLegLocked 历史字段,策略已不再使用,保留列以兼容已有库
RallyLegLocked bool `gorm:"column:rally_leg_locked;not null;default:false"`
// TrailArmed 浮盈已达 profitArmPct正在跟踪高点此阶段不因达线而直接平仓
TrailArmed bool `gorm:"column:trail_armed;not null;default:false"`
// TrailPeakUSDT 跟踪期内的最高价USDT/枚);未武装或未更新时为 0
TrailPeakUSDT float64 `gorm:"column:trail_peak_usdt;not null;default:0"`
// OpenReboundLow 空仓等首买时跟踪的阶段低价≤0 表示尚未锚定本轮观察
OpenReboundLow float64 `gorm:"column:open_rebound_low;not null;default:0"`
// DipReboundLow 已跌破加仓线后跟踪的阶段低价≤0 表示未进入等反弹状态(离开超跌区会清零)
DipReboundLow float64 `gorm:"column:dip_rebound_low;not null;default:0"`
}
func init() {
database.AppendMigrate(&SpotPosition{})
}
// TableName 显式表名,避免与结构体名推导不一致。
func (SpotPosition) TableName() string {
return "spot_positions"
}
// SpotPortfolioSnapshot 内存中的组合视图:从数据库加载后填充 Positions。
type SpotPortfolioSnapshot struct {
Positions map[string]*SpotPosition
}
// NewSpotPortfolioSnapshot 创建空快照。
func NewSpotPortfolioSnapshot() *SpotPortfolioSnapshot {
return &SpotPortfolioSnapshot{Positions: make(map[string]*SpotPosition)}
}