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.
90 lines
1.8 KiB
90 lines
1.8 KiB
package strategy |
|
|
|
import ( |
|
"fmt" |
|
|
|
"github.com/spf13/cast" |
|
) |
|
|
|
// 策略默认参数 |
|
type ISigStrategyDefaultParam interface { |
|
DefaultParam() map[string]string |
|
} |
|
|
|
// 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 ISigStrategyParamGenerator interface { |
|
NextParam(map[string]string) (map[string]string, bool) // 根据当前策略参数, 返回下一批策略参数(并行回测 stateless) |
|
} |
|
|
|
type StrategyParam map[string]string |
|
|
|
func (s *StrategyParam) Get(key string) (v string, ok bool) { |
|
if len(*s) == 0 { |
|
return |
|
} |
|
v, ok = (*s)[key] |
|
return |
|
} |
|
|
|
func (s *StrategyParam) GetE(key string) (v string, err error) { |
|
if len(*s) == 0 { |
|
return |
|
} |
|
v, ok := (*s)[key] |
|
if !ok { |
|
err = fmt.Errorf("param %s not provided", key) |
|
return |
|
} |
|
return |
|
} |
|
|
|
func (s *StrategyParam) GetInt(key string) (r int, ok bool) { |
|
v, ok := s.Get(key) |
|
if !ok { |
|
return |
|
} |
|
r, err := cast.ToIntE(v) |
|
if ok = err == nil; !ok { |
|
return |
|
} |
|
return |
|
} |
|
|
|
func (s *StrategyParam) GetInt16E(key string) (r int16, err error) { |
|
v, err := s.GetE(key) |
|
if err != nil { |
|
return |
|
} |
|
r, err = cast.ToInt16E(v) |
|
return |
|
} |
|
|
|
func (s *StrategyParam) GetFloat64(key string) (r float64, ok bool) { |
|
r, err := s.GetFloat64E(key) |
|
if err != nil { |
|
return |
|
} |
|
return r, true |
|
} |
|
func (s *StrategyParam) GetFloat64E(key string) (r float64, err error) { |
|
v, err := s.GetE(key) |
|
if err != nil { |
|
return |
|
} |
|
r, err = cast.ToFloat64E(v) |
|
return |
|
} |
|
|
|
func (s *StrategyParam) GetBool(key string) (r bool, ok bool) { |
|
v, ok := s.Get(key) |
|
if !ok { |
|
return |
|
} |
|
r, err := cast.ToBoolE(v) |
|
if ok = err == nil; !ok { |
|
return |
|
} |
|
return |
|
}
|
|
|