load exchange sessions dynamically

This commit is contained in:
c9s 2020-10-26 17:00:17 +08:00
parent 7764560f3d
commit 502e5bdc04
4 changed files with 50 additions and 16 deletions

View File

@ -14,6 +14,7 @@ reportTrades:
"ethusdt": "bbgo-ethusdt"
"bnbusdt": "bbgo-bnbusdt"
"sxpusdt": "bbgo-sxpusdt"
"maxusdt": "max-maxusdt"
reportPnL:
- averageCostBySymbols:
@ -27,10 +28,10 @@ reportPnL:
sessions:
max:
exchange: max
envVarPrefix: "MAX_API_"
envVarPrefix: max
binance:
exchange: binance
envVarPrefix: "BINANCE_API_"
envVarPrefix: binance
exchangeStrategies:
- on: binance

View File

@ -24,12 +24,10 @@ reportPnL:
sessions:
max:
exchange: max
keyVar: MAX_API_KEY
secretVar: MAX_API_SECRET
envVarPrefix: max
binance:
exchange: binance
keyVar: BINANCE_API_KEY
secretVar: BINANCE_API_SECRET
envVarPrefix: binance
exchangeStrategies:
- on: max

View File

@ -9,13 +9,16 @@ import (
"github.com/c9s/bbgo/pkg/types"
)
// NewExchange constructor exchange object from viper config.
func NewExchange(n types.ExchangeName) (types.Exchange, error) {
func NewExchangeWithEnvVarPrefix(n types.ExchangeName, varPrefix string) (types.Exchange, error) {
if len(varPrefix) == 0 {
varPrefix = n.String()
}
switch n {
case types.ExchangeBinance:
key := viper.GetString("binance-api-key")
secret := viper.GetString("binance-api-secret")
key := viper.GetString(varPrefix + "-api-key")
secret := viper.GetString(varPrefix + "-api-secret")
if len(key) == 0 || len(secret) == 0 {
return nil, errors.New("binance: empty key or secret")
}
@ -23,15 +26,21 @@ func NewExchange(n types.ExchangeName) (types.Exchange, error) {
return binance.New(key, secret), nil
case types.ExchangeMax:
key := viper.GetString("max-api-key")
secret := viper.GetString("max-api-secret")
key := viper.GetString(varPrefix + "-api-key")
secret := viper.GetString(varPrefix + "-api-secret")
if len(key) == 0 || len(secret) == 0 {
return nil, errors.New("max: empty key or secret")
}
return max.New(key, secret), nil
}
default:
return nil, errors.Errorf("unsupported exchange: %v", n)
return nil, nil
}
}
// NewExchange constructor exchange object from viper config.
func NewExchange(n types.ExchangeName) (types.Exchange, error) {
return NewExchangeWithEnvVarPrefix(n, "")
}

View File

@ -23,6 +23,7 @@ import (
"github.com/c9s/bbgo/pkg/config"
"github.com/c9s/bbgo/pkg/notifier/slacknotifier"
"github.com/c9s/bbgo/pkg/slack/slacklog"
"github.com/c9s/bbgo/pkg/types"
// import built-in strategies
_ "github.com/c9s/bbgo/pkg/strategy/buyandhold"
@ -86,13 +87,39 @@ func runConfig(ctx context.Context, userConfig *config.Config) error {
return err
}
environ := bbgo.NewDefaultEnvironment()
environ := bbgo.NewEnvironment()
environ.SyncTrades(db)
environ.ReportTrade(notifierSet)
trader := bbgo.NewTrader(environ)
trader.AddNotifier(notifierSet)
if len(userConfig.Sessions) == 0 {
for _, n := range bbgo.SupportedExchanges {
if viper.IsSet(string(n) + "-api-key") {
exchange, err := cmdutil.NewExchangeWithEnvVarPrefix(n, "")
if err != nil {
panic(err)
}
environ.AddExchange(n.String(), exchange)
}
}
} else {
for sessionName, sessionConfig := range userConfig.Sessions {
exchangeName, err := types.ValidExchangeName(sessionConfig.ExchangeName)
if err != nil {
return err
}
exchange, err := cmdutil.NewExchangeWithEnvVarPrefix(exchangeName, sessionConfig.EnvVarPrefix)
if err != nil {
return err
}
environ.AddExchange(sessionName, exchange)
}
}
for _, entry := range userConfig.ExchangeStrategies {
for _, mount := range entry.Mounts {
log.Infof("attaching strategy %T on %s...", entry.Strategy, mount)
@ -213,7 +240,6 @@ func build(ctx context.Context, buildDir string, userConfig *config.Config, goOS
buildTarget := filepath.Join(cwd, buildDir)
binary := fmt.Sprintf("bbgow-%s-%s", goOS, goArch)
if output != nil && len(*output) > 0 {
binary = *output