bbgo_origin/pkg/exchange/ftx/convert.go

181 lines
4.8 KiB
Go
Raw Normal View History

2021-02-08 10:59:01 +00:00
package ftx
2021-03-07 04:46:26 +00:00
import (
"fmt"
"strings"
2021-03-21 12:17:41 +00:00
"time"
log "github.com/sirupsen/logrus"
2021-03-07 04:46:26 +00:00
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)
2021-02-08 10:59:01 +00:00
2021-02-08 14:29:50 +00:00
func toGlobalCurrency(original string) string {
2021-03-07 04:46:26 +00:00
return TrimUpperString(original)
}
func toGlobalSymbol(original string) string {
2021-04-01 03:54:16 +00:00
return strings.ReplaceAll(TrimUpperString(original), "/", "")
2021-03-07 04:46:26 +00:00
}
func toLocalSymbol(original string) string {
if symbolMap[original] == "" {
return original
}
return symbolMap[original]
}
2021-03-07 04:46:26 +00:00
func TrimUpperString(original string) string {
2021-02-08 14:29:50 +00:00
return strings.ToUpper(strings.TrimSpace(original))
2021-02-08 10:59:01 +00:00
}
2021-03-07 04:46:26 +00:00
2021-03-13 01:51:16 +00:00
func TrimLowerString(original string) string {
return strings.ToLower(strings.TrimSpace(original))
}
2021-03-07 04:46:26 +00:00
var errUnsupportedOrderStatus = fmt.Errorf("unsupported order status")
2021-03-13 01:51:31 +00:00
func toGlobalOrder(r order) (types.Order, error) {
// In exchange/max/convert.go, it only parses these fields.
2021-03-07 04:46:26 +00:00
o := types.Order{
SubmitOrder: types.SubmitOrder{
ClientOrderID: r.ClientId,
Symbol: toGlobalSymbol(r.Market),
Side: types.SideType(TrimUpperString(r.Side)),
// order type definition: https://github.com/ftexchange/ftx/blob/master/rest/client.py#L122
Type: types.OrderType(TrimUpperString(r.Type)),
Quantity: r.Size,
Price: r.Price,
TimeInForce: "GTC",
},
Exchange: types.ExchangeFTX,
2021-03-07 04:46:26 +00:00
IsWorking: r.Status == "open",
OrderID: uint64(r.ID),
Status: "",
ExecutedQuantity: r.FilledSize,
2021-05-19 17:32:26 +00:00
CreationTime: types.Time(r.CreatedAt.Time),
UpdateTime: types.Time(r.CreatedAt.Time),
2021-03-07 04:46:26 +00:00
}
// `new` (accepted but not processed yet), `open`, or `closed` (filled or cancelled)
switch r.Status {
case "new":
o.Status = types.OrderStatusNew
case "open":
if fixedpoint.NewFromFloat(o.ExecutedQuantity) != fixedpoint.NewFromInt(0) {
o.Status = types.OrderStatusPartiallyFilled
} else {
o.Status = types.OrderStatusNew
}
case "closed":
// filled or canceled
if fixedpoint.NewFromFloat(o.Quantity) == fixedpoint.NewFromFloat(o.ExecutedQuantity) {
o.Status = types.OrderStatusFilled
} else {
// can't distinguish it's canceled or rejected from order response, so always set to canceled
o.Status = types.OrderStatusCanceled
}
default:
return types.Order{}, fmt.Errorf("unsupported status %s: %w", r.Status, errUnsupportedOrderStatus)
}
return o, nil
}
2021-03-21 12:17:41 +00:00
func toGlobalDeposit(input depositHistory) (types.Deposit, error) {
s, err := toGlobalDepositStatus(input.Status)
if err != nil {
log.WithError(err).Warnf("assign empty string to the deposit status")
}
t := input.Time
2021-03-25 08:57:54 +00:00
if input.ConfirmedTime.Time != (time.Time{}) {
2021-03-21 12:17:41 +00:00
t = input.ConfirmedTime
}
d := types.Deposit{
GID: 0,
Exchange: types.ExchangeFTX,
2021-05-19 17:32:26 +00:00
Time: types.Time(t.Time),
2021-03-21 12:17:41 +00:00
Amount: input.Size,
Asset: toGlobalCurrency(input.Coin),
TransactionID: input.TxID,
Status: s,
Address: input.Address.Address,
AddressTag: input.Address.Tag,
}
return d, nil
}
func toGlobalDepositStatus(input string) (types.DepositStatus, error) {
// The document only list `confirmed` status
switch input {
case "confirmed", "complete":
return types.DepositSuccess, nil
}
return "", fmt.Errorf("unsupported status %s", input)
}
2021-03-25 08:57:54 +00:00
func toGlobalTrade(f fill) (types.Trade, error) {
return types.Trade{
ID: f.TradeId,
GID: 0,
OrderID: f.OrderId,
Exchange: types.ExchangeFTX,
2021-03-25 08:57:54 +00:00
Price: f.Price,
Quantity: f.Size,
QuoteQuantity: f.Price * f.Size,
Symbol: toGlobalSymbol(f.Market),
Side: f.Side,
IsBuyer: f.Side == types.SideTypeBuy,
IsMaker: f.Liquidity == "maker",
2021-05-19 17:32:26 +00:00
Time: types.Time(f.Time.Time),
2021-03-25 08:57:54 +00:00
Fee: f.Fee,
FeeCurrency: f.FeeCurrency,
IsMargin: false,
IsIsolated: false,
}, nil
}
2021-03-31 10:09:13 +00:00
func toGlobalKLine(symbol string, interval types.Interval, h Candle) (types.KLine, error) {
return types.KLine{
Exchange: types.ExchangeFTX,
2021-04-01 03:54:16 +00:00
Symbol: toGlobalSymbol(symbol),
StartTime: types.Time(h.StartTime.Time),
EndTime: types.Time(h.StartTime.Add(interval.Duration())),
2021-03-31 10:09:13 +00:00
Interval: interval,
Open: h.Open,
Close: h.Close,
High: h.High,
Low: h.Low,
Volume: h.Volume,
Closed: true,
}, nil
}
type OrderType string
const (
OrderTypeLimit OrderType = "limit"
OrderTypeMarket OrderType = "market"
)
func toLocalOrderType(orderType types.OrderType) (OrderType, bool, bool, error) {
switch orderType {
case types.OrderTypeLimitMaker:
return OrderTypeLimit, true, false, nil
case types.OrderTypeLimit:
return OrderTypeLimit, false, false, nil
case types.OrderTypeMarket:
return OrderTypeMarket, false, false, nil
case types.OrderTypeIOCLimit:
return OrderTypeLimit, false, true, nil
}
return "", false, false, fmt.Errorf("order type %s not supported", orderType)
}