backtest: allocate public exchange (with empty key secret) for backtesting

This commit is contained in:
c9s 2021-12-24 00:24:00 +08:00
parent c4838f5631
commit 49e516b80e
4 changed files with 20 additions and 15 deletions

View File

@ -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
}

View File

@ -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 {

View File

@ -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
}

View File

@ -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(),
})