binance: add query orders & trades

This commit is contained in:
austin362667 2021-12-14 18:01:24 +08:00
parent f315d9f348
commit a0130affe4
2 changed files with 167 additions and 10 deletions

View File

@ -312,6 +312,59 @@ func toGlobalTrade(t binance.TradeV3, isMargin bool) (*types.Trade, error) {
}, nil
}
func toGlobalFuturesTrade(t futures.AccountTrade) (*types.Trade, error) {
// skip trade ID that is the same. however this should not happen
var side types.SideType
if t.Buyer {
side = types.SideTypeBuy
} else {
side = types.SideTypeSell
}
price, err := strconv.ParseFloat(t.Price, 64)
if err != nil {
return nil, errors.Wrapf(err, "price parse error, price: %+v", t.Price)
}
quantity, err := strconv.ParseFloat(t.Quantity, 64)
if err != nil {
return nil, errors.Wrapf(err, "quantity parse error, quantity: %+v", t.Quantity)
}
var quoteQuantity = 0.0
if len(t.QuoteQuantity) > 0 {
quoteQuantity, err = strconv.ParseFloat(t.QuoteQuantity, 64)
if err != nil {
return nil, errors.Wrapf(err, "quote quantity parse error, quoteQuantity: %+v", t.QuoteQuantity)
}
} else {
quoteQuantity = price * quantity
}
fee, err := strconv.ParseFloat(t.Commission, 64)
if err != nil {
return nil, errors.Wrapf(err, "commission parse error, commission: %+v", t.Commission)
}
return &types.Trade{
ID: t.ID,
OrderID: uint64(t.OrderID),
Price: price,
Symbol: t.Symbol,
Exchange: "binance",
Quantity: quantity,
QuoteQuantity: quoteQuantity,
Side: side,
IsBuyer: t.Buyer,
IsMaker: t.Maker,
Fee: fee,
FeeCurrency: t.CommissionAsset,
Time: types.Time(millisecondTime(t.Time)),
IsFutures: true,
// IsIsolated: t.IsIsolated,
}, nil
}
func toGlobalSideType(side binance.SideType) types.SideType {
switch side {
case binance.SideTypeBuy:

View File

@ -431,6 +431,18 @@ func (e *Exchange) QueryOpenOrders(ctx context.Context, symbol string) (orders [
return toGlobalOrders(binanceOrders)
}
if e.IsFutures {
req := e.futuresClient.NewListOpenOrdersService().Symbol(symbol)
// req.IsIsolated(e.IsIsolatedFutures)
binanceOrders, err := req.Do(ctx)
if err != nil {
return orders, err
}
return toGlobalFuturesOrders(binanceOrders)
}
binanceOrders, err := e.Client.NewListOpenOrdersService().Symbol(symbol).Do(ctx)
if err != nil {
return orders, err
@ -465,6 +477,25 @@ func (e *Exchange) QueryClosedOrders(ctx context.Context, symbol string, since,
return toGlobalOrders(binanceOrders)
}
if e.IsFutures {
req := e.futuresClient.NewListOrdersService().Symbol(symbol)
// req.IsIsolated(e.IsIsolatedFutures)
if lastOrderID > 0 {
req.OrderID(int64(lastOrderID))
} else {
req.StartTime(since.UnixNano() / int64(time.Millisecond)).
EndTime(until.UnixNano() / int64(time.Millisecond))
}
binanceOrders, err := req.Do(ctx)
if err != nil {
return orders, err
}
return toGlobalFuturesOrders(binanceOrders)
}
req := e.Client.NewListOrdersService().
Symbol(symbol)
@ -484,6 +515,29 @@ func (e *Exchange) QueryClosedOrders(ctx context.Context, symbol string, since,
}
func (e *Exchange) CancelOrders(ctx context.Context, orders ...types.Order) (err2 error) {
if e.IsFutures {
for _, o := range orders {
var req = e.futuresClient.NewCancelOrderService()
// Mandatory
req.Symbol(o.Symbol)
if o.OrderID > 0 {
req.OrderID(int64(o.OrderID))
} else if len(o.ClientOrderID) > 0 {
// req.NewClientOrderID(o.ClientOrderID) // TODO
}
_, err := req.Do(ctx)
if err != nil {
log.WithError(err).Errorf("order cancel error")
err2 = err
}
}
return err2
}
for _, o := range orders {
var req = e.Client.NewCancelOrderService()
@ -886,9 +940,9 @@ func (e *Exchange) QueryKLines(ctx context.Context, symbol string, interval type
}
func (e *Exchange) QueryTrades(ctx context.Context, symbol string, options *types.TradeQueryOptions) (trades []types.Trade, err error) {
var remoteTrades []*binance.TradeV3
if e.IsMargin {
var remoteTrades []*binance.TradeV3
req := e.Client.NewListMarginTradesService().
IsIsolated(e.IsIsolatedMargin).
Symbol(symbol)
@ -916,7 +970,58 @@ func (e *Exchange) QueryTrades(ctx context.Context, symbol string, options *type
if err != nil {
return nil, err
}
for _, t := range remoteTrades {
localTrade, err := toGlobalTrade(*t, e.IsMargin)
if err != nil {
log.WithError(err).Errorf("can not convert binance trade: %+v", t)
continue
}
trades = append(trades, *localTrade)
}
return trades, nil
} else if e.IsFutures {
var remoteTrades []*futures.AccountTrade
req := e.futuresClient.NewListAccountTradeService(). // IsIsolated(e.IsIsolatedFutures).
Symbol(symbol)
if options.Limit > 0 {
req.Limit(int(options.Limit))
} else {
req.Limit(1000)
}
// if options.StartTime != nil {
// req.StartTime(options.StartTime.UnixNano() / int64(time.Millisecond))
// }
// if options.EndTime != nil {
// req.EndTime(options.EndTime.UnixNano() / int64(time.Millisecond))
// }
// BINANCE uses inclusive last trade ID
if options.LastTradeID > 0 {
req.FromID(options.LastTradeID)
}
remoteTrades, err = req.Do(ctx)
if err != nil {
return nil, err
}
for _, t := range remoteTrades {
localTrade, err := toGlobalFuturesTrade(*t)
if err != nil {
log.WithError(err).Errorf("can not convert binance trade: %+v", t)
continue
}
trades = append(trades, *localTrade)
}
return trades, nil
} else {
var remoteTrades []*binance.TradeV3
req := e.Client.NewListTradesService().
Symbol(symbol)
@ -942,19 +1047,18 @@ func (e *Exchange) QueryTrades(ctx context.Context, symbol string, options *type
if err != nil {
return nil, err
}
}
for _, t := range remoteTrades {
localTrade, err := toGlobalTrade(*t, e.IsMargin)
if err != nil {
log.WithError(err).Errorf("can not convert binance trade: %+v", t)
continue
}
for _, t := range remoteTrades {
localTrade, err := toGlobalTrade(*t, e.IsMargin)
if err != nil {
log.WithError(err).Errorf("can not convert binance trade: %+v", t)
continue
trades = append(trades, *localTrade)
}
trades = append(trades, *localTrade)
return trades, nil
}
return trades, nil
}
func (e *Exchange) BatchQueryKLines(ctx context.Context, symbol string, interval types.Interval, startTime, endTime time.Time) ([]types.KLine, error) {