Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REFACTOR: [binance] rewrite binance depth requestgen #1528

Merged
merged 10 commits into from
Feb 6, 2024
16 changes: 16 additions & 0 deletions pkg/exchange/binance/binanceapi/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,19 @@ func TestClient_NewPlaceMarginOrderRequest(t *testing.T) {
assert.NotEmpty(t, res)
t.Logf("result: %+v", res)
}

func TestClient_GetDepth(t *testing.T) {
client := getTestClientOrSkip(t)
ctx := context.Background()

err := client.SetTimeOffsetFromServer(ctx)
assert.NoError(t, err)

req := client.NewGetDepthRequest().Symbol("BTCUSDT").Limit(1000)
resp, err := req.Do(ctx)
if assert.NoError(t, err) {
assert.NotNil(t, resp)
assert.NotEmpty(t, resp)
t.Logf("response: %+v", resp)
}
}
25 changes: 25 additions & 0 deletions pkg/exchange/binance/binanceapi/get_depth_request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package binanceapi

import (
"github.com/c9s/requestgen"

"github.com/c9s/bbgo/pkg/fixedpoint"
)

type Depth struct {
LastUpdateId int64 `json:"lastUpdateId"`
Bids [][]fixedpoint.Value `json:"bids"`
Asks [][]fixedpoint.Value `json:"asks"`
}

//go:generate requestgen -method GET -url "/api/v3/depth" -type GetDepthRequest -responseType .Depth
type GetDepthRequest struct {
client requestgen.APIClient

symbol string `param:"symbol"`
limit int `param:"limit" defaultValue:"1000"`
}

func (c *RestClient) NewGetDepthRequest() *GetDepthRequest {
return &GetDepthRequest{client: c}
}
190 changes: 190 additions & 0 deletions pkg/exchange/binance/binanceapi/get_depth_request_requestgen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 20 additions & 3 deletions pkg/exchange/binance/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -1346,15 +1346,32 @@ func (e *Exchange) QueryDepth(ctx context.Context, symbol string) (snapshot type
return e.queryFuturesDepth(ctx, symbol)
}

response, err := e.client.NewDepthService().Symbol(symbol).Limit(DefaultDepthLimit).Do(ctx)
response, err := e.client2.NewGetDepthRequest().Symbol(symbol).Limit(DefaultDepthLimit).Do(ctx)
if err != nil {
return snapshot, finalUpdateID, err
}

return convertDepth(snapshot, symbol, finalUpdateID, response)
return convertDepth(symbol, response)
}

func convertDepth(
func convertDepth(symbol string, response *binanceapi.Depth) (snapshot types.SliceOrderBook, finalUpdateID int64, err error) {
snapshot.Symbol = symbol
snapshot.Time = time.Now()
snapshot.LastUpdateId = response.LastUpdateId

finalUpdateID = response.LastUpdateId
for _, entry := range response.Bids {
snapshot.Bids = append(snapshot.Bids, types.PriceVolume{Price: entry[0], Volume: entry[1]})
}

for _, entry := range response.Asks {
snapshot.Asks = append(snapshot.Asks, types.PriceVolume{Price: entry[0], Volume: entry[1]})
}

return snapshot, finalUpdateID, err
}

func convertDepthLegacy(
snapshot types.SliceOrderBook, symbol string, finalUpdateID int64, response *binance.DepthResponse,
) (types.SliceOrderBook, int64, error) {
snapshot.Symbol = symbol
Expand Down
24 changes: 17 additions & 7 deletions pkg/exchange/binance/futures.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ 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) {
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 {
Expand All @@ -34,7 +36,9 @@ func (e *Exchange) queryFuturesClosedOrders(ctx context.Context, symbol string,
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.Asset(asset)
req.Amount(amount.String())
Expand Down Expand Up @@ -63,7 +67,7 @@ func (e *Exchange) TransferFuturesAccountAsset(ctx context.Context, asset string
// Balance.Available = Wallet Balance(in Binance UI) - Used Margin
// Balance.Locked = Used Margin
func (e *Exchange) QueryFuturesAccount(ctx context.Context) (*types.Account, error) {
//account, err := e.futuresClient.NewGetAccountService().Do(ctx)
// account, err := e.futuresClient.NewGetAccountService().Do(ctx)
reqAccount := e.futuresClient2.NewFuturesGetAccountRequest()
account, err := reqAccount.Do(ctx)
if err != nil {
Expand Down Expand Up @@ -218,7 +222,9 @@ func (e *Exchange) submitFuturesOrder(ctx context.Context, order types.SubmitOrd
return createdOrder, err
}

func (e *Exchange) QueryFuturesKLines(ctx context.Context, symbol string, interval types.Interval, options types.KLineQueryOptions) ([]types.KLine, error) {
func (e *Exchange) QueryFuturesKLines(
ctx context.Context, symbol string, interval types.Interval, options types.KLineQueryOptions,
) ([]types.KLine, error) {

var limit = 1000
if options.Limit > 0 {
Expand Down Expand Up @@ -272,7 +278,9 @@ func (e *Exchange) QueryFuturesKLines(ctx context.Context, symbol string, interv
return kLines, nil
}

func (e *Exchange) queryFuturesTrades(ctx context.Context, symbol string, options *types.TradeQueryOptions) (trades []types.Trade, err error) {
func (e *Exchange) queryFuturesTrades(
ctx context.Context, symbol string, options *types.TradeQueryOptions,
) (trades []types.Trade, err error) {

var remoteTrades []*futures.AccountTrade
req := e.futuresClient.NewListAccountTradeService().
Expand Down Expand Up @@ -376,7 +384,7 @@ func (e *Exchange) queryFuturesDepth(ctx context.Context, symbol string) (snapsh
Asks: res.Asks,
}

return convertDepth(snapshot, symbol, finalUpdateID, response)
return convertDepthLegacy(snapshot, symbol, finalUpdateID, response)
}

func (e *Exchange) GetFuturesClient() *binanceapi.FuturesRestClient {
Expand All @@ -386,7 +394,9 @@ func (e *Exchange) GetFuturesClient() *binanceapi.FuturesRestClient {
// QueryFuturesIncomeHistory queries the income history on the binance futures account
// This is more binance futures specific API, the convert function is not designed yet.
// TODO: consider other futures platforms and design the common data structure for this
func (e *Exchange) QueryFuturesIncomeHistory(ctx context.Context, symbol string, incomeType binanceapi.FuturesIncomeType, startTime, endTime *time.Time) ([]binanceapi.FuturesIncome, error) {
func (e *Exchange) QueryFuturesIncomeHistory(
ctx context.Context, symbol string, incomeType binanceapi.FuturesIncomeType, startTime, endTime *time.Time,
) ([]binanceapi.FuturesIncome, error) {
req := e.futuresClient2.NewFuturesGetIncomeHistoryRequest()
req.Symbol(symbol)
req.IncomeType(incomeType)
Expand Down
Loading
Loading