forked from saniales/go-hitbtc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhitbtc.go
293 lines (256 loc) · 7.97 KB
/
hitbtc.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
// Package hitbtc is an implementation of the HitBTC API in Golang.
package hitbtc
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"
"strings"
"time"
)
const (
API_BASE = "https://api.hitbtc.com/api/2" // HitBtc API endpoint
)
// New returns an instantiated HitBTC struct
func New(apiKey, apiSecret string) *HitBtc {
client := NewClient(apiKey, apiSecret)
return &HitBtc{client}
}
// NewWithCustomHttpClient returns an instantiated HitBTC struct with custom http client
func NewWithCustomHttpClient(apiKey, apiSecret string, httpClient *http.Client) *HitBtc {
client := NewClientWithCustomHttpConfig(apiKey, apiSecret, httpClient)
return &HitBtc{client}
}
// NewWithCustomTimeout returns an instantiated HitBTC struct with custom timeout
func NewWithCustomTimeout(apiKey, apiSecret string, timeout time.Duration) *HitBtc {
client := NewClientWithCustomTimeout(apiKey, apiSecret, timeout)
return &HitBtc{client}
}
// HitBtc represent a HitBTC client
type HitBtc struct {
client *client
}
// SetDebug sets enable/disable http request/response dump
func (b *HitBtc) SetDebug(enable bool) {
b.client.debug = enable
}
// GetCurrencies is used to get all supported currencies at HitBtc along with other meta data.
func (b *HitBtc) GetCurrencies() (currencies []Currency, err error) {
r, err := b.client.do("GET", "public/currency", nil, false)
if err != nil {
return
}
err = json.Unmarshal(r, ¤cies)
return
}
// GetSymbols is used to get the open and available trading markets at HitBtc along with other meta data.
func (b *HitBtc) GetSymbols() (symbols []Symbol, err error) {
r, err := b.client.do("GET", "public/symbol", nil, false)
if err != nil {
return
}
err = json.Unmarshal(r, &symbols)
return
}
// GetTicker is used to get the current ticker values for a market.
func (b *HitBtc) GetTicker(market string) (ticker Ticker, err error) {
r, err := b.client.do("GET", "public/ticker/"+strings.ToUpper(market), nil, false)
if err != nil {
return
}
err = json.Unmarshal(r, &ticker)
return
}
// GetOrderbook is used to get the current order book for a market.
func (b *HitBtc) GetOrderbook(market string) (orderbook Orderbook, err error) {
r, err := b.client.do("GET", "public/orderbook/"+strings.ToUpper(market), nil, false)
if err != nil {
return
}
err = json.Unmarshal(r, &orderbook)
return
}
// GetAllTicker is used to get the current ticker values for all markets.
func (b *HitBtc) GetAllTicker() (tickers Tickers, err error) {
r, err := b.client.do("GET", "public/ticker", nil, false)
if err != nil {
return
}
err = json.Unmarshal(r, &tickers)
return
}
// GetBalances is used to retrieve all balances from your account
func (b *HitBtc) GetBalances() (balances []Balance, err error) {
r, err := b.client.do("GET", "trading/balance", nil, true)
if err != nil {
return
}
err = json.Unmarshal(r, &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 *HitBtc) GetBalance(currency string) (balance Balance, err error) {
balances, err := b.GetBalances()
currency = strings.ToUpper(currency)
for _, balance = range balances {
if balance.Currency == currency {
return
}
}
return Balance{}, errors.New("Currency not found")
}
// GetTrades used to retrieve your trade history.
// market string literal for the market (ie. BTC/LTC). If set to "all", will return for all market
func (b *HitBtc) GetTrades(currencyPair string) (trades []Trade, err error) {
payload := make(map[string]string)
if currencyPair != "all" {
payload["symbol"] = currencyPair
}
r, err := b.client.do("GET", "history/trades", payload, true)
if err != nil {
return
}
err = json.Unmarshal(r, &trades)
return
}
// CancelOrder cancels a pending order
func (b *HitBtc) CancelOrder(currencyPair string) (orders []Order, err error) {
payload := make(map[string]string)
if currencyPair != "all" {
payload["symbol"] = currencyPair
}
r, err := b.client.do("DELETE", "order", payload, true)
if err != nil {
return
}
err = json.Unmarshal(r, &orders)
return
}
// GetOrder gets a pending order data.
func (b *HitBtc) GetOrder(orderId string) (orders []Order, err error) {
payload := make(map[string]string)
payload["clientOrderId"] = orderId
r, err := b.client.do("GET", "history/order", payload, true)
if err != nil {
return
}
err = json.Unmarshal(r, &orders)
return
}
// GetOrderHistory gets the history of orders for an user.
func (b *HitBtc) GetOrderHistory() (orders []Order, err error) {
r, err := b.client.do("GET", "history/order", nil, true)
if err != nil {
return
}
err = json.Unmarshal(r, &orders)
return
}
// GetOpenOrders gets the open orders of an user.
func (b *HitBtc) GetOpenOrders() (orders []Order, err error) {
r, err := b.client.do("GET", "order", nil, true)
if err != nil {
return
}
err = json.Unmarshal(r, &orders)
return
}
// PlaceOrder creates a new order.
func (b *HitBtc) PlaceOrder(requestOrder Order) (responseOrder Order, err error) {
payload := make(map[string]string, 6)
payload["symbol"] = requestOrder.Symbol
payload["side"] = requestOrder.Side
payload["type"] = requestOrder.Type
payload["timeInForce"] = requestOrder.TimeInForce
payload["quantity"] = fmt.Sprintf("%.8f", requestOrder.Quantity)
payload["price"] = fmt.Sprintf("%.8f", requestOrder.Price)
method := "POST"
resource := "order"
if requestOrder.ClientOrderId != "" {
method = "PUT"
resource = fmt.Sprintf("%s/%s", resource, requestOrder.ClientOrderId)
}
r, err := b.client.do(method, resource, payload, true)
if err != nil {
return
}
err = json.Unmarshal(r, &responseOrder)
return
}
// GetTransactions is used to retrieve your withdrawal and deposit history
// "Start" and "end" are given in UNIX timestamp format in miliseconds and used to specify the date range for the data returned.
func (b *HitBtc) GetTransactions(start uint64, end uint64, limit uint32) (transactions []Transaction, err error) {
payload := make(map[string]string)
if start > 0 {
payload["from"] = strconv.FormatUint(uint64(start), 10)
}
if end == 0 {
end = uint64(time.Now().Unix()) * 1000
}
if end > 0 {
payload["till"] = strconv.FormatUint(uint64(end), 10)
}
if limit > 1000 {
limit = 1000
}
if limit > 0 {
payload["limit"] = strconv.FormatUint(uint64(limit), 10)
}
r, err := b.client.do("GET", "account/transactions", payload, true)
if err != nil {
return
}
err = json.Unmarshal(r, &transactions)
return
}
// Withdraw performs a withdrawal operation.
func (b *HitBtc) Withdraw(address string, currency string, amount float64) (withdrawID string, err error) {
type withdrawResponse struct {
ID string `json:"id"`
}
payload := map[string]string{
"currency": currency,
"address": address,
"amount": fmt.Sprint(amount),
}
r, err := b.client.do("POST", "account/crypto/withdraw", payload, true)
if err != nil {
return
}
var withdraw withdrawResponse
if err = json.Unmarshal(r, &withdraw); err != nil {
return
}
withdrawID = withdraw.ID
return
}
type transferType string
const (
// TransferTypeBankToExchange represent a transfer from bank (withdraw) balance to exchange (trading) balance.
TransferTypeBankToExchange transferType = "bankToExchange"
// TransferTypeExchangeToBank represent a transfer from exchange (trading) balance to bank (withdraw) balance.
TransferTypeExchangeToBank transferType = "exchangeToBank"
)
// TransferBalance performs a balance transfer operation between trading and bank accounts (both directions).
func (b *HitBtc) TransferBalance(currency string, amount float64, transferType transferType) (transferID string, err error) {
type transferResponse struct {
ID string `json:"id"`
}
payload := map[string]string{
"currency": currency,
"amount": fmt.Sprint(amount),
"type": string(transferType),
}
r, err := b.client.do("POST", "account/transfer", payload, true)
if err != nil {
return
}
var transfer transferResponse
if err = json.Unmarshal(r, &transfer); err != nil {
return
}
transferID = transfer.ID
return
}