You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

209 lines
6.7 KiB

package backtest
import (
"sig-pub/pkg/trade"
"sig-pub/pkg/types"
"sig-pub/pkg/types/decimals"
)
// CloseManager 管理持仓平仓逻辑:stoploss/takeprofit 与 基于信号的平仓
// 1.交易信号和持单方向相反时是否进行平仓
// 2.计算止盈止损时是否包含手续费
type CloseManager struct {
StopLossPct float64 // static stoploss
TakeProfitPct float64 // static take profit
TrailingPct float64 // trailing stop percent (e.g. 0.02 = 2%);移动止损百分比(例如 0.02 表示从最高价回撤 2% 时触发追踪止损)。
MinProfitToTrail float64 // minimum profit (fraction) before trailing activates (e.g. 0.01 = 1%);启动移动止损的最小盈利阈值(例如达到 1% 后才开始追踪)。
ProfitRetracePct float64 // close when profit retraces more than this fraction of peak profit;基于最高利润回撤触发平仓(例如从最高利润回撤超过 30% 则平仓)。
}
func NewCloseManager(stopLossPct, takeProfitPct float64) *CloseManager {
return &CloseManager{StopLossPct: stopLossPct, TakeProfitPct: takeProfitPct}
}
func (m *CloseManager) SetDynamicParams(trailingPct, minProfitToTrail, profitRetracePct float64) {
m.TrailingPct = trailingPct
m.MinProfitToTrail = minProfitToTrail
m.ProfitRetracePct = profitRetracePct
}
func (m *CloseManager) Init(param trade.CloseStrategyParam) {
}
// OnKline 根据最新 kline 检查是否触发 stoploss 或 takeprofit,触发则平仓(市价)
// 返回发生的平仓成交记录
func (m *CloseManager) OnKline(k types.Kline, acct *Account) (trades []*Trade) {
if acct == nil {
return
}
if (m.StopLossPct <= 0) && (m.TakeProfitPct <= 0) {
return
}
// collect indices to close to avoid modifying slice during iteration
type closeTask struct {
tradeId int64
cause string
}
var toClose []closeTask
closePrice := decimals.MustToFloat64(k.Close)
for _, p := range acct.Positions {
if p == nil {
continue
}
entry := p.EntryPx
// update peak px
switch p.Side {
case types.SideLong:
if closePrice > p.PeakPx {
p.PeakPx = closePrice
}
case types.SideShort:
if closePrice < p.PeakPx {
p.PeakPx = closePrice
}
}
// 固定止盈止损
if p.Side == types.SideLong {
// stoploss
if m.StopLossPct > 0 && closePrice <= entry*(1-m.StopLossPct) {
acct.Stat.StoplossTimes++
toClose = append(toClose, closeTask{tradeId: p.TradeId, cause: "stoploss"})
continue
}
// takeprofit
// if m.TakeProfitPct > 0 && priceHigh >= entry*(1+m.TakeProfitPct) {
// acct.Stat.TakeprofitTimes++
// toClose = append(toClose, closeTask{tradeId: p.TradeId, cause: "takeprofit"})
// continue
// }
// dynamic trailing stop based on peak price
if m.TrailingPct > 0 && m.MinProfitToTrail > 0 {
// peak profit fraction
peakProfit := (p.PeakPx - entry) / entry
if peakProfit >= m.MinProfitToTrail { // 最高盈利百分比
// trailing level
// trailLevel := p.PeakPx * (1 - m.TrailingPct)
// if closePrice <= trailLevel {
trail := (p.PeakPx - entry) * (1 - m.TrailingPct)
if (closePrice - entry) < trail {
acct.Stat.TrailingTimes++
toClose = append(toClose, closeTask{tradeId: p.TradeId, cause: "trailing"})
continue
}
}
}
// profit retrace rule: if peakProfit>0 and current retrace > ProfitRetracePct
// if m.ProfitRetracePct > 0 {
// peakProfit := (p.PeakPx - entry) / entry
// curProfit := (closePrice - entry) / entry
// if peakProfit > 0 {
// retrace := (peakProfit - curProfit) / peakProfit
// if retrace >= m.ProfitRetracePct {
// acct.Stat.RetraceTimes++
// toClose = append(toClose, closeTask{tradeId: p.TradeId, cause: "retrace"})
// continue
// }
// }
// }
} else if p.Side == types.SideShort {
// short: stoploss if high >= entry*(1+stop), takeprofit if low <= entry*(1-tp)
if m.StopLossPct > 0 && closePrice >= entry*(1+m.StopLossPct) {
acct.Stat.StoplossTimes++
toClose = append(toClose, closeTask{tradeId: p.TradeId, cause: "stoploss"})
continue
}
// if m.TakeProfitPct > 0 && priceLow <= entry*(1-m.TakeProfitPct) {
// acct.Stat.TakeprofitTimes++
// toClose = append(toClose, closeTask{tradeId: p.TradeId, cause: "takeprofit"})
// continue
// }
// update trailing for short based on PeakPx (lower is better for short)
if m.TrailingPct > 0 && m.MinProfitToTrail > 0 {
peakProfit := (entry - p.PeakPx) / entry
if peakProfit >= m.MinProfitToTrail {
// trailLevel := p.PeakPx * (1 + m.TrailingPct)
// if closePrice >= trailLevel {
trail := (entry - p.PeakPx) * (1 - m.TrailingPct)
if (entry - closePrice) < trail {
acct.Stat.TrailingTimes++
toClose = append(toClose, closeTask{tradeId: p.TradeId, cause: "trailing"})
continue
}
}
}
// if m.ProfitRetracePct > 0 {
// peakProfit := (entry - p.PeakPx) / entry
// curProfit := (entry - closePrice) / entry
// if peakProfit > 0 {
// retrace := (peakProfit - curProfit) / peakProfit
// if retrace >= m.ProfitRetracePct {
// acct.Stat.RetraceTimes++
// toClose = append(toClose, closeTask{tradeId: p.TradeId, cause: "retrace"})
// continue
// }
// }
// }
}
}
// close collected positions (process from high index to low to safely remove)
for j := len(toClose) - 1; j >= 0; j-- {
tradeId := toClose[j].tradeId
cause := toClose[j].cause
var pos *Position
var index int
for i, p := range acct.Positions {
if p.TradeId == tradeId {
pos = p
index = i
break
}
}
if pos == nil {
return
}
// perform market close: side opposite
tr, ok := acct.ClosePosition(index, pos, k, k.Ts, cause)
if ok {
trades = append(trades, tr)
}
}
return
}
// CloseBySignal 根据策略信号尝试平掉相反方向的仓位。例如策略返回 SELL 时,尝试平掉所有 BUY 持仓
func (m *CloseManager) CloseBySignal(sigSide types.Side, acct *Account, k types.Kline) (trades []*Trade) {
if acct == nil {
return
}
// determine which positions to close: positions with opposite side to sigSide
type closeTask struct {
idx int
cause string
pos *Position
}
var toClose []closeTask
for i, p := range acct.Positions {
if p == nil {
continue
}
if sigSide == types.SideLong && p.Side == types.SideShort {
toClose = append(toClose, closeTask{idx: i, cause: "signal", pos: p})
} else if sigSide == types.SideShort && p.Side == types.SideLong {
toClose = append(toClose, closeTask{idx: i, cause: "signal", pos: p})
}
}
for j := len(toClose) - 1; j >= 0; j-- {
idx := toClose[j].idx
cause := toClose[j].cause
tr, ok := acct.ClosePosition(idx, toClose[j].pos, k, k.Ts, cause)
if ok {
trades = append(trades, tr)
}
}
return
}