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.
105 lines
3.5 KiB
105 lines
3.5 KiB
package trade |
|
|
|
import ( |
|
"sig-pub/pkg/data" |
|
"sig-pub/pkg/types" |
|
) |
|
|
|
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 float64 // 持仓量 |
|
Leverage int32 // 杠杆倍数 |
|
EntryPx float64 // 入场价格(均价) |
|
EntryTs int64 // 入场时间 |
|
PeakPx float64 // 持仓最高价格(空单最低价格) |
|
} |
|
|
|
// TradeOrder 交易订单 |
|
type TradeOrder struct { |
|
TradeId int64 // 交易单id |
|
TradeType TradeType // 交易类型: 1.开仓 2.平仓 |
|
InstId string // 交易产品id |
|
Side types.Side // 交易方向 |
|
Qty float64 // 交易量 |
|
Price float64 // 交易价格 |
|
Fee float64 // 手续费 |
|
Leverage int32 // 杠杆倍数 |
|
Ctime int64 // 交易时间 |
|
Status data.Status // 1.交易成功 2.交易中 4.交易失败 |
|
PeakPx float64 // 持仓最高价格(空单最低价格) |
|
// 平仓单信息 |
|
Cash float64 // 平仓后账户净值 |
|
HoldTime string // 持仓时间 |
|
Pnl float64 // 浮盈 |
|
Profit float64 // 利润 |
|
EntryPx float64 // 开仓价格(均价) |
|
EntryFee float64 // 开仓手续费(总计) |
|
EntryTs int64 // 开仓时间 |
|
Trades []int64 // 关联的平仓交易单 |
|
} |
|
|
|
type TradeType int32 |
|
|
|
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 // 当前持有相同方向单时 |
|
}
|
|
|