2020-08-31 04:32:51 +00:00
|
|
|
package max
|
|
|
|
|
2020-10-05 10:26:31 +00:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
maxapi "github.com/c9s/bbgo/exchange/max/maxapi"
|
|
|
|
"github.com/c9s/bbgo/types"
|
2020-10-06 09:28:13 +00:00
|
|
|
"github.com/c9s/bbgo/util"
|
2020-10-05 10:26:31 +00:00
|
|
|
)
|
|
|
|
|
2020-08-31 04:32:51 +00:00
|
|
|
type Exchange struct {
|
2020-10-05 11:01:43 +00:00
|
|
|
client *maxapi.RestClient
|
2020-10-05 10:26:31 +00:00
|
|
|
key, secret string
|
2020-08-31 04:32:51 +00:00
|
|
|
}
|
|
|
|
|
2020-10-05 10:26:31 +00:00
|
|
|
func New(key, secret string) *Exchange {
|
|
|
|
client := maxapi.NewRestClient(maxapi.ProductionAPIURL)
|
|
|
|
client.Auth(key, secret)
|
2020-08-31 04:32:51 +00:00
|
|
|
return &Exchange{
|
2020-10-05 10:26:31 +00:00
|
|
|
client: client,
|
2020-10-05 11:01:43 +00:00
|
|
|
key: key,
|
2020-10-05 10:26:31 +00:00
|
|
|
secret: secret,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exchange) NewStream() types.Stream {
|
|
|
|
return NewStream(e.key, e.secret)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exchange) SubmitOrder(ctx context.Context, order *types.SubmitOrder) error {
|
|
|
|
orderType, err := toLocalOrderType(order.Type)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-06 09:28:13 +00:00
|
|
|
req := e.client.OrderService.NewCreateOrderRequest().
|
|
|
|
Market(order.Symbol).
|
|
|
|
OrderType(string(orderType)).
|
|
|
|
Side(toLocalSideType(order.Side)).
|
|
|
|
Volume(order.QuantityString).
|
|
|
|
Price(order.PriceString)
|
2020-10-05 10:26:31 +00:00
|
|
|
|
2020-10-06 09:28:13 +00:00
|
|
|
retOrder, err := req.Do(ctx)
|
2020-10-05 10:26:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-05 11:01:43 +00:00
|
|
|
logger.Infof("order created: %+v", retOrder)
|
2020-10-05 10:26:31 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// PlatformFeeCurrency
|
|
|
|
func (e *Exchange) PlatformFeeCurrency() string {
|
|
|
|
return "max"
|
|
|
|
}
|
|
|
|
|
2020-10-06 09:28:13 +00:00
|
|
|
func (e *Exchange) QueryAccount(ctx context.Context) (*types.Account, error) {
|
|
|
|
userInfo, err := e.client.AccountService.Me()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var balances = make(types.BalanceMap)
|
|
|
|
for _, a := range userInfo.Accounts {
|
|
|
|
balances[toGlobalCurrency(a.Currency)] = types.Balance{
|
|
|
|
Currency: toGlobalCurrency(a.Currency),
|
|
|
|
Available: util.MustParseFloat(a.Balance),
|
|
|
|
Locked: util.MustParseFloat(a.Locked),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &types.Account{
|
|
|
|
MakerCommission: 15, // 0.15%
|
|
|
|
TakerCommission: 15, // 0.15%
|
|
|
|
Balances: balances,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exchange) QueryAccountBalances(ctx context.Context) (types.BalanceMap, error) {
|
|
|
|
accounts, err := e.client.AccountService.Accounts()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var balances = make(types.BalanceMap)
|
|
|
|
|
|
|
|
for _, a := range accounts {
|
|
|
|
balances[toGlobalCurrency(a.Currency)] = types.Balance{
|
|
|
|
Currency: toGlobalCurrency(a.Currency),
|
|
|
|
Available: util.MustParseFloat(a.Balance),
|
|
|
|
Locked: util.MustParseFloat(a.Locked),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return balances, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func toGlobalCurrency(currency string) string {
|
|
|
|
return strings.ToUpper(currency)
|
|
|
|
}
|
|
|
|
|
2020-10-05 10:26:31 +00:00
|
|
|
func toLocalCurrency(currency string) string {
|
|
|
|
return strings.ToLower(currency)
|
|
|
|
}
|
|
|
|
|
|
|
|
func toLocalSideType(side types.SideType) string {
|
|
|
|
return strings.ToLower(string(side))
|
|
|
|
}
|
|
|
|
|
|
|
|
func toLocalOrderType(orderType types.OrderType) (maxapi.OrderType, error) {
|
|
|
|
switch orderType {
|
|
|
|
case types.OrderTypeLimit:
|
|
|
|
return maxapi.OrderTypeLimit, nil
|
|
|
|
|
|
|
|
case types.OrderTypeMarket:
|
|
|
|
return maxapi.OrderTypeMarket, nil
|
2020-08-31 04:32:51 +00:00
|
|
|
}
|
2020-10-05 10:26:31 +00:00
|
|
|
|
|
|
|
return "", fmt.Errorf("order type %s not supported", orderType)
|
2020-08-31 04:32:51 +00:00
|
|
|
}
|