implement QueryAccount and QueryAccountBalances

This commit is contained in:
c9s 2020-10-06 17:28:13 +08:00
parent 8a250db582
commit f5624ce9d1
3 changed files with 56 additions and 9 deletions

View File

@ -7,6 +7,7 @@ import (
maxapi "github.com/c9s/bbgo/exchange/max/maxapi"
"github.com/c9s/bbgo/types"
"github.com/c9s/bbgo/util"
)
type Exchange struct {
@ -34,14 +35,14 @@ func (e *Exchange) SubmitOrder(ctx context.Context, order *types.SubmitOrder) er
return err
}
retOrder, err := e.client.OrderService.Create(
order.Symbol,
toLocalSideType(order.Side),
order.Quantity,
order.Price,
string(orderType),
maxapi.Options{})
req := e.client.OrderService.NewCreateOrderRequest().
Market(order.Symbol).
OrderType(string(orderType)).
Side(toLocalSideType(order.Side)).
Volume(order.QuantityString).
Price(order.PriceString)
retOrder, err := req.Do(ctx)
if err != nil {
return err
}
@ -55,6 +56,51 @@ func (e *Exchange) PlatformFeeCurrency() string {
return "max"
}
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)
}
func toLocalCurrency(currency string) string {
return strings.ToLower(currency)
}

View File

@ -6,6 +6,7 @@ type Balance struct {
Locked float64 `json:"locked"`
}
type BalanceMap map[string]Balance
type Account struct {
MakerCommission int64

View File

@ -10,7 +10,8 @@ type Exchange interface {
NewStream() Stream
QueryAccountBalances(ctx context.Context) (map[string]Balance, error)
QueryAccount(ctx context.Context) (*Account, error)
QueryAccountBalances(ctx context.Context) (BalanceMap, error)
QueryKLines(ctx context.Context, symbol string, interval string, options KLineQueryOptions) ([]KLine, error)
@ -26,4 +27,3 @@ type TradeQueryOptions struct {
Limit int
LastTradeID int64
}