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.
 
 

51 lines
1.3 KiB

package strategy
import (
"fmt"
"sig-pub/pkg/utils/collect"
)
// 指标注册器
type SigStrategyRegistry struct {
sigStrategies *collect.SyncMap[string, ISigStrategy] // 注册信号策略
intervalSigStrategies *collect.SyncMap[string, IIntervalsSigStrategy] // 注册窗口指标
}
func NewSigStrategyRegistry() *SigStrategyRegistry {
return &SigStrategyRegistry{
sigStrategies: collect.NewSyncMap[string, ISigStrategy](),
intervalSigStrategies: collect.NewSyncMap[string, IIntervalsSigStrategy](),
}
}
func (r *SigStrategyRegistry) Init() (err error) {
// indicator regist
r.MustRegistStrategy(&GoldX{})
return
}
// RegisterStrategy
func (svc *SigStrategyRegistry) RegistStrategy(strategy ISigStrategy) (err error) {
strategyName := strategy.Meta().Name
_, loaded := svc.sigStrategies.LoadOrStore(strategyName, strategy)
if loaded {
err = fmt.Errorf("strategy name %s already duplicated", strategyName)
return
}
return
}
func (svc *SigStrategyRegistry) MustRegistStrategy(strategy ISigStrategy) {
if err := svc.RegistStrategy(strategy); err != nil {
panic(err)
}
}
// NewSigStrategy
func (r *SigStrategyRegistry) NewSigStrategy(name string) (strategy ISigStrategy, ok bool) {
strategy, ok = r.sigStrategies.Load(name)
if ok {
strategy = strategy.New()
}
return
}