Merge pull request #678 from andycheng123/fix/interact

interact: fix missing make()
This commit is contained in:
Yo-An Lin 2022-06-07 12:31:53 +08:00 committed by GitHub
commit 037f2949bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 9 deletions

View File

@ -54,6 +54,7 @@ func getStrategySignatures(exchangeStrategies map[string]SingleExchangeStrategy)
func filterStrategyByInterface(checkInterface interface{}, exchangeStrategies map[string]SingleExchangeStrategy) (strategies map[string]SingleExchangeStrategy, found bool) {
found = false
strategies = make(map[string]SingleExchangeStrategy)
rt := reflect.TypeOf(checkInterface).Elem()
for signature, strategy := range exchangeStrategies {
if ok := reflect.TypeOf(strategy).Implements(rt); ok {
@ -404,20 +405,21 @@ func (it *CoreInteraction) Initialize() error {
return nil
}
// getStrategySignature returns strategy instance unique signature
func getStrategySignature(strategy SingleExchangeStrategy) (string, error) {
// Returns instance ID
var signature = callID(strategy)
if signature != "" {
return signature, nil
}
// Use reflect to build instance signature
rv := reflect.ValueOf(strategy).Elem()
if rv.Kind() != reflect.Struct {
return "", fmt.Errorf("strategy %T instance is not a struct", strategy)
}
var signature = path.Base(rv.Type().PkgPath())
var id = strategy.ID()
if !strings.EqualFold(id, signature) {
signature += "." + strings.ToLower(id)
}
signature = path.Base(rv.Type().PkgPath())
for i := 0; i < rv.NumField(); i++ {
field := rv.Field(i)
fieldName := rv.Type().Field(i).Name

View File

@ -2,6 +2,7 @@ package bbgo
import (
"context"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
@ -15,6 +16,10 @@ func (m myStrategy) ID() string {
return "mystrategy"
}
func (m myStrategy) InstanceID() string {
return fmt.Sprintf("%s:%s", m.ID(), m.Symbol)
}
func (m *myStrategy) Run(ctx context.Context, orderExecutor OrderExecutor, session *ExchangeSession) error {
return nil
}
@ -24,5 +29,5 @@ func Test_getStrategySignature(t *testing.T) {
Symbol: "BTCUSDT",
})
assert.NoError(t, err)
assert.Equal(t, "bbgo.mystrategy.BTCUSDT", signature)
assert.Equal(t, "mystrategy:BTCUSDT", signature)
}

View File

@ -189,6 +189,10 @@ func (s *Strategy) ID() string {
return ID
}
func (s *Strategy) InstanceID() string {
return fmt.Sprintf("%s:%s", ID, s.Symbol)
}
func (s *Strategy) Validate() error {
if s.Quantity.IsZero() && s.ScaleQuantity == nil {
return fmt.Errorf("quantity or scaleQuantity can not be zero")