diff --git a/pkg/bbgo/environment.go b/pkg/bbgo/environment.go index d2043507e..b7d2358c4 100644 --- a/pkg/bbgo/environment.go +++ b/pkg/bbgo/environment.go @@ -213,7 +213,7 @@ func (environ *Environment) AddExchangesByViperKeys() error { func (environ *Environment) AddExchangesFromSessionConfig(sessions map[string]*ExchangeSession) error { for sessionName, session := range sessions { - if err := InitExchangeSession(sessionName, session); err != nil { + if err := InitExchangeSession(sessionName, session, nil); err != nil { return err } diff --git a/pkg/bbgo/session.go b/pkg/bbgo/session.go index 2a5cac831..f0d4a20df 100644 --- a/pkg/bbgo/session.go +++ b/pkg/bbgo/session.go @@ -680,20 +680,21 @@ func (session *ExchangeSession) FindPossibleSymbols() (symbols []string, err err return symbols, nil } -func InitExchangeSession(name string, session *ExchangeSession) error { +func InitExchangeSession(name string, session *ExchangeSession, exchange types.Exchange) error { var err error var exchangeName = session.ExchangeName - var exchange types.Exchange - if session.Key != "" && session.Secret != "" { - if !session.PublicOnly { - if len(session.Key) == 0 || len(session.Secret) == 0 { - return fmt.Errorf("can not create exchange %s: empty key or secret", exchangeName) + if exchange == nil { + if session.Key != "" && session.Secret != "" { + if !session.PublicOnly { + if len(session.Key) == 0 || len(session.Secret) == 0 { + return fmt.Errorf("can not create exchange %s: empty key or secret", exchangeName) + } } - } - exchange, err = cmdutil.NewExchangeStandard(exchangeName, session.Key, session.Secret, "", session.SubAccount) - } else { - exchange, err = cmdutil.NewExchangeWithEnvVarPrefix(exchangeName, session.EnvVarPrefix) + exchange, err = cmdutil.NewExchangeStandard(exchangeName, session.Key, session.Secret, "", session.SubAccount) + } else { + exchange, err = cmdutil.NewExchangeWithEnvVarPrefix(exchangeName, session.EnvVarPrefix) + } } if err != nil { diff --git a/pkg/cmd/backtest.go b/pkg/cmd/backtest.go index e30271562..a99e0b969 100644 --- a/pkg/cmd/backtest.go +++ b/pkg/cmd/backtest.go @@ -127,10 +127,14 @@ var BacktestCmd = &cobra.Command{ for key, session := range userConfig.Sessions { if exchangeNameStr == key { - err := bbgo.InitExchangeSession(session.Name, session) + publicExchange, err := cmdutil.NewExchangeStandard(session.ExchangeName, "", "", "", "") if err != nil { return err } + + if err := bbgo.InitExchangeSession(session.Name, session, publicExchange) ; err != nil { + return err + } sourceExchange = session.Exchange exchangeName = session.ExchangeName } @@ -142,7 +146,7 @@ var BacktestCmd = &cobra.Command{ return err } - sourceExchange, err = cmdutil.NewExchange(exchangeName) + sourceExchange, err = cmdutil.NewExchangeStandard(exchangeName, "", "", "", "") if err != nil { return err } diff --git a/pkg/server/routes.go b/pkg/server/routes.go index d428f0270..97a2a4434 100644 --- a/pkg/server/routes.go +++ b/pkg/server/routes.go @@ -133,7 +133,7 @@ func (s *Server) newEngine() *gin.Engine { return } - err := bbgo.InitExchangeSession(session.ExchangeName.String(), &session) + err := bbgo.InitExchangeSession(session.ExchangeName.String(), &session, nil) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{ "error": err.Error(), @@ -182,7 +182,7 @@ func (s *Server) newEngine() *gin.Engine { return } - if err := bbgo.InitExchangeSession(session.ExchangeName.String(), &session); err != nil { + if err := bbgo.InitExchangeSession(session.ExchangeName.String(), &session, nil); err != nil { c.JSON(http.StatusInternalServerError, gin.H{ "error": err.Error(), })