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.
57 lines
1.9 KiB
57 lines
1.9 KiB
package strategy |
|
|
|
import ( |
|
"fmt" |
|
"sig-pub/api/pb" |
|
"sig-pub/pkg/indicator" |
|
"sig-pub/pkg/types" |
|
"sig-pub/pkg/types/series" |
|
"sig-pub/pkg/utils/collect" |
|
"strings" |
|
) |
|
|
|
// ISigStrategy 交易信号策略接口 |
|
type ISigStrategy interface { |
|
New() ISigStrategy |
|
Meta() StrategyMeta |
|
Update(ctx ISigStrategyContext) |
|
} |
|
|
|
// todo Meta 策略调参, 回测引擎自动调参回测(最佳参数) argGenerator.next() (arg, ok) |
|
// argA range -> [1,...,5], argB:=[0.1,...,0.7], argC:=[true,false] |
|
// 可变参数组合 argValidate(argA, ArgB, argC...) bool(true则使用该组合进行回测,记录组合参数回测结果) |
|
type ISigStrategyAdjustable interface { |
|
ISigStrategy |
|
NextParams() map[string]any // 根据当前策略参数, 返回下一批策略参数(并行回测 stateless) |
|
AdjustParams(map[string]any) // 重置策略设置策略参数 |
|
} |
|
|
|
type StrategyMeta struct { |
|
// Id string `json:"id"` // 策略注册/执行器系统分配 |
|
Name string |
|
Desc string |
|
} |
|
|
|
// ISigStrategyContext 策略外部访问能力 |
|
// klineSeries, Indicator |
|
type ISigStrategyContext interface { |
|
Buy() // 发出多信号 |
|
Sell() // 发出空信号 |
|
|
|
// Get [0]当前k线 |
|
Get(offset int16) types.Kline |
|
// Series [offset...end] |
|
Series(offset, count int16) (klines series.Klines) |
|
// 获取窗口类型指标 |
|
IndicatorW(name string, window int16) indicator.IIndicatorSeries |
|
} |
|
|
|
// DriverIntervalKey 生成周期驱动事件key |
|
// interval/BTC_USDT/OKX,BINANCE/1m,3m,5m |
|
func DriverIntervalKey(instId string, intervals []types.Interval, exchanges ...pb.ExchangeType) string { |
|
types.IntervalsSort(intervals) |
|
strIntervals := collect.Mapping(intervals, func(_ int, interval types.Interval) string { return string(interval) }) |
|
strExchanges := collect.Mapping(exchanges, func(_ int, exchange pb.ExchangeType) string { return exchange.String() }) |
|
pubKey := fmt.Sprintf("/interval/%s/%s/%s", instId, strings.Join(strExchanges, ","), strings.Join(strIntervals, ",")) |
|
return pubKey |
|
}
|
|
|