2020-08-31 04:32:51 +00:00
|
|
|
package max
|
|
|
|
|
2020-10-05 10:26:31 +00:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-10-06 10:44:56 +00:00
|
|
|
"strconv"
|
2020-10-05 10:26:31 +00:00
|
|
|
"strings"
|
2020-10-06 10:44:56 +00:00
|
|
|
"time"
|
2020-10-05 10:26:31 +00:00
|
|
|
|
2020-10-10 09:50:49 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2020-10-05 10:26:31 +00:00
|
|
|
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 {
|
2020-10-06 09:32:41 +00:00
|
|
|
return toGlobalCurrency("MAX")
|
2020-10-05 10:26:31 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-10-06 10:44:56 +00:00
|
|
|
func (e *Exchange) QueryTrades(ctx context.Context, symbol string, options *types.TradeQueryOptions) (trades []types.Trade, err error) {
|
|
|
|
req := e.client.TradeService.NewPrivateTradeRequest()
|
2020-10-10 09:50:49 +00:00
|
|
|
req.Market(toLocalSymbol(symbol))
|
2020-10-06 10:44:56 +00:00
|
|
|
|
|
|
|
if options.Limit > 0 {
|
|
|
|
req.Limit(options.Limit)
|
|
|
|
}
|
|
|
|
|
|
|
|
if options.LastTradeID > 0 {
|
|
|
|
req.From(options.LastTradeID)
|
|
|
|
}
|
|
|
|
|
2020-10-10 09:50:49 +00:00
|
|
|
// make it compatible with binance, we need the last trade id for the next page.
|
|
|
|
req.OrderBy("asc")
|
|
|
|
|
2020-10-06 10:44:56 +00:00
|
|
|
remoteTrades, err := req.Do(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, t := range remoteTrades {
|
|
|
|
localTrade, err := convertRemoteTrade(t)
|
|
|
|
if err != nil {
|
2020-10-10 09:50:49 +00:00
|
|
|
logger.WithError(err).Errorf("can not convert trade: %+v", t)
|
2020-10-06 10:44:56 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-10-10 09:50:49 +00:00
|
|
|
|
|
|
|
logger.Infof("T: id=%d % 4s %s P=%f Q=%f %s", localTrade.ID, localTrade.Symbol, localTrade.Side, localTrade.Price, localTrade.Quantity, localTrade.Time)
|
|
|
|
|
2020-10-06 10:44:56 +00:00
|
|
|
trades = append(trades, *localTrade)
|
|
|
|
}
|
|
|
|
|
|
|
|
return trades, nil
|
|
|
|
}
|
|
|
|
|
2020-10-10 09:50:49 +00:00
|
|
|
func (e *Exchange) QueryKLines(ctx context.Context, symbol, interval string, options types.KLineQueryOptions) ([]types.KLine, error) {
|
|
|
|
var limit = 5000
|
|
|
|
if options.Limit > 0 {
|
|
|
|
// default limit == 500
|
|
|
|
limit = options.Limit
|
|
|
|
}
|
|
|
|
|
|
|
|
if options.StartTime == nil {
|
|
|
|
return nil, errors.New("start time can not be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
if options.EndTime != nil {
|
|
|
|
return nil, errors.New("end time is not supported")
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("querying kline %s %s %v", symbol, interval, options)
|
|
|
|
|
|
|
|
// avoid rate limit
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
|
|
|
|
localKlines, err := e.client.PublicService.KLines(toLocalSymbol(symbol), interval, *options.StartTime, limit)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var kLines []types.KLine
|
|
|
|
for _, k := range localKlines {
|
|
|
|
kLines = append(kLines, k.KLine())
|
|
|
|
}
|
|
|
|
|
|
|
|
return kLines, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exchange) QueryAveragePrice(ctx context.Context, symbol string) (float64, error) {
|
|
|
|
ticker, err := e.client.PublicService.Ticker(toLocalSymbol(symbol))
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return (util.MustParseFloat(ticker.Sell) + util.MustParseFloat(ticker.Buy)) / 2, nil
|
|
|
|
}
|
|
|
|
|
2020-10-06 09:28:13 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-10-10 09:50:49 +00:00
|
|
|
func toLocalSymbol(symbol string) string {
|
|
|
|
return strings.ToLower(symbol)
|
|
|
|
}
|
|
|
|
|
|
|
|
func toGlobalSymbol(symbol string) string {
|
2020-10-10 09:55:05 +00:00
|
|
|
return strings.ToUpper(symbol)
|
2020-10-10 09:50:49 +00:00
|
|
|
}
|
|
|
|
|
2020-10-05 10:26:31 +00:00
|
|
|
func toLocalSideType(side types.SideType) string {
|
|
|
|
return strings.ToLower(string(side))
|
|
|
|
}
|
|
|
|
|
2020-10-06 10:44:56 +00:00
|
|
|
func toGlobalSideType(v string) string {
|
|
|
|
switch strings.ToLower(v) {
|
|
|
|
case "bid":
|
|
|
|
return "BUY"
|
|
|
|
|
|
|
|
case "ask":
|
|
|
|
return "SELL"
|
|
|
|
|
2020-10-10 09:50:49 +00:00
|
|
|
case "self-trade":
|
|
|
|
return "SELF"
|
|
|
|
|
2020-10-06 10:44:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return strings.ToUpper(v)
|
|
|
|
}
|
|
|
|
|
2020-10-05 10:26:31 +00:00
|
|
|
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
|
|
|
}
|
2020-10-06 10:44:56 +00:00
|
|
|
|
|
|
|
func convertRemoteTrade(t maxapi.Trade) (*types.Trade, error) {
|
|
|
|
// skip trade ID that is the same. however this should not happen
|
|
|
|
var side = toGlobalSideType(t.Side)
|
|
|
|
|
|
|
|
// trade time
|
|
|
|
mts := time.Unix(0, t.CreatedAtMilliSeconds*int64(time.Millisecond))
|
|
|
|
|
|
|
|
price, err := strconv.ParseFloat(t.Price, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
quantity, err := strconv.ParseFloat(t.Volume, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
quoteQuantity, err := strconv.ParseFloat(t.Funds, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fee, err := strconv.ParseFloat(t.Fee, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &types.Trade{
|
|
|
|
ID: int64(t.ID),
|
|
|
|
Price: price,
|
2020-10-10 09:50:49 +00:00
|
|
|
Symbol: toGlobalSymbol(t.Market),
|
2020-10-06 10:44:56 +00:00
|
|
|
Exchange: "max",
|
|
|
|
Quantity: quantity,
|
|
|
|
Side: side,
|
|
|
|
IsBuyer: t.IsBuyer(),
|
|
|
|
IsMaker: t.IsMaker(),
|
|
|
|
Fee: fee,
|
2020-10-10 09:58:22 +00:00
|
|
|
FeeCurrency: toGlobalCurrency(t.FeeCurrency),
|
2020-10-06 10:44:56 +00:00
|
|
|
QuoteQuantity: quoteQuantity,
|
|
|
|
Time: mts,
|
|
|
|
}, nil
|
|
|
|
}
|