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.
84 lines
2.4 KiB
84 lines
2.4 KiB
package strategy |
|
|
|
import ( |
|
"fmt" |
|
"sig-pub/pkg/utils/collect" |
|
) |
|
|
|
// 指标注册器 |
|
type SigStrategyRegistry struct { |
|
sigStrategies *collect.SyncMap[string, SigStrategyType] // 注册策略 |
|
singleSigStrategies *collect.SyncMap[string, ISingleSigStrategy] // 单周期策略 |
|
intervalSigStrategies *collect.SyncMap[string, IIntervalSigStrategy] // 多周期策略 |
|
} |
|
|
|
func NewSigStrategyRegistry() *SigStrategyRegistry { |
|
return &SigStrategyRegistry{ |
|
sigStrategies: collect.NewSyncMap[string, SigStrategyType](), |
|
singleSigStrategies: collect.NewSyncMap[string, ISingleSigStrategy](), |
|
intervalSigStrategies: collect.NewSyncMap[string, IIntervalSigStrategy](), |
|
} |
|
} |
|
|
|
func (r *SigStrategyRegistry) Init() (err error) { |
|
// indicator regist |
|
r.MustRegistStrategy(&GoldX{}) |
|
r.MustRegistStrategy(&SupertrendBOSWaves{}) |
|
r.MustRegistStrategy(&CrossStar{}) |
|
r.MustRegistStrategy(&SuperTrendRSI{}) |
|
r.MustRegistStrategy(&SuperTrend2Macd{}) |
|
r.MustRegistStrategy(&SuperTrendMacdRSI{}) |
|
return |
|
} |
|
|
|
// RegisterStrategy |
|
func (r *SigStrategyRegistry) RegistStrategy(strategy ISigStrategy) (err error) { |
|
var sigType SigStrategyType |
|
if _, ok := strategy.(ISingleSigStrategy); ok { |
|
sigType = SigStrategyTypeSingle |
|
} else if _, ok := strategy.(IIntervalSigStrategy); ok { |
|
sigType = SigStrategyTypeInterval |
|
} else { |
|
err = fmt.Errorf("sig strategy must be one of the [ISingleSigStrategy, IIntervalSigStrategy]") |
|
return |
|
} |
|
|
|
strategyName := strategy.Meta().Name |
|
_, loaded := r.sigStrategies.LoadOrStore(strategyName, sigType) |
|
if loaded { |
|
err = fmt.Errorf("strategy name %s duplicated", strategyName) |
|
return |
|
} |
|
|
|
switch sigType { |
|
case SigStrategyTypeSingle: |
|
r.singleSigStrategies.Store(strategyName, strategy.(ISingleSigStrategy)) |
|
case SigStrategyTypeInterval: |
|
r.intervalSigStrategies.Store(strategyName, strategy.(IIntervalSigStrategy)) |
|
} |
|
return |
|
} |
|
|
|
func (r *SigStrategyRegistry) MustRegistStrategy(strategy ISigStrategy) { |
|
if err := r.RegistStrategy(strategy); err != nil { |
|
panic(err) |
|
} |
|
} |
|
|
|
// NewSigStrategy |
|
func (r *SigStrategyRegistry) NewSigStrategy(strategyName string) (sigType SigStrategyType, strategy ISigStrategy, ok bool) { |
|
sigType, ok = r.sigStrategies.Load(strategyName) |
|
if !ok { |
|
return |
|
} |
|
switch sigType { |
|
case SigStrategyTypeSingle: |
|
strategy, ok = r.singleSigStrategies.Load(strategyName) |
|
case SigStrategyTypeInterval: |
|
strategy, ok = r.intervalSigStrategies.Load(strategyName) |
|
} |
|
if ok { |
|
strategy = strategy.New() |
|
} |
|
return |
|
}
|
|
|