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

FEATURE: [binance] add margin request #1525

Merged
merged 3 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion pkg/exchange/binance/binanceapi/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/stretchr/testify/assert"

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

Expand Down Expand Up @@ -168,7 +169,6 @@ func TestClient_NewTransferAssetRequest(t *testing.T) {
req.FromSymbol("BTCUSDT")
req.ToSymbol("BTCUSDT")
req.Amount("0.01")
req.Timestamp(time.Now())
req.TransferType(TransferAssetTypeIsolatedMarginToMain)
res, err := req.Do(ctx)
assert.NoError(t, err)
Expand Down Expand Up @@ -197,3 +197,50 @@ func TestClient_GetMarginBorrowRepayHistoryRequest(t *testing.T) {
assert.NotEmpty(t, res)
t.Logf("result: %+v", res)
}

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

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

res, err := client.NewPlaceMarginOrderRequest().
Asset("USDT").
Amount(fixedpoint.NewFromFloat(5)).
IsIsolated(true).
Symbol("BNBUSDT").
SetBorrowRepayType(BorrowRepayTypeBorrow).
Do(ctx)
assert.NoError(t, err)
assert.NotNil(t, res)
assert.NotEmpty(t, res)
t.Logf("result: %+v", res)

<-time.After(time.Second)
end := time.Now()
start := end.Add(-24 * time.Hour * 30)
histories, err := client.NewGetMarginBorrowRepayHistoryRequest().
StartTime(start).
EndTime(end).
Asset("BNB").
IsolatedSymbol("BNBUSDT").
SetBorrowRepayType(BorrowRepayTypeBorrow).
Do(ctx)
assert.NoError(t, err)
assert.NotNil(t, histories)
assert.NotEmpty(t, histories)
t.Logf("result: %+v", histories)

res, err = client.NewPlaceMarginOrderRequest().
Asset("USDT").
Amount(fixedpoint.NewFromFloat(5)).
IsIsolated(true).
Symbol("BNBUSDT").
SetBorrowRepayType(BorrowRepayTypeRepay).
Do(ctx)
assert.NoError(t, err)
assert.NotNil(t, res)
assert.NotEmpty(t, res)
t.Logf("result: %+v", res)
}
28 changes: 28 additions & 0 deletions pkg/exchange/binance/binanceapi/place_margin_order_request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package binanceapi

import (
"github.com/c9s/requestgen"

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

//go:generate requestgen -method POST -url "/sapi/v1/margin/borrow-repay" -type PlaceMarginOrderRequest -responseType .TransferResponse
type PlaceMarginOrderRequest struct {
client requestgen.AuthenticatedAPIClient

asset string `param:"asset"`

// TRUE for Isolated Margin, FALSE for Cross Margin, Default FALSE
isIsolated bool `param:"isIsolated"`

// Only for Isolated margin
symbol *string `param:"symbol"`

amount fixedpoint.Value `param:"amount"`

BorrowRepayType BorrowRepayType `param:"type"`
}

func (c *RestClient) NewPlaceMarginOrderRequest() *PlaceMarginOrderRequest {
return &PlaceMarginOrderRequest{client: c}
}

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

4 changes: 0 additions & 4 deletions pkg/exchange/binance/binanceapi/transfer_asset_request.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package binanceapi

import (
"time"

"github.com/c9s/requestgen"
)

Expand All @@ -29,8 +27,6 @@ type TransferAssetRequest struct {

amount string `param:"amount"`

timestamp time.Time `param:"timestamp,milliseconds,query"`

fromSymbol *string `param:"fromSymbol"`
toSymbol *string `param:"toSymbol"`
}
Expand Down

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

Loading