move and rename isSymbolBasedStrategy

This commit is contained in:
c9s 2022-06-29 18:51:22 +08:00
parent 3013eeccc7
commit cf0ca70d24
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
4 changed files with 24 additions and 20 deletions

View File

@ -13,6 +13,7 @@ import (
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"github.com/c9s/bbgo/pkg/datatype" "github.com/c9s/bbgo/pkg/datatype"
"github.com/c9s/bbgo/pkg/dynamic"
"github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/service" "github.com/c9s/bbgo/pkg/service"
"github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/types"
@ -387,7 +388,7 @@ func (c *Config) GetSignature() string {
id := strategy.ID() id := strategy.ID()
ps = append(ps, id) ps = append(ps, id)
if symbol, ok := isSymbolBasedStrategy(reflect.ValueOf(strategy)); ok { if symbol, ok := dynamic.LookupSymbolField(reflect.ValueOf(strategy)); ok {
ps = append(ps, symbol) ps = append(ps, symbol)
} }
} }

View File

@ -2,6 +2,8 @@ package bbgo
import ( import (
"reflect" "reflect"
"github.com/c9s/bbgo/pkg/dynamic"
) )
type InstanceIDProvider interface { type InstanceIDProvider interface {
@ -17,7 +19,7 @@ func callID(obj interface{}) string {
return ret[0].String() return ret[0].String()
} }
if symbol, ok := isSymbolBasedStrategy(sv); ok { if symbol, ok := dynamic.LookupSymbolField(sv); ok {
m := sv.MethodByName("ID") m := sv.MethodByName("ID")
ret := m.Call(nil) ret := m.Call(nil)
return ret[0].String() + ":" + symbol return ret[0].String() + ":" + symbol
@ -29,20 +31,3 @@ func callID(obj interface{}) string {
return ret[0].String() + ":" return ret[0].String() + ":"
} }
func isSymbolBasedStrategy(rs reflect.Value) (string, bool) {
if rs.Kind() == reflect.Ptr {
rs = rs.Elem()
}
field := rs.FieldByName("Symbol")
if !field.IsValid() {
return "", false
}
if field.Kind() != reflect.String {
return "", false
}
return field.String(), true
}

View File

@ -202,7 +202,7 @@ func (trader *Trader) RunSingleExchangeStrategy(ctx context.Context, strategy Si
return errors.Wrapf(err, "failed to inject OrderExecutor on %T", strategy) return errors.Wrapf(err, "failed to inject OrderExecutor on %T", strategy)
} }
if symbol, ok := isSymbolBasedStrategy(rs); ok { if symbol, ok := dynamic.LookupSymbolField(rs); ok {
log.Infof("found symbol based strategy from %s", rs.Type()) log.Infof("found symbol based strategy from %s", rs.Type())
market, ok := session.Market(symbol) market, ok := session.Market(symbol)

View File

@ -6,3 +6,21 @@ func HasField(rs reflect.Value, fieldName string) (field reflect.Value, ok bool)
field = rs.FieldByName(fieldName) field = rs.FieldByName(fieldName)
return field, field.IsValid() return field, field.IsValid()
} }
func LookupSymbolField(rs reflect.Value) (string, bool) {
if rs.Kind() == reflect.Ptr {
rs = rs.Elem()
}
field := rs.FieldByName("Symbol")
if !field.IsValid() {
return "", false
}
if field.Kind() != reflect.String {
return "", false
}
return field.String(), true
}