2020-10-25 10:26:10 +00:00
|
|
|
package max
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2021-03-22 09:25:25 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2020-10-25 10:26:10 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/exchange/max/maxapi"
|
|
|
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
"github.com/c9s/bbgo/pkg/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
func toGlobalCurrency(currency string) string {
|
|
|
|
return strings.ToUpper(currency)
|
|
|
|
}
|
|
|
|
|
|
|
|
func toLocalCurrency(currency string) string {
|
|
|
|
return strings.ToLower(currency)
|
|
|
|
}
|
|
|
|
|
|
|
|
func toLocalSymbol(symbol string) string {
|
|
|
|
return strings.ToLower(symbol)
|
|
|
|
}
|
|
|
|
|
|
|
|
func toGlobalSymbol(symbol string) string {
|
|
|
|
return strings.ToUpper(symbol)
|
|
|
|
}
|
|
|
|
|
|
|
|
func toLocalSideType(side types.SideType) string {
|
|
|
|
return strings.ToLower(string(side))
|
|
|
|
}
|
|
|
|
|
|
|
|
func toGlobalSideType(v string) types.SideType {
|
|
|
|
switch strings.ToLower(v) {
|
|
|
|
case "bid", "buy":
|
|
|
|
return types.SideTypeBuy
|
|
|
|
|
|
|
|
case "ask", "sell":
|
|
|
|
return types.SideTypeSell
|
|
|
|
|
|
|
|
case "self-trade":
|
|
|
|
return types.SideTypeSelf
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return types.SideType(v)
|
|
|
|
}
|
|
|
|
|
2021-02-23 08:39:48 +00:00
|
|
|
func toGlobalRewards(maxRewards []max.Reward) ([]types.Reward, error) {
|
|
|
|
// convert to global reward
|
|
|
|
var rewards []types.Reward
|
|
|
|
for _, r := range maxRewards {
|
|
|
|
// ignore "accepted"
|
|
|
|
if r.State != "done" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
reward, err := r.Reward()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rewards = append(rewards, *reward)
|
|
|
|
}
|
|
|
|
|
|
|
|
return rewards, nil
|
|
|
|
}
|
|
|
|
|
2020-10-25 10:26:10 +00:00
|
|
|
func toGlobalOrderStatus(orderStatus max.OrderState, executedVolume, remainingVolume fixedpoint.Value) types.OrderStatus {
|
|
|
|
|
|
|
|
switch orderStatus {
|
|
|
|
|
|
|
|
case max.OrderStateCancel:
|
|
|
|
return types.OrderStatusCanceled
|
|
|
|
|
|
|
|
case max.OrderStateFinalizing, max.OrderStateDone:
|
|
|
|
if executedVolume > 0 && remainingVolume > 0 {
|
|
|
|
return types.OrderStatusPartiallyFilled
|
|
|
|
} else if remainingVolume == 0 {
|
|
|
|
return types.OrderStatusFilled
|
|
|
|
}
|
|
|
|
|
|
|
|
return types.OrderStatusFilled
|
|
|
|
|
|
|
|
case max.OrderStateWait:
|
|
|
|
if executedVolume > 0 && remainingVolume > 0 {
|
|
|
|
return types.OrderStatusPartiallyFilled
|
|
|
|
}
|
|
|
|
|
|
|
|
return types.OrderStatusNew
|
|
|
|
|
|
|
|
case max.OrderStateConvert:
|
|
|
|
if executedVolume > 0 && remainingVolume > 0 {
|
|
|
|
return types.OrderStatusPartiallyFilled
|
|
|
|
}
|
|
|
|
|
|
|
|
return types.OrderStatusNew
|
|
|
|
|
|
|
|
case max.OrderStateFailed:
|
|
|
|
return types.OrderStatusRejected
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Errorf("unknown order status: %v", orderStatus)
|
|
|
|
return types.OrderStatus(orderStatus)
|
|
|
|
}
|
|
|
|
|
|
|
|
func toGlobalOrderType(orderType max.OrderType) types.OrderType {
|
|
|
|
switch orderType {
|
|
|
|
case max.OrderTypeLimit:
|
|
|
|
return types.OrderTypeLimit
|
|
|
|
|
|
|
|
case max.OrderTypeMarket:
|
|
|
|
return types.OrderTypeMarket
|
|
|
|
|
|
|
|
case max.OrderTypeStopLimit:
|
|
|
|
return types.OrderTypeStopLimit
|
|
|
|
|
|
|
|
case max.OrderTypeStopMarket:
|
|
|
|
return types.OrderTypeStopMarket
|
|
|
|
|
2021-04-11 12:07:05 +00:00
|
|
|
case max.OrderTypeIOCLimit:
|
|
|
|
return types.OrderTypeIOCLimit
|
|
|
|
|
2021-05-14 03:53:07 +00:00
|
|
|
case max.OrderTypePostOnly:
|
|
|
|
return types.OrderTypeLimitMaker
|
|
|
|
|
2020-10-25 10:26:10 +00:00
|
|
|
}
|
|
|
|
|
2021-05-14 03:53:07 +00:00
|
|
|
logger.Errorf("order convert error, unknown order type: %v", orderType)
|
2020-10-25 10:26:10 +00:00
|
|
|
return types.OrderType(orderType)
|
|
|
|
}
|
|
|
|
|
|
|
|
func toLocalOrderType(orderType types.OrderType) (max.OrderType, error) {
|
|
|
|
switch orderType {
|
|
|
|
|
|
|
|
case types.OrderTypeStopLimit:
|
|
|
|
return max.OrderTypeStopLimit, nil
|
|
|
|
|
|
|
|
case types.OrderTypeStopMarket:
|
|
|
|
return max.OrderTypeStopMarket, nil
|
|
|
|
|
2021-03-22 09:25:25 +00:00
|
|
|
case types.OrderTypeLimitMaker:
|
|
|
|
return max.OrderTypePostOnly, nil
|
|
|
|
|
2020-10-25 10:26:10 +00:00
|
|
|
case types.OrderTypeLimit:
|
|
|
|
return max.OrderTypeLimit, nil
|
|
|
|
|
|
|
|
case types.OrderTypeMarket:
|
|
|
|
return max.OrderTypeMarket, nil
|
2021-04-11 12:07:05 +00:00
|
|
|
|
|
|
|
case types.OrderTypeIOCLimit:
|
|
|
|
return max.OrderTypeIOCLimit, nil
|
2020-10-25 10:26:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return "", fmt.Errorf("order type %s not supported", orderType)
|
|
|
|
}
|
|
|
|
|
2020-12-29 08:00:03 +00:00
|
|
|
func toGlobalOrders(maxOrders []max.Order) (orders []types.Order, err error) {
|
|
|
|
for _, localOrder := range maxOrders {
|
|
|
|
o, err := toGlobalOrder(localOrder)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("order convert error")
|
|
|
|
}
|
|
|
|
|
|
|
|
orders = append(orders, *o)
|
|
|
|
}
|
|
|
|
|
|
|
|
return orders, err
|
|
|
|
}
|
|
|
|
|
2020-10-25 10:26:10 +00:00
|
|
|
func toGlobalOrder(maxOrder max.Order) (*types.Order, error) {
|
|
|
|
executedVolume, err := fixedpoint.NewFromString(maxOrder.ExecutedVolume)
|
|
|
|
if err != nil {
|
2021-03-22 09:25:25 +00:00
|
|
|
return nil, errors.Wrapf(err, "parse executed_volume failed: %+v", maxOrder)
|
2020-10-25 10:26:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
remainingVolume, err := fixedpoint.NewFromString(maxOrder.RemainingVolume)
|
|
|
|
if err != nil {
|
2021-03-22 09:25:25 +00:00
|
|
|
return nil, errors.Wrapf(err, "parse remaining volume failed: %+v", maxOrder)
|
2020-10-25 10:26:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &types.Order{
|
|
|
|
SubmitOrder: types.SubmitOrder{
|
2020-10-25 16:26:17 +00:00
|
|
|
ClientOrderID: maxOrder.ClientOID,
|
2020-11-03 09:55:20 +00:00
|
|
|
Symbol: toGlobalSymbol(maxOrder.Market),
|
|
|
|
Side: toGlobalSideType(maxOrder.Side),
|
|
|
|
Type: toGlobalOrderType(maxOrder.OrderType),
|
|
|
|
Quantity: util.MustParseFloat(maxOrder.Volume),
|
|
|
|
Price: util.MustParseFloat(maxOrder.Price),
|
|
|
|
TimeInForce: "GTC", // MAX only supports GTC
|
2021-01-23 09:17:46 +00:00
|
|
|
GroupID: maxOrder.GroupID,
|
2020-10-25 10:26:10 +00:00
|
|
|
},
|
2021-05-16 09:02:23 +00:00
|
|
|
Exchange: types.ExchangeMax,
|
2020-11-03 10:45:14 +00:00
|
|
|
IsWorking: maxOrder.State == "wait",
|
2020-10-25 10:26:10 +00:00
|
|
|
OrderID: maxOrder.ID,
|
|
|
|
Status: toGlobalOrderStatus(maxOrder.State, executedVolume, remainingVolume),
|
|
|
|
ExecutedQuantity: executedVolume.Float64(),
|
2021-05-19 17:32:26 +00:00
|
|
|
CreationTime: types.Time(maxOrder.CreatedAt),
|
|
|
|
UpdateTime: types.Time(maxOrder.CreatedAt),
|
2020-10-25 10:26:10 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func toGlobalTrade(t max.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),
|
2020-11-03 09:55:20 +00:00
|
|
|
OrderID: t.OrderID,
|
2020-10-25 10:26:10 +00:00
|
|
|
Price: price,
|
|
|
|
Symbol: toGlobalSymbol(t.Market),
|
|
|
|
Exchange: "max",
|
|
|
|
Quantity: quantity,
|
|
|
|
Side: side,
|
|
|
|
IsBuyer: t.IsBuyer(),
|
|
|
|
IsMaker: t.IsMaker(),
|
|
|
|
Fee: fee,
|
|
|
|
FeeCurrency: toGlobalCurrency(t.FeeCurrency),
|
|
|
|
QuoteQuantity: quoteQuantity,
|
2021-05-19 17:32:26 +00:00
|
|
|
Time: types.Time(mts),
|
2020-10-25 10:26:10 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func toGlobalDepositStatus(a string) types.DepositStatus {
|
|
|
|
switch a {
|
|
|
|
case "submitting", "submitted", "checking":
|
|
|
|
return types.DepositPending
|
|
|
|
|
|
|
|
case "accepted":
|
|
|
|
return types.DepositSuccess
|
|
|
|
|
|
|
|
case "rejected":
|
|
|
|
return types.DepositRejected
|
|
|
|
|
|
|
|
case "canceled":
|
|
|
|
return types.DepositCancelled
|
|
|
|
|
|
|
|
case "suspect", "refunded":
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return types.DepositStatus(a)
|
|
|
|
}
|