From 6d882fce1deb49d8b5c13d5e9cdc06b859796aac Mon Sep 17 00:00:00 2001 From: ddxyq <657023321@qq.com> Date: Thu, 5 Dec 2024 22:08:57 +0800 Subject: [PATCH] support /fapi/v3/positionRisk (#644) --- v2/futures/client.go | 5 +- v2/futures/position_risk.go | 81 ++++++++++++++++++++++--- v2/futures/position_risk_test.go | 100 +++++++++++++++++++++++++++++-- 3 files changed, 173 insertions(+), 13 deletions(-) diff --git a/v2/futures/client.go b/v2/futures/client.go index ed4ae426..17c6f54a 100644 --- a/v2/futures/client.go +++ b/v2/futures/client.go @@ -525,7 +525,10 @@ func (c *Client) NewGetBalanceService() *GetBalanceService { return &GetBalanceService{c: c} } -// NewGetPositionRiskService init getting position risk service +func (c *Client) NewGetPositionRiskV2Service() *GetPositionRiskV2Service { + return &GetPositionRiskV2Service{c: c} +} + func (c *Client) NewGetPositionRiskService() *GetPositionRiskService { return &GetPositionRiskService{c: c} } diff --git a/v2/futures/position_risk.go b/v2/futures/position_risk.go index a22b9fa4..b9930c68 100644 --- a/v2/futures/position_risk.go +++ b/v2/futures/position_risk.go @@ -7,19 +7,19 @@ import ( ) // GetPositionRiskService get account balance -type GetPositionRiskService struct { +type GetPositionRiskV2Service struct { c *Client symbol string } // Symbol set symbol -func (s *GetPositionRiskService) Symbol(symbol string) *GetPositionRiskService { +func (s *GetPositionRiskV2Service) Symbol(symbol string) *GetPositionRiskV2Service { s.symbol = symbol return s } // Do send request -func (s *GetPositionRiskService) Do(ctx context.Context, opts ...RequestOption) (res []*PositionRisk, err error) { +func (s *GetPositionRiskV2Service) Do(ctx context.Context, opts ...RequestOption) (res []*PositionRiskV2, err error) { r := &request{ method: http.MethodGet, endpoint: "/fapi/v2/positionRisk", @@ -30,18 +30,18 @@ func (s *GetPositionRiskService) Do(ctx context.Context, opts ...RequestOption) } data, _, err := s.c.callAPI(ctx, r, opts...) if err != nil { - return []*PositionRisk{}, err + return []*PositionRiskV2{}, err } - res = make([]*PositionRisk, 0) + res = make([]*PositionRiskV2, 0) err = json.Unmarshal(data, &res) if err != nil { - return []*PositionRisk{}, err + return []*PositionRiskV2{}, err } return res, nil } // PositionRisk define position risk info -type PositionRisk struct { +type PositionRiskV2 struct { EntryPrice string `json:"entryPrice"` BreakEvenPrice string `json:"breakEvenPrice"` MarginType string `json:"marginType"` @@ -58,3 +58,70 @@ type PositionRisk struct { Notional string `json:"notional"` IsolatedWallet string `json:"isolatedWallet"` } + +type GetPositionRiskService struct { + c *Client + symbol string + recvWindow *int64 +} + +// Symbol set symbol +func (s *GetPositionRiskService) Symbol(symbol string) *GetPositionRiskService { + s.symbol = symbol + return s +} + +func (s *GetPositionRiskService) RecvWindow(rw int64) *GetPositionRiskService { + s.recvWindow = &rw + return s +} + +// Do send request +func (s *GetPositionRiskService) Do(ctx context.Context, opts ...RequestOption) (res []*PositionRisk, err error) { + r := &request{ + method: http.MethodGet, + endpoint: "/fapi/v3/positionRisk", + secType: secTypeSigned, + } + if s.symbol != "" { + r.setParam("symbol", s.symbol) + } + if s.recvWindow != nil { + r.recvWindow = *s.recvWindow + } + + data, _, err := s.c.callAPI(ctx, r, opts...) + if err != nil { + return []*PositionRisk{}, err + } + res = make([]*PositionRisk, 0) + err = json.Unmarshal(data, &res) + if err != nil { + return []*PositionRisk{}, err + } + return res, nil +} + +// PositionRisk define position risk info +type PositionRisk struct { + Symbol string `json:"symbol"` + PositionSide string `json:"positionSide"` + PositionAmt string `json:"positionAmt"` + EntryPrice string `json:"entryPrice"` + BreakEvenPrice string `json:"breakEvenPrice"` + MarkPrice string `json:"markPrice"` + UnRealizedProfit string `json:"unRealizedProfit"` + LiquidationPrice string `json:"liquidationPrice"` + IsolatedMargin string `json:"isolatedMargin"` + Notional string `json:"notional"` + MarginAsset string `json:"marginAsset"` + IsolatedWallet string `json:"isolatedWallet"` + InitialMargin string `json:"initialMargin"` + MaintMargin string `json:"maintMargin"` + PositionInitialMargin string `json:"positionInitialMargin"` + OpenOrderInitialMargin string `json:"openOrderInitialMargin"` + Adl int64 `json:"adl"` + BidNotional string `json:"bidNotional"` + AskNotional string `json:"askNotional"` + UpdateTime int64 `json:"updateTime"` +} diff --git a/v2/futures/position_risk_test.go b/v2/futures/position_risk_test.go index 0e3fb611..b607571e 100644 --- a/v2/futures/position_risk_test.go +++ b/v2/futures/position_risk_test.go @@ -14,7 +14,7 @@ func TestPositionRiskTestService(t *testing.T) { suite.Run(t, new(positionRiskServiceTestSuite)) } -func (s *positionRiskServiceTestSuite) TestGetPositionRisk() { +func (s *positionRiskServiceTestSuite) TestGetPositionRiskV2() { data := []byte(`[ { "entryPrice": "10359.38000", @@ -44,12 +44,12 @@ func (s *positionRiskServiceTestSuite) TestGetPositionRisk() { }) s.assertRequestEqual(e, r) }) - res, err := s.client.NewGetPositionRiskService().Symbol(symbol). + res, err := s.client.NewGetPositionRiskV2Service().Symbol(symbol). Do(newContext(), WithRecvWindow(recvWindow)) r := s.r() r.NoError(err) r.Len(res, 1) - e := &PositionRisk{ + e := &PositionRiskV2{ EntryPrice: "10359.38000", BreakEvenPrice: "10387.38000", MarginType: "isolated", @@ -64,10 +64,10 @@ func (s *positionRiskServiceTestSuite) TestGetPositionRisk() { UnRealizedProfit: "-0.03331353", PositionSide: "BOTH", } - s.assertPositionRiskEqual(e, res[0]) + s.assertPositionRiskV2Equal(e, res[0]) } -func (s *positionRiskServiceTestSuite) assertPositionRiskEqual(e, a *PositionRisk) { +func (s *positionRiskServiceTestSuite) assertPositionRiskV2Equal(e, a *PositionRiskV2) { r := s.r() r.Equal(e.EntryPrice, a.EntryPrice, "EntryPrice") r.Equal(e.BreakEvenPrice, a.BreakEvenPrice, "BreakEvenPrice") @@ -83,3 +83,93 @@ func (s *positionRiskServiceTestSuite) assertPositionRiskEqual(e, a *PositionRis r.Equal(e.UnRealizedProfit, a.UnRealizedProfit, "UnRealizedProfit") r.Equal(e.PositionSide, a.PositionSide, "PositionSide") } + +func (s *positionRiskServiceTestSuite) TestGetPositionRisk() { + data := []byte(`[ + { + "symbol": "BTCUSDT", + "positionSide": "BOTH", + "positionAmt": "0.000", + "entryPrice": "0.0", + "breakEvenPrice": "0.0", + "markPrice": "96658.09948227", + "unRealizedProfit": "0.00000000", + "liquidationPrice": "0", + "isolatedMargin": "0", + "notional": "0", + "marginAsset": "USDT", + "isolatedWallet": "0", + "initialMargin": "23.75000000", + "maintMargin": "0", + "positionInitialMargin": "0", + "openOrderInitialMargin": "23.75000000", + "adl": 0, + "bidNotional": "190", + "askNotional": "0", + "updateTime": 0 + } +]`) + s.mockDo(data, nil) + defer s.assertDo() + + symbol := "BTCUSDT" + recvWindow := int64(1000) + s.assertReq(func(r *request) { + e := newSignedRequest().setParams(params{ + "symbol": symbol, + "recvWindow": recvWindow, + }) + s.assertRequestEqual(e, r) + }) + res, err := s.client.NewGetPositionRiskService().Symbol(symbol). + Do(newContext(), WithRecvWindow(recvWindow)) + r := s.r() + r.NoError(err) + r.Len(res, 1) + e := &PositionRisk{ + Symbol: "BTCUSDT", + PositionSide: "BOTH", + PositionAmt: "0.000", + EntryPrice: "0.0", + BreakEvenPrice: "0.0", + MarkPrice: "96658.09948227", + UnRealizedProfit: "0.00000000", + LiquidationPrice: "0", + IsolatedMargin: "0", + Notional: "0", + MarginAsset: "USDT", + IsolatedWallet: "0", + InitialMargin: "23.75000000", + MaintMargin: "0", + PositionInitialMargin: "0", + OpenOrderInitialMargin: "23.75000000", + Adl: 0, + BidNotional: "190", + AskNotional: "0", + UpdateTime: 0, + } + s.assertPositionRiskEqual(e, res[0]) +} + +func (s *positionRiskServiceTestSuite) assertPositionRiskEqual(e, a *PositionRisk) { + r := s.r() + r.Equal(e.Symbol, a.Symbol, "Symbol") + r.Equal(e.PositionSide, a.PositionSide, "PositionSide") + r.Equal(e.PositionAmt, a.PositionAmt, "PositionAmt") + r.Equal(e.EntryPrice, a.EntryPrice, "EntryPrice") + r.Equal(e.BreakEvenPrice, a.BreakEvenPrice, "BreakEvenPrice") + r.Equal(e.MarkPrice, a.MarkPrice, "MarkPrice") + r.Equal(e.UnRealizedProfit, a.UnRealizedProfit, "UnRealizedProfit") + r.Equal(e.LiquidationPrice, a.LiquidationPrice, "LiquidationPrice") + r.Equal(e.IsolatedMargin, a.IsolatedMargin, "IsolatedMargin") + r.Equal(e.Notional, a.Notional, "Notional") + r.Equal(e.MarginAsset, a.MarginAsset, "MarginAsset") + r.Equal(e.IsolatedWallet, a.IsolatedWallet, "IsolatedWallet") + r.Equal(e.InitialMargin, a.InitialMargin, "InitialMargin") + r.Equal(e.MaintMargin, a.MaintMargin, "MaintMargin") + r.Equal(e.PositionInitialMargin, a.PositionInitialMargin, "PositionInitialMargin") + r.Equal(e.Adl, a.Adl, "Adl") + r.Equal(e.BidNotional, a.BidNotional, "BidNotional") + r.Equal(e.AskNotional, a.AskNotional, "AskNotional") + r.Equal(e.UpdateTime, a.UpdateTime, "UpdateTime") +}