forked from hirokisan/bybit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathv5_position_service.go
331 lines (273 loc) · 9.89 KB
/
v5_position_service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package bybit
import (
"encoding/json"
"fmt"
"github.com/google/go-querystring/query"
)
// V5PositionServiceI :
type V5PositionServiceI interface {
GetPositionInfo(V5GetPositionInfoParam) (*V5GetPositionInfoResponse, error)
SetLeverage(V5SetLeverageParam) (*V5SetLeverageResponse, error)
SetTradingStop(V5SetTradingStopParam) (*V5SetTradingStopResponse, error)
SetTpSlMode(V5SetTpSlModeParam) (*V5SetTpSlModeResponse, error)
SwitchPositionMode(V5SwitchPositionModeParam) (*V5SwitchPositionModeResponse, error)
GetClosedPnL(V5GetClosedPnLParam) (*V5GetClosedPnLResponse, error)
}
// V5PositionService :
type V5PositionService struct {
client *Client
}
// V5GetPositionInfoParam :
type V5GetPositionInfoParam struct {
Category CategoryV5 `url:"category"`
Symbol *SymbolV5 `url:"symbol,omitempty"`
BaseCoin *Coin `url:"baseCoin,omitempty"` // option only
SettleCoin *Coin `url:"settleCoin,omitempty"` // Settle coin. For linear & inverse, either symbol or settleCon is required. symbol has a higher priority
Limit *int `url:"limit,omitempty"` // Limit for data size per page. [1, 200]. Default: 200
Cursor *string `url:"cursor,omitempty"` // Cursor. Used for pagination
}
// V5GetPositionInfoResponse :
type V5GetPositionInfoResponse struct {
CommonV5Response `json:",inline"`
Result V5GetPositionInfoResult `json:"result"`
}
// V5GetPositionInfoResult :
type V5GetPositionInfoResult struct {
Category CategoryV5 `json:"category"`
NextPageCursor string `json:"nextPageCursor"`
List V5GetPositionInfoList `json:"list"`
}
// V5GetPositionInfoList :
type V5GetPositionInfoList []V5GetPositionInfoItem
// V5GetPositionInfoItem :
type V5GetPositionInfoItem struct {
Symbol SymbolV5 `json:"symbol"`
Leverage string `json:"leverage"`
AvgPrice string `json:"avgPrice"`
LiqPrice string `json:"liqPrice"`
RiskLimitValue string `json:"riskLimitValue"`
TakeProfit string `json:"takeProfit"`
PositionValue string `json:"positionValue"`
TpSlMode TpSlMode `json:"tpslMode"`
RiskID int `json:"riskId"`
TrailingStop string `json:"trailingStop"`
UnrealisedPnl string `json:"unrealisedPnl"`
MarkPrice string `json:"markPrice"`
CumRealisedPnl string `json:"cumRealisedPnl"`
PositionMM string `json:"positionMM"`
CreatedTime string `json:"createdTime"`
PositionIdx int `json:"positionIdx"`
PositionIM string `json:"positionIM"`
UpdatedTime string `json:"updatedTime"`
Side Side `json:"side"`
BustPrice string `json:"bustPrice"`
Size string `json:"size"`
PositionStatus string `json:"positionStatus"`
StopLoss string `json:"stopLoss"`
TradeMode int `json:"tradeMode"`
}
// GetPositionInfo :
func (s *V5PositionService) GetPositionInfo(param V5GetPositionInfoParam) (*V5GetPositionInfoResponse, error) {
var res V5GetPositionInfoResponse
queryString, err := query.Values(param)
if err != nil {
return nil, err
}
if err := s.client.getV5Privately("/v5/position/list", queryString, &res); err != nil {
return nil, err
}
return &res, nil
}
// V5SetLeverageParam :
type V5SetLeverageParam struct {
Category CategoryV5 `json:"category"`
Symbol SymbolV5 `json:"symbol"`
BuyLeverage string `json:"buyLeverage"`
SellLeverage string `json:"sellLeverage"`
}
// V5SetLeverageResponse :
type V5SetLeverageResponse struct {
CommonV5Response `json:",inline"`
Result interface{} `json:"result"`
}
// SetLeverage :
func (s *V5PositionService) SetLeverage(param V5SetLeverageParam) (*V5SetLeverageResponse, error) {
var res V5SetLeverageResponse
if param.Category == "" || param.Symbol == "" || param.BuyLeverage == "" || param.SellLeverage == "" {
return nil, fmt.Errorf("Category, Symbol, BuyLeverage and SellLeverage needed")
}
body, err := json.Marshal(param)
if err != nil {
return &res, fmt.Errorf("json marshal: %w", err)
}
if err := s.client.postV5JSON("/v5/position/set-leverage", body, &res); err != nil {
return &res, err
}
return &res, nil
}
// V5SetTradingStopParam :
type V5SetTradingStopParam struct {
Category CategoryV5 `json:"category"`
Symbol SymbolV5 `json:"symbol"`
PositionIdx PositionIdx `json:"positionIdx"`
TakeProfit *string `json:"takeProfit,omitempty"`
StopLoss *string `json:"stopLoss,omitempty"`
TrailingStop *string `json:"trailingStop,omitempty"`
TpTriggerBy *TriggerBy `json:"tpTriggerBy,omitempty"`
SlTriggerBy *TriggerBy `json:"slTriggerBy,omitempty"`
ActivePrice *string `json:"activePrice,omitempty"`
TpSize *string `json:"tpSize,omitempty"`
SlSize *string `json:"slSize,omitempty"`
}
func (p V5SetTradingStopParam) validate() error {
if p.Category != CategoryV5Linear && p.Category != CategoryV5Inverse {
return fmt.Errorf("only linear and inverse are supported for category")
}
if p.TakeProfit == nil && p.StopLoss == nil {
return fmt.Errorf("takeProfit or stopLoss needed")
}
return nil
}
// V5SetTradingStopResponse :
type V5SetTradingStopResponse struct {
CommonV5Response `json:",inline"`
Result interface{} `json:"result"` // no content
}
// SetTradingStop :
func (s *V5PositionService) SetTradingStop(param V5SetTradingStopParam) (*V5SetTradingStopResponse, error) {
var res V5SetTradingStopResponse
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/trading-stop", body, &res); err != nil {
return &res, err
}
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"`
Mode PositionMode `json:"mode"`
Symbol *SymbolV5 `json:"symbol,omitempty"`
Coin *Coin `json:"coin,omitempty"`
}
func (p V5SwitchPositionModeParam) validate() error {
if p.Symbol == nil && p.Coin == nil {
return fmt.Errorf("symbol or coin is required")
}
return nil
}
// V5SwitchPositionModeResponse :
type V5SwitchPositionModeResponse struct {
CommonV5Response `json:",inline"`
Result interface{} `json:"result"` // no content
}
// SwitchPositionMode :
func (s *V5PositionService) SwitchPositionMode(param V5SwitchPositionModeParam) (*V5SwitchPositionModeResponse, error) {
var res V5SwitchPositionModeResponse
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/switch-mode", body, &res); err != nil {
return &res, err
}
return &res, nil
}
// V5GetClosedPnLParam :
type V5GetClosedPnLParam struct {
Category CategoryV5 `url:"category"`
Symbol *SymbolV5 `url:"symbol,omitempty"`
StartTime *int64 `url:"startTime,omitempty"` // The start timestamp (ms)
EndTime *int64 `url:"endTime,omitempty"` // The start timestamp (ms)
Limit *int `url:"limit,omitempty"` // Limit for data size per page. [1, 100]. Default: 50
Cursor *string `url:"cursor,omitempty"`
}
// V5GetClosedPnLResponse :
type V5GetClosedPnLResponse struct {
CommonV5Response `json:",inline"`
Result V5GetClosedPnLResult `json:"result"`
}
// V5GetClosedPnLResult :
type V5GetClosedPnLResult struct {
Category CategoryV5 `json:"category"`
NextPageCursor string `json:"nextPageCursor"`
List V5GetClosedPnLList `json:"list"`
}
// V5GetClosedPnLList :
type V5GetClosedPnLList []V5GetClosedPnLItem
// V5GetClosedPnLItem :
type V5GetClosedPnLItem struct {
Symbol SymbolV5 `json:"symbol"`
OrderID string `json:"orderId"`
Side Side `json:"side"`
Qty string `json:"qty"`
OrderPrice string `json:"orderPrice"`
OrderType OrderType `json:"orderType"`
ExecType ExecTypeV5 `json:"execType"`
ClosedSize string `json:"closedSize"`
CumEntryValue string `json:"cumEntryValue"`
AvgEntryPrice string `json:"avgEntryPrice"`
CumExitValue string `json:"cumExitValue"`
AvgExitPrice string `json:"avgExitPrice"`
ClosedPnl string `json:"closedPnl"`
FillCount string `json:"fillCount"`
Leverage string `json:"leverage"`
CreatedTime string `json:"createdTime"`
UpdatedTime string `json:"updatedTime"`
}
// GetClosedPnL :
func (s *V5PositionService) GetClosedPnL(param V5GetClosedPnLParam) (*V5GetClosedPnLResponse, error) {
var res V5GetClosedPnLResponse
queryString, err := query.Values(param)
if err != nil {
return nil, err
}
if err := s.client.getV5Privately("/v5/position/closed-pnl", queryString, &res); err != nil {
return nil, err
}
return &res, nil
}