Skip to content

Commit

Permalink
feat: v5 set risk limit (#137)
Browse files Browse the repository at this point in the history
* feat: implement

* test: integration

* test: unit

* docs: update
  • Loading branch information
hirokisan authored Aug 11, 2023
1 parent e03fd39 commit fc1d4be
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ The following API endpoints have been implemented
- [`/v5/position/switch-mode` Switch Position Mode](https://bybit-exchange.github.io/docs/v5/position/position-mode)
- [`/v5/position/set-tpsl-mode` Set TP/SL Mode](https://bybit-exchange.github.io/docs/v5/position/tpsl-mode)
- [`/v5/position/closed-pnl` Get Closed PnL](https://bybit-exchange.github.io/docs/v5/position/close-pnl)
- [`/v5/position/set-risk-limit` Set Risk Limit](https://bybit-exchange.github.io/docs/v5/position/set-risk-limit)

#### Order

Expand Down
16 changes: 16 additions & 0 deletions integrationtest/v5/position/position_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,19 @@ func TestSwitchPositionMarginMode(t *testing.T) {
testhelper.UpdateFile(t, goldenFilename, testhelper.ConvertToJSON(res.Result))
}
}

func TestSetRiskLimit(t *testing.T) {
client := bybit.NewTestClient().WithAuthFromEnv()

res, err := client.V5().Position().SetRiskLimit(bybit.V5SetRiskLimitParam{
Category: bybit.CategoryV5Linear,
Symbol: bybit.SymbolV5BTCUSDT,
RiskID: 3,
})
require.NoError(t, err)
{
goldenFilename := "./testdata/v5-position-set-risk-limit.json"
testhelper.Compare(t, goldenFilename, testhelper.ConvertToJSON(res.Result))
testhelper.UpdateFile(t, goldenFilename, testhelper.ConvertToJSON(res.Result))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"category": "linear",
"riskId": 3,
"riskLimitValue": "6000000"
}
39 changes: 39 additions & 0 deletions v5_position_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type V5PositionServiceI interface {
SwitchPositionMode(V5SwitchPositionModeParam) (*V5SwitchPositionModeResponse, error)
GetClosedPnL(V5GetClosedPnLParam) (*V5GetClosedPnLResponse, error)
SwitchPositionMarginMode(V5SwitchPositionMarginModeParam) (*V5SwitchPositionMarginModeResponse, error)
SetRiskLimit(V5SetRiskLimitParam) (*V5SetRiskLimitResponse, error)
}

// V5PositionService :
Expand Down Expand Up @@ -380,3 +381,41 @@ func (s *V5PositionService) SwitchPositionMarginMode(param V5SwitchPositionMargi

return &res, nil
}

// V5SetRiskLimitParam :
type V5SetRiskLimitParam struct {
Category CategoryV5 `json:"category"`
Symbol SymbolV5 `json:"symbol"`
RiskID int64 `json:"riskId"`

PositionIdx *PositionIdx `json:"positionIdx,omitempty"`
}

// V5SetRiskLimitResponse :
type V5SetRiskLimitResponse struct {
CommonV5Response `json:",inline"`
Result V5SetRiskLimitResult `json:"result"`
}

// V5SetRiskLimitResult :
type V5SetRiskLimitResult struct {
Category CategoryV5 `json:"category"`
RiskID int64 `json:"riskId"`
RiskLimitValue string `json:"riskLimitValue"`
}

// SetRiskLimit :
func (s *V5PositionService) SetRiskLimit(param V5SetRiskLimitParam) (*V5SetRiskLimitResponse, error) {
var res V5SetRiskLimitResponse

body, err := json.Marshal(param)
if err != nil {
return &res, fmt.Errorf("json marshal: %w", err)
}

if err := s.client.postV5JSON("/v5/position/set-risk-limit", body, &res); err != nil {
return &res, err
}

return &res, nil
}
69 changes: 69 additions & 0 deletions v5_position_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -558,3 +558,72 @@ func TestV5Position_SwitchPositionMarginMode(t *testing.T) {
assert.Error(t, err)
})
}

func TestV5Position_SetRiskLimit(t *testing.T) {
t.Run("success", func(t *testing.T) {
param := V5SetRiskLimitParam{
Category: CategoryV5Linear,
Symbol: SymbolV5BTCUSDT,
RiskID: 3,
}

path := "/v5/position/set-risk-limit"
method := http.MethodPost
status := http.StatusOK
respBody := map[string]interface{}{
"result": map[string]interface{}{
"category": "linear",
"riskId": 3,
"riskLimitValue": "6000000",
},
}
bytesBody, err := json.Marshal(respBody)
require.NoError(t, err)

server, teardown := testhelper.NewServer(
testhelper.WithHandlerOption(path, method, status, bytesBody),
)
defer teardown()

client := NewTestClient().
WithBaseURL(server.URL).
WithAuth("test", "test")

resp, err := client.V5().Position().SetRiskLimit(param)
require.NoError(t, err)

require.NotNil(t, resp)
testhelper.Compare(t, respBody["result"], resp.Result)
})
t.Run("authentication required", func(t *testing.T) {
param := V5SetRiskLimitParam{
Category: CategoryV5Linear,
Symbol: SymbolV5BTCUSDT,
RiskID: 3,
}

path := "/v5/position/set-risk-limit"
method := http.MethodPost
status := http.StatusOK
respBody := map[string]interface{}{
"result": map[string]interface{}{
"category": "linear",
"riskId": 3,
"riskLimitValue": "6000000",
},
}
bytesBody, err := json.Marshal(respBody)
require.NoError(t, err)

server, teardown := testhelper.NewServer(
testhelper.WithHandlerOption(path, method, status, bytesBody),
)
defer teardown()

client := NewTestClient().
WithBaseURL(server.URL)

_, err = client.V5().Position().SetRiskLimit(param)
assert.Error(t, err)
})
}

0 comments on commit fc1d4be

Please sign in to comment.