6 changed files with 92 additions and 94 deletions
@ -0,0 +1,60 @@ |
|||||||
|
package trade |
||||||
|
|
||||||
|
import ( |
||||||
|
"sig-pub/pkg/strategy" |
||||||
|
"sig-pub/pkg/types" |
||||||
|
|
||||||
|
"github.com/govalues/decimal" |
||||||
|
) |
||||||
|
|
||||||
|
// ITradeAnalyzer 建仓到平仓分析器
|
||||||
|
type ITradeAnalyzer interface { |
||||||
|
IEntryAnalyzer |
||||||
|
IExitAnalyzer |
||||||
|
|
||||||
|
// Init 校验参数, 并根据参数初始化策略
|
||||||
|
Init(input types.Input) (err error) |
||||||
|
} |
||||||
|
|
||||||
|
// IEntryAnalyzer 建仓下单策略
|
||||||
|
type IEntryAnalyzer interface { |
||||||
|
strategy.ISigStrategy |
||||||
|
|
||||||
|
// 需要的各周期最小数据k线数
|
||||||
|
CandlePeriods(ctx strategy.IInstanceIntervalSigStrategyContext) (tradeInsts []string, iPeriods *types.IntervalState[int16]) |
||||||
|
|
||||||
|
// EntryAnalysis 生成下单参数(交易量/方向/杠杆) 建仓分析:确定是否入场、下单价格及仓位大小
|
||||||
|
// 币种持仓中不能改变杠杆
|
||||||
|
// tickets 建仓单信息
|
||||||
|
// skipCause 跳过交易信号原因
|
||||||
|
EntryAnalysis(ctx strategy.IInstanceIntervalSigStrategyContext, account ITradeAccount, sigInstId string, sigSide types.Side) (tickets []TradeTicket, skipCause Cause, err error) |
||||||
|
} |
||||||
|
|
||||||
|
// IExitAnalyzer 平仓评估评估、止盈止损策略(trading service 管理)
|
||||||
|
type IExitAnalyzer interface { |
||||||
|
strategy.ISigStrategy |
||||||
|
|
||||||
|
// 需要的各周期最小数据k线数
|
||||||
|
CandlePeriods(ctx strategy.IInstanceIntervalSigStrategyContext) (tradeInsts []string, iPeriods *types.IntervalState[int16]) |
||||||
|
|
||||||
|
// ExitAnalysisOnPrice 价格更新评估是否平仓
|
||||||
|
// @return closeTicket平仓单信息
|
||||||
|
ExitAnalysisOnPrice(ctx strategy.IInstanceIntervalSigStrategyContext, account ITradeAccount, instId string, price float64) (closeTickets []TradeTicket, err error) |
||||||
|
|
||||||
|
// ExitAnalysisOnSig 信号触发时评估是否平仓
|
||||||
|
ExitAnalysisOnSig(ctx strategy.IInstanceIntervalSigStrategyContext, account ITradeAccount, instId string, sigSide types.Side) (closeTickets []TradeTicket, err error) |
||||||
|
} |
||||||
|
|
||||||
|
type TradeTicket struct { |
||||||
|
TradeType TradeType |
||||||
|
InstId string |
||||||
|
Side types.Side // 开仓方向
|
||||||
|
Price float64 // 开仓价格
|
||||||
|
Leverage int32 // 杠杆倍数
|
||||||
|
Qty decimal.Decimal // 交易量 qty为交易产品数量 todo decimal
|
||||||
|
Interval string // k线周期
|
||||||
|
Ktime int64 // k线时间
|
||||||
|
Ctime int64 // 创建时间(ctime-ktime=信号延迟)
|
||||||
|
Cause Cause |
||||||
|
TradesId []int64 // 关联交易订单id (仅平仓使用)
|
||||||
|
} |
||||||
@ -0,0 +1,17 @@ |
|||||||
|
package trade |
||||||
|
|
||||||
|
// 1. 基于风险敞口的策略 (Risk-Based Sizing)
|
||||||
|
// 1.1 固定风险金额模型 (Fixed Fractional Risk)
|
||||||
|
// 1.2 波动率倒数模型 (Volatility Targeting / ATR Sizing)
|
||||||
|
// 2. 基于资金占比的策略 (Equity-Based Sizing)
|
||||||
|
// 2.1 固定资金比例 (Fixed Percentage of Capital)
|
||||||
|
// 2.2 最大持仓限制 (Max Position Constraints)
|
||||||
|
// 3. 基于胜率与信号的策略 (Probability & Signal Based)
|
||||||
|
// 3.1 凯利公式 (The Kelly Criterion)
|
||||||
|
// 3.2 信号强度加权 (Signal Strength Weighting)
|
||||||
|
|
||||||
|
// 策略名称,适用场景,优点,缺点,推荐指数
|
||||||
|
// 固定风险金额,趋势跟踪、波段交易,风险恒定,极度稳健,防守性强,计算稍繁琐,需严格设止损,⭐⭐⭐⭐⭐ (首选)
|
||||||
|
// ATR波动率模型,多品种CTA、资产组合,平滑不同品种间的波动差异,对ATR参数敏感,⭐⭐⭐⭐⭐ (机构首选)
|
||||||
|
// 固定资金比例,股票长线投资,简单直观,便于执行,忽略了品种的风险差异,⭐⭐⭐
|
||||||
|
// 凯利公式,高频交易、高胜率策略,理论收益最大化,风险极高,极易破产,⭐⭐ (仅限高手)
|
||||||
@ -1,61 +0,0 @@ |
|||||||
package trade |
|
||||||
|
|
||||||
import ( |
|
||||||
"sig-pub/pkg/strategy" |
|
||||||
"sig-pub/pkg/types" |
|
||||||
|
|
||||||
"github.com/govalues/decimal" |
|
||||||
) |
|
||||||
|
|
||||||
// IRiskStrategy 风险控制接口
|
|
||||||
type IRiskStrategy interface { |
|
||||||
strategy.ISigStrategy |
|
||||||
|
|
||||||
// 需要的各周期最小数据k线数
|
|
||||||
CandlePeriods(ctx strategy.IInstanceIntervalSigStrategyContext) (tradeInsts []string, iPeriods *types.IntervalState[int16]) |
|
||||||
|
|
||||||
// RishAssess 信号风险评估, 是否进行交易
|
|
||||||
RishAssess(ctx strategy.IInstanceIntervalSigStrategyContext, account ITradeAccount, sigInstId string, sigSide types.Side) (ok bool, cause Cause, err error) |
|
||||||
} |
|
||||||
|
|
||||||
// ITradeStrategy 下单策略
|
|
||||||
type ITradeStrategy interface { |
|
||||||
strategy.ISigStrategy |
|
||||||
|
|
||||||
// 需要的各周期最小数据k线数
|
|
||||||
CandlePeriods(ctx strategy.IInstanceIntervalSigStrategyContext) (tradeInsts []string, iPeriods *types.IntervalState[int16]) |
|
||||||
|
|
||||||
// TradeAssess 生成下单参数(交易量/方向/杠杆)
|
|
||||||
// 控制滑点, 仓位管理
|
|
||||||
// 持仓中币种不能改变杠杆
|
|
||||||
TradeAssess(ctx strategy.IInstanceIntervalSigStrategyContext, account ITradeAccount, sigInstId string, sigSide types.Side) (tickets []TradeTicket, err error) |
|
||||||
} |
|
||||||
|
|
||||||
// Exit 止盈止损策略(trading service 管理)
|
|
||||||
type ICloseStrategy interface { |
|
||||||
strategy.ISigStrategy |
|
||||||
|
|
||||||
// 需要的各周期最小数据k线数
|
|
||||||
CandlePeriods(ctx strategy.IInstanceIntervalSigStrategyContext) (tradeInsts []string, iPeriods *types.IntervalState[int16]) |
|
||||||
|
|
||||||
// CloseAssess 价格更新评估是否平仓
|
|
||||||
// @return closeTicket平仓单信息
|
|
||||||
CloseAssessOnPrice(ctx strategy.IInstanceIntervalSigStrategyContext, account ITradeAccount, instId string, price float64) (closeTickets []TradeTicket, err error) |
|
||||||
|
|
||||||
// CloseAssessOnSig 信号触发时评估是否平仓
|
|
||||||
CloseAssessOnSig(ctx strategy.IInstanceIntervalSigStrategyContext, account ITradeAccount, instId string, sigSide types.Side) (closeTickets []TradeTicket, err error) |
|
||||||
} |
|
||||||
|
|
||||||
type TradeTicket struct { |
|
||||||
TradeType TradeType |
|
||||||
InstId string |
|
||||||
Side types.Side // 开仓方向
|
|
||||||
Price float64 // 开仓价格
|
|
||||||
Leverage int32 // 杠杆倍数
|
|
||||||
Qty decimal.Decimal // 交易量 qty为交易产品数量 todo decimal
|
|
||||||
Interval string // k线周期
|
|
||||||
Ktime int64 // k线时间
|
|
||||||
Ctime int64 // 创建时间(ctime-ktime=信号延迟)
|
|
||||||
Cause Cause |
|
||||||
TradesId []int64 // 关联交易订单id (仅平仓使用)
|
|
||||||
} |
|
||||||
Loading…
Reference in new issue