forked from amir-the-h/okex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
account.go
355 lines (318 loc) · 9.96 KB
/
account.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package rest
import (
"encoding/json"
"github.com/amir-the-h/okex"
requests "github.com/amir-the-h/okex/requests/rest/account"
responses "github.com/amir-the-h/okex/responses/account"
"net/http"
"strings"
)
// Account
//
// https://www.okex.com/docs-v5/en/#rest-api-account
type Account struct {
client *ClientRest
}
// NewAccount returns a pointer to a fresh Account
func NewAccount(c *ClientRest) *Account {
return &Account{c}
}
// GetBalance
// Retrieve a list of assets (with non-zero balance), remaining balance, and available amount in the account.
//
// https://www.okex.com/docs-v5/en/#rest-api-account-get-balance
func (c *Account) GetBalance(req requests.GetBalance) (response responses.GetBalance, err error) {
p := "/api/v5/account/balance"
m := okex.S2M(req)
if len(req.Ccy) > 0 {
m["ccy"] = strings.Join(req.Ccy, ",")
}
res, err := c.client.Do(http.MethodGet, p, true, m)
if err != nil {
return
}
defer res.Body.Close()
d := json.NewDecoder(res.Body)
err = d.Decode(&response)
return
}
// GetPositions
// Retrieve information on your positions. When the account is in net mode, net positions will be displayed, and when the account is in long/short mode, long or short positions will be displayed.
//
// https://www.okex.com/docs-v5/en/#rest-api-account-get-positions
func (c *Account) GetPositions(req requests.GetPositions) (response responses.GetPositions, err error) {
p := "/api/v5/account/positions"
m := okex.S2M(req)
if len(req.InstID) > 0 {
m["instId"] = strings.Join(req.InstID, ",")
}
if len(req.PosID) > 0 {
m["posId"] = strings.Join(req.PosID, ",")
}
res, err := c.client.Do(http.MethodGet, p, true, m)
if err != nil {
return
}
defer res.Body.Close()
d := json.NewDecoder(res.Body)
err = d.Decode(&response)
return
}
// GetAccountAndPositionRisk
// Get account and position risk
//
// https://www.okex.com/docs-v5/en/#rest-api-account-get-account-and-position-risk
func (c *Account) GetAccountAndPositionRisk(req requests.GetAccountAndPositionRisk) (response responses.GetAccountAndPositionRisk, err error) {
p := "/api/v5/account/positions"
m := okex.S2M(req)
res, err := c.client.Do(http.MethodGet, p, true, m)
if err != nil {
return
}
defer res.Body.Close()
d := json.NewDecoder(res.Body)
err = d.Decode(&response)
return
}
// GetBills
// Retrieve the bills of the account. The bill refers to all transaction records that result in changing the balance of an account. Pagination is supported, and the response is sorted with the most recent first. This endpoint can retrieve data from the last 7 days.
//
// https://www.okex.com/docs-v5/en/#rest-api-account-get-bills-details-last-7-days
//
// Retrieve the account’s bills. The bill refers to all transaction records that result in changing the balance of an account. Pagination is supported, and the response is sorted with most recent first. This endpoint can retrieve data from the last 3 months.
//
// https://www.okex.com/docs-v5/en/#rest-api-account-get-bills-details-last-3-months
func (c *Account) GetBills(req requests.GetBills, arc bool) (response responses.GetBills, err error) {
p := "/api/v5/account/bills"
if arc {
p = "/api/account/bills-archive"
}
m := okex.S2M(req)
res, err := c.client.Do(http.MethodGet, p, true, m)
if err != nil {
return
}
defer res.Body.Close()
d := json.NewDecoder(res.Body)
err = d.Decode(&response)
return
}
// GetConfig
// Retrieve current account configuration.
//
// https://www.okex.com/docs-v5/en/#rest-api-account-get-account-configuration
func (c *Account) GetConfig() (response responses.GetConfig, err error) {
p := "/api/v5/account/config"
res, err := c.client.Do(http.MethodGet, p, true)
if err != nil {
return
}
defer res.Body.Close()
d := json.NewDecoder(res.Body)
err = d.Decode(&response)
return
}
// SetPositionMode
// FUTURES and SWAP support both long/short mode and net mode. In net mode, users can only have positions in one direction; In long/short mode, users can hold positions in long and short directions.
//
// https://www.okex.com/docs-v5/en/#rest-api-account-set-position-mode
func (c *Account) SetPositionMode(req requests.SetPositionMode) (response responses.SetPositionMode, err error) {
p := "/api/v5/account/set-position-mode"
m := okex.S2M(req)
res, err := c.client.Do(http.MethodPost, p, true, m)
if err != nil {
return
}
defer res.Body.Close()
d := json.NewDecoder(res.Body)
err = d.Decode(&response)
return
}
// SetLeverage
// The following are the setting leverage cases for an instrument:
//
// Set leverage for isolated MARGIN at pairs level.
//
// Set leverage for cross MARGIN in Single-currency margin at pairs level.
//
// Set leverage for cross MARGIN in Multi-currency margin at currency level.
//
// Set leverage for cross/isolated FUTURES/SWAP at underlying/contract level.
// https://www.okex.com/docs-v5/en/#rest-api-account-set-leverage
func (c *Account) SetLeverage(req requests.SetLeverage) (response responses.Leverage, err error) {
p := "/api/v5/account/set-leverage"
m := okex.S2M(req)
res, err := c.client.Do(http.MethodPost, p, true, m)
if err != nil {
return
}
defer res.Body.Close()
d := json.NewDecoder(res.Body)
err = d.Decode(&response)
return
}
// GetMaxBuySellAmount
//
// https://www.okex.com/docs-v5/en/#rest-api-account-get-maximum-buy-sell-amount-or-open-amount
func (c *Account) GetMaxBuySellAmount(req requests.GetMaxBuySellAmount) (response responses.GetMaxBuySellAmount, err error) {
p := "/api/v5/account/max-size"
m := okex.S2M(req)
if len(req.InstID) > 0 {
m["instId"] = strings.Join(req.InstID, ",")
}
res, err := c.client.Do(http.MethodGet, p, true, m)
if err != nil {
return
}
defer res.Body.Close()
d := json.NewDecoder(res.Body)
err = d.Decode(&response)
return
}
// GetMaxAvailableTradeAmount
//
// https://www.okex.com/docs-v5/en/#rest-api-account-get-maximum-available-tradable-amount
func (c *Account) GetMaxAvailableTradeAmount(req requests.GetMaxAvailableTradeAmount) (response responses.GetMaxAvailableTradeAmount, err error) {
p := "/api/v5/account/max-avail-size"
m := okex.S2M(req)
res, err := c.client.Do(http.MethodGet, p, true, m)
if err != nil {
return
}
defer res.Body.Close()
d := json.NewDecoder(res.Body)
err = d.Decode(&response)
return
}
// IncreaseDecreaseMargin
// Increase or decrease the margin of the isolated position.
//
// https://www.okex.com/docs-v5/en/#rest-api-account-increase-decrease-margin
func (c *Account) IncreaseDecreaseMargin(req requests.IncreaseDecreaseMargin) (response responses.IncreaseDecreaseMargin, err error) {
p := "/api/v5/account/position/margin-balance"
m := okex.S2M(req)
res, err := c.client.Do(http.MethodPost, p, true, m)
if err != nil {
return
}
defer res.Body.Close()
d := json.NewDecoder(res.Body)
err = d.Decode(&response)
return
}
// GetLeverage
//
// https://www.okex.com/docs-v5/en/#rest-api-account-get-leverage
func (c *Account) GetLeverage(req requests.GetLeverage) (response responses.Leverage, err error) {
p := "/api/v5/account/leverage-info"
m := okex.S2M(req)
if len(req.InstID) > 0 {
m["instId"] = strings.Join(req.InstID, ",")
}
res, err := c.client.Do(http.MethodGet, p, true, m)
if err != nil {
return
}
defer res.Body.Close()
d := json.NewDecoder(res.Body)
err = d.Decode(&response)
return
}
// GetMaxLoan
//
// https://www.okex.com/docs-v5/en/#rest-api-account-get-the-maximum-loan-of-instrument
func (c *Account) GetMaxLoan(req requests.GetMaxLoan) (response responses.GetMaxLoan, err error) {
p := "/api/v5/account/max-loan"
m := okex.S2M(req)
res, err := c.client.Do(http.MethodGet, p, true, m)
if err != nil {
return
}
defer res.Body.Close()
d := json.NewDecoder(res.Body)
err = d.Decode(&response)
return
}
// GetFeeRates
//
// https://www.okex.com/docs-v5/en/#rest-api-account-get-fee-rates
func (c *Account) GetFeeRates(req requests.GetFeeRates) (response responses.GetFeeRates, err error) {
p := "/api/v5/account/trade-fee"
m := okex.S2M(req)
res, err := c.client.Do(http.MethodGet, p, true, m)
if err != nil {
return
}
defer res.Body.Close()
d := json.NewDecoder(res.Body)
err = d.Decode(&response)
return
}
// GetInterestAccrued
//
// https://www.okex.com/docs-v5/en/#rest-api-account-get-interest-accrued
func (c *Account) GetInterestAccrued(req requests.GetInterestAccrued) (response responses.GetInterestAccrued, err error) {
p := "/api/v5/account/interest-accrued"
m := okex.S2M(req)
res, err := c.client.Do(http.MethodGet, p, true, m)
if err != nil {
return
}
defer res.Body.Close()
d := json.NewDecoder(res.Body)
err = d.Decode(&response)
return
}
// GetInterestRates
// Get the user's current leveraged currency borrowing interest rate
//
// https://www.okex.com/docs-v5/en/#rest-api-account-get-interest-rate
func (c *Account) GetInterestRates(req requests.GetBalance) (response responses.GetInterestRates, err error) {
p := "/api/v5/account/interest-rate"
m := okex.S2M(req)
if len(req.Ccy) > 0 {
m["ccy"] = strings.Join(req.Ccy, ",")
}
res, err := c.client.Do(http.MethodGet, p, true, m)
if err != nil {
return
}
defer res.Body.Close()
d := json.NewDecoder(res.Body)
err = d.Decode(&response)
return
}
// SetGreeks
// Set the display type of Greeks.
//
// https://www.okex.com/docs-v5/en/#rest-api-account-set-greeks-m-bs
func (c *Account) SetGreeks(req requests.SetGreeks) (response responses.SetGreeks, err error) {
p := "/api/v5/account/set-greeks"
m := okex.S2M(req)
res, err := c.client.Do(http.MethodPost, p, true, m)
if err != nil {
return
}
defer res.Body.Close()
d := json.NewDecoder(res.Body)
err = d.Decode(&response)
return
}
// GetMaxWithdrawals
//
// https://www.okex.com/docs-v5/en/#rest-api-account-get-maximum-withdrawals
func (c *Account) GetMaxWithdrawals(req requests.GetBalance) (response responses.GetMaxWithdrawals, err error) {
p := "/api/v5/account/max-withdrawal"
m := okex.S2M(req)
if len(req.Ccy) > 0 {
m["ccy"] = strings.Join(req.Ccy, ",")
}
res, err := c.client.Do(http.MethodGet, p, true, m)
if err != nil {
return
}
defer res.Body.Close()
d := json.NewDecoder(res.Body)
err = d.Decode(&response)
return
}