-
Notifications
You must be signed in to change notification settings - Fork 87
/
isolatedmarginclient.go
248 lines (207 loc) · 6.85 KB
/
isolatedmarginclient.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
package client
import (
"encoding/json"
"errors"
"github.com/huobirdcenter/huobi_golang/internal"
"github.com/huobirdcenter/huobi_golang/internal/requestbuilder"
"github.com/huobirdcenter/huobi_golang/pkg/model"
"github.com/huobirdcenter/huobi_golang/pkg/model/margin"
"strconv"
)
// Responsible to operate isolated margin
type IsolatedMarginClient struct {
privateUrlBuilder *requestbuilder.PrivateUrlBuilder
}
// Initializer
func (p *IsolatedMarginClient) Init(accessKey string, secretKey string, host string) *IsolatedMarginClient {
p.privateUrlBuilder = new(requestbuilder.PrivateUrlBuilder).Init(accessKey, secretKey, host)
return p
}
// Transfer specific asset from spot trading account to isolated margin account
func (p *IsolatedMarginClient) TransferIn(request margin.IsolatedMarginTransferRequest) (int, error) {
postBody, jsonErr := model.ToJson(request)
if jsonErr != nil {
return 0, jsonErr
}
url := p.privateUrlBuilder.Build("POST", "/v1/dw/transfer-in/margin", nil)
postResp, postErr := internal.HttpPost(url, postBody)
if postErr != nil {
return 0, postErr
}
result := margin.TransferResponse{}
jsonErr = json.Unmarshal([]byte(postResp), &result)
if jsonErr != nil {
return 0, jsonErr
}
if result.Status != "ok" {
return 0, errors.New(postResp)
}
return result.Data, nil
}
// Transfer specific asset from isolated margin account to spot trading account
func (p *IsolatedMarginClient) TransferOut(request margin.IsolatedMarginTransferRequest) (int, error) {
postBody, jsonErr := model.ToJson(request)
if jsonErr != nil {
return 0, jsonErr
}
url := p.privateUrlBuilder.Build("POST", "/v1/dw/transfer-out/margin", nil)
postResp, postErr := internal.HttpPost(url, postBody)
if postErr != nil {
return 0, postErr
}
result := margin.TransferResponse{}
jsonErr = json.Unmarshal([]byte(postResp), &result)
if jsonErr != nil {
return 0, jsonErr
}
if result.Status != "ok" {
return 0, errors.New(postResp)
}
return result.Data, nil
}
// Returns loan interest rates and quota applied on the user
func (p *IsolatedMarginClient) GetMarginLoanInfo(optionalRequest margin.GetMarginLoanInfoOptionalRequest) ([]margin.IsolatedMarginLoanInfo, error) {
request := new(model.GetRequest).Init()
if optionalRequest.Symbols != "" {
request.AddParam("symbols", optionalRequest.Symbols)
}
url := p.privateUrlBuilder.Build("GET", "/v1/margin/loan-info", request)
getResp, getErr := internal.HttpGet(url)
if getErr != nil {
return nil, getErr
}
result := margin.GetIsolatedMarginLoanInfoResponse{}
jsonErr := json.Unmarshal([]byte(getResp), &result)
if jsonErr != nil {
return nil, jsonErr
}
if result.Status == "ok" && result.Data != nil {
return result.Data, nil
}
return nil, errors.New(getResp)
}
// Place an order to apply a margin loan.
func (p *IsolatedMarginClient) Apply(request margin.IsolatedMarginOrdersRequest) (int, error) {
postBody, jsonErr := model.ToJson(request)
if jsonErr != nil {
return 0, jsonErr
}
url := p.privateUrlBuilder.Build("POST", "/v1/margin/orders", nil)
postResp, postErr := internal.HttpPost(url, postBody)
if postErr != nil {
return 0, postErr
}
result := margin.MarginOrdersResponse{}
jsonErr = json.Unmarshal([]byte(postResp), &result)
if jsonErr != nil {
return 0, jsonErr
}
if result.Status != "ok" {
return 0, errors.New(postResp)
}
return result.Data, nil
}
// Repays margin loan with you asset in your margin account.
func (p *IsolatedMarginClient) Repay(orderId string, request margin.MarginOrdersRepayRequest) (int, error) {
postBody, jsonErr := model.ToJson(request)
if jsonErr != nil {
return 0, jsonErr
}
url := p.privateUrlBuilder.Build("POST", "/v1/margin/orders/"+orderId+"/repay", nil)
postResp, postErr := internal.HttpPost(url, postBody)
if postErr != nil {
return 0, postErr
}
result := margin.MarginOrdersRepayResponse{}
jsonErr = json.Unmarshal([]byte(postResp), &result)
if jsonErr != nil {
return 0, jsonErr
}
if result.Status != "ok" {
return 0, errors.New(postResp)
}
return result.Data, nil
}
// Returns margin orders based on a specific searching criteria.
func (p *IsolatedMarginClient) MarginLoanOrders(symbol string, optionalRequest margin.IsolatedMarginLoanOrdersOptionalRequest) ([]margin.IsolatedMarginLoanOrder, error) {
request := new(model.GetRequest).Init()
request.AddParam("symbol", symbol)
if optionalRequest.Size != "" {
request.AddParam("size", optionalRequest.Size)
}
if optionalRequest.Direct != "" {
request.AddParam("direct", optionalRequest.Direct)
}
if optionalRequest.EndDate != "" {
request.AddParam("end-date", optionalRequest.EndDate)
}
if optionalRequest.From != "" {
request.AddParam("from", optionalRequest.From)
}
if optionalRequest.StartDate != "" {
request.AddParam("start-date", optionalRequest.StartDate)
}
if optionalRequest.States != "" {
request.AddParam("states", optionalRequest.States)
}
if optionalRequest.SubUid != 0 {
request.AddParam("sub-uid", strconv.Itoa(optionalRequest.SubUid))
}
url := p.privateUrlBuilder.Build("GET", "/v1/margin/loan-orders", request)
getResp, getErr := internal.HttpGet(url)
if getErr != nil {
return nil, getErr
}
result := margin.IsolatedMarginLoanOrdersResponse{}
jsonErr := json.Unmarshal([]byte(getResp), &result)
if jsonErr != nil {
return nil, jsonErr
}
if result.Status == "ok" && result.Data != nil {
return result.Data, nil
}
return nil, errors.New(getResp)
}
// Returns the balance of the margin loan account.
func (p *IsolatedMarginClient) MarginAccountsBalance(optionalRequest margin.MarginAccountsBalanceOptionalRequest) ([]margin.IsolatedMarginAccountsBalance, error) {
request := new(model.GetRequest).Init()
if optionalRequest.SubUid != 0 {
request.AddParam("sub-uid", strconv.Itoa(optionalRequest.SubUid))
}
if optionalRequest.Symbol != "" {
request.AddParam("symbol", optionalRequest.Symbol)
}
url := p.privateUrlBuilder.Build("GET", "/v1/margin/accounts/balance", request)
getResp, getErr := internal.HttpGet(url)
if getErr != nil {
return nil, getErr
}
result := margin.IsolatedMarginAccountsBalanceResponse{}
jsonErr := json.Unmarshal([]byte(getResp), &result)
if jsonErr != nil {
return nil, jsonErr
}
if result.Status == "ok" && result.Data != nil {
return result.Data, nil
}
return nil, errors.New(getResp)
}
// 获取杠杆持仓限额(全仓)
func (p *IsolatedMarginClient) GetMarginLimit(currency string) ([]margin.MarginLimit, error) {
request := new(model.GetRequest).Init()
request.AddParam("currency", currency)
url := p.privateUrlBuilder.Build("GET", "/v2/margin/limit", request)
getResp, getErr := internal.HttpGet(url)
if getErr != nil {
return nil, getErr
}
result := margin.GetMarginLimitResponse{}
jsonErr := json.Unmarshal([]byte(getResp), &result)
if jsonErr != nil {
return nil, jsonErr
}
if result.Status == "ok" && result.Data != nil {
return result.Data, nil
}
return nil, errors.New(getResp)
}