Skip to content

Commit

Permalink
support /fapi/v3/positionRisk (#644)
Browse files Browse the repository at this point in the history
  • Loading branch information
xyq-c-cpp authored Dec 5, 2024
1 parent a85b442 commit 6d882fc
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 13 deletions.
5 changes: 4 additions & 1 deletion v2/futures/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}
Expand Down
81 changes: 74 additions & 7 deletions v2/futures/position_risk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"`
Expand All @@ -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"`
}
100 changes: 95 additions & 5 deletions v2/futures/position_risk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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")
Expand All @@ -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")
}

0 comments on commit 6d882fc

Please sign in to comment.