forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
btcmarkets.go
392 lines (333 loc) · 10.1 KB
/
btcmarkets.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
package main
import (
"bytes"
"fmt"
"log"
"strconv"
"time"
)
const (
BTCMARKETS_API_URL = "https://api.btcmarkets.net"
BTCMARKETS_API_VERSION = "0"
BTCMARKETS_ACCOUNT_BALANCE = "/account/balance"
BTCMARKETS_ORDER_CREATE = "/order/create"
BTCMARKETS_ORDER_CANCEL = "/order/cancel"
BTCMARKETS_ORDER_HISTORY = "/order/history"
BTCMARKETS_ORDER_OPEN = "/order/open"
BTCMARKETS_ORDER_TRADE_HISTORY = "/order/trade/history"
BTCMARKETS_ORDER_DETAIL = "/order/detail"
)
type BTCMarkets struct {
Name string
Enabled bool
Verbose bool
Websocket bool
RESTPollingDelay time.Duration
Fee float64
Ticker map[string]BTCMarketsTicker
AuthenticatedAPISupport bool
APIKey, APISecret string
BaseCurrencies []string
AvailablePairs []string
EnabledPairs []string
}
type BTCMarketsTicker struct {
BestBID float64
BestAsk float64
LastPrice float64
Currency string
Instrument string
Timestamp int64
}
type BTCMarketsTrade struct {
TradeID int64 `json:"tid"`
Amount float64 `json:"amount"`
Price float64 `json:"price"`
Date int64 `json:"date"`
}
type BTCMarketsOrderbook struct {
Currency string `json:"currency"`
Instrument string `json:"instrument"`
Timestamp int64 `json:"timestamp"`
Asks [][]float64 `json:"asks"`
Bids [][]float64 `json:"bids"`
}
type BTCMarketsTradeResponse struct {
ID int64 `json:"id"`
CreationTime float64 `json:"creationTime"`
Description string `json:"description"`
Price float64 `json:"price"`
Volume float64 `json:"volume"`
Fee float64 `json:"fee"`
}
type BTCMarketsOrderResponse struct {
ID float64 `json:"id"`
Currency string `json:"currency"`
Instrument string `json:"instrument"`
OrderSide string `json:"orderSide"`
OrderType string `json:"ordertype"`
CreationTime float64 `json:"creationTime"`
Status string `json:"status"`
ErrorMessage string `json:"errorMessage"`
Price float64 `json:"price"`
Volume float64 `json:"volume"`
OpenVolume float64 `json:"openVolume"`
ClientRequestId string `json:"clientRequestId"`
}
func (b *BTCMarkets) SetDefaults() {
b.Name = "BTC Markets"
b.Enabled = true
b.Fee = 0.85
b.Verbose = false
b.Websocket = false
b.RESTPollingDelay = 10
b.Ticker = make(map[string]BTCMarketsTicker)
}
func (b *BTCMarkets) GetName() string {
return b.Name
}
func (b *BTCMarkets) SetEnabled(enabled bool) {
b.Enabled = enabled
}
func (b *BTCMarkets) IsEnabled() bool {
return b.Enabled
}
func (b *BTCMarkets) SetAPIKeys(apiKey, apiSecret string) {
if !b.AuthenticatedAPISupport {
return
}
b.APIKey = apiKey
result, err := Base64Decode(apiSecret)
if err != nil {
log.Printf("%s unable to decode secret key.\n", b.GetName())
b.Enabled = false
return
}
b.APISecret = string(result)
}
func (b *BTCMarkets) GetFee() float64 {
return b.Fee
}
func (b *BTCMarkets) Run() {
if b.Verbose {
log.Printf("%s polling delay: %ds.\n", b.GetName(), b.RESTPollingDelay)
log.Printf("%s %d currencies enabled: %s.\n", b.GetName(), len(b.EnabledPairs), b.EnabledPairs)
}
for b.Enabled {
for _, x := range b.EnabledPairs {
currency := x
go func() {
ticker, err := b.GetTicker(currency)
if err != nil {
log.Println(err)
return
}
b.Ticker[currency] = ticker
BTCMarketsLastUSD, _ := ConvertCurrency(ticker.LastPrice, "AUD", "USD")
BTCMarketsBestBidUSD, _ := ConvertCurrency(ticker.BestBID, "AUD", "USD")
BTCMarketsBestAskUSD, _ := ConvertCurrency(ticker.BestAsk, "AUD", "USD")
log.Printf("BTC Markets %s: Last %f (%f) Bid %f (%f) Ask %f (%f)\n", currency, BTCMarketsLastUSD, ticker.LastPrice, BTCMarketsBestBidUSD, ticker.BestBID, BTCMarketsBestAskUSD, ticker.BestAsk)
AddExchangeInfo(b.GetName(), currency[0:3], currency[3:], ticker.LastPrice, 0)
AddExchangeInfo(b.GetName(), currency[0:3], "USD", BTCMarketsLastUSD, 0)
}()
}
time.Sleep(time.Second * b.RESTPollingDelay)
}
}
func (b *BTCMarkets) GetTicker(symbol string) (BTCMarketsTicker, error) {
ticker := BTCMarketsTicker{}
path := fmt.Sprintf("/market/%s/AUD/tick", symbol)
err := SendHTTPGetRequest(BTCMARKETS_API_URL+path, true, &ticker)
if err != nil {
return BTCMarketsTicker{}, err
}
return ticker, nil
}
func (b *BTCMarkets) GetOrderbook(symbol string) (BTCMarketsOrderbook, error) {
orderbook := BTCMarketsOrderbook{}
path := fmt.Sprintf("/market/%s/AUD/orderbook", symbol)
err := SendHTTPGetRequest(BTCMARKETS_API_URL+path, true, &orderbook)
if err != nil {
return BTCMarketsOrderbook{}, err
}
return orderbook, nil
}
func (b *BTCMarkets) GetTrades(symbol, since string) ([]BTCMarketsTrade, error) {
trades := []BTCMarketsTrade{}
path := ""
if len(since) > 0 {
path = fmt.Sprintf("/market/%s/AUD/trades?since=%s", symbol, since)
} else {
path = fmt.Sprintf("/market/%s/AUD/trades", symbol)
}
err := SendHTTPGetRequest(BTCMARKETS_API_URL+path, true, &trades)
if err != nil {
return nil, err
}
return trades, nil
}
func (b *BTCMarkets) Order(currency, instrument string, price, amount int64, orderSide, orderType, clientReq string) (int, error) {
type Order struct {
Currency string `json:"currency"`
Instrument string `json:"instrument"`
Price int64 `json:"price"`
Volume int64 `json:"volume"`
OrderSide string `json:"orderSide"`
OrderType string `json:"ordertype"`
ClientRequestId string `json:"clientRequestId"`
}
order := Order{}
order.Currency = currency
order.Instrument = instrument
order.Price = price
order.Volume = amount
order.OrderSide = orderSide
order.OrderType = orderType
order.ClientRequestId = clientReq
JSONPayload, err := JSONEncode(order)
if err != nil {
return 0, err
}
type Response struct {
Success bool `json:"success"`
ErrorCode int `json:"errorCode"`
ErrorMessage string `json:"errorMessage"`
ID int `json:"id"`
ClientRequestID string `json:"clientRequestId"`
}
var resp Response
err = b.SendAuthenticatedRequest("POST", BTCMARKETS_ORDER_CREATE, JSONPayload, &resp)
if err != nil {
return 0, err
}
if !resp.Success {
return 0, fmt.Errorf("%s Unable to place order. Error message: %s\n", b.GetName(), resp.ErrorMessage)
}
return resp.ID, nil
}
func (b *BTCMarkets) CancelOrder(orderID []int64) (bool, error) {
type CancelOrder struct {
OrderIDs []int64 `json:"orderIds"`
}
orders := CancelOrder{}
orders.OrderIDs = append(orders.OrderIDs, orderID...)
JSONPayload, err := JSONEncode(orders)
if err != nil {
return false, err
}
type Response struct {
Success bool `json:"success"`
ErrorCode int `json:"errorCode"`
ErrorMessage string `json:"errorMessage"`
Responses []struct {
Success bool `json:"success"`
ErrorCode int `json:"errorCode"`
ErrorMessage string `json:"errorMessage"`
ID int64 `json:"id"`
}
ClientRequestID string `json:"clientRequestId"`
}
var resp Response
err = b.SendAuthenticatedRequest("POST", BTCMARKETS_ORDER_CANCEL, JSONPayload, &resp)
if err != nil {
return false, err
}
if !resp.Success {
return false, fmt.Errorf("%s Unable to cancel order. Error message: %s\n", b.GetName(), resp.ErrorMessage)
}
ordersToBeCancelled := len(orderID)
ordersCancelled := 0
for _, y := range resp.Responses {
if y.Success {
ordersCancelled++
log.Printf("%s Cancelled order %d.\n", b.GetName(), y.ID)
} else {
log.Printf("%s Unable to cancel order %d. Error message: %s\n", b.GetName(), y.ID, y.ErrorMessage)
}
}
if ordersCancelled == ordersToBeCancelled {
return true, nil
} else {
return false, fmt.Errorf("%s Unable to cancel order(s).", b.GetName())
}
}
func (b *BTCMarkets) GetOrders(currency, instrument string, limit, since int64, historic bool) {
request := make(map[string]interface{})
request["currency"] = currency
request["instrument"] = instrument
request["limit"] = limit
request["since"] = since
JSONPayload, err := JSONEncode(request)
if err != nil {
log.Println(err)
return
}
path := BTCMARKETS_ORDER_OPEN
if historic {
path = BTCMARKETS_ORDER_HISTORY
}
err = b.SendAuthenticatedRequest("POST", path, JSONPayload, nil)
if err != nil {
log.Println(err)
}
}
func (b *BTCMarkets) GetOrderDetail(orderID []int64) {
type OrderDetail struct {
OrderIDs []int64 `json:"orderIds"`
}
orders := OrderDetail{}
orders.OrderIDs = append(orders.OrderIDs, orderID...)
JSONPayload, err := JSONEncode(orders)
if err != nil {
log.Println(err)
return
}
err = b.SendAuthenticatedRequest("POST", BTCMARKETS_ORDER_DETAIL, JSONPayload, nil)
if err != nil {
log.Println(err)
}
}
func (b *BTCMarkets) GetAccountBalance() {
type Balance struct {
Balance float64 `json:"balance"`
PendingFunds float64 `json:"pendingFunds"`
Currency string `json:"currency"`
}
balance := []Balance{}
err := b.SendAuthenticatedRequest("GET", BTCMARKETS_ACCOUNT_BALANCE, nil, &balance)
if err != nil {
log.Println(err)
}
}
func (b *BTCMarkets) SendAuthenticatedRequest(reqType, path string, data []byte, result interface{}) error {
nonce := strconv.FormatInt(time.Now().UnixNano(), 10)[0:13]
request := ""
if data != nil {
request = path + "\n" + nonce + "\n" + string(data)
} else {
request = path + "\n" + nonce + "\n"
}
hmac := GetHMAC(HASH_SHA512, []byte(request), []byte(b.APISecret))
if b.Verbose {
log.Printf("Sending %s request to URL %s with params %s\n", reqType, BTCMARKETS_API_URL+path, request)
}
headers := make(map[string]string)
headers["Accept"] = "application/json"
headers["Accept-Charset"] = "UTF-8"
headers["Content-Type"] = "application/json"
headers["apikey"] = b.APIKey
headers["timestamp"] = nonce
headers["signature"] = Base64Encode(hmac)
resp, err := SendHTTPRequest(reqType, BTCMARKETS_API_URL+path, headers, bytes.NewBuffer(data))
if err != nil {
return err
}
if b.Verbose {
log.Printf("Recieved raw: %s\n", resp)
}
err = JSONDecode([]byte(resp), &result)
if err != nil {
return err
}
return nil
}