-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccount.go
421 lines (327 loc) · 10.2 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
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
package gobitpanda
import (
"errors"
"fmt"
"strconv"
"time"
)
// NewAccountDepositAddress creates a new deposit address for the given currency code.
func (c *Client) NewAccountDepositAddress(currency *CurrencyCode) (*DepositReturn, error) {
if currency.Code == "" {
return nil, errors.New("No currency code provided")
}
if currency.Code == CurrencyEUR {
return nil, errors.New("Can't get a deposit address for FIAT currency codes")
}
deposit := &DepositReturn{}
req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/account/deposit/crypto"), currency)
if err != nil {
return nil, err
}
err = c.SendWithAuth(req, deposit)
if err != nil {
return nil, err
}
return deposit, nil
}
// NewAccountFIATDeposit returns deposit information for sepa payments (EUR)
func (c *Client) NewAccountFIATDeposit() (*FiatDepositReturn, error) {
deposit := &FiatDepositReturn{}
req, err := c.NewRequest("GET", fmt.Sprintf("%s%s", c.APIBase, "/v1/account/deposit/fiat/EUR"), nil)
if err != nil {
return nil, err
}
err = c.SendWithAuth(req, deposit)
if err != nil {
return nil, err
}
return deposit, nil
}
// Withdrawl initiates a withdrawal.
func (c *Client) Withdrawl(w *Withdraw) (*WithdrawReturn, error) {
if w == nil {
return nil, errors.New("No withdraw info provided")
}
if w.Currency == "" {
return nil, errors.New("No currency code provided")
}
if w.Currency == CurrencyEUR {
return nil, errors.New("Can't withdraw FIAT currency")
}
withdraw := &WithdrawReturn{}
req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/account/withdraw/crypto"), w)
if err != nil {
return nil, err
}
err = c.SendWithAuth(req, withdraw)
if err != nil {
return nil, err
}
return withdraw, nil
}
// GetAccountBalances get the balance details for an account.
func (c *Client) GetAccountBalances() (*Account, error) {
acc := &Account{}
req, err := c.NewRequest("GET", fmt.Sprintf("%s%s", c.APIBase, "/v1/account/balances"), nil)
if err != nil {
return acc, err
}
if err = c.SendWithAuth(req, acc); err != nil {
return acc, err
}
return acc, nil
}
// GetAccountDepositAddress get a deposit address for the given crypto currency code
func (c *Client) GetAccountDepositAddress(currency string) (*DepositReturn, error) {
if currency == "" {
return nil, errors.New("No currency code provided")
}
if currency == CurrencyEUR {
return nil, errors.New("Can't get a deposit address for FIAT currency codes")
}
deposit := &DepositReturn{}
req, err := c.NewRequest("GET", fmt.Sprintf("%s%s%s", c.APIBase, "/v1/account/deposit/crypto/", currency), nil)
if err != nil {
return nil, err
}
err = c.SendWithAuth(req, deposit)
if err != nil {
return nil, err
}
return deposit, nil
}
// GetAccountFees gets the fee details for an account.
func (c *Client) GetAccountFees() (*AccountFees, error) {
fees := &AccountFees{}
req, err := c.NewRequest("GET", fmt.Sprintf("%s%s", c.APIBase, "/v1/account/fees"), nil)
if err != nil {
return fees, err
}
if err = c.SendWithAuth(req, fees); err != nil {
return fees, err
}
return fees, nil
}
// SetAccountFeeMode updates the fee toggle to enable or disable fee collection with BEST
func (c *Client) SetAccountFeeMode(enableBESTMode bool) (*AccountFees, error) {
fees := &AccountFees{}
req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/account/fees"), &FeeMode{CollectFeesInBest: enableBESTMode})
if err != nil {
return fees, err
}
if err = c.SendWithAuth(req, fees); err != nil {
return fees, err
}
return fees, nil
}
// GetAccountOrders gets a paginated report on currently open orders, sorted by timestamp (newest first).
// Use query parameters and filters to specify if historical orders should be reported as well.
// If no query filters are defined it returns all orders which are currently active.
// If you want to query specific time frame parameters, FROM and TO are mandatory, otherwise it will start from the newest orders.
// The maximum time-frame you can query at one time is 100 days.
func (c *Client) GetAccountOrders(
from time.Time,
to time.Time,
instrumentCode string,
withCancelledAndRejected bool,
withJustFilledInactive bool,
maxPageSize string,
cursor string,
) (*OrderHistory, error) {
orders := &OrderHistory{}
var params []string
paramsString := ""
if !from.IsZero() {
params = append(params, "from="+from.UTC().Format(time.RFC3339))
}
if !to.IsZero() {
params = append(params, "to="+to.UTC().Format(time.RFC3339))
}
if instrumentCode != "" {
params = append(params, "instrument_code="+instrumentCode)
}
if maxPageSize != "" {
params = append(params, "max_page_size="+maxPageSize)
}
if cursor != "" {
params = append(params, "cursor="+cursor)
}
params = append(params, "with_cancelled_and_rejected="+strconv.FormatBool(withCancelledAndRejected))
params = append(params, "with_just_filled_inactive="+strconv.FormatBool(withJustFilledInactive))
for i, p := range params {
if i == 0 {
paramsString += "?" + p
} else {
paramsString += "&" + p
}
}
req, err := c.NewRequest("GET", fmt.Sprintf("%s%s%s", c.APIBase, "/v1/account/orders", paramsString), nil)
if err != nil {
return orders, err
}
if err = c.SendWithAuth(req, orders); err != nil {
return orders, err
}
return orders, nil
}
// GetAccountOrderByID gets information for an order by it's ID
func (c *Client) GetAccountOrderByID(ID string) (*OrderHistoryEntry, error) {
if ID == "" {
return nil, errors.New("Order ID can not be empty")
}
order := &OrderHistoryEntry{}
req, err := c.NewRequest("GET", fmt.Sprintf("%s%s%s", c.APIBase, "/v1/account/orders/", ID), nil)
if err != nil {
return order, err
}
if err = c.SendWithAuth(req, order); err != nil {
return order, err
}
return order, nil
}
// NewOrder creates a new order
func (c *Client) NewOrder(o *CreateOrder) (*Order, error) {
if o == nil {
return nil, errors.New("Invalid input")
}
if o.InstrumentCode == "" || o.Side == "" || o.Type == "" || o.Amount == "" {
return nil, errors.New("InstrumentCode, Side, Type and Ammount can not be empty")
}
if o.Type == OrderTypeLimit && o.Price == "" {
return nil, errors.New("Price can not be empty")
}
if o.Type == OrderTypeStop && (o.Price == "" || o.TriggerPrice == "") {
return nil, errors.New("Price and TriggerPrice can not be empty")
}
order := &Order{}
req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/account/orders"), o)
if err != nil {
return order, err
}
if err = c.SendWithAuth(req, order); err != nil {
return order, err
}
return order, nil
}
// CloseOrders closes all orders. If an instrument code is given, only orders in this market will be closed.
// Returns an array with closed order IDs
func (c *Client) CloseOrders(m ...string) ([]string, error) {
if len(m) > 1 {
return nil, errors.New("Too manny arguments")
}
var orderIDs []string
marketLimit := "?instrument_code="
if len(m) == 1 {
marketLimit += m[0]
} else {
marketLimit = ""
}
req, err := c.NewRequest("DELETE", fmt.Sprintf("%s%s%s", c.APIBase, "/v1/account/orders", marketLimit), nil)
if err != nil {
return orderIDs, err
}
if err = c.SendWithAuth(req, &orderIDs); err != nil {
return orderIDs, err
}
return orderIDs, nil
}
// CloseOrderByID closes an order by it's ID
func (c *Client) CloseOrderByID(ID string) error {
if ID == "" {
return errors.New("Order ID can not be empty")
}
req, err := c.NewRequest("DELETE", fmt.Sprintf("%s%s%s", c.APIBase, "/v1/account/orders/", ID), nil)
if err != nil {
return err
}
if err = c.SendWithAuth(req, nil); err != nil {
return err
}
return nil
}
// GetAccountTrades gets a paginated report on past trades, sorted by timestamp (newest first).
// If no query parameters are defined, it returns the last 100 trades.
func (c *Client) GetAccountTrades(
from time.Time,
to time.Time,
instrumentCode string,
maxPageSize string,
cursor string,
) (*TradeHistory, error) {
trades := &TradeHistory{}
var params []string
paramsString := ""
if !from.IsZero() {
params = append(params, "from="+from.UTC().Format(time.RFC3339))
}
if !to.IsZero() {
params = append(params, "to="+to.UTC().Format(time.RFC3339))
}
if instrumentCode != "" {
params = append(params, "instrument_code="+instrumentCode)
}
if maxPageSize != "" {
params = append(params, "max_page_size="+maxPageSize)
}
if cursor != "" {
params = append(params, "cursor="+cursor)
}
for i, p := range params {
if i == 0 {
paramsString += "?" + p
} else {
paramsString += "&" + p
}
}
req, err := c.NewRequest("GET", fmt.Sprintf("%s%s%s", c.APIBase, "/v1/account/trades", paramsString), nil)
if err != nil {
return trades, err
}
if err = c.SendWithAuth(req, trades); err != nil {
return trades, err
}
return trades, nil
}
// GetAccountTradeByID gets information for an trade by it' ID
func (c *Client) GetAccountTradeByID(ID string) (*TradeHistoryEntry, error) {
if ID == "" {
return nil, errors.New("Trade ID can not be empty")
}
trade := &TradeHistoryEntry{}
req, err := c.NewRequest("GET", fmt.Sprintf("%s%s%s", c.APIBase, "/v1/account/trades/", ID), nil)
if err != nil {
return trade, err
}
if err = c.SendWithAuth(req, trade); err != nil {
return trade, err
}
return trade, nil
}
// GetAccountTradesByOrderID gets trade information for a specific order by it's order ID
func (c *Client) GetAccountTradesByOrderID(ID string) (*TradeHistory, error) {
if ID == "" {
return nil, errors.New("Order ID can not be empty")
}
trade := &TradeHistory{}
req, err := c.NewRequest("GET", fmt.Sprintf("%s%s%s%s", c.APIBase, "/v1/account/orders/", ID, "/trades"), nil)
if err != nil {
return trade, err
}
if err = c.SendWithAuth(req, trade); err != nil {
return trade, err
}
return trade, nil
}
// GetAccountTradingVolume gets the running trading volume for this account.
// It is calculated over a 30 day running window and updated once every 24hrs.
func (c *Client) GetAccountTradingVolume() (*TradingVolume, error) {
tradingVolume := &TradingVolume{}
req, err := c.NewRequest("GET", fmt.Sprintf("%s%s", c.APIBase, "/v1/account/trading-volume"), nil)
if err != nil {
return tradingVolume, err
}
if err = c.SendWithAuth(req, tradingVolume); err != nil {
return tradingVolume, err
}
return tradingVolume, nil
}