package trade import ( "sig-pub/pkg/data" "sig-pub/pkg/types" "github.com/govalues/decimal" "github.com/lib/pq" ) type Cause int32 const ( // _ Cause = iota CauseCloseForced Cause = 1001 // 平仓:强制平仓 CauseCloseStoploss Cause = 1002 // 平仓:固定止损 CauseCloseTakeprofit Cause = 1003 // 平仓:固定止盈 CauseCloseReverseSingal Cause = 1004 // 平仓:策略反向信号 CauseCloseTrailing Cause = 1005 // 平仓:移动止损基于最高利润点回撤百分比 CauseRiskAlreadyTrade Cause = 2001 // 风控:已有持仓 CauseRiskSideAlreadyTrade Cause = 2002 // 风控:相同方向已有持仓 ) func (c Cause) String() string { switch c { default: return "" case CauseCloseForced: return "closeForced" case CauseCloseStoploss: return "closeStoploss" case CauseCloseTakeprofit: return "closeTakeprofit" case CauseCloseReverseSingal: return "closeReverseSingal" case CauseCloseTrailing: return "closeTrailing" case CauseRiskAlreadyTrade: return "riskAlreadyTrade" case CauseRiskSideAlreadyTrade: return "riskSideAlreadyTrade" } } // Position 持仓仓位 type Position struct { InstId string // 交易产品id Side types.Side // 交易方向 Qty decimal.Decimal // 持仓量 Leverage int32 // 杠杆倍数 EntryPx float64 // 入场价格(均价) EntryTs int64 // 入场时间 PeakPx float64 // 持仓最高价格(空单最低价格) LastPx float64 // 最后更新价格 } // TradeOrder 交易订单 type TradeOrder struct { BacktestId int64 `jsno:"backtestId" gorm:"column:backtest_id"` // 回测单id TradeId int64 `jsno:"tradeId" gorm:"column:trade_id"` // 交易单id TradeType TradeType `jsno:"tradeType" gorm:"column:trade_type"` // 交易类型: 1.开仓 2.平仓 InstId string `jsno:"instId" gorm:"column:inst_id"` // 交易产品id Side types.Side `jsno:"side" gorm:"column:side"` // 交易方向 Qty decimal.Decimal `jsno:"qty" gorm:"column:qty"` // 交易量 Price float64 `jsno:"price" gorm:"column:price"` // 交易价格 Fee float64 `jsno:"fee" gorm:"column:fee"` // 手续费 Leverage int32 `jsno:"leverage" gorm:"column:leverage"` // 杠杆倍数 Ctime int64 `jsno:"ctime" gorm:"column:ctime"` // 交易时间 Status data.Status `jsno:"status" gorm:"column:status"` // 1.交易成功 2.交易中 4.交易失败 PeakPx float64 `jsno:"peakPx" gorm:"column:peak_px"` // 持仓最高价格(空单最低价格) // ----------------- 平仓单信息 CloseCause Cause `json:"closeCause" gorm:"column:close_cause"` // 平仓原因 ["stoploss", "takeprofit", "trailing", "retrace", "signal"](“止损”、“止盈”、“动态跟踪”、“回撤”、“信号”) Equity float64 `json:"equity" gorm:"column:equity"` // 平仓后账户净值 HoldTime string `json:"holdTime" gorm:"column:hold_time"` // 持仓时间 Profit float64 `json:"profit" gorm:"column:profit"` // 利润 LastPx float64 `jsno:"lastPx" gorm:"column:last_px"` // 最后更新价格 EntryPx float64 `json:"entryPx" gorm:"column:entry_px"` // 开仓价格(均价) EntryFee float64 `json:"entryFee" gorm:"column:entry_fee"` // 开仓手续费(总计) EntryTime int64 `json:"entryTime" gorm:"column:entry_time"` // 开仓时间(最早) Trades pq.Int64Array `json:"trades" gorm:"column:trades;type:int8[]"` // 关联的平仓交易单 // Pnl float64 `json:"pnl" gorm:"column:pnl"` // 浮盈 // TimeK int64 `json:"timeK" gorm:"column:time_k"` // 开仓K线时间 // CloseTimeK int64 `json:"closeTimeK" gorm:"column:close_time_k"` // 平仓K线时间 } func (TradeOrder) TableName() string { return "t_backtest_trading_order" } type TradeType int16 const ( TradeTypeOpen TradeType = 1 // 开仓 TradeTypeClose TradeType = 2 // 平仓 ) // TradeStrategyInput 交易策略参数 // Kelly准则优化方法 type TradeStrategyInput struct { MaxPosPct float64 // 单笔交易最大仓位占比 MaxExposurePct float64 // 最大总敞口占比 MaxLots float64 // 最大手数/数量 (optional) } // CloseStrategyInput 平仓策略参数 type CloseStrategyInput struct { StopLossPct float64 `json:"stopLossPct"` // 固定止损 static stoploss TakeProfitPct float64 `json:"takeProfitPct"` // 固定止盈 static take profit ProfitRetracePcts [][]float64 `json:"profitRetracePcts"` // 基于最高利润回撤触发平仓 (例如 [[0.01, 0.3], [0.02, 0.2]] 最高利润超过1%时30%回撤则触发平仓,最高利润超过2%时20%回撤就触发平仓) CloseOnSideReverse bool `json:"closeOnSideReverse"` // 交易信号和持单方向相反时是否进行平仓 Fee bool `json:"fee"` // 计算止盈止损时是否包含手续费 } // RiskStrategyInput 风险管理策略测试 type RiskStrategyInput struct { SkipOnSideOpposite bool // 当前持有反方向单时 SkipOnSideSame bool // 当前持有相同方向单时 }