Files
gocryptotrader/exchanges/gdax/gdax_wrapper.go

178 lines
5.8 KiB
Go

package gdax
import (
"errors"
"log"
"sync"
"github.com/thrasher-/gocryptotrader/common"
"github.com/thrasher-/gocryptotrader/currency/pair"
"github.com/thrasher-/gocryptotrader/exchanges"
"github.com/thrasher-/gocryptotrader/exchanges/orderbook"
"github.com/thrasher-/gocryptotrader/exchanges/ticker"
)
// Start starts the GDAX go routine
func (g *GDAX) Start(wg *sync.WaitGroup) {
wg.Add(1)
go func() {
g.Run()
wg.Done()
}()
}
// Run implements the GDAX wrapper
func (g *GDAX) Run() {
if g.Verbose {
log.Printf("%s Websocket: %s. (url: %s).\n", g.GetName(), common.IsEnabled(g.Websocket), gdaxWebsocketURL)
log.Printf("%s polling delay: %ds.\n", g.GetName(), g.RESTPollingDelay)
log.Printf("%s %d currencies enabled: %s.\n", g.GetName(), len(g.EnabledPairs), g.EnabledPairs)
}
if g.Websocket {
go g.WebsocketClient()
}
exchangeProducts, err := g.GetProducts()
if err != nil {
log.Printf("%s Failed to get available products.\n", g.GetName())
} else {
currencies := []string{}
for _, x := range exchangeProducts {
if x.ID != "BTC" && x.ID != "USD" && x.ID != "GBP" {
currencies = append(currencies, x.ID[0:3]+x.ID[4:])
}
}
err = g.UpdateCurrencies(currencies, false, false)
if err != nil {
log.Printf("%s Failed to update available currencies.\n", g.GetName())
}
}
}
// GetExchangeAccountInfo retrieves balances for all enabled currencies for the
// GDAX exchange
func (g *GDAX) GetExchangeAccountInfo() (exchange.AccountInfo, error) {
var response exchange.AccountInfo
response.ExchangeName = g.GetName()
accountBalance, err := g.GetAccounts()
if err != nil {
return response, err
}
for i := 0; i < len(accountBalance); i++ {
var exchangeCurrency exchange.AccountCurrencyInfo
exchangeCurrency.CurrencyName = accountBalance[i].Currency
exchangeCurrency.TotalValue = accountBalance[i].Available
exchangeCurrency.Hold = accountBalance[i].Hold
response.Currencies = append(response.Currencies, exchangeCurrency)
}
return response, nil
}
// UpdateTicker updates and returns the ticker for a currency pair
func (g *GDAX) UpdateTicker(p pair.CurrencyPair, assetType string) (ticker.Price, error) {
var tickerPrice ticker.Price
tick, err := g.GetTicker(exchange.FormatExchangeCurrency(g.Name, p).String())
if err != nil {
return ticker.Price{}, err
}
stats, err := g.GetStats(exchange.FormatExchangeCurrency(g.Name, p).String())
if err != nil {
return ticker.Price{}, err
}
tickerPrice.Pair = p
tickerPrice.Volume = stats.Volume
tickerPrice.Last = tick.Price
tickerPrice.High = stats.High
tickerPrice.Low = stats.Low
ticker.ProcessTicker(g.GetName(), p, tickerPrice, assetType)
return ticker.GetTicker(g.Name, p, assetType)
}
// GetTickerPrice returns the ticker for a currency pair
func (g *GDAX) GetTickerPrice(p pair.CurrencyPair, assetType string) (ticker.Price, error) {
tickerNew, err := ticker.GetTicker(g.GetName(), p, assetType)
if err != nil {
return g.UpdateTicker(p, assetType)
}
return tickerNew, nil
}
// GetOrderbookEx returns orderbook base on the currency pair
func (g *GDAX) GetOrderbookEx(p pair.CurrencyPair, assetType string) (orderbook.Base, error) {
ob, err := orderbook.GetOrderbook(g.GetName(), p, assetType)
if err != nil {
return g.UpdateOrderbook(p, assetType)
}
return ob, nil
}
// UpdateOrderbook updates and returns the orderbook for a currency pair
func (g *GDAX) UpdateOrderbook(p pair.CurrencyPair, assetType string) (orderbook.Base, error) {
var orderBook orderbook.Base
orderbookNew, err := g.GetOrderbook(exchange.FormatExchangeCurrency(g.Name, p).String(), 2)
if err != nil {
return orderBook, err
}
obNew := orderbookNew.(OrderbookL1L2)
for x := range obNew.Bids {
orderBook.Bids = append(orderBook.Bids, orderbook.Item{Amount: obNew.Bids[x].Amount, Price: obNew.Bids[x].Price})
}
for x := range obNew.Asks {
orderBook.Asks = append(orderBook.Asks, orderbook.Item{Amount: obNew.Bids[x].Amount, Price: obNew.Bids[x].Price})
}
orderbook.ProcessOrderbook(g.GetName(), p, orderBook, assetType)
return orderbook.GetOrderbook(g.Name, p, assetType)
}
// GetExchangeHistory returns historic trade data since exchange opening.
func (g *GDAX) GetExchangeHistory(p pair.CurrencyPair, assetType string) ([]exchange.TradeHistory, error) {
var resp []exchange.TradeHistory
return resp, errors.New("trade history not yet implemented")
}
// SubmitExchangeOrder submits a new order
func (g *GDAX) SubmitExchangeOrder(p pair.CurrencyPair, side string, orderType int, amount, price float64) (int64, error) {
return 0, errors.New("not yet implemented")
}
// ModifyExchangeOrder will allow of changing orderbook placement and limit to
// market conversion
func (g *GDAX) ModifyExchangeOrder(p pair.CurrencyPair, orderID, action int64) (int64, error) {
return 0, errors.New("not yet implemented")
}
// CancelExchangeOrder cancels an order by its corresponding ID number
func (g *GDAX) CancelExchangeOrder(p pair.CurrencyPair, orderID int64) (int64, error) {
return 0, errors.New("not yet implemented")
}
// CancelAllExchangeOrders cancels all orders associated with a currency pair
func (g *GDAX) CancelAllExchangeOrders(p pair.CurrencyPair) error {
return errors.New("not yet implemented")
}
// GetExchangeOrderInfo returns information on a current open order
func (g *GDAX) GetExchangeOrderInfo(orderID int64) (float64, error) {
return 0, errors.New("not yet implemented")
}
// GetExchangeDepositAddress returns a deposit address for a specified currency
func (g *GDAX) GetExchangeDepositAddress(p pair.CurrencyPair) (string, error) {
return "", errors.New("not yet implemented")
}
// WithdrawExchangeFunds returns a withdrawal ID when a withdrawal is submitted
func (g *GDAX) WithdrawExchangeFunds(address string, p pair.CurrencyPair, amount float64) (string, error) {
return "", errors.New("not yet implemented")
}