forked from thrasher-corp/gocryptotrader
/
btse_wrapper.go
384 lines (331 loc) · 11.3 KB
/
btse_wrapper.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
package btse
import (
"errors"
"fmt"
"strings"
"sync"
"time"
"github.com/thrasher-/gocryptotrader/common"
"github.com/thrasher-/gocryptotrader/currency"
exchange "github.com/thrasher-/gocryptotrader/exchanges"
"github.com/thrasher-/gocryptotrader/exchanges/orderbook"
"github.com/thrasher-/gocryptotrader/exchanges/ticker"
log "github.com/thrasher-/gocryptotrader/logger"
)
// Start starts the BTSE go routine
func (b *BTSE) Start(wg *sync.WaitGroup) {
wg.Add(1)
go func() {
b.Run()
wg.Done()
}()
}
// Run implements the BTSE wrapper
func (b *BTSE) Run() {
if b.Verbose {
log.Debugf("%s Websocket: %s. (url: %s).\n", b.GetName(), common.IsEnabled(b.Websocket.IsEnabled()), b.Websocket.GetWebsocketURL())
log.Debugf("%s polling delay: %ds.\n", b.GetName(), b.RESTPollingDelay)
log.Debugf("%s %d currencies enabled: %s.\n", b.GetName(), len(b.EnabledPairs), b.EnabledPairs)
}
markets, err := b.GetMarkets()
if err != nil {
log.Errorf("%s failed to get trading pairs. Err: %s", b.Name, err)
} else {
var currencies []string
for _, m := range *markets {
currencies = append(currencies, m.ID)
}
err = b.UpdateCurrencies(currency.NewPairsFromStrings(currencies),
false,
false)
if err != nil {
log.Errorf("%s Failed to update available currencies.\n", b.Name)
}
}
}
// UpdateTicker updates and returns the ticker for a currency pair
func (b *BTSE) UpdateTicker(p currency.Pair, assetType string) (ticker.Price, error) {
var tickerPrice ticker.Price
t, err := b.GetTicker(exchange.FormatExchangeCurrency(b.Name, p).String())
if err != nil {
return tickerPrice, err
}
s, err := b.GetMarketStatistics(exchange.FormatExchangeCurrency(b.Name, p).String())
if err != nil {
return tickerPrice, err
}
tickerPrice.Pair = p
tickerPrice.Ask = t.Ask
tickerPrice.Bid = t.Bid
tickerPrice.Low = s.Low
tickerPrice.Last = t.Price
tickerPrice.Volume = s.Volume
tickerPrice.High = s.High
err = ticker.ProcessTicker(b.GetName(), &tickerPrice, assetType)
if err != nil {
return tickerPrice, err
}
return ticker.GetTicker(b.Name, p, assetType)
}
// GetTickerPrice returns the ticker for a currency pair
func (b *BTSE) GetTickerPrice(p currency.Pair, assetType string) (ticker.Price, error) {
tickerNew, err := ticker.GetTicker(b.GetName(), p, assetType)
if err != nil {
return b.UpdateTicker(p, assetType)
}
return tickerNew, nil
}
// GetOrderbookEx returns orderbook base on the currency pair
func (b *BTSE) GetOrderbookEx(p currency.Pair, assetType string) (orderbook.Base, error) {
ob, err := orderbook.Get(b.GetName(), p, assetType)
if err != nil {
return b.UpdateOrderbook(p, assetType)
}
return ob, nil
}
// UpdateOrderbook updates and returns the orderbook for a currency pair
func (b *BTSE) UpdateOrderbook(p currency.Pair, assetType string) (orderbook.Base, error) {
return orderbook.Base{}, common.ErrFunctionNotSupported
}
// GetAccountInfo retrieves balances for all enabled currencies for the
// BTSE exchange
func (b *BTSE) GetAccountInfo() (exchange.AccountInfo, error) {
var a exchange.AccountInfo
balance, err := b.GetAccountBalance()
if err != nil {
return a, err
}
var currencies []exchange.AccountCurrencyInfo
for _, b := range *balance {
currencies = append(currencies,
exchange.AccountCurrencyInfo{
CurrencyName: currency.NewCode(b.Currency),
TotalValue: b.Total,
Hold: b.Available,
},
)
}
a.Exchange = b.Name
a.Accounts = []exchange.Account{
{
Currencies: currencies,
},
}
return a, nil
}
// GetFundingHistory returns funding history, deposits and
// withdrawals
func (b *BTSE) GetFundingHistory() ([]exchange.FundHistory, error) {
return nil, common.ErrFunctionNotSupported
}
// GetExchangeHistory returns historic trade data since exchange opening.
func (b *BTSE) GetExchangeHistory(p currency.Pair, assetType string) ([]exchange.TradeHistory, error) {
return nil, common.ErrNotYetImplemented
}
// SubmitOrder submits a new order
func (b *BTSE) SubmitOrder(p currency.Pair, side exchange.OrderSide, orderType exchange.OrderType, amount, price float64, clientID string) (exchange.SubmitOrderResponse, error) {
var resp exchange.SubmitOrderResponse
r, err := b.CreateOrder(amount, price, side.ToString(),
orderType.ToString(), exchange.FormatExchangeCurrency(b.Name, p).String(), "GTC", clientID)
if err != nil {
return resp, err
}
if *r != "" {
resp.IsOrderPlaced = true
resp.OrderID = *r
}
return resp, nil
}
// ModifyOrder will allow of changing orderbook placement and limit to
// market conversion
func (b *BTSE) ModifyOrder(action *exchange.ModifyOrder) (string, error) {
return "", common.ErrFunctionNotSupported
}
// CancelOrder cancels an order by its corresponding ID number
func (b *BTSE) CancelOrder(order *exchange.OrderCancellation) error {
r, err := b.CancelExistingOrder(order.OrderID,
exchange.FormatExchangeCurrency(b.Name, order.CurrencyPair).String())
if err != nil {
return err
}
switch r.Code {
case -1:
return errors.New("order cancellation unsuccessful")
case 4:
return errors.New("order cancellation timeout")
}
return nil
}
// CancelAllOrders cancels all orders associated with a currency pair
// If product ID is sent, all orders of that specified market will be cancelled
// If not specified, all orders of all markets will be cancelled
func (b *BTSE) CancelAllOrders(orderCancellation *exchange.OrderCancellation) (exchange.CancelAllOrdersResponse, error) {
var resp exchange.CancelAllOrdersResponse
r, err := b.CancelOrders(exchange.FormatExchangeCurrency(b.Name,
orderCancellation.CurrencyPair).String())
if err != nil {
return resp, err
}
switch r.Code {
case -1:
return resp, errors.New("order cancellation unsuccessful")
case 4:
return resp, errors.New("order cancellation timeout")
}
return resp, nil
}
// GetOrderInfo returns information on a current open order
func (b *BTSE) GetOrderInfo(orderID string) (exchange.OrderDetail, error) {
o, err := b.GetOrders("")
if err != nil {
return exchange.OrderDetail{}, err
}
var od exchange.OrderDetail
if len(*o) == 0 {
return od, errors.New("no orders found")
}
for i := range *o {
o := (*o)[i]
if o.ID != orderID {
continue
}
var side = exchange.BuyOrderSide
if strings.EqualFold(o.Side, exchange.AskOrderSide.ToString()) {
side = exchange.SellOrderSide
}
od.CurrencyPair = currency.NewPairDelimiter(o.ProductID,
b.ConfigCurrencyPairFormat.Delimiter)
od.Exchange = b.Name
od.Amount = o.Amount
od.ID = o.ID
od.OrderDate = parseOrderTime(o.CreatedAt)
od.OrderSide = side
od.OrderType = exchange.OrderType(strings.ToUpper(o.Type))
od.Price = o.Price
od.Status = o.Status
fills, err := b.GetFills(orderID, "", "", "", "")
if err != nil {
return od, fmt.Errorf("unable to get order fills for orderID %s", orderID)
}
for i := range *fills {
f := (*fills)[i]
createdAt, _ := time.Parse(time.RFC3339, f.CreatedAt)
od.Trades = append(od.Trades, exchange.TradeHistory{
Timestamp: createdAt,
TID: f.ID,
Price: f.Price,
Amount: f.Amount,
Exchange: b.Name,
Type: exchange.OrderSide(f.Side).ToString(),
Fee: f.Fee,
})
}
}
return od, nil
}
// GetDepositAddress returns a deposit address for a specified currency
func (b *BTSE) GetDepositAddress(cryptocurrency currency.Code, accountID string) (string, error) {
return "", common.ErrFunctionNotSupported
}
// WithdrawCryptocurrencyFunds returns a withdrawal ID when a withdrawal is
// submitted
func (b *BTSE) WithdrawCryptocurrencyFunds(withdrawRequest *exchange.WithdrawRequest) (string, error) {
return "", common.ErrFunctionNotSupported
}
// WithdrawFiatFunds returns a withdrawal ID when a withdrawal is
// submitted
func (b *BTSE) WithdrawFiatFunds(withdrawRequest *exchange.WithdrawRequest) (string, error) {
return "", common.ErrFunctionNotSupported
}
// WithdrawFiatFundsToInternationalBank returns a withdrawal ID when a withdrawal is
// submitted
func (b *BTSE) WithdrawFiatFundsToInternationalBank(withdrawRequest *exchange.WithdrawRequest) (string, error) {
return "", common.ErrFunctionNotSupported
}
// GetWebsocket returns a pointer to the exchange websocket
func (b *BTSE) GetWebsocket() (*exchange.Websocket, error) {
return b.Websocket, nil
}
// GetActiveOrders retrieves any orders that are active/open
func (b *BTSE) GetActiveOrders(getOrdersRequest *exchange.GetOrdersRequest) ([]exchange.OrderDetail, error) {
resp, err := b.GetOrders("")
if err != nil {
return nil, err
}
var orders []exchange.OrderDetail
for i := range *resp {
order := (*resp)[i]
var side = exchange.BuyOrderSide
if strings.EqualFold(order.Side, exchange.AskOrderSide.ToString()) {
side = exchange.SellOrderSide
}
openOrder := exchange.OrderDetail{
CurrencyPair: currency.NewPairDelimiter(order.ProductID,
b.ConfigCurrencyPairFormat.Delimiter),
Exchange: b.Name,
Amount: order.Amount,
ID: order.ID,
OrderDate: parseOrderTime(order.CreatedAt),
OrderSide: side,
OrderType: exchange.OrderType(strings.ToUpper(order.Type)),
Price: order.Price,
Status: order.Status,
}
fills, err := b.GetFills(order.ID, "", "", "", "")
if err != nil {
log.Errorf("unable to get order fills for orderID %s", order.ID)
continue
}
for i := range *fills {
f := (*fills)[i]
createdAt, _ := time.Parse(time.RFC3339, f.CreatedAt)
openOrder.Trades = append(openOrder.Trades, exchange.TradeHistory{
Timestamp: createdAt,
TID: f.ID,
Price: f.Price,
Amount: f.Amount,
Exchange: b.Name,
Type: exchange.OrderSide(f.Side).ToString(),
Fee: f.Fee,
})
}
orders = append(orders, openOrder)
}
exchange.FilterOrdersByType(&orders, getOrdersRequest.OrderType)
exchange.FilterOrdersByTickRange(&orders, getOrdersRequest.StartTicks, getOrdersRequest.EndTicks)
exchange.FilterOrdersBySide(&orders, getOrdersRequest.OrderSide)
return orders, nil
}
// GetOrderHistory retrieves account order information
// Can Limit response to specific order status
func (b *BTSE) GetOrderHistory(getOrdersRequest *exchange.GetOrdersRequest) ([]exchange.OrderDetail, error) {
return nil, common.ErrFunctionNotSupported
}
// GetFeeByType returns an estimate of fee based on type of transaction
func (b *BTSE) GetFeeByType(feeBuilder *exchange.FeeBuilder) (float64, error) {
if (b.APIKey == "" || b.APISecret == "") && // Todo check connection status
feeBuilder.FeeType == exchange.CryptocurrencyTradeFee {
feeBuilder.FeeType = exchange.OfflineTradeFee
}
return b.GetFee(feeBuilder)
}
// SubscribeToWebsocketChannels appends to ChannelsToSubscribe
// which lets websocket.manageSubscriptions handle subscribing
func (b *BTSE) SubscribeToWebsocketChannels(channels []exchange.WebsocketChannelSubscription) error {
b.Websocket.SubscribeToChannels(channels)
return nil
}
// UnsubscribeToWebsocketChannels removes from ChannelsToSubscribe
// which lets websocket.manageSubscriptions handle unsubscribing
func (b *BTSE) UnsubscribeToWebsocketChannels(channels []exchange.WebsocketChannelSubscription) error {
b.Websocket.UnsubscribeToChannels(channels)
return nil
}
// GetSubscriptions returns a copied list of subscriptions
func (b *BTSE) GetSubscriptions() ([]exchange.WebsocketChannelSubscription, error) {
return b.Websocket.GetSubscriptions(), nil
}
// AuthenticateWebsocket sends an authentication message to the websocket
func (b *BTSE) AuthenticateWebsocket() error {
return common.ErrFunctionNotSupported
}