Skip to content

Commit

Permalink
feat: v5 set tpsl mode (#115)
Browse files Browse the repository at this point in the history
* feat: implement

* test: integration

* test: unit

* docs: update
  • Loading branch information
hirokisan authored Apr 3, 2023
1 parent e131f45 commit cf09cb0
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ The following API endpoints have been implemented
- [`/v5/position/set-leverage` Set Position](https://bybit-exchange.github.io/docs/v5/position/leverage)
- [`/v5/position/trading-stop` Set Trading Stop](https://bybit-exchange.github.io/docs/v5/position/trading-stop)
- [`/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)

#### Order

Expand Down
38 changes: 38 additions & 0 deletions integrationtest/v5/position/position_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,44 @@ func TestSetLeverage(t *testing.T) {
}
}

func TestSetTpSlMode(t *testing.T) {
client := bybit.NewTestClient().WithAuthFromEnv()
category := bybit.CategoryV5Linear
symbol := bybit.SymbolV5BTCUSDT
qty := "0.01"
{
_, err := client.V5().Order().CreateOrder(bybit.V5CreateOrderParam{
Category: category,
Symbol: symbol,
Side: bybit.SideBuy,
OrderType: bybit.OrderTypeMarket,
Qty: qty,
})
require.NoError(t, err)
}
res, err := client.V5().Position().SetTpSlMode(bybit.V5SetTpSlModeParam{
Category: category,
Symbol: symbol,
TpSlMode: bybit.TpSlModeFull,
})
require.NoError(t, err)
{
goldenFilename := "./testdata/v5-position-set-tpsl-mode.json"
testhelper.Compare(t, goldenFilename, testhelper.ConvertToJSON(res.Result))
testhelper.UpdateFile(t, goldenFilename, testhelper.ConvertToJSON(res.Result))
}
{
_, err := client.V5().Order().CreateOrder(bybit.V5CreateOrderParam{
Category: category,
Symbol: symbol,
Side: bybit.SideSell,
OrderType: bybit.OrderTypeMarket,
Qty: qty,
})
require.NoError(t, err)
}
}

func TestSetTradingStop(t *testing.T) {
client := bybit.NewTestClient().WithAuthFromEnv()
category := bybit.CategoryV5Linear
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"tpSlMode": "Full"
}
46 changes: 46 additions & 0 deletions v5_position_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type V5PositionServiceI interface {
GetPositionInfo(V5GetPositionInfoParam) (*V5GetPositionInfoResponse, error)
SetLeverage(V5SetLeverageParam) (*V5SetLeverageResponse, error)
SetTradingStop(V5SetTradingStopParam) (*V5SetTradingStopResponse, error)
SetTpSlMode(V5SetTpSlModeParam) (*V5SetTpSlModeResponse, error)
SwitchPositionMode(V5SwitchPositionModeParam) (*V5SwitchPositionModeResponse, error)
}

Expand Down Expand Up @@ -177,6 +178,51 @@ func (s *V5PositionService) SetTradingStop(param V5SetTradingStopParam) (*V5SetT
return &res, nil
}

// V5SetTpSlModeParam :
type V5SetTpSlModeParam struct {
Category CategoryV5 `json:"category"`
Symbol SymbolV5 `json:"symbol"`
TpSlMode TpSlMode `json:"tpSlMode"`
}

func (p V5SetTpSlModeParam) validate() error {
if p.Category != CategoryV5Linear && p.Category != CategoryV5Inverse {
return fmt.Errorf("only linear and inverse are supported for category")
}
return nil
}

// V5SetTpSlModeResponse :
type V5SetTpSlModeResponse struct {
CommonV5Response `json:",inline"`
Result V5SetTpSlModeResult `json:"result"`
}

// V5SetTpSlModeResult :
type V5SetTpSlModeResult struct {
TpSlMode TpSlMode `json:"tpSlMode"`
}

// SetTpSlMode :
func (s *V5PositionService) SetTpSlMode(param V5SetTpSlModeParam) (*V5SetTpSlModeResponse, error) {
var res V5SetTpSlModeResponse

if err := param.validate(); err != nil {
return nil, fmt.Errorf("validate param: %w", err)
}

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

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

return &res, nil
}

// V5SwitchPositionModeParam :
type V5SwitchPositionModeParam struct {
Category CategoryV5 `json:"category"`
Expand Down
65 changes: 65 additions & 0 deletions v5_position_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,71 @@ func TestV5Position_SetTradingStop(t *testing.T) {
})
}

func TestV5Position_SetTpSlMode(t *testing.T) {
t.Run("success", func(t *testing.T) {
param := V5SetTpSlModeParam{
Category: CategoryV5Linear,
Symbol: SymbolV5BTCUSDT,
TpSlMode: TpSlModeFull,
}

path := "/v5/position/set-tpsl-mode"
method := http.MethodPost
status := http.StatusOK
respBody := map[string]interface{}{
"result": map[string]interface{}{
"tpSlMode": "Full",
},
}
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().SetTpSlMode(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 := V5SetTpSlModeParam{
Category: CategoryV5Linear,
Symbol: SymbolV5BTCUSDT,
TpSlMode: TpSlModeFull,
}

path := "/v5/position/set-tpsl-mode"
method := http.MethodPost
status := http.StatusOK
respBody := map[string]interface{}{
"result": map[string]interface{}{
"tpSlMode": "Full",
},
}
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().SetTpSlMode(param)
assert.Error(t, err)
})
}

func TestV5Position_SwitchPositionMode(t *testing.T) {
t.Run("success", func(t *testing.T) {
coin := CoinBTC
Expand Down

0 comments on commit cf09cb0

Please sign in to comment.