mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-26 00:35:15 +00:00
binance: refactor binance exchange code for futures api
This commit is contained in:
parent
071825e982
commit
ed4d32c59a
|
@ -787,22 +787,7 @@ func (e *Exchange) QueryClosedOrders(ctx context.Context, symbol string, since,
|
|||
}
|
||||
|
||||
if e.IsFutures {
|
||||
req := e.futuresClient.NewListOrdersService().Symbol(symbol)
|
||||
|
||||
if lastOrderID > 0 {
|
||||
req.OrderID(int64(lastOrderID))
|
||||
} else {
|
||||
req.StartTime(since.UnixNano() / int64(time.Millisecond))
|
||||
if until.Sub(since) < 24*time.Hour {
|
||||
req.EndTime(until.UnixNano() / int64(time.Millisecond))
|
||||
}
|
||||
}
|
||||
|
||||
binanceOrders, err := req.Do(ctx)
|
||||
if err != nil {
|
||||
return orders, err
|
||||
}
|
||||
return toGlobalFuturesOrders(binanceOrders, false)
|
||||
return e.queryFuturesClosedOrders(ctx, symbol, since, until, lastOrderID)
|
||||
}
|
||||
|
||||
// If orderId is set, it will get orders >= that orderId. Otherwise most recent orders are returned.
|
||||
|
@ -1012,36 +997,6 @@ func newSpotClientOrderID(originalID string) (clientOrderID string) {
|
|||
return clientOrderID
|
||||
}
|
||||
|
||||
// BBGO is a futures broker on Binance
|
||||
const futuresBrokerID = "gBhMvywy"
|
||||
|
||||
func newFuturesClientOrderID(originalID string) (clientOrderID string) {
|
||||
if originalID == types.NoClientOrderID {
|
||||
return ""
|
||||
}
|
||||
|
||||
prefix := "x-" + futuresBrokerID
|
||||
prefixLen := len(prefix)
|
||||
|
||||
if originalID != "" {
|
||||
// try to keep the whole original client order ID if user specifies it.
|
||||
if prefixLen+len(originalID) > 32 {
|
||||
return originalID
|
||||
}
|
||||
|
||||
clientOrderID = prefix + originalID
|
||||
return clientOrderID
|
||||
}
|
||||
|
||||
clientOrderID = uuid.New().String()
|
||||
clientOrderID = prefix + clientOrderID
|
||||
if len(clientOrderID) > 32 {
|
||||
return clientOrderID[0:32]
|
||||
}
|
||||
|
||||
return clientOrderID
|
||||
}
|
||||
|
||||
func (e *Exchange) submitSpotOrder(ctx context.Context, order types.SubmitOrder) (*types.Order, error) {
|
||||
orderType, err := toLocalOrderType(order.Type)
|
||||
if err != nil {
|
||||
|
@ -1326,26 +1281,36 @@ func (e *Exchange) DefaultFeeRates() types.ExchangeFee {
|
|||
}
|
||||
}
|
||||
|
||||
// QueryDepth query the order book depth of a symbol
|
||||
func (e *Exchange) QueryDepth(ctx context.Context, symbol string) (snapshot types.SliceOrderBook, finalUpdateID int64, err error) {
|
||||
var response *binance.DepthResponse
|
||||
if e.IsFutures {
|
||||
res, err := e.futuresClient.NewDepthService().Symbol(symbol).Do(ctx)
|
||||
if err != nil {
|
||||
return snapshot, finalUpdateID, err
|
||||
}
|
||||
response = &binance.DepthResponse{
|
||||
LastUpdateID: res.LastUpdateID,
|
||||
Bids: res.Bids,
|
||||
Asks: res.Asks,
|
||||
}
|
||||
} else {
|
||||
response, err = e.client.NewDepthService().Symbol(symbol).Do(ctx)
|
||||
if err != nil {
|
||||
return snapshot, finalUpdateID, err
|
||||
}
|
||||
func (e *Exchange) queryFuturesDepth(ctx context.Context, symbol string) (snapshot types.SliceOrderBook, finalUpdateID int64, err error) {
|
||||
res, err := e.futuresClient.NewDepthService().Symbol(symbol).Do(ctx)
|
||||
if err != nil {
|
||||
return snapshot, finalUpdateID, err
|
||||
}
|
||||
|
||||
response := &binance.DepthResponse{
|
||||
LastUpdateID: res.LastUpdateID,
|
||||
Bids: res.Bids,
|
||||
Asks: res.Asks,
|
||||
}
|
||||
|
||||
return convertDepth(snapshot, symbol, finalUpdateID, response)
|
||||
}
|
||||
|
||||
// QueryDepth query the order book depth of a symbol
|
||||
func (e *Exchange) QueryDepth(ctx context.Context, symbol string) (snapshot types.SliceOrderBook, finalUpdateID int64, err error) {
|
||||
if e.IsFutures {
|
||||
return e.queryFuturesDepth(ctx, symbol)
|
||||
}
|
||||
|
||||
response, err := e.client.NewDepthService().Symbol(symbol).Do(ctx)
|
||||
if err != nil {
|
||||
return snapshot, finalUpdateID, err
|
||||
}
|
||||
|
||||
return convertDepth(snapshot, symbol, finalUpdateID, response)
|
||||
}
|
||||
|
||||
func convertDepth(snapshot types.SliceOrderBook, symbol string, finalUpdateID int64, response *binance.DepthResponse) (types.SliceOrderBook, int64, error) {
|
||||
snapshot.Symbol = symbol
|
||||
finalUpdateID = response.LastUpdateID
|
||||
for _, entry := range response.Bids {
|
||||
|
|
|
@ -6,6 +6,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/adshao/go-binance/v2/futures"
|
||||
"github.com/google/uuid"
|
||||
"go.uber.org/multierr"
|
||||
|
||||
"github.com/c9s/bbgo/pkg/exchange/binance/binanceapi"
|
||||
|
@ -13,6 +14,25 @@ import (
|
|||
"github.com/c9s/bbgo/pkg/types"
|
||||
)
|
||||
|
||||
func (e *Exchange) queryFuturesClosedOrders(ctx context.Context, symbol string, since, until time.Time, lastOrderID uint64) (orders []types.Order, err error) {
|
||||
req := e.futuresClient.NewListOrdersService().Symbol(symbol)
|
||||
|
||||
if lastOrderID > 0 {
|
||||
req.OrderID(int64(lastOrderID))
|
||||
} else {
|
||||
req.StartTime(since.UnixNano() / int64(time.Millisecond))
|
||||
if until.Sub(since) < 24*time.Hour {
|
||||
req.EndTime(until.UnixNano() / int64(time.Millisecond))
|
||||
}
|
||||
}
|
||||
|
||||
binanceOrders, err := req.Do(ctx)
|
||||
if err != nil {
|
||||
return orders, err
|
||||
}
|
||||
return toGlobalFuturesOrders(binanceOrders, false)
|
||||
}
|
||||
|
||||
func (e *Exchange) TransferFuturesAccountAsset(ctx context.Context, asset string, amount fixedpoint.Value, io types.TransferDirection) error {
|
||||
req := e.client2.NewFuturesTransferRequest()
|
||||
req.Asset(asset)
|
||||
|
@ -286,3 +306,33 @@ func (e *Exchange) queryFuturesTrades(ctx context.Context, symbol string, option
|
|||
trades = types.SortTradesAscending(trades)
|
||||
return trades, nil
|
||||
}
|
||||
|
||||
// BBGO is a futures broker on Binance
|
||||
const futuresBrokerID = "gBhMvywy"
|
||||
|
||||
func newFuturesClientOrderID(originalID string) (clientOrderID string) {
|
||||
if originalID == types.NoClientOrderID {
|
||||
return ""
|
||||
}
|
||||
|
||||
prefix := "x-" + futuresBrokerID
|
||||
prefixLen := len(prefix)
|
||||
|
||||
if originalID != "" {
|
||||
// try to keep the whole original client order ID if user specifies it.
|
||||
if prefixLen+len(originalID) > 32 {
|
||||
return originalID
|
||||
}
|
||||
|
||||
clientOrderID = prefix + originalID
|
||||
return clientOrderID
|
||||
}
|
||||
|
||||
clientOrderID = uuid.New().String()
|
||||
clientOrderID = prefix + clientOrderID
|
||||
if len(clientOrderID) > 32 {
|
||||
return clientOrderID[0:32]
|
||||
}
|
||||
|
||||
return clientOrderID
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user