forked from toorop/go-bittrex
/
bittrex.go
454 lines (422 loc) · 12.7 KB
/
bittrex.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
// Package Bittrex is an implementation of the Biitrex API (public and private).
package bittrex
import (
"encoding/json"
"errors"
"fmt"
"math/rand"
"strconv"
"strings"
)
const (
API_BASE = "https://bittrex.com/api/" // Bittrex API endpoint
API_VERSION = "v1.1" // Bittrex API version
DEFAULT_HTTPCLIENT_TIMEOUT = 30 // HTTP client timeout
)
// New return a instanciate bittrex struct
func New(apiKey, apiSecret string) *Bittrex {
client := NewClient(apiKey, apiSecret)
return &Bittrex{client}
}
// handleErr gets JSON response from Bittrex API en deal with error
func handleErr(r jsonResponse) error {
if !r.Success {
return errors.New(r.Message)
}
return nil
}
// bittrex represent a bittrex client
type Bittrex struct {
client *client
}
// GetCandles is used to get the ohlcv.
func (b *Bittrex) GetHisCandles(market string) (candles []Candle, err error) {
r, err := b.client.do("GET", "https://bittrex.com/Market/Pub_GetTickData?MarketName="+strings.ToUpper(market)+fmt.Sprintf("&_=%d", rand.Float64()), "", false)
if err != nil {
return
}
if err = json.Unmarshal(r, &candles); err != nil {
return
}
return
}
// GetCandles is used to get the ohlcv.
func (b *Bittrex) GetNewCandles(market, LastEpoch string) (candles []Candle, err error) {
r, err := b.client.do("GET", "https://bittrex.com/Market/Pub_GetNewTickData?MarketName="+strings.ToUpper(market)+"&LastEpoch="+LastEpoch, "", false)
if err != nil {
return
}
var newCandles NewCandles
if err = json.Unmarshal(r, &newCandles); err != nil {
return
}
if err != nil {
return
}
candles = newCandles.Ticks
return
}
// GetMarkets is used to get the open and available trading markets at Bittrex along with other meta data.
func (b *Bittrex) GetMarkets() (markets []Market, err error) {
r, err := b.client.do("GET", "public/getmarkets", "", false)
if err != nil {
return
}
var response jsonResponse
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
err = json.Unmarshal(response.Result, &markets)
return
}
// GetTicker is used to get the current ticker values for a market.
func (b *Bittrex) GetTicker(market string) (ticker Ticker, err error) {
r, err := b.client.do("GET", "public/getticker?market="+strings.ToUpper(market), "", false)
if err != nil {
return
}
var response jsonResponse
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
err = json.Unmarshal(response.Result, &ticker)
return
}
// GetMarketSummaries is used to get the last 24 hour summary of all active exchanges
func (b *Bittrex) GetMarketSummaries() (marketSummaries []MarketSummary, err error) {
r, err := b.client.do("GET", "public/getmarketsummaries", "", false)
if err != nil {
return
}
var response jsonResponse
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
err = json.Unmarshal(response.Result, &marketSummaries)
return
}
// GetOrderBook is used to get retrieve the orderbook for a given market
// market: a string literal for the market (ex: BTC-LTC)
// cat: buy, sell or both to identify the type of orderbook to return.
// depth: how deep of an order book to retrieve. Max is 100
func (b *Bittrex) GetOrderBook(market, cat string, depth int) (orderBook OrderBook, err error) {
if cat != "buy" && cat != "sell" && cat != "both" {
cat = "both"
}
if depth > 100 {
depth = 100
}
if depth < 1 {
depth = 1
}
r, err := b.client.do("GET", fmt.Sprintf("public/getorderbook?market=%s&type=%s&depth=%d", strings.ToUpper(market), cat, depth), "", false)
if err != nil {
return
}
var response jsonResponse
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
err = json.Unmarshal(response.Result, &orderBook)
return
}
// GetMarketHistory is used to retrieve the latest trades that have occured for a specific market.
// mark a string literal for the market (ex: BTC-LTC)
// count a number between 1-100 for the number of entries to return
func (b *Bittrex) GetMarketHistory(market string, count int) (trades []Trade, err error) {
if count > 100 {
count = 100
}
if count < 1 {
count = 1
}
r, err := b.client.do("GET", fmt.Sprintf("public/getmarkethistory?market=%s&count=%d", strings.ToUpper(market), count), "", false)
if err != nil {
return
}
var response jsonResponse
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
err = json.Unmarshal(response.Result, &trades)
return
}
// Market
// BuyLimit is used to place a limited buy order in a specific market.
func (b *Bittrex) BuyLimit(market string, quantity, rate float64) (uuid string, err error) {
r, err := b.client.do("GET", "market/buylimit?market="+market+"&quantity="+strconv.FormatFloat(quantity, 'f', 8, 64)+"&rate="+strconv.FormatFloat(rate, 'f', 8, 64), "", true)
if err != nil {
return
}
var response jsonResponse
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
var u Uuid
err = json.Unmarshal(response.Result, &u)
uuid = u.Id
return
}
// BuyMarket is used to place a market buy order in a spacific market.
func (b *Bittrex) BuyMarket(market string, quantity float64) (uuid string, err error) {
r, err := b.client.do("GET", "market/buymarket?market="+market+"&quantity="+strconv.FormatFloat(quantity, 'f', 8, 64), "", true)
if err != nil {
return
}
var response jsonResponse
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
var u Uuid
err = json.Unmarshal(response.Result, &u)
uuid = u.Id
return
}
// SellLimit is used to place a limited sell order in a specific market.
func (b *Bittrex) SellLimit(market string, quantity, rate float64) (uuid string, err error) {
r, err := b.client.do("GET", "market/selllimit?market="+market+"&quantity="+strconv.FormatFloat(quantity, 'f', 8, 64)+"&rate="+strconv.FormatFloat(rate, 'f', 8, 64), "", true)
if err != nil {
return
}
var response jsonResponse
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
var u Uuid
err = json.Unmarshal(response.Result, &u)
uuid = u.Id
return
}
// SellMarket is used to place a market sell order in a specific market.
func (b *Bittrex) SellMarket(market string, quantity float64) (uuid string, err error) {
r, err := b.client.do("GET", "market/selllimit?market="+market+"&quantity="+strconv.FormatFloat(quantity, 'f', 8, 64), "", true)
if err != nil {
return
}
var response jsonResponse
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
var u Uuid
err = json.Unmarshal(response.Result, &u)
uuid = u.Id
return
}
// CancelOrder is used to cancel a buy or sell order.
func (b *Bittrex) CancelOrder(orderID string) (err error) {
r, err := b.client.do("GET", "market/cancel?uuid="+orderID, "", true)
if err != nil {
return
}
var response jsonResponse
if err = json.Unmarshal(r, &response); err != nil {
return
}
err = handleErr(response)
return
}
// GetOpenOrders returns orders that you currently have opened.
// If market is set to "all", GetOpenOrders return all orders
// If market is set to a specific order, GetOpenOrders return orders for this market
func (b *Bittrex) GetOpenOrders(market string) (openOrders []Order, err error) {
ressource := "market/getopenorders"
if market != "all" {
ressource += "?market=" + strings.ToUpper(market)
}
r, err := b.client.do("GET", ressource, "", true)
if err != nil {
return
}
var response jsonResponse
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = json.Unmarshal(r, &response); err != nil {
return
}
err = json.Unmarshal(response.Result, &openOrders)
return
}
// Account
// GetBalances is used to retrieve all balances from your account
func (b *Bittrex) GetBalances() (balances map[string]Balance, err error) {
r, err := b.client.do("GET", "account/getbalances", "", true)
if err != nil {
return
}
var response jsonResponse
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
err = json.Unmarshal(response.Result, &balances)
return
}
// Getbalance is used to retrieve the balance from your account for a specific currency.
// currency: a string literal for the currency (ex: LTC)
func (b *Bittrex) GetBalance(currency string) (balance Balance, err error) {
r, err := b.client.do("GET", "account/getbalance?currency="+strings.ToUpper(currency), "", true)
if err != nil {
return
}
var response jsonResponse
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
err = json.Unmarshal(response.Result, &balance)
return
}
// GetDepositAddress is sed to generate or retrieve an address for a specific currency.
// currency a string literal for the currency (ie. BTC)
func (b *Bittrex) GetDepositAddress(currency string) (address Address, err error) {
r, err := b.client.do("GET", "account/getdepositaddress?currency="+strings.ToUpper(currency), "", true)
if err != nil {
return
}
var response jsonResponse
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
err = json.Unmarshal(response.Result, &address)
return
}
// Withdraw is used to withdraw funds from your account.
// address string the address where to send the funds.
// currency string literal for the currency (ie. BTC)
// quantity float the quantity of coins to withdraw
func (b *Bittrex) Withdraw(address, currency string, quantity float64) (withdrawUuid string, err error) {
r, err := b.client.do("GET", "account/withdraw?currency="+strings.ToUpper(currency)+"&quantity="+strconv.FormatFloat(quantity, 'f', 8, 64)+"&address="+address, "", true)
if err != nil {
return
}
var response jsonResponse
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
var u Uuid
err = json.Unmarshal(response.Result, &u)
withdrawUuid = u.Id
return
}
// GetOrderHistory used to retrieve your order history.
// market string literal for the market (ie. BTC-LTC). If set to "all", will return for all market
// count int : the number of records to return. Is set to 0, will return max history
func (b *Bittrex) GetOrderHistory(market string, count int) (orders []Order, err error) {
ressource := "account/getorderhistory"
if count != 0 || market != "all" {
ressource += "?"
}
if count != 0 {
ressource += fmt.Sprintf("count=%d&", count)
}
if market != "all" {
ressource += "market=" + market
}
r, err := b.client.do("GET", ressource, "", true)
if err != nil {
return
}
var response jsonResponse
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
err = json.Unmarshal(response.Result, &orders)
return
}
// GetWithdrawalHistory is used to retrieve your withdrawal history
// currency string a string literal for the currency (ie. BTC). If set to "all", will return for all currencies
// count int the number of records to return. Is set to 0 will return the max set.
func (b *Bittrex) GetWithdrawalHistory(currency string, count int) (withdrawals []Withdrawal, err error) {
ressource := "account/getwithdrawalhistory"
if count != 0 || currency != "all" {
ressource += "?"
}
if count != 0 {
ressource += fmt.Sprintf("count=%d&", count)
}
if currency != "all" {
ressource += "currency=" + currency
}
r, err := b.client.do("GET", ressource, "", true)
if err != nil {
return
}
var response jsonResponse
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
err = json.Unmarshal(response.Result, &withdrawals)
return
}
// GetDepositHistory is used to retrieve your deposit history
// currency string a string literal for the currency (ie. BTC). If set to "all", will return for all currencies
// count int the number of records to return. Is set to 0 will return the max set.
func (b *Bittrex) GetDepositHistory(currency string, count int) (deposits []Deposit, err error) {
ressource := "account/getdeposithistory"
if count != 0 || currency != "all" {
ressource += "?"
}
if count != 0 {
ressource += fmt.Sprintf("count=%d&", count)
}
if currency != "all" {
ressource += "currency=" + currency
}
r, err := b.client.do("GET", ressource, "", true)
if err != nil {
return
}
var response jsonResponse
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
err = json.Unmarshal(response.Result, &deposits)
return
}