2020-10-12 14:46:06 +00:00
|
|
|
package cmdutil
|
|
|
|
|
|
|
|
import (
|
2020-11-09 08:34:35 +00:00
|
|
|
"fmt"
|
2021-02-04 07:14:54 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
2020-11-09 08:34:35 +00:00
|
|
|
|
2020-10-12 14:46:06 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/exchange/binance"
|
2021-03-15 02:22:45 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/exchange/ftx"
|
2021-12-21 16:30:50 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/exchange/kucoin"
|
2020-10-12 14:46:06 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/exchange/max"
|
2021-05-26 16:35:51 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/exchange/okex"
|
2020-10-12 14:46:06 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
2021-12-25 15:24:42 +00:00
|
|
|
func NewExchangePublic(exchangeName types.ExchangeName) (types.Exchange, error){
|
|
|
|
return NewExchangeStandard(exchangeName, "", "", "", "")
|
|
|
|
}
|
|
|
|
|
2021-05-26 16:35:51 +00:00
|
|
|
func NewExchangeStandard(n types.ExchangeName, key, secret, passphrase, subAccount string) (types.Exchange, error) {
|
2020-10-12 14:46:06 +00:00
|
|
|
switch n {
|
|
|
|
|
2021-03-15 02:22:45 +00:00
|
|
|
case types.ExchangeFTX:
|
|
|
|
return ftx.NewExchange(key, secret, subAccount), nil
|
|
|
|
|
2020-10-12 14:46:06 +00:00
|
|
|
case types.ExchangeBinance:
|
|
|
|
return binance.New(key, secret), nil
|
|
|
|
|
|
|
|
case types.ExchangeMax:
|
|
|
|
return max.New(key, secret), nil
|
|
|
|
|
2021-05-26 16:35:51 +00:00
|
|
|
case types.ExchangeOKEx:
|
|
|
|
return okex.New(key, secret, passphrase), nil
|
2021-05-26 16:29:16 +00:00
|
|
|
|
2021-12-21 16:30:50 +00:00
|
|
|
case types.ExchangeKucoin:
|
|
|
|
return kucoin.New(key, secret, passphrase), nil
|
|
|
|
|
2020-10-26 09:00:17 +00:00
|
|
|
default:
|
2020-11-09 08:34:35 +00:00
|
|
|
return nil, fmt.Errorf("unsupported exchange: %v", n)
|
2020-10-26 09:00:17 +00:00
|
|
|
|
2020-10-12 14:46:06 +00:00
|
|
|
}
|
2020-10-26 09:00:17 +00:00
|
|
|
}
|
2020-10-12 14:46:06 +00:00
|
|
|
|
2021-02-02 03:44:07 +00:00
|
|
|
func NewExchangeWithEnvVarPrefix(n types.ExchangeName, varPrefix string) (types.Exchange, error) {
|
|
|
|
if len(varPrefix) == 0 {
|
|
|
|
varPrefix = n.String()
|
|
|
|
}
|
|
|
|
|
2021-02-04 07:14:54 +00:00
|
|
|
varPrefix = strings.ToUpper(varPrefix)
|
|
|
|
|
|
|
|
key := os.Getenv(varPrefix + "_API_KEY")
|
|
|
|
secret := os.Getenv(varPrefix + "_API_SECRET")
|
2021-02-02 03:44:07 +00:00
|
|
|
if len(key) == 0 || len(secret) == 0 {
|
2021-03-22 09:40:17 +00:00
|
|
|
return nil, fmt.Errorf("can not initialize exchange %s: empty key or secret, env var prefix: %s", n, varPrefix)
|
2021-02-02 03:44:07 +00:00
|
|
|
}
|
|
|
|
|
2021-05-26 16:35:51 +00:00
|
|
|
passphrase := os.Getenv(varPrefix + "_API_PASSPHRASE")
|
2021-03-15 02:22:45 +00:00
|
|
|
subAccount := os.Getenv(varPrefix + "_SUBACCOUNT")
|
2021-05-26 16:35:51 +00:00
|
|
|
return NewExchangeStandard(n, key, secret, passphrase, subAccount)
|
2021-02-02 03:44:07 +00:00
|
|
|
}
|
|
|
|
|
2020-10-26 09:00:17 +00:00
|
|
|
// NewExchange constructor exchange object from viper config.
|
|
|
|
func NewExchange(n types.ExchangeName) (types.Exchange, error) {
|
|
|
|
return NewExchangeWithEnvVarPrefix(n, "")
|
2020-10-12 14:46:06 +00:00
|
|
|
}
|