Files
gocryptotrader/exchanges/kraken/kraken_wrapper.go
Andrew d01e7bad72 Implement Logger (#228)
* Added new base logger

* updated example and test configs

* updated exchange helpers restful router & server

* logPath is now passed to the logger to remove dependency on common package

* updated everything besides exchanges to use new logger

* alphapoint to bitmex done

* updated bitmex bitstamp bittrex btcc and also performance changes to logger

* btcmarkets coinbase coinut exmo gateio wrappers updated

* gateio and gemini logger updated

* hitbtc huobi itbit & kraken updated

* All exchanges updatd

* return correct error for disabled websocket

* don't disconnect client on invalid json

* updated router internal logging

* log.Fatal to t.Error for tests

* Changed from fatal to error failure to set maxprocs

* output ANSI codes for everything but windows for now due to lack of windows support

* added error handling to logger and unit tests

* clear wording on print -> log.print

* added benchmark test

* cleaned up import sections

* Updated logger based on PR requests (added default config options on failure/setting errors)

* ah this should fix travici enc config issue

* Load entire config and clear out logging to hopefully fix travisci issue

* wording & test error handling

* fixed formatting issues based on feedback

* fixed formatting issues based on feedback

* changed CheckDir to use mkdirall instead of mkdir and other changes based on feedback
2019-01-08 21:56:22 +11:00

275 lines
8.6 KiB
Go

package kraken
import (
"strings"
"sync"
"github.com/thrasher-/gocryptotrader/common"
"github.com/thrasher-/gocryptotrader/currency/pair"
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 Kraken go routine
func (k *Kraken) Start(wg *sync.WaitGroup) {
wg.Add(1)
go func() {
k.Run()
wg.Done()
}()
}
// Run implements the Kraken wrapper
func (k *Kraken) Run() {
if k.Verbose {
log.Debugf("%s polling delay: %ds.\n", k.GetName(), k.RESTPollingDelay)
log.Debugf("%s %d currencies enabled: %s.\n", k.GetName(), len(k.EnabledPairs), k.EnabledPairs)
}
assetPairs, err := k.GetAssetPairs()
if err != nil {
log.Errorf("%s Failed to get available symbols.\n", k.GetName())
} else {
forceUpgrade := false
if !common.StringDataContains(k.EnabledPairs, "-") || !common.StringDataContains(k.AvailablePairs, "-") {
forceUpgrade = true
}
var exchangeProducts []string
for _, v := range assetPairs {
if common.StringContains(v.Altname, ".d") {
continue
}
if v.Base[0] == 'X' {
if len(v.Base) > 3 {
v.Base = v.Base[1:]
}
}
if v.Quote[0] == 'Z' || v.Quote[0] == 'X' {
v.Quote = v.Quote[1:]
}
exchangeProducts = append(exchangeProducts, v.Base+"-"+v.Quote)
}
if forceUpgrade {
enabledPairs := []string{"XBT-USD"}
log.Warn("Available pairs for Kraken reset due to config upgrade, please enable the ones you would like again")
err = k.UpdateCurrencies(enabledPairs, true, true)
if err != nil {
log.Errorf("%s Failed to get config.\n", k.GetName())
}
}
err = k.UpdateCurrencies(exchangeProducts, false, forceUpgrade)
if err != nil {
log.Errorf("%s Failed to get config.\n", k.GetName())
}
}
}
// UpdateTicker updates and returns the ticker for a currency pair
func (k *Kraken) UpdateTicker(p pair.CurrencyPair, assetType string) (ticker.Price, error) {
var tickerPrice ticker.Price
pairs := k.GetEnabledCurrencies()
pairsCollated, err := exchange.GetAndFormatExchangeCurrencies(k.Name, pairs)
if err != nil {
return tickerPrice, err
}
tickers, err := k.GetTickers(pairsCollated.String())
if err != nil {
return tickerPrice, err
}
for _, x := range pairs {
for y, z := range tickers {
if common.StringContains(y, x.FirstCurrency.Upper().String()) && common.StringContains(y, x.SecondCurrency.Upper().String()) {
var tp ticker.Price
tp.Pair = x
tp.Last = z.Last
tp.Ask = z.Ask
tp.Bid = z.Bid
tp.High = z.High
tp.Low = z.Low
tp.Volume = z.Volume
ticker.ProcessTicker(k.GetName(), x, tp, assetType)
}
}
}
return ticker.GetTicker(k.GetName(), p, assetType)
}
// GetTickerPrice returns the ticker for a currency pair
func (k *Kraken) GetTickerPrice(p pair.CurrencyPair, assetType string) (ticker.Price, error) {
tickerNew, err := ticker.GetTicker(k.GetName(), p, assetType)
if err != nil {
return k.UpdateTicker(p, assetType)
}
return tickerNew, nil
}
// GetOrderbookEx returns orderbook base on the currency pair
func (k *Kraken) GetOrderbookEx(p pair.CurrencyPair, assetType string) (orderbook.Base, error) {
ob, err := orderbook.GetOrderbook(k.GetName(), p, assetType)
if err != nil {
return k.UpdateOrderbook(p, assetType)
}
return ob, nil
}
// UpdateOrderbook updates and returns the orderbook for a currency pair
func (k *Kraken) UpdateOrderbook(p pair.CurrencyPair, assetType string) (orderbook.Base, error) {
var orderBook orderbook.Base
orderbookNew, err := k.GetDepth(exchange.FormatExchangeCurrency(k.GetName(), p).String())
if err != nil {
return orderBook, err
}
for x := range orderbookNew.Bids {
orderBook.Bids = append(orderBook.Bids, orderbook.Item{Amount: orderbookNew.Bids[x].Amount, Price: orderbookNew.Bids[x].Price})
}
for x := range orderbookNew.Asks {
orderBook.Asks = append(orderBook.Asks, orderbook.Item{Amount: orderbookNew.Asks[x].Amount, Price: orderbookNew.Asks[x].Price})
}
orderbook.ProcessOrderbook(k.GetName(), p, orderBook, assetType)
return orderbook.GetOrderbook(k.Name, p, assetType)
}
// GetAccountInfo retrieves balances for all enabled currencies for the
// Kraken exchange - to-do
func (k *Kraken) GetAccountInfo() (exchange.AccountInfo, error) {
var info exchange.AccountInfo
info.ExchangeName = k.GetName()
bal, err := k.GetBalance()
if err != nil {
return info, err
}
var balances []exchange.AccountCurrencyInfo
for key, data := range bal {
balances = append(balances, exchange.AccountCurrencyInfo{
CurrencyName: key,
TotalValue: data,
})
}
info.Currencies = balances
return info, nil
}
// GetFundingHistory returns funding history, deposits and
// withdrawals
func (k *Kraken) GetFundingHistory() ([]exchange.FundHistory, error) {
var fundHistory []exchange.FundHistory
return fundHistory, common.ErrFunctionNotSupported
}
// GetExchangeHistory returns historic trade data since exchange opening.
func (k *Kraken) GetExchangeHistory(p pair.CurrencyPair, assetType string) ([]exchange.TradeHistory, error) {
var resp []exchange.TradeHistory
return resp, common.ErrNotYetImplemented
}
// SubmitOrder submits a new order
func (k *Kraken) SubmitOrder(p pair.CurrencyPair, side exchange.OrderSide, orderType exchange.OrderType, amount, price float64, clientID string) (exchange.SubmitOrderResponse, error) {
var submitOrderResponse exchange.SubmitOrderResponse
var args = AddOrderOptions{}
response, err := k.AddOrder(p.Pair().String(), side.ToString(), orderType.ToString(), amount, price, 0, 0, args)
if len(response.TransactionIds) > 0 {
submitOrderResponse.OrderID = strings.Join(response.TransactionIds, ", ")
}
if err == nil {
submitOrderResponse.IsOrderPlaced = true
}
return submitOrderResponse, err
}
// ModifyOrder will allow of changing orderbook placement and limit to
// market conversion
func (k *Kraken) ModifyOrder(action exchange.ModifyOrder) (string, error) {
return "", common.ErrFunctionNotSupported
}
// CancelOrder cancels an order by its corresponding ID number
func (k *Kraken) CancelOrder(order exchange.OrderCancellation) error {
_, err := k.CancelExistingOrder(order.OrderID)
return err
}
// CancelAllOrders cancels all orders associated with a currency pair
func (k *Kraken) CancelAllOrders(orderCancellation exchange.OrderCancellation) (exchange.CancelAllOrdersResponse, error) {
cancelAllOrdersResponse := exchange.CancelAllOrdersResponse{
OrderStatus: make(map[string]string),
}
var emptyOrderOptions OrderInfoOptions
openOrders, err := k.GetOpenOrders(emptyOrderOptions)
if err != nil {
return cancelAllOrdersResponse, err
}
if openOrders.Count > 0 {
for orderID := range openOrders.Open {
_, err = k.CancelExistingOrder(orderID)
if err != nil {
cancelAllOrdersResponse.OrderStatus[orderID] = err.Error()
}
}
}
return cancelAllOrdersResponse, nil
}
// GetOrderInfo returns information on a current open order
func (k *Kraken) GetOrderInfo(orderID int64) (exchange.OrderDetail, error) {
var orderDetail exchange.OrderDetail
return orderDetail, common.ErrNotYetImplemented
}
// GetDepositAddress returns a deposit address for a specified currency
func (k *Kraken) GetDepositAddress(cryptocurrency pair.CurrencyItem) (string, error) {
return "", common.ErrNotYetImplemented
}
// WithdrawCryptocurrencyFunds returns a withdrawal ID when a withdrawal
// Populate exchange.WithdrawRequest.TradePassword with withdrawal key name, as set up on your account
func (k *Kraken) WithdrawCryptocurrencyFunds(withdrawRequest exchange.WithdrawRequest) (string, error) {
return k.Withdraw(withdrawRequest.Currency.String(), withdrawRequest.TradePassword, withdrawRequest.Amount)
}
// WithdrawFiatFunds returns a withdrawal ID when a
// withdrawal is submitted
func (k *Kraken) WithdrawFiatFunds(withdrawRequest exchange.WithdrawRequest) (string, error) {
return "", common.ErrNotYetImplemented
}
// WithdrawFiatFundsToInternationalBank returns a withdrawal ID when a
// withdrawal is submitted
func (k *Kraken) WithdrawFiatFundsToInternationalBank(withdrawRequest exchange.WithdrawRequest) (string, error) {
return "", common.ErrNotYetImplemented
}
// GetWebsocket returns a pointer to the exchange websocket
func (k *Kraken) GetWebsocket() (*exchange.Websocket, error) {
return nil, common.ErrNotYetImplemented
}
// GetFeeByType returns an estimate of fee based on type of transaction
func (k *Kraken) GetFeeByType(feeBuilder exchange.FeeBuilder) (float64, error) {
return k.GetFee(feeBuilder)
}
// GetWithdrawCapabilities returns the types of withdrawal methods permitted by the exchange
func (k *Kraken) GetWithdrawCapabilities() uint32 {
return k.GetWithdrawPermissions()
}