diff --git a/pkg/bbgo/environment.go b/pkg/bbgo/environment.go index 658d848d1..0765a5eb5 100644 --- a/pkg/bbgo/environment.go +++ b/pkg/bbgo/environment.go @@ -584,7 +584,7 @@ func (environ *Environment) SyncSession(ctx context.Context, session *ExchangeSe } func (environ *Environment) syncSession(ctx context.Context, session *ExchangeSession, defaultSymbols ...string) error { - symbols, err := getSessionSymbols(session, defaultSymbols...) + symbols, err := session.getSessionSymbols(defaultSymbols...) if err != nil { return err } @@ -594,17 +594,6 @@ func (environ *Environment) syncSession(ctx context.Context, session *ExchangeSe return environ.SyncService.SyncSessionSymbols(ctx, session.Exchange, environ.syncStartTime, symbols...) } -func getSessionSymbols(session *ExchangeSession, defaultSymbols ...string) ([]string, error) { - if session.IsolatedMargin { - return []string{session.IsolatedMarginSymbol}, nil - } - - if len(defaultSymbols) > 0 { - return defaultSymbols, nil - } - - return session.FindPossibleSymbols() -} func (environ *Environment) ConfigureNotificationSystem(userConfig *Config) error { environ.Notifiability = Notifiability{ @@ -654,6 +643,10 @@ func (environ *Environment) ConfigureNotificationSystem(userConfig *Config) erro return nil } +// getAuthStoreID returns the authentication store id +// if telegram bot token is defined, the bot id will be used. +// if not, env var $USER will be used. +// if both are not defined, a default "default" will be used. func getAuthStoreID() string { telegramBotToken := viper.GetString("telegram-bot-token") if len(telegramBotToken) > 0 { @@ -924,3 +917,15 @@ And then enter your token `, token) } + +func (session *ExchangeSession) getSessionSymbols(defaultSymbols ...string) ([]string, error) { + if session.IsolatedMargin { + return []string{session.IsolatedMarginSymbol}, nil + } + + if len(defaultSymbols) > 0 { + return defaultSymbols, nil + } + + return session.FindPossibleSymbols() +} diff --git a/pkg/bbgo/injection.go b/pkg/bbgo/injection.go index a339db7e3..03ff9fd7f 100644 --- a/pkg/bbgo/injection.go +++ b/pkg/bbgo/injection.go @@ -56,6 +56,9 @@ func injectField(rs reflect.Value, fieldName string, obj interface{}, pointerOnl return nil } +// parseStructAndInject parses the struct fields and injects the objects into the corresponding fields by its type. +// if the given object is a reference of an object, the type of the target field MUST BE a pointer field. +// if the given object is a struct value, the type of the target field CAN BE a pointer field or a struct value field. func parseStructAndInject(f interface{}, objects ...interface{}) error { sv := reflect.ValueOf(f) st := reflect.TypeOf(f) @@ -95,7 +98,13 @@ func parseStructAndInject(f interface{}, objects ...interface{}) error { if !fv.CanSet() { return fmt.Errorf("field %v of %s can not be set to %s, make sure it is an exported field", fv, sv.Type(), ot) } - fv.Set(reflect.ValueOf(obj)) + + if k == reflect.Ptr && ot.Kind() == reflect.Struct { + fv.Set(reflect.ValueOf(obj).Addr()) + } else { + fv.Set(reflect.ValueOf(obj)) + } + } } diff --git a/pkg/bbgo/trader.go b/pkg/bbgo/trader.go index 707c0dd80..ec35fb230 100644 --- a/pkg/bbgo/trader.go +++ b/pkg/bbgo/trader.go @@ -245,8 +245,6 @@ func (trader *Trader) RunSingleExchangeStrategy(ctx context.Context, strategy Si return fmt.Errorf("marketDataStore of symbol %s not found", symbol) } - - if err := parseStructAndInject(strategy, market, indicatorSet,