bbgo_origin/pkg/exchange/binance/convert.go

378 lines
12 KiB
Go
Raw Permalink Normal View History

package binance
import (
"fmt"
2021-05-25 11:13:10 +00:00
"strings"
"time"
"github.com/adshao/go-binance/v2"
2021-12-08 16:08:25 +00:00
"github.com/adshao/go-binance/v2/futures"
2020-11-30 10:29:22 +00:00
"github.com/pkg/errors"
"github.com/c9s/bbgo/pkg/exchange/binance/binanceapi"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)
func toGlobalWithdrawStatus(status binanceapi.WithdrawStatus) (types.WithdrawStatus, error) {
switch status {
case binanceapi.WithdrawStatusEmailSent:
return types.WithdrawStatusSent, nil
case binanceapi.WithdrawStatusCancelled:
return types.WithdrawStatusCancelled, nil
case binanceapi.WithdrawStatusAwaitingApproval:
return types.WithdrawStatusAwaitingApproval, nil
case binanceapi.WithdrawStatusRejected:
return types.WithdrawStatusRejected, nil
case binanceapi.WithdrawStatusProcessing:
return types.WithdrawStatusProcessing, nil
case binanceapi.WithdrawStatusFailure:
return types.WithdrawStatusFailed, nil
case binanceapi.WithdrawStatusCompleted:
return types.WithdrawStatusCompleted, nil
default:
return types.WithdrawStatusUnknown, fmt.Errorf("unable to convert the withdraw status: %s", status)
}
}
2021-12-08 16:05:36 +00:00
func toGlobalMarket(symbol binance.Symbol) types.Market {
market := types.Market{
Exchange: types.ExchangeBinance,
2021-12-08 16:05:36 +00:00
Symbol: symbol.Symbol,
LocalSymbol: symbol.Symbol,
PricePrecision: symbol.QuotePrecision,
VolumePrecision: symbol.BaseAssetPrecision,
QuoteCurrency: symbol.QuoteAsset,
BaseCurrency: symbol.BaseAsset,
}
2024-03-13 10:14:24 +00:00
if f := symbol.NotionalFilter(); f != nil {
market.MinNotional = fixedpoint.MustNewFromString(f.MinNotional)
market.MinAmount = fixedpoint.MustNewFromString(f.MinNotional)
2021-12-08 16:05:36 +00:00
}
// The LOT_SIZE filter defines the quantity (aka "lots" in auction terms) rules for a symbol.
// There are 3 parts:
// minQty defines the minimum quantity/icebergQty allowed.
// maxQty defines the maximum quantity/icebergQty allowed.
// stepSize defines the intervals that a quantity/icebergQty can be increased/decreased by.
if f := symbol.LotSizeFilter(); f != nil {
market.MinQuantity = fixedpoint.MustNewFromString(f.MinQuantity)
market.MaxQuantity = fixedpoint.MustNewFromString(f.MaxQuantity)
market.StepSize = fixedpoint.MustNewFromString(f.StepSize)
2021-12-08 16:05:36 +00:00
}
if f := symbol.PriceFilter(); f != nil {
market.MaxPrice = fixedpoint.MustNewFromString(f.MaxPrice)
market.MinPrice = fixedpoint.MustNewFromString(f.MinPrice)
market.TickSize = fixedpoint.MustNewFromString(f.TickSize)
2021-12-08 16:05:36 +00:00
}
2023-06-13 09:09:37 +00:00
if market.MinNotional.IsZero() {
2023-06-14 08:39:44 +00:00
log.Warnf("binance market %s minNotional is zero", market.Symbol)
2023-06-13 09:09:37 +00:00
}
if market.MinQuantity.IsZero() {
2023-06-14 08:39:44 +00:00
log.Warnf("binance market %s minQuantity is zero", market.Symbol)
2023-06-13 09:09:37 +00:00
}
2021-12-08 16:05:36 +00:00
return market
}
2022-01-03 19:21:38 +00:00
// TODO: Cuz it returns types.Market as well, merge following to the above function
func toGlobalFuturesMarket(symbol futures.Symbol) types.Market {
market := types.Market{
Exchange: types.ExchangeBinance,
2022-01-03 19:21:38 +00:00
Symbol: symbol.Symbol,
LocalSymbol: symbol.Symbol,
PricePrecision: symbol.QuotePrecision,
VolumePrecision: symbol.BaseAssetPrecision,
QuoteCurrency: symbol.QuoteAsset,
BaseCurrency: symbol.BaseAsset,
}
if f := symbol.MinNotionalFilter(); f != nil {
market.MinNotional = fixedpoint.MustNewFromString(f.Notional)
market.MinAmount = fixedpoint.MustNewFromString(f.Notional)
2022-01-03 19:21:38 +00:00
}
// The LOT_SIZE filter defines the quantity (aka "lots" in auction terms) rules for a symbol.
// There are 3 parts:
// minQty defines the minimum quantity/icebergQty allowed.
// maxQty defines the maximum quantity/icebergQty allowed.
// stepSize defines the intervals that a quantity/icebergQty can be increased/decreased by.
if f := symbol.LotSizeFilter(); f != nil {
market.MinQuantity = fixedpoint.MustNewFromString(f.MinQuantity)
market.MaxQuantity = fixedpoint.MustNewFromString(f.MaxQuantity)
market.StepSize = fixedpoint.MustNewFromString(f.StepSize)
2022-01-03 19:21:38 +00:00
}
if f := symbol.PriceFilter(); f != nil {
market.MaxPrice = fixedpoint.MustNewFromString(f.MaxPrice)
market.MinPrice = fixedpoint.MustNewFromString(f.MinPrice)
market.TickSize = fixedpoint.MustNewFromString(f.TickSize)
2022-01-03 19:21:38 +00:00
}
return market
}
2022-08-12 03:11:45 +00:00
// func toGlobalIsolatedMarginAccount(account *binance.IsolatedMarginAccount) *types.IsolatedMarginAccount {
// return &types.IsolatedMarginAccount{
// TotalAssetOfBTC: fixedpoint.MustNewFromString(account.TotalNetAssetOfBTC),
// TotalLiabilityOfBTC: fixedpoint.MustNewFromString(account.TotalLiabilityOfBTC),
// TotalNetAssetOfBTC: fixedpoint.MustNewFromString(account.TotalNetAssetOfBTC),
// Assets: toGlobalIsolatedMarginAssets(account.Assets),
// }
2022-08-12 03:11:45 +00:00
// }
2021-12-08 16:05:36 +00:00
func toGlobalTicker(stats *binance.PriceChangeStats) (*types.Ticker, error) {
return &types.Ticker{
Volume: fixedpoint.MustNewFromString(stats.Volume),
Last: fixedpoint.MustNewFromString(stats.LastPrice),
Open: fixedpoint.MustNewFromString(stats.OpenPrice),
High: fixedpoint.MustNewFromString(stats.HighPrice),
Low: fixedpoint.MustNewFromString(stats.LowPrice),
Buy: fixedpoint.MustNewFromString(stats.BidPrice),
Sell: fixedpoint.MustNewFromString(stats.AskPrice),
Time: time.Unix(0, stats.CloseTime*int64(time.Millisecond)),
}, nil
}
func toGlobalFuturesTicker(stats *futures.PriceChangeStats) (*types.Ticker, error) {
return &types.Ticker{
Volume: fixedpoint.MustNewFromString(stats.Volume),
Last: fixedpoint.MustNewFromString(stats.LastPrice),
Open: fixedpoint.MustNewFromString(stats.OpenPrice),
High: fixedpoint.MustNewFromString(stats.HighPrice),
Low: fixedpoint.MustNewFromString(stats.LowPrice),
Buy: fixedpoint.MustNewFromString(stats.LastPrice),
Sell: fixedpoint.MustNewFromString(stats.LastPrice),
Time: time.Unix(0, stats.CloseTime*int64(time.Millisecond)),
}, nil
}
func toLocalOrderType(orderType types.OrderType) (binance.OrderType, error) {
switch orderType {
case types.OrderTypeLimitMaker:
return binance.OrderTypeLimitMaker, nil
case types.OrderTypeLimit:
return binance.OrderTypeLimit, nil
case types.OrderTypeStopLimit:
return binance.OrderTypeStopLossLimit, nil
case types.OrderTypeStopMarket:
return binance.OrderTypeStopLoss, nil
case types.OrderTypeMarket:
return binance.OrderTypeMarket, nil
}
return "", fmt.Errorf("can not convert to local order, order type %s not supported", orderType)
}
func toGlobalOrders(binanceOrders []*binance.Order, isMargin bool) (orders []types.Order, err error) {
for _, binanceOrder := range binanceOrders {
order, err := toGlobalOrder(binanceOrder, isMargin)
if err != nil {
return orders, err
}
orders = append(orders, *order)
}
return orders, err
}
func toGlobalOrder(binanceOrder *binance.Order, isMargin bool) (*types.Order, error) {
return &types.Order{
SubmitOrder: types.SubmitOrder{
ClientOrderID: binanceOrder.ClientOrderID,
Symbol: binanceOrder.Symbol,
Side: toGlobalSideType(binanceOrder.Side),
Type: toGlobalOrderType(binanceOrder.Type),
Quantity: fixedpoint.MustNewFromString(binanceOrder.OrigQuantity),
Price: fixedpoint.MustNewFromString(binanceOrder.Price),
2022-02-18 05:52:13 +00:00
TimeInForce: types.TimeInForce(binanceOrder.TimeInForce),
},
Exchange: types.ExchangeBinance,
2020-11-03 10:45:14 +00:00
IsWorking: binanceOrder.IsWorking,
OrderID: uint64(binanceOrder.OrderID),
Status: toGlobalOrderStatus(binanceOrder.Status),
OriginalStatus: string(binanceOrder.Status),
ExecutedQuantity: fixedpoint.MustNewFromString(binanceOrder.ExecutedQuantity),
2021-05-19 17:32:26 +00:00
CreationTime: types.Time(millisecondTime(binanceOrder.Time)),
UpdateTime: types.Time(millisecondTime(binanceOrder.UpdateTime)),
2021-01-19 17:24:29 +00:00
IsMargin: isMargin,
IsIsolated: binanceOrder.IsIsolated,
}, nil
}
2020-11-05 05:35:04 +00:00
func millisecondTime(t int64) time.Time {
return time.Unix(0, t*int64(time.Millisecond))
}
func toGlobalTrade(t binance.TradeV3, isMargin bool) (*types.Trade, error) {
// skip trade ID that is the same. however this should not happen
var side types.SideType
if t.IsBuyer {
side = types.SideTypeBuy
} else {
side = types.SideTypeSell
}
price, err := fixedpoint.NewFromString(t.Price)
if err != nil {
2020-11-30 10:29:22 +00:00
return nil, errors.Wrapf(err, "price parse error, price: %+v", t.Price)
}
quantity, err := fixedpoint.NewFromString(t.Quantity)
if err != nil {
2020-11-30 10:29:22 +00:00
return nil, errors.Wrapf(err, "quantity parse error, quantity: %+v", t.Quantity)
}
var quoteQuantity fixedpoint.Value
2020-11-30 10:29:22 +00:00
if len(t.QuoteQuantity) > 0 {
quoteQuantity, err = fixedpoint.NewFromString(t.QuoteQuantity)
2020-11-30 10:29:22 +00:00
if err != nil {
return nil, errors.Wrapf(err, "quote quantity parse error, quoteQuantity: %+v", t.QuoteQuantity)
}
} else {
quoteQuantity = price.Mul(quantity)
}
fee, err := fixedpoint.NewFromString(t.Commission)
if err != nil {
2020-11-30 10:29:22 +00:00
return nil, errors.Wrapf(err, "commission parse error, commission: %+v", t.Commission)
}
return &types.Trade{
2021-12-23 05:15:27 +00:00
ID: uint64(t.ID),
2020-11-03 09:55:20 +00:00
OrderID: uint64(t.OrderID),
Price: price,
Symbol: t.Symbol,
Exchange: types.ExchangeBinance,
Quantity: quantity,
QuoteQuantity: quoteQuantity,
Side: side,
IsBuyer: t.IsBuyer,
IsMaker: t.IsMaker,
Fee: fee,
FeeCurrency: t.CommissionAsset,
2021-05-19 17:32:26 +00:00
Time: types.Time(millisecondTime(t.Time)),
2021-01-19 17:24:29 +00:00
IsMargin: isMargin,
IsIsolated: t.IsIsolated,
}, nil
}
func toGlobalSideType(side binance.SideType) types.SideType {
switch side {
case binance.SideTypeBuy:
return types.SideTypeBuy
case binance.SideTypeSell:
return types.SideTypeSell
default:
log.Errorf("can not convert binance side type, unknown side type: %q", side)
return ""
}
}
func toGlobalOrderType(orderType binance.OrderType) types.OrderType {
switch orderType {
case binance.OrderTypeLimit,
binance.OrderTypeLimitMaker, binance.OrderTypeTakeProfitLimit:
return types.OrderTypeLimit
case binance.OrderTypeMarket:
return types.OrderTypeMarket
case binance.OrderTypeStopLossLimit:
return types.OrderTypeStopLimit
case binance.OrderTypeStopLoss:
return types.OrderTypeStopMarket
default:
log.Errorf("unsupported order type: %v", orderType)
return ""
}
}
func toGlobalOrderStatus(orderStatus binance.OrderStatusType) types.OrderStatus {
switch orderStatus {
case binance.OrderStatusTypeNew:
return types.OrderStatusNew
case binance.OrderStatusTypeRejected:
return types.OrderStatusRejected
2022-08-12 03:11:45 +00:00
case binance.OrderStatusTypeCanceled, binance.OrderStatusTypeExpired, binance.OrderStatusTypePendingCancel:
return types.OrderStatusCanceled
case binance.OrderStatusTypePartiallyFilled:
return types.OrderStatusPartiallyFilled
case binance.OrderStatusTypeFilled:
return types.OrderStatusFilled
}
return types.OrderStatus(orderStatus)
}
2020-11-30 10:29:22 +00:00
2021-05-25 11:13:10 +00:00
func convertSubscription(s types.Subscription) string {
// binance uses lower case symbol name,
// for kline, it's "<symbol>@kline_<interval>"
// for depth, it's "<symbol>@depth OR <symbol>@depth@100ms"
2022-10-17 08:01:46 +00:00
// for trade, it's "<symbol>@trade"
// for aggregated trade, it's "<symbol>@aggTrade"
2021-05-25 11:13:10 +00:00
switch s.Channel {
case types.KLineChannel:
return fmt.Sprintf("%s@%s_%s", strings.ToLower(s.Symbol), s.Channel, s.Options.String())
case types.BookChannel:
// depth values: 5, 10, 20
// Stream Names: <symbol>@depth<levels> OR <symbol>@depth<levels>@100ms.
// Update speed: 1000ms or 100ms
2022-01-12 14:27:42 +00:00
n := strings.ToLower(s.Symbol) + "@depth"
switch s.Options.Depth {
case types.DepthLevel5:
n += "5"
2024-01-24 09:51:02 +00:00
case types.DepthLevel10:
n += "10"
case types.DepthLevel20, types.DepthLevelMedium:
2022-01-12 14:27:42 +00:00
n += "20"
2024-01-24 09:51:02 +00:00
// default to full
2022-01-12 14:27:42 +00:00
case types.DepthLevelFull:
default:
}
switch s.Options.Speed {
case types.SpeedHigh:
n += "@100ms"
case types.SpeedLow:
n += "@1000ms"
}
return n
case types.BookTickerChannel:
return fmt.Sprintf("%s@bookTicker", strings.ToLower(s.Symbol))
2022-10-17 08:01:46 +00:00
case types.MarketTradeChannel:
return fmt.Sprintf("%s@trade", strings.ToLower(s.Symbol))
case types.AggTradeChannel:
return fmt.Sprintf("%s@aggTrade", strings.ToLower(s.Symbol))
case types.ForceOrderChannel:
return fmt.Sprintf("%s@forceOrder", strings.ToLower(s.Symbol))
2021-05-25 11:13:10 +00:00
}
return fmt.Sprintf("%s@%s", strings.ToLower(s.Symbol), s.Channel)
}