package strategy import ( "fmt" "sig-pub/pkg/types" "github.com/spf13/cast" ) // 参数类型 type ParamType int8 const ( _ ParamType = iota ParamTypeBool ParamTypeFloat ParamTypeUFloat ParamTypeInt ParamTypeUInt ParamTypeString ParamTypeSelect // 单选 ParamTypeCheckBox // 多选 ) type Param struct { Name string `json:"name"` Desc string `json:"desc"` Type ParamType `json:"type"` // 参数类型 Options []ParamOption `json:"options"` // 单选/多选选项列表 } type ParamOption struct { Name string `json:"name"` Desc string `json:"desc"` } // CastValidate 数据类型校验 func (t Param) TypeValidate(v string) bool { switch t.Type { default: return false case ParamTypeBool: if _, e := cast.ToBoolE(v); e != nil { return false } return true case ParamTypeString: return true case ParamTypeInt: fallthrough case ParamTypeUInt: if r, e := cast.ToIntE(v); e != nil { return false } else if t.Type == ParamTypeUInt { return r >= 0 } return true case ParamTypeFloat: fallthrough case ParamTypeUFloat: if r, e := cast.ToFloat64E(v); e != nil { return false } else if t.Type == ParamTypeUFloat { return r >= 0 } return true } } // 策略默认参数 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 SigStrategyParam struct { Interval types.Interval `json:"interval"` // 策略驱动周期 Param map[string]string `json:"param"` // 策略执行参数 } func (s *SigStrategyParam) Get(key string) (v string, ok bool) { if len(s.Param) == 0 { return } v, ok = s.Param[key] return } func (s *SigStrategyParam) 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 *SigStrategyParam) GetInt16E(key string) (r int16, err error) { v, ok := s.Get(key) if !ok { err = fmt.Errorf("param %s not provided", key) return } r, err = cast.ToInt16E(v) return } func (s *SigStrategyParam) GetFloat64(key string) (r float64, ok bool) { v, ok := s.Get(key) if !ok { return } r, err := cast.ToFloat64E(v) if ok = err == nil; !ok { return } return } func (s *SigStrategyParam) 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 }