forked from adshao/go-binance
-
Notifications
You must be signed in to change notification settings - Fork 1
/
position_service.go
213 lines (190 loc) · 4.84 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
package delivery
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: "/dapi/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"`
MaxQuantity string `json:"maxQty"`
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: "/dapi/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: "/dapi/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 string
}
// Change user's position mode: true - Hedge Mode, false - One-way Mode
func (s *ChangePositionModeService) DualSide(dualSide bool) *ChangePositionModeService {
if dualSide {
s.dualSide = "true"
} else {
s.dualSide = "false"
}
return s
}
// Do send request
func (s *ChangePositionModeService) Do(ctx context.Context, opts ...RequestOption) (err error) {
r := &request{
method: http.MethodPost,
endpoint: "/dapi/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: "/dapi/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
}