forked from adshao/go-binance
/
position_service.go
268 lines (239 loc) · 6.27 KB
/
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
package futures
import (
"context"
"encoding/json"
"net/http"
)
// ChangeLeverageService change user's initial leverage of specific symbol market
type ChangeLeverageService struct {
c *Client
symbol string
leverage int
}
// Symbol set symbol
func (s *ChangeLeverageService) Symbol(symbol string) *ChangeLeverageService {
s.symbol = symbol
return s
}
// Leverage set leverage
func (s *ChangeLeverageService) Leverage(leverage int) *ChangeLeverageService {
s.leverage = leverage
return s
}
// Do send request
func (s *ChangeLeverageService) Do(ctx context.Context, opts ...RequestOption) (res *SymbolLeverage, err error) {
r := &request{
method: http.MethodPost,
endpoint: "/fapi/v1/leverage",
secType: secTypeSigned,
}
r.setFormParams(params{
"symbol": s.symbol,
"leverage": s.leverage,
})
data, _, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return nil, err
}
res = new(SymbolLeverage)
err = json.Unmarshal(data, &res)
if err != nil {
return nil, err
}
return res, nil
}
// SymbolLeverage define leverage info of symbol
type SymbolLeverage struct {
Leverage int `json:"leverage"`
MaxNotionalValue string `json:"maxNotionalValue"`
Symbol string `json:"symbol"`
}
// ChangeMarginTypeService change user's margin type of specific symbol market
type ChangeMarginTypeService struct {
c *Client
symbol string
marginType MarginType
}
// Symbol set symbol
func (s *ChangeMarginTypeService) Symbol(symbol string) *ChangeMarginTypeService {
s.symbol = symbol
return s
}
// MarginType set margin type
func (s *ChangeMarginTypeService) MarginType(marginType MarginType) *ChangeMarginTypeService {
s.marginType = marginType
return s
}
// Do send request
func (s *ChangeMarginTypeService) Do(ctx context.Context, opts ...RequestOption) (err error) {
r := &request{
method: http.MethodPost,
endpoint: "/fapi/v1/marginType",
secType: secTypeSigned,
}
r.setFormParams(params{
"symbol": s.symbol,
"marginType": s.marginType,
})
_, _, err = s.c.callAPI(ctx, r, opts...)
if err != nil {
return err
}
return nil
}
// UpdatePositionMarginService update isolated position margin
type UpdatePositionMarginService struct {
c *Client
symbol string
positionSide *PositionSideType
amount string
actionType int
}
// Symbol set symbol
func (s *UpdatePositionMarginService) Symbol(symbol string) *UpdatePositionMarginService {
s.symbol = symbol
return s
}
// Side set side
func (s *UpdatePositionMarginService) PositionSide(positionSide PositionSideType) *UpdatePositionMarginService {
s.positionSide = &positionSide
return s
}
// Amount set position margin amount
func (s *UpdatePositionMarginService) Amount(amount string) *UpdatePositionMarginService {
s.amount = amount
return s
}
// Type set action type: 1: Add postion margin,2: Reduce postion margin
func (s *UpdatePositionMarginService) Type(actionType int) *UpdatePositionMarginService {
s.actionType = actionType
return s
}
// Do send request
func (s *UpdatePositionMarginService) Do(ctx context.Context, opts ...RequestOption) (err error) {
r := &request{
method: http.MethodPost,
endpoint: "/fapi/v1/positionMargin",
secType: secTypeSigned,
}
m := params{
"symbol": s.symbol,
"amount": s.amount,
"type": s.actionType,
}
if s.positionSide != nil {
m["positionSide"] = *s.positionSide
}
r.setFormParams(m)
_, _, err = s.c.callAPI(ctx, r, opts...)
if err != nil {
return err
}
return nil
}
// ChangePositionModeService change user's position mode
type ChangePositionModeService struct {
c *Client
dualSide bool
}
// Change user's position mode: true - Hedge Mode, false - One-way Mode
func (s *ChangePositionModeService) DualSide(dualSide bool) *ChangePositionModeService {
s.dualSide = dualSide
return s
}
// Do send request
func (s *ChangePositionModeService) Do(ctx context.Context, opts ...RequestOption) (err error) {
r := &request{
method: http.MethodPost,
endpoint: "/fapi/v1/positionSide/dual",
secType: secTypeSigned,
}
r.setFormParams(params{
"dualSidePosition": s.dualSide,
})
_, _, err = s.c.callAPI(ctx, r, opts...)
if err != nil {
return err
}
return nil
}
// GetPositionModeService get user's position mode
type GetPositionModeService struct {
c *Client
}
// Response of user's position mode
type PositionMode struct {
DualSidePosition bool `json:"dualSidePosition"`
}
// Do send request
func (s *GetPositionModeService) Do(ctx context.Context, opts ...RequestOption) (res *PositionMode, err error) {
r := &request{
method: http.MethodGet,
endpoint: "/fapi/v1/positionSide/dual",
secType: secTypeSigned,
}
r.setFormParams(params{})
data, _, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return nil, err
}
res = &PositionMode{}
err = json.Unmarshal(data, &res)
if err != nil {
return nil, err
}
return res, nil
}
// ChangeMultiAssetModeService change user's multi-asset mode
type ChangeMultiAssetModeService struct {
c *Client
multiAssetsMargin bool
}
// MultiAssetsMargin set multiAssetsMargin
func (s *ChangeMultiAssetModeService) MultiAssetsMargin(multiAssetsMargin bool) *ChangeMultiAssetModeService {
s.multiAssetsMargin = multiAssetsMargin
return s
}
// Do send request
func (s *ChangeMultiAssetModeService) Do(ctx context.Context, opts ...RequestOption) (err error) {
r := &request{
method: http.MethodPost,
endpoint: "/fapi/v1/multiAssetsMargin",
secType: secTypeSigned,
}
r.setFormParams(params{
"multiAssetsMargin": s.multiAssetsMargin,
})
_, _, err = s.c.callAPI(ctx, r, opts...)
if err != nil {
return err
}
return nil
}
// GetMultiAssetModeService get user's multi-asset mode
type GetMultiAssetModeService struct {
c *Client
}
// Response of user's multi-asset mode
type MultiAssetMode struct {
MultiAssetsMargin bool `json:"multiAssetsMargin"`
}
// Do send request
func (s *GetMultiAssetModeService) Do(ctx context.Context, opts ...RequestOption) (res *MultiAssetMode, err error) {
r := &request{
method: http.MethodGet,
endpoint: "/fapi/v1/multiAssetsMargin",
secType: secTypeSigned,
}
r.setFormParams(params{})
data, _, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return nil, err
}
res = &MultiAssetMode{}
err = json.Unmarshal(data, &res)
if err != nil {
return nil, err
}
return res, nil
}