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.
158 lines
8.4 KiB
158 lines
8.4 KiB
package trade |
|
|
|
import ( |
|
"sig-pub/api/pb" |
|
"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 // 最后更新价格 |
|
} |
|
|
|
// BacktestTradingPlan 交易计划回测结果 |
|
type BacktestTradingPlan struct { |
|
Id int64 `json:"id" gorm:"column:id;primaryKey"` // 测试id |
|
UserId int64 `json:"userId" gorm:"column:user_id"` // 用户id |
|
PlanId int64 `json:"planId" gorm:"column:plan_id"` // 交易计划id |
|
InstId string `json:"instId" gorm:"column:inst_id"` // 交易产品id |
|
Exchange pb.ExchangeType `json:"exchange" gorm:"column:exchange"` // 交易所 |
|
Interval string `json:"interval" gorm:"column:interval"` // 交易周期 |
|
SeriesBefore int64 `json:"seriesBefore" gorm:"column:series_before"` // 回测周期开始时间 |
|
SeriesAfter int64 `json:"seriesAfter" gorm:"column:series_after"` // 回测周期结束时间 |
|
Ctime int64 `json:"ctime" gorm:"column:ctime"` // 创建时间(测试时间) |
|
Etime int64 `json:"etime" gorm:"column:etime"` // 测试结束时间 |
|
Cash float64 `json:"cash" gorm:"column:cash"` // 起始金额 |
|
EndCash float64 `json:"endCash" gorm:"column:end_cash"` // 结束金额 |
|
Profit float64 `json:"profit" gorm:"column:profit"` // 利润 |
|
Singals int `json:"singals" gorm:"column:singals"` // 交易信号数 |
|
TotalTrades int `json:"totalTrades" gorm:"column:total_trades"` // 总单数 |
|
WinningTrades int `json:"winningTrades" gorm:"column:winning_trades"` // 盈利单数 |
|
LosingTrades int `json:"losingTrades" gorm:"column:losing_trades"` // 亏损单数 |
|
Fee float64 `json:"fee" gorm:"column:fee"` // 总手续费 |
|
MaxDrawdown float64 `json:"maxDrawdown" gorm:"column:max_drawdown"` // 最大回撤 |
|
SharpeRatio float64 `json:"sharpeRatio" gorm:"column:sharpe_ratio"` // 夏普比率 |
|
SortinoRatio float64 `json:"sortinoRatio" gorm:"column:sortino_ratio"` // 索提诺比率 |
|
CalmarRatio float64 `json:"calmarRatio" gorm:"column:calmar_ratio"` // 卡尔玛比率 |
|
ProfitFactor float64 `json:"profitFactor" gorm:"column:profit_factor"` // 盈利因子 |
|
WinRate float64 `json:"winRate" gorm:"column:win_rate"` // 胜率 |
|
Trades []*TradeOrder `json:"-" gorm:"-"` // 回测交易单 |
|
RunningStatus int32 `json:"-" gorm:"-"` // 运行中回测状态 |
|
RunningTaskId string `json:"-" gorm:"-"` // 运行中回测任务id |
|
RunningProgress int64 `json:"-" gorm:"-"` |
|
RunningTotal int64 `json:"-" gorm:"-"` |
|
RunningPct string `json:"-" gorm:"-"` |
|
} |
|
|
|
func (BacktestTradingPlan) TableName() string { |
|
return "t_backtest_trading_plan" |
|
} |
|
|
|
// TradeOrder 交易订单 |
|
type TradeOrder struct { |
|
BacktestId int64 `json:"backtestId" gorm:"column:backtest_id"` // 回测单id |
|
TradeId int64 `json:"tradeId" gorm:"column:trade_id"` // 交易单id |
|
TradeType TradeType `json:"tradeType" gorm:"column:trade_type"` // 交易类型: 1.开仓 2.平仓 |
|
InstId string `json:"instId" gorm:"column:inst_id"` // 交易产品id |
|
Side types.Side `json:"side" gorm:"column:side"` // 交易方向 |
|
Qty decimal.Decimal `json:"qty" gorm:"column:qty"` // 交易量 |
|
Price float64 `json:"price" gorm:"column:price"` // 交易价格 |
|
Fee float64 `json:"fee" gorm:"column:fee"` // 手续费 |
|
Leverage int32 `json:"leverage" gorm:"column:leverage"` // 杠杆倍数 |
|
Ctime int64 `json:"ctime" gorm:"column:ctime"` // 交易时间 |
|
Status data.Status `json:"status" gorm:"column:status"` // 1.交易成功 2.交易中 4.交易失败 |
|
PeakPx float64 `json:"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 // 当前持有相同方向单时 |
|
}
|
|
|