mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-26 08:45:16 +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 {
|
if e.IsFutures {
|
||||||
req := e.futuresClient.NewListOrdersService().Symbol(symbol)
|
return e.queryFuturesClosedOrders(ctx, symbol, since, until, lastOrderID)
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// If orderId is set, it will get orders >= that orderId. Otherwise most recent orders are returned.
|
// 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
|
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) {
|
func (e *Exchange) submitSpotOrder(ctx context.Context, order types.SubmitOrder) (*types.Order, error) {
|
||||||
orderType, err := toLocalOrderType(order.Type)
|
orderType, err := toLocalOrderType(order.Type)
|
||||||
if err != nil {
|
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) queryFuturesDepth(ctx context.Context, symbol string) (snapshot types.SliceOrderBook, finalUpdateID int64, err error) {
|
||||||
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)
|
res, err := e.futuresClient.NewDepthService().Symbol(symbol).Do(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return snapshot, finalUpdateID, err
|
return snapshot, finalUpdateID, err
|
||||||
}
|
}
|
||||||
response = &binance.DepthResponse{
|
|
||||||
|
response := &binance.DepthResponse{
|
||||||
LastUpdateID: res.LastUpdateID,
|
LastUpdateID: res.LastUpdateID,
|
||||||
Bids: res.Bids,
|
Bids: res.Bids,
|
||||||
Asks: res.Asks,
|
Asks: res.Asks,
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
response, err = e.client.NewDepthService().Symbol(symbol).Do(ctx)
|
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 {
|
if err != nil {
|
||||||
return snapshot, finalUpdateID, err
|
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
|
snapshot.Symbol = symbol
|
||||||
finalUpdateID = response.LastUpdateID
|
finalUpdateID = response.LastUpdateID
|
||||||
for _, entry := range response.Bids {
|
for _, entry := range response.Bids {
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/adshao/go-binance/v2/futures"
|
"github.com/adshao/go-binance/v2/futures"
|
||||||
|
"github.com/google/uuid"
|
||||||
"go.uber.org/multierr"
|
"go.uber.org/multierr"
|
||||||
|
|
||||||
"github.com/c9s/bbgo/pkg/exchange/binance/binanceapi"
|
"github.com/c9s/bbgo/pkg/exchange/binance/binanceapi"
|
||||||
|
@ -13,6 +14,25 @@ import (
|
||||||
"github.com/c9s/bbgo/pkg/types"
|
"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 {
|
func (e *Exchange) TransferFuturesAccountAsset(ctx context.Context, asset string, amount fixedpoint.Value, io types.TransferDirection) error {
|
||||||
req := e.client2.NewFuturesTransferRequest()
|
req := e.client2.NewFuturesTransferRequest()
|
||||||
req.Asset(asset)
|
req.Asset(asset)
|
||||||
|
@ -286,3 +306,33 @@ func (e *Exchange) queryFuturesTrades(ctx context.Context, symbol string, option
|
||||||
trades = types.SortTradesAscending(trades)
|
trades = types.SortTradesAscending(trades)
|
||||||
return trades, nil
|
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