forked from Kucoin/kucoin-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmargin.go
515 lines (440 loc) · 18.2 KB
/
margin.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
package kucoin
import (
"encoding/json"
"fmt"
"net/http"
)
// MarkPriceModel represents mark price of a symbol
type MarkPriceModel struct {
Symbol string `json:"symbol"`
Granularity json.Number `json:"granularity"`
TimePoint json.Number `json:"timePoint"`
Value json.Number `json:"value"`
}
// CurrentMarkPrice returns current mark price of the input symbol
func (as *ApiService) CurrentMarkPrice(symbol string) (*ApiResponse, error) {
req := NewRequest(http.MethodGet, fmt.Sprintf("/api/v1/mark-price/%s/current", symbol), nil)
return as.Call(req)
}
// MarginConfigModel represents a margin configuration
type MarginConfigModel struct {
CurrencyList []string `json:"currencyList"`
WarningDebtRatio json.Number `json:"warningDebtRatio"`
LiqDebtRatio json.Number `json:"liqDebtRatio"`
MaxLeverage json.Number `json:"maxLeverage"`
}
// MarginConfig returns a margin configuration
func (as *ApiService) MarginConfig() (*ApiResponse, error) {
req := NewRequest(http.MethodGet, "/api/v1/margin/config", nil)
return as.Call(req)
}
// MarginAccountModel represents a margin account information
type MarginAccountModel struct {
Accounts []struct {
AvailableBalance json.Number `json:"availableBalance"`
Currency string `json:"currency"`
HoldBalance json.Number `json:"holdBalance"`
Liability json.Number `json:"liability"`
MaxBorrowSize json.Number `json:"maxBorrowSize"`
TotalBalance json.Number `json:"totalBalance"`
} `json:"accounts"`
DebtRatio json.Number `json:"debtRatio"`
}
// MarginAccount returns a margin account information
func (as *ApiService) MarginAccount() (*ApiResponse, error) {
req := NewRequest(http.MethodGet, "/api/v1/margin/account", nil)
return as.Call(req)
}
// CreateBorrowOrderResultModel represents the result of create a borrow order
type CreateBorrowOrderResultModel struct {
OrderId string `json:"orderId"`
Currency string `json:"currency"`
}
// CreateBorrowOrder returns the result of create a borrow order
func (as *ApiService) CreateBorrowOrder(params map[string]string) (*ApiResponse, error) {
req := NewRequest(http.MethodPost, "/api/v1/margin/borrow", params)
return as.Call(req)
}
// BorrowOrderModel represents a borrow order
type BorrowOrderModel struct {
OrderId string `json:"orderId"`
Currency string `json:"currency"`
Size json.Number `json:"size"`
Filled json.Number `json:"filled"`
Status string `json:"status"`
MatchList []struct {
Currency string `json:"currency"`
DailyIntRate json.Number `json:"dailyIntRate"`
Size json.Number `json:"size"`
Term json.Number `json:"term"`
Timestamp json.Number `json:"timestamp"`
TradeId string `json:"tradeId"`
} `json:"matchList"`
}
// BorrowOrder returns a specific borrow order
func (as *ApiService) BorrowOrder(orderId string) (*ApiResponse, error) {
params := map[string]string{}
if orderId != "" {
params["orderId"] = orderId
}
req := NewRequest(http.MethodGet, "/api/v1/margin/borrow", params)
return as.Call(req)
}
// BorrowOutstandingRecordModel represents borrow outstanding record
type BorrowOutstandingRecordModel struct {
Currency string `json:"currency"`
TradeId string `json:"tradeId"`
Liability json.Number `json:"liability"`
Principal json.Number `json:"principal"`
AccruedInterest json.Number `json:"accruedInterest"`
CreatedAt json.Number `json:"createdAt"`
MaturityTime json.Number `json:"maturityTime"`
Term json.Number `json:"term"`
RepaidSize json.Number `json:"repaidSize"`
DailyIntRate json.Number `json:"dailyIntRate"`
}
// BorrowOutstandingRecordsModel is a list of *BorrowOutstandingRecordModel
type BorrowOutstandingRecordsModel []*BorrowOutstandingRecordModel
// BorrowOutstandingRecords returns borrow outstanding records
func (as *ApiService) BorrowOutstandingRecords(currency string, pagination *PaginationParam) (*ApiResponse, error) {
params := map[string]string{}
if currency != "" {
params["currency"] = currency
}
pagination.ReadParam(params)
req := NewRequest(http.MethodGet, "/api/v1/margin/borrow/outstanding", params)
return as.Call(req)
}
// BorrowRepaidRecordModel represents a repaid borrow record
type BorrowRepaidRecordModel struct {
Currency string `json:"currency"`
DailyIntRate json.Number `json:"dailyIntRate"`
Interest json.Number `json:"interest"`
Principal json.Number `json:"principal"`
RepaidSize json.Number `json:"repaidSize"`
RepayTime json.Number `json:"repayTime"`
Term json.Number `json:"term"`
TradeId string `json:"tradeId"`
}
// BorrowRepaidRecordsModel is a list of *BorrowRepaidRecordModel
type BorrowRepaidRecordsModel []*BorrowRepaidRecordModel
// BorrowRepaidRecords returns repaid borrow records
func (as *ApiService) BorrowRepaidRecords(currency string, pagination *PaginationParam) (*ApiResponse, error) {
params := map[string]string{}
if currency != "" {
params["currency"] = currency
}
pagination.ReadParam(params)
req := NewRequest(http.MethodGet, "/api/v1/margin/borrow/repaid", params)
return as.Call(req)
}
// RepayAll repay borrow orders of one currency
func (as *ApiService) RepayAll(params map[string]string) (*ApiResponse, error) {
req := NewRequest(http.MethodPost, "/api/v1/margin/repay/all", params)
return as.Call(req)
}
// RepaySingle repay a single borrow order
func (as *ApiService) RepaySingle(params map[string]string) (*ApiResponse, error) {
req := NewRequest(http.MethodPost, "/api/v1/margin/repay/single", params)
return as.Call(req)
}
// CreateLendOrderResultModel the result of create a lend order
type CreateLendOrderResultModel struct {
OrderId string `json:"orderId"`
}
// CreateLendOrder returns the result of create a lend order
func (as *ApiService) CreateLendOrder(params map[string]string) (*ApiResponse, error) {
req := NewRequest(http.MethodPost, "/api/v1/margin/lend", params)
return as.Call(req)
}
// CancelLendOrder cancel a lend order
func (as *ApiService) CancelLendOrder(orderId string) (*ApiResponse, error) {
req := NewRequest(http.MethodDelete, fmt.Sprintf("/api/v1/margin/lend/%s", orderId), nil)
return as.Call(req)
}
// ToggleAutoLend set auto lend rules
func (as *ApiService) ToggleAutoLend(params map[string]string) (*ApiResponse, error) {
req := NewRequest(http.MethodPost, "/api/v1/margin/toggle-auto-lend", params)
return as.Call(req)
}
// LendOrderBaseModel represents Base model of lend order
type LendOrderBaseModel struct {
OrderId string `json:"orderId"`
Currency string `json:"currency"`
Size json.Number `json:"size"`
FilledSize json.Number `json:"filledSize"`
DailyIntRate json.Number `json:"dailyIntRate"`
Term json.Number `json:"term"`
CreatedAt json.Number `json:"createdAt"`
}
// LendActiveOrderModel represents a active lend order
type LendActiveOrderModel struct {
LendOrderBaseModel
}
// LendActiveOrdersModel is a list of *LendActiveOrderModel
type LendActiveOrdersModel []*LendActiveOrderModel
// LendActiveOrders returns the active lend orders
func (as *ApiService) LendActiveOrders(currency string, pagination *PaginationParam) (*ApiResponse, error) {
params := map[string]string{}
if currency != "" {
params["currency"] = currency
}
pagination.ReadParam(params)
req := NewRequest(http.MethodGet, "/api/v1/margin/lend/active", params)
return as.Call(req)
}
// LendDoneOrderModel represents a history lend order
type LendDoneOrderModel struct {
LendOrderBaseModel
Status string `json:"status"`
}
// LendDoneOrdersModel is a list of *LendDoneOrderModel
type LendDoneOrdersModel []*LendDoneOrderModel
// LendDoneOrders returns the history lend orders
func (as *ApiService) LendDoneOrders(currency string, pagination *PaginationParam) (*ApiResponse, error) {
params := map[string]string{}
if currency != "" {
params["currency"] = currency
}
pagination.ReadParam(params)
req := NewRequest(http.MethodGet, "/api/v1/margin/lend/done", params)
return as.Call(req)
}
// LendTradeUnsettledRecordModel represents a unsettled lend record
type LendTradeUnsettledRecordModel struct {
TradeId string `json:"tradeId"`
Currency string `json:"currency"`
Size json.Number `json:"size"`
AccruedInterest json.Number `json:"accruedInterest"`
Repaid json.Number `json:"repaid"`
DailyIntRate json.Number `json:"dailyIntRate"`
Term json.Number `json:"term"`
MaturityTime json.Number `json:"maturityTime"`
}
// LendTradeUnsettledRecordsModel is a list of *LendTradeUnsettledRecordModel
type LendTradeUnsettledRecordsModel []*LendTradeUnsettledRecordModel
// LendTradeUnsettledRecords returns unsettled lend records
func (as *ApiService) LendTradeUnsettledRecords(currency string, pagination *PaginationParam) (*ApiResponse, error) {
params := map[string]string{}
if currency != "" {
params["currency"] = currency
}
pagination.ReadParam(params)
req := NewRequest(http.MethodGet, "/api/v1/margin/lend/trade/unsettled", params)
return as.Call(req)
}
// LendTradeSettledRecordModel represents a settled lend record
type LendTradeSettledRecordModel struct {
TradeId string `json:"tradeId"`
Currency string `json:"currency"`
Size json.Number `json:"size"`
Interest json.Number `json:"interest"`
Repaid json.Number `json:"repaid"`
DailyIntRate json.Number `json:"dailyIntRate"`
Term json.Number `json:"term"`
SettledAt json.Number `json:"settledAt"`
Note string `json:"note"`
}
// LendTradeSettledRecordsModel is a list of *LendTradeSettledRecordModel
type LendTradeSettledRecordsModel []*LendTradeSettledRecordModel
// LendTradeSettledRecords returns settled lend records
func (as *ApiService) LendTradeSettledRecords(currency string, pagination *PaginationParam) (*ApiResponse, error) {
params := map[string]string{}
if currency != "" {
params["currency"] = currency
}
pagination.ReadParam(params)
req := NewRequest(http.MethodGet, "/api/v1/margin/lend/trade/settled", params)
return as.Call(req)
}
// LendAssetModel represents account lend asset
type LendAssetModel struct {
Currency string `json:"currency"`
Outstanding json.Number `json:"outstanding"`
FilledSize json.Number `json:"filledSize"`
AccruedInterest json.Number `json:"accruedInterest"`
RealizedProfit json.Number `json:"realizedProfit"`
IsAutoLend bool `json:"isAutoLend"`
}
// LendAssetsModel is a list of *LendAssetModel
type LendAssetsModel []*LendAssetModel
// LendAssets returns account lend assets
func (as *ApiService) LendAssets(currency string) (*ApiResponse, error) {
params := map[string]string{}
if currency != "" {
params["currency"] = currency
}
req := NewRequest(http.MethodGet, "/api/v1/margin/lend/assets", params)
return as.Call(req)
}
// MarginMarketModel represents lending market data
type MarginMarketModel struct {
DailyIntRate json.Number `json:"dailyIntRate"`
Term json.Number `json:"term"`
Size json.Number `json:"size"`
}
// MarginMarketsModel is a list of *MarginMarketModel
type MarginMarketsModel []*MarginMarketModel
// MarginMarkets returns lending market data
func (as *ApiService) MarginMarkets(params map[string]string) (*ApiResponse, error) {
req := NewRequest(http.MethodGet, "/api/v1/margin/market", params)
return as.Call(req)
}
// MarginTradeModel represents lending market trade data
type MarginTradeModel struct {
TradeId string `json:"tradeId"`
Currency string `json:"currency"`
Size json.Number `json:"size"`
DailyIntRate json.Number `json:"dailyIntRate"`
Term json.Number `json:"term"`
Timestamp json.Number `json:"timestamp"`
}
// MarginTradesModel is a list of *MarginTradeModel
type MarginTradesModel []*MarginTradeModel
// MarginTradeLast returns latest lending market trade datas
func (as *ApiService) MarginTradeLast(currency string) (*ApiResponse, error) {
params := map[string]string{}
if currency != "" {
params["currency"] = currency
}
req := NewRequest(http.MethodGet, "/api/v1/margin/trade/last", params)
return as.Call(req)
}
// MarginRiskLimitItemModel is item of *MarginRiskLimitModel
type MarginRiskLimitItemModel struct {
Currency string `json:"currency"`
BorrowMaxAmount string `json:"borrowMaxAmount"`
BuyMaxAmount string `json:"buyMaxAmount"`
Precision json.Number `json:"precision"`
}
// MarginRiskLimitModel is a list of *MarginRiskLimitModel
type MarginRiskLimitModel []*MarginRiskLimitItemModel
func (as *ApiService) MarginRiskLimit(marginModel string) (*ApiResponse, error) {
params := map[string]string{}
if marginModel != "" {
params["marginModel"] = marginModel
}
req := NewRequest(http.MethodGet, "/api/v1/risk/limit/strategy", params)
return as.Call(req)
}
// MarginIsolatedSymbols query margin isolated symbols
func (as *ApiService) MarginIsolatedSymbols() (*ApiResponse, error) {
p := map[string]string{}
req := NewRequest(http.MethodGet, "/api/v1/isolated/symbols", p)
return as.Call(req)
}
type MarginIsolatedSymbolsModel []*MarginIsolatedSymbolModel
type MarginIsolatedSymbolModel struct {
Symbol string `json:"symbol"`
SymbolName string `json:"symbolName"`
BaseCurrency string `json:"baseCurrency"`
QuoteCurrency string `json:"quoteCurrency"`
MaxLeverage int64 `json:"maxLeverage"`
FlDebtRatio string `json:"flDebtRatio"`
TradeEnable bool `json:"tradeEnable"`
AutoRenewMaxDebtRatio string `json:"autoRenewMaxDebtRatio"`
BaseBorrowEnable bool `json:"baseBorrowEnable"`
QuoteBorrowEnable bool `json:"quoteBorrowEnable"`
BaseTransferInEnable bool `json:"baseTransferInEnable"`
QuoteTransferInEnable bool `json:"quoteTransferInEnable"`
}
// MarginIsolatedAccounts query margin isolated account
func (as *ApiService) MarginIsolatedAccounts(balanceCurrency string) (*ApiResponse, error) {
p := map[string]string{
"balanceCurrency": balanceCurrency,
}
req := NewRequest(http.MethodGet, "/api/v1/isolated/accounts", p)
return as.Call(req)
}
type MarginIsolatedAccountsModel struct {
TotalConversionBalance string `json:"totalConversionBalance"`
LiabilityConversionBalance string `json:"liabilityConversionBalance"`
Assets []*MarginIsolatedAccountAssetsModel `json:"assets"`
}
type MarginIsolatedAccountAssetsModel struct {
Symbol string `json:"symbol"`
Status string `json:"status"`
DebtRatio string `json:"debtRatio"`
BaseAsset struct {
Currency string `json:"currency"`
TotalBalance string `json:"totalBalance"`
HoldBalance string `json:"holdBalance"`
AvailableBalance string `json:"availableBalance"`
Liability string `json:"liability"`
Interest string `json:"interest"`
BorrowableAmount string `json:"borrowableAmount"`
} `json:"baseAsset"`
QuoteAsset struct {
Currency string `json:"currency"`
TotalBalance string `json:"totalBalance"`
HoldBalance string `json:"holdBalance"`
AvailableBalance string `json:"availableBalance"`
Liability string `json:"liability"`
Interest string `json:"interest"`
BorrowableAmount string `json:"borrowableAmount"`
} `json:"quoteAsset"`
}
// IsolatedAccount query margin isolated account by symbol
func (as *ApiService) IsolatedAccount(symbol string) (*ApiResponse, error) {
req := NewRequest(http.MethodGet, "/api/v1/isolated/account/"+symbol, nil)
return as.Call(req)
}
// IsolatedBorrow margin isolated borrow
func (as *ApiService) IsolatedBorrow(params map[string]string) (*ApiResponse, error) {
req := NewRequest(http.MethodPost, "/api/v1/isolated/borrow", params)
return as.Call(req)
}
type MarginIsolatedBorrowRes struct {
OrderId string `json:"orderId"`
Currency string `json:"currency"`
ActualSize string `json:"actualSize"`
}
// IsolatedBorrowOutstandingRecord query margin isolated borrow outstanding records
func (as *ApiService) IsolatedBorrowOutstandingRecord(params map[string]string, pagination *PaginationParam) (*ApiResponse, error) {
pagination.ReadParam(params)
req := NewRequest(http.MethodGet, "/api/v1/isolated/borrow/outstanding", params)
return as.Call(req)
}
type IsolatedBorrowOutstandingRecordsModel []*IsolatedBorrowOutstandingRecordModel
type IsolatedBorrowOutstandingRecordModel struct {
LoanId string `json:"loanId"`
Symbol string `json:"symbol"`
Currency string `json:"currency"`
LiabilityBalance string `json:"liabilityBalance"`
PrincipalTotal string `json:"principalTotal"`
InterestBalance string `json:"interestBalance"`
CreatedAt json.Number `json:"createdAt"`
MaturityTime json.Number `json:"maturityTime"`
Period int64 `json:"period"`
RepaidSize string `json:"repaidSize"`
DailyInterestRate string `json:"dailyInterestRate"`
}
// IsolatedBorrowRepaidRecord query margin isolated borrow repaid records
func (as *ApiService) IsolatedBorrowRepaidRecord(params map[string]string, pagination *PaginationParam) (*ApiResponse, error) {
pagination.ReadParam(params)
req := NewRequest(http.MethodGet, "/api/v1/isolated/borrow/repaid", params)
return as.Call(req)
}
type IsolatedBorrowRepaidRecordRecordsModel []*IsolatedBorrowRepaidRecordRecordModel
type IsolatedBorrowRepaidRecordRecordModel struct {
LoanId string `json:"loanId"`
Symbol string `json:"symbol"`
Currency string `json:"currency"`
PrincipalTotal string `json:"principalTotal"`
InterestBalance string `json:"interestBalance"`
RepaidSize string `json:"repaidSize"`
CreatedAt json.Number `json:"createdAt"`
Period int64 `json:"period"`
DailyInterestRate string `json:"dailyInterestRate"`
RepayFinishAt json.Number `json:"repayFinishAt"`
}
// IsolatedRepayAll repay all isolated
func (as *ApiService) IsolatedRepayAll(params map[string]string) (*ApiResponse, error) {
req := NewRequest(http.MethodPost, "/api/v1/isolated/repay/all", params)
return as.Call(req)
}
// IsolatedRepaySingle repay single isolated
func (as *ApiService) IsolatedRepaySingle(params map[string]string) (*ApiResponse, error) {
req := NewRequest(http.MethodPost, "/api/v1/isolated/repay/single", params)
return as.Call(req)
}