From ad7ae88ff4d8b2b199eda2db115511720dd393fc Mon Sep 17 00:00:00 2001 From: Adrian Gallagher Date: Wed, 30 Aug 2017 16:12:18 +1000 Subject: [PATCH] Add UpdateOrderbook exchange function and update all exchanges to support it --- exchanges/alphapoint/alphapoint_wrapper.go | 19 ++++--- exchanges/anx/anx_wrapper.go | 23 +++++++-- exchanges/bitfinex/bitfinex_wrapper.go | 15 ++++-- exchanges/bitstamp/bitstamp_wrapper.go | 13 +++-- exchanges/bittrex/bittrex_wrapper.go | 15 ++++-- exchanges/btcc/btcc_wrapper.go | 19 +++++-- exchanges/btce/btce_wrapper.go | 24 ++++++--- exchanges/btcmarkets/btcmarkets_wrapper.go | 13 +++-- exchanges/coinut/coinut_wrapper.go | 15 +++++- exchanges/exchange.go | 1 + exchanges/gdax/gdax_wrapper.go | 15 +++++- exchanges/gemini/gemini_wrapper.go | 20 ++++++-- exchanges/huobi/huobi_wrapper.go | 20 ++++++-- exchanges/itbit/itbit_wrapper.go | 17 +++++-- exchanges/kraken/kraken.go | 50 +++++++++++++++++-- exchanges/kraken/kraken_types.go | 12 +++++ exchanges/kraken/kraken_wrapper.go | 42 +++++++++++++--- exchanges/lakebtc/lakebtc_wrapper.go | 14 +++++- exchanges/liqui/liqui_wrapper.go | 21 ++++++-- .../localbitcoins/localbitcoins_wrapper.go | 43 ++++++++++++++-- exchanges/okcoin/okcoin_wrapper.go | 20 ++++++-- exchanges/poloniex/poloniex_wrapper.go | 25 +++++++--- main.go | 1 + routines.go | 36 ++++++++++++- 24 files changed, 404 insertions(+), 89 deletions(-) diff --git a/exchanges/alphapoint/alphapoint_wrapper.go b/exchanges/alphapoint/alphapoint_wrapper.go index dd8465df..8ec18e8d 100644 --- a/exchanges/alphapoint/alphapoint_wrapper.go +++ b/exchanges/alphapoint/alphapoint_wrapper.go @@ -28,6 +28,7 @@ func (a *Alphapoint) GetExchangeAccountInfo() (exchange.AccountInfo, error) { return response, nil } +// UpdateTicker updates and returns the ticker for a currency pair func (a *Alphapoint) UpdateTicker(p pair.CurrencyPair) (ticker.TickerPrice, error) { var tickerPrice ticker.TickerPrice tick, err := a.GetTicker(p.Pair().String()) @@ -46,6 +47,7 @@ func (a *Alphapoint) UpdateTicker(p pair.CurrencyPair) (ticker.TickerPrice, erro return tickerPrice, nil } +// GetTickerPrice returns the ticker for a currency pair func (a *Alphapoint) GetTickerPrice(p pair.CurrencyPair) (ticker.TickerPrice, error) { tick, err := ticker.GetTicker(a.GetName(), p) if err != nil { @@ -54,12 +56,8 @@ func (a *Alphapoint) GetTickerPrice(p pair.CurrencyPair) (ticker.TickerPrice, er return tick, nil } -func (a *Alphapoint) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, error) { - ob, err := orderbook.GetOrderbook(a.GetName(), p) - if err == nil { - return ob, nil - } - +// UpdateOrderbook updates and returns the orderbook for a currency pair +func (a *Alphapoint) UpdateOrderbook(p pair.CurrencyPair) (orderbook.OrderbookBase, error) { var orderBook orderbook.OrderbookBase orderbookNew, err := a.GetOrderbook(p.Pair().String()) if err != nil { @@ -80,3 +78,12 @@ func (a *Alphapoint) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBas orderbook.ProcessOrderbook(a.GetName(), p, orderBook) return orderBook, nil } + +// GetOrderbookEx returns the orderbook for a currency pair +func (a *Alphapoint) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, error) { + ob, err := orderbook.GetOrderbook(a.GetName(), p) + if err == nil { + return a.UpdateOrderbook(p) + } + return ob, nil +} diff --git a/exchanges/anx/anx_wrapper.go b/exchanges/anx/anx_wrapper.go index 87d88824..e6288c5d 100644 --- a/exchanges/anx/anx_wrapper.go +++ b/exchanges/anx/anx_wrapper.go @@ -12,10 +12,12 @@ import ( "github.com/thrasher-/gocryptotrader/exchanges/ticker" ) +// Start starts the ANX go routine func (a *ANX) Start() { go a.Run() } +// Run implements the ANX wrapper func (a *ANX) Run() { if a.Verbose { log.Printf("%s polling delay: %ds.\n", a.GetName(), a.RESTPollingDelay) @@ -40,6 +42,7 @@ func (a *ANX) Run() { } } +// UpdateTicker updates and returns the ticker for a currency pair func (a *ANX) UpdateTicker(p pair.CurrencyPair) (ticker.TickerPrice, error) { var tickerPrice ticker.TickerPrice tick, err := a.GetTicker(p.Pair().String()) @@ -106,6 +109,7 @@ func (a *ANX) UpdateTicker(p pair.CurrencyPair) (ticker.TickerPrice, error) { return tickerPrice, nil } +// GetTickerPrice returns the ticker for a currency pair func (a *ANX) GetTickerPrice(p pair.CurrencyPair) (ticker.TickerPrice, error) { tickerNew, err := ticker.GetTicker(a.GetName(), p) if err != nil { @@ -114,13 +118,24 @@ func (a *ANX) GetTickerPrice(p pair.CurrencyPair) (ticker.TickerPrice, error) { return tickerNew, nil } -func (e *ANX) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, error) { - return orderbook.OrderbookBase{}, nil +// GetOrderbookEx returns the orderbook for a currency pair +func (a *ANX) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, error) { + ob, err := orderbook.GetOrderbook(a.GetName(), p) + if err == nil { + return a.UpdateOrderbook(p) + } + return ob, nil +} + +// UpdateOrderbook updates and returns the orderbook for a currency pair +func (a *ANX) UpdateOrderbook(p pair.CurrencyPair) (orderbook.OrderbookBase, error) { + var orderBook orderbook.OrderbookBase + return orderBook, nil } //GetExchangeAccountInfo : Retrieves balances for all enabled currencies for the ANX exchange -func (e *ANX) GetExchangeAccountInfo() (exchange.AccountInfo, error) { +func (a *ANX) GetExchangeAccountInfo() (exchange.AccountInfo, error) { var response exchange.AccountInfo - response.ExchangeName = e.GetName() + response.ExchangeName = a.GetName() return response, nil } diff --git a/exchanges/bitfinex/bitfinex_wrapper.go b/exchanges/bitfinex/bitfinex_wrapper.go index 7ee90ddb..d09f1020 100644 --- a/exchanges/bitfinex/bitfinex_wrapper.go +++ b/exchanges/bitfinex/bitfinex_wrapper.go @@ -10,12 +10,12 @@ import ( "github.com/thrasher-/gocryptotrader/exchanges/ticker" ) -// Start starts a new wrapper through a go routine +// Start starts the Bitfinex go routine func (b *Bitfinex) Start() { go b.Run() } -// Run starts a new websocketclient connection and monitors ticker information +// Run implements the Bitfinex wrapper func (b *Bitfinex) Run() { if b.Verbose { log.Printf("%s Websocket: %s.", b.GetName(), common.IsEnabled(b.Websocket)) @@ -38,7 +38,7 @@ func (b *Bitfinex) Run() { } } -// UpdateTicker updates and returns the ticker +// UpdateTicker updates and returns the ticker for a currency pair func (b *Bitfinex) UpdateTicker(p pair.CurrencyPair) (ticker.TickerPrice, error) { var tickerPrice ticker.TickerPrice tickerNew, err := b.GetTicker(p.Pair().String(), nil) @@ -57,7 +57,7 @@ func (b *Bitfinex) UpdateTicker(p pair.CurrencyPair) (ticker.TickerPrice, error) return tickerPrice, nil } -// GetTickerPrice returns the ticker +// GetTickerPrice returns the ticker for a currency pair func (b *Bitfinex) GetTickerPrice(p pair.CurrencyPair) (ticker.TickerPrice, error) { tick, err := ticker.GetTicker(b.GetName(), p) if err != nil { @@ -66,12 +66,17 @@ func (b *Bitfinex) GetTickerPrice(p pair.CurrencyPair) (ticker.TickerPrice, erro return tick, nil } +// GetOrderbookEx returns the orderbook for a currency pair func (b *Bitfinex) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, error) { ob, err := orderbook.GetOrderbook(b.GetName(), p) if err == nil { - return ob, nil + return b.UpdateOrderbook(p) } + return ob, nil +} +// UpdateOrderbook updates and returns the orderbook for a currency pair +func (b *Bitfinex) UpdateOrderbook(p pair.CurrencyPair) (orderbook.OrderbookBase, error) { var orderBook orderbook.OrderbookBase orderbookNew, err := b.GetOrderbook(p.Pair().String(), nil) if err != nil { diff --git a/exchanges/bitstamp/bitstamp_wrapper.go b/exchanges/bitstamp/bitstamp_wrapper.go index c166b4db..2e8609ed 100644 --- a/exchanges/bitstamp/bitstamp_wrapper.go +++ b/exchanges/bitstamp/bitstamp_wrapper.go @@ -10,12 +10,12 @@ import ( "github.com/thrasher-/gocryptotrader/exchanges/ticker" ) -// Start starts a new go routine run +// Start starts the Bitstamp go routine func (b *Bitstamp) Start() { go b.Run() } -// Run starts a new websocket connection runs a new go routine pusher +// Run implements the Bitstamp wrapper func (b *Bitstamp) Run() { if b.Verbose { log.Printf("%s Websocket: %s.", b.GetName(), common.IsEnabled(b.Websocket)) @@ -28,6 +28,7 @@ func (b *Bitstamp) Run() { } } +// UpdateTicker updates and returns the ticker for a currency pair func (b *Bitstamp) UpdateTicker(p pair.CurrencyPair) (ticker.TickerPrice, error) { var tickerPrice ticker.TickerPrice tick, err := b.GetTicker(p.Pair().String(), false) @@ -46,6 +47,7 @@ func (b *Bitstamp) UpdateTicker(p pair.CurrencyPair) (ticker.TickerPrice, error) return tickerPrice, nil } +// GetTickerPrice returns the ticker for a currency pair func (b *Bitstamp) GetTickerPrice(p pair.CurrencyPair) (ticker.TickerPrice, error) { tick, err := ticker.GetTicker(b.GetName(), p) if err != nil { @@ -54,12 +56,17 @@ func (b *Bitstamp) GetTickerPrice(p pair.CurrencyPair) (ticker.TickerPrice, erro return tick, nil } +// GetOrderbookEx returns the orderbook for a currency pair func (b *Bitstamp) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, error) { ob, err := orderbook.GetOrderbook(b.GetName(), p) if err == nil { - return ob, nil + return b.UpdateOrderbook(p) } + return ob, nil +} +// UpdateOrderbook updates and returns the orderbook for a currency pair +func (b *Bitstamp) UpdateOrderbook(p pair.CurrencyPair) (orderbook.OrderbookBase, error) { var orderBook orderbook.OrderbookBase orderbookNew, err := b.GetOrderbook(p.Pair().String()) if err != nil { diff --git a/exchanges/bittrex/bittrex_wrapper.go b/exchanges/bittrex/bittrex_wrapper.go index a7292076..72797a54 100644 --- a/exchanges/bittrex/bittrex_wrapper.go +++ b/exchanges/bittrex/bittrex_wrapper.go @@ -10,7 +10,7 @@ import ( "github.com/thrasher-/gocryptotrader/exchanges/ticker" ) -// Start stats the Bittrex go routine +// Start starts the Bittrex go routine func (b *Bittrex) Start() { go b.Run() } @@ -54,7 +54,8 @@ func (b *Bittrex) Run() { } } -//GetExchangeAccountInfo Retrieves balances for all enabled currencies for the Bittrexexchange +// GetExchangeAccountInfo Retrieves balances for all enabled currencies for the +// Bittrex exchange func (b *Bittrex) GetExchangeAccountInfo() (exchange.AccountInfo, error) { var response exchange.AccountInfo response.ExchangeName = b.GetName() @@ -73,6 +74,7 @@ func (b *Bittrex) GetExchangeAccountInfo() (exchange.AccountInfo, error) { return response, nil } +// UpdateTicker updates and returns the ticker for a currency pair func (b *Bittrex) UpdateTicker(p pair.CurrencyPair) (ticker.TickerPrice, error) { var tickerPrice ticker.TickerPrice tick, err := b.GetMarketSummary(exchange.FormatExchangeCurrency(b.GetName(), p).String()) @@ -88,6 +90,7 @@ func (b *Bittrex) UpdateTicker(p pair.CurrencyPair) (ticker.TickerPrice, error) return tickerPrice, nil } +// GetTickerPrice returns the ticker for a currency pair func (b *Bittrex) GetTickerPrice(p pair.CurrencyPair) (ticker.TickerPrice, error) { tick, err := ticker.GetTicker(b.GetName(), p) if err != nil { @@ -96,13 +99,17 @@ func (b *Bittrex) GetTickerPrice(p pair.CurrencyPair) (ticker.TickerPrice, error return tick, nil } -// GetOrderbookEx returns the orderbook for a currencyp pair +// GetOrderbookEx returns the orderbook for a currency pair func (b *Bittrex) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, error) { ob, err := orderbook.GetOrderbook(b.GetName(), p) if err == nil { - return ob, nil + return b.UpdateOrderbook(p) } + return ob, nil +} +// UpdateOrderbook updates and returns the orderbook for a currency pair +func (b *Bittrex) UpdateOrderbook(p pair.CurrencyPair) (orderbook.OrderbookBase, error) { var orderBook orderbook.OrderbookBase orderbookNew, err := b.GetOrderbook(exchange.FormatExchangeCurrency(b.GetName(), p).String()) if err != nil { diff --git a/exchanges/btcc/btcc_wrapper.go b/exchanges/btcc/btcc_wrapper.go index 6a621925..b009339d 100644 --- a/exchanges/btcc/btcc_wrapper.go +++ b/exchanges/btcc/btcc_wrapper.go @@ -10,10 +10,12 @@ import ( "github.com/thrasher-/gocryptotrader/exchanges/ticker" ) +// Start starts the BTCC go routine func (b *BTCC) Start() { go b.Run() } +// Run implements the BTCC wrapper func (b *BTCC) Run() { if b.Verbose { log.Printf("%s Websocket: %s.", b.GetName(), common.IsEnabled(b.Websocket)) @@ -26,6 +28,7 @@ func (b *BTCC) Run() { } } +// UpdateTicker updates and returns the ticker for a currency pair func (b *BTCC) UpdateTicker(p pair.CurrencyPair) (ticker.TickerPrice, error) { var tickerPrice ticker.TickerPrice tick, err := b.GetTicker(exchange.FormatExchangeCurrency(b.GetName(), p).String()) @@ -43,6 +46,7 @@ func (b *BTCC) UpdateTicker(p pair.CurrencyPair) (ticker.TickerPrice, error) { return tickerPrice, nil } +// GetTickerPrice returns the ticker for a currency pair func (b *BTCC) GetTickerPrice(p pair.CurrencyPair) (ticker.TickerPrice, error) { tickerNew, err := ticker.GetTicker(b.GetName(), p) if err != nil { @@ -51,12 +55,17 @@ func (b *BTCC) GetTickerPrice(p pair.CurrencyPair) (ticker.TickerPrice, error) { return tickerNew, nil } +// GetOrderbookEx returns the orderbook for a currency pair func (b *BTCC) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, error) { ob, err := orderbook.GetOrderbook(b.GetName(), p) if err == nil { - return ob, nil + return b.UpdateOrderbook(p) } + return ob, nil +} +// UpdateOrderbook updates and returns the orderbook for a currency pair +func (b *BTCC) UpdateOrderbook(p pair.CurrencyPair) (orderbook.OrderbookBase, error) { var orderBook orderbook.OrderbookBase orderbookNew, err := b.GetOrderBook(exchange.FormatExchangeCurrency(b.GetName(), p).String(), 100) if err != nil { @@ -65,12 +74,12 @@ func (b *BTCC) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, err for x := range orderbookNew.Bids { data := orderbookNew.Bids[x] - orderBook.Bids = append(ob.Bids, orderbook.OrderbookItem{Price: data[0], Amount: data[1]}) + orderBook.Bids = append(orderBook.Bids, orderbook.OrderbookItem{Price: data[0], Amount: data[1]}) } for x := range orderbookNew.Asks { data := orderbookNew.Asks[x] - orderBook.Asks = append(ob.Asks, orderbook.OrderbookItem{Price: data[0], Amount: data[1]}) + orderBook.Asks = append(orderBook.Asks, orderbook.OrderbookItem{Price: data[0], Amount: data[1]}) } orderBook.Pair = p @@ -78,8 +87,8 @@ func (b *BTCC) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, err return orderBook, nil } -//TODO: Retrieve BTCC info -//GetExchangeAccountInfo : Retrieves balances for all enabled currencies for the Kraken exchange +// GetExchangeAccountInfo : Retrieves balances for all enabled currencies for +// the Kraken exchange - TODO func (b *BTCC) GetExchangeAccountInfo() (exchange.AccountInfo, error) { var response exchange.AccountInfo response.ExchangeName = b.GetName() diff --git a/exchanges/btce/btce_wrapper.go b/exchanges/btce/btce_wrapper.go index 320e5876..b37814a6 100644 --- a/exchanges/btce/btce_wrapper.go +++ b/exchanges/btce/btce_wrapper.go @@ -13,10 +13,12 @@ import ( "github.com/thrasher-/gocryptotrader/exchanges/ticker" ) +// Start starts the BTCE go routine func (b *BTCE) Start() { go b.Run() } +// Run implements the BTCE wrapper func (b *BTCE) Run() { if b.Verbose { log.Printf("%s Websocket: %s.", b.GetName(), common.IsEnabled(b.Websocket)) @@ -50,6 +52,7 @@ func (b *BTCE) Run() { } } +// UpdateTicker updates and returns the ticker for a currency pair func (b *BTCE) UpdateTicker(p pair.CurrencyPair) (ticker.TickerPrice, error) { var tickerPrice ticker.TickerPrice result, err := b.GetTicker(p.Pair().String()) @@ -72,6 +75,7 @@ func (b *BTCE) UpdateTicker(p pair.CurrencyPair) (ticker.TickerPrice, error) { return tickerPrice, nil } +// GetTickerPrice returns the ticker for a currency pair func (b *BTCE) GetTickerPrice(p pair.CurrencyPair) (ticker.TickerPrice, error) { tick, err := ticker.GetTicker(b.GetName(), p) if err != nil { @@ -80,12 +84,17 @@ func (b *BTCE) GetTickerPrice(p pair.CurrencyPair) (ticker.TickerPrice, error) { return tick, nil } +// GetOrderbookEx returns the orderbook for a currency pair func (b *BTCE) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, error) { ob, err := orderbook.GetOrderbook(b.GetName(), p) if err == nil { - return ob, nil + return b.UpdateOrderbook(p) } + return ob, nil +} +// UpdateOrderbook updates and returns the orderbook for a currency pair +func (b *BTCE) UpdateOrderbook(p pair.CurrencyPair) (orderbook.OrderbookBase, error) { var orderBook orderbook.OrderbookBase orderbookNew, err := b.GetDepth(exchange.FormatExchangeCurrency(b.Name, p).String()) if err != nil { @@ -94,12 +103,12 @@ func (b *BTCE) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, err for x := range orderbookNew.Bids { data := orderbookNew.Bids[x] - orderBook.Bids = append(ob.Bids, orderbook.OrderbookItem{Price: data[0], Amount: data[1]}) + orderBook.Bids = append(orderBook.Bids, orderbook.OrderbookItem{Price: data[0], Amount: data[1]}) } for x := range orderbookNew.Asks { data := orderbookNew.Asks[x] - orderBook.Asks = append(ob.Asks, orderbook.OrderbookItem{Price: data[0], Amount: data[1]}) + orderBook.Asks = append(orderBook.Asks, orderbook.OrderbookItem{Price: data[0], Amount: data[1]}) } orderBook.Pair = p @@ -107,11 +116,12 @@ func (b *BTCE) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, err return orderBook, nil } -//GetExchangeAccountInfo : Retrieves balances for all enabled currencies for the BTCE exchange -func (e *BTCE) GetExchangeAccountInfo() (exchange.AccountInfo, error) { +// GetExchangeAccountInfo retrieves balances for all enabled currencies for the +// BTCE exchange +func (b *BTCE) GetExchangeAccountInfo() (exchange.AccountInfo, error) { var response exchange.AccountInfo - response.ExchangeName = e.GetName() - accountBalance, err := e.GetAccountInfo() + response.ExchangeName = b.GetName() + accountBalance, err := b.GetAccountInfo() if err != nil { return response, err } diff --git a/exchanges/btcmarkets/btcmarkets_wrapper.go b/exchanges/btcmarkets/btcmarkets_wrapper.go index 3e85669b..a06b7bcb 100644 --- a/exchanges/btcmarkets/btcmarkets_wrapper.go +++ b/exchanges/btcmarkets/btcmarkets_wrapper.go @@ -11,12 +11,12 @@ import ( "github.com/thrasher-/gocryptotrader/exchanges/ticker" ) -// Start runs ticker monitor in a new routine +// Start starts the BTC Markets go routine func (b *BTCMarkets) Start() { go b.Run() } -// Run starts a go routine to monitor ticker price +// Run implements the BTC Markets wrapper func (b *BTCMarkets) Run() { if b.Verbose { log.Printf("%s polling delay: %ds.\n", b.GetName(), b.RESTPollingDelay) @@ -50,6 +50,7 @@ func (b *BTCMarkets) Run() { } } +// UpdateTicker updates and returns the ticker for a currency pair func (b *BTCMarkets) UpdateTicker(p pair.CurrencyPair) (ticker.TickerPrice, error) { var tickerPrice ticker.TickerPrice tick, err := b.GetTicker(p.GetFirstCurrency().String()) @@ -63,6 +64,8 @@ func (b *BTCMarkets) UpdateTicker(p pair.CurrencyPair) (ticker.TickerPrice, erro ticker.ProcessTicker(b.GetName(), p, tickerPrice) return tickerPrice, nil } + +// GetTickerPrice returns the ticker for a currency pair func (b *BTCMarkets) GetTickerPrice(p pair.CurrencyPair) (ticker.TickerPrice, error) { tickerNew, err := ticker.GetTicker(b.GetName(), p) if err != nil { @@ -75,9 +78,13 @@ func (b *BTCMarkets) GetTickerPrice(p pair.CurrencyPair) (ticker.TickerPrice, er func (b *BTCMarkets) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, error) { ob, err := orderbook.GetOrderbook(b.GetName(), p) if err == nil { - return ob, nil + return b.UpdateOrderbook(p) } + return ob, nil +} +// UpdateOrderbook updates and returns the orderbook for a currency pair +func (b *BTCMarkets) UpdateOrderbook(p pair.CurrencyPair) (orderbook.OrderbookBase, error) { var orderBook orderbook.OrderbookBase orderbookNew, err := b.GetOrderbook(p.GetFirstCurrency().String()) if err != nil { diff --git a/exchanges/coinut/coinut_wrapper.go b/exchanges/coinut/coinut_wrapper.go index 32621e94..a183016e 100644 --- a/exchanges/coinut/coinut_wrapper.go +++ b/exchanges/coinut/coinut_wrapper.go @@ -10,10 +10,12 @@ import ( "github.com/thrasher-/gocryptotrader/exchanges/ticker" ) +// Start starts the COINUT go routine func (c *COINUT) Start() { go c.Run() } +// Run implements the COINUT wrapper func (c *COINUT) Run() { if c.Verbose { log.Printf("%s Websocket: %s. (url: %s).\n", c.GetName(), common.IsEnabled(c.Websocket), COINUT_WEBSOCKET_URL) @@ -44,7 +46,8 @@ func (c *COINUT) Run() { } } -// GetExchangeAccountInfo : Retrieves balances for all enabled currencies for the COINUT exchange +// GetExchangeAccountInfo retrieves balances for all enabled currencies for the +// COINUT exchange func (c *COINUT) GetExchangeAccountInfo() (exchange.AccountInfo, error) { var response exchange.AccountInfo /* @@ -65,6 +68,7 @@ func (c *COINUT) GetExchangeAccountInfo() (exchange.AccountInfo, error) { return response, nil } +// UpdateTicker updates and returns the ticker for a currency pair func (c *COINUT) UpdateTicker(p pair.CurrencyPair) (ticker.TickerPrice, error) { var tickerPrice ticker.TickerPrice tick, err := c.GetInstrumentTicker(c.InstrumentMap[p.Pair().String()]) @@ -82,6 +86,7 @@ func (c *COINUT) UpdateTicker(p pair.CurrencyPair) (ticker.TickerPrice, error) { } +// GetTickerPrice returns the ticker for a currency pair func (c *COINUT) GetTickerPrice(p pair.CurrencyPair) (ticker.TickerPrice, error) { tickerNew, err := ticker.GetTicker(c.GetName(), p) if err != nil { @@ -90,12 +95,17 @@ func (c *COINUT) GetTickerPrice(p pair.CurrencyPair) (ticker.TickerPrice, error) return tickerNew, nil } +// GetOrderbookEx returns orderbook base on the currency pair func (c *COINUT) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, error) { ob, err := orderbook.GetOrderbook(c.GetName(), p) if err == nil { - return ob, nil + return c.UpdateOrderbook(p) } + return ob, nil +} +// UpdateOrderbook updates and returns the orderbook for a currency pair +func (c *COINUT) UpdateOrderbook(p pair.CurrencyPair) (orderbook.OrderbookBase, error) { var orderBook orderbook.OrderbookBase orderbookNew, err := c.GetInstrumentOrderbook(c.InstrumentMap[p.Pair().String()], 200) if err != nil { @@ -109,6 +119,7 @@ func (c *COINUT) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, e for x := range orderbookNew.Sell { orderBook.Asks = append(orderBook.Asks, orderbook.OrderbookItem{Amount: orderbookNew.Sell[x].Quantity, Price: orderbookNew.Sell[x].Price}) } + orderBook.Pair = p orderbook.ProcessOrderbook(c.GetName(), p, orderBook) return orderBook, nil diff --git a/exchanges/exchange.go b/exchanges/exchange.go index ef7a2358..9ebb7ce7 100644 --- a/exchanges/exchange.go +++ b/exchanges/exchange.go @@ -66,6 +66,7 @@ type IBotExchange interface { GetTickerPrice(currency pair.CurrencyPair) (ticker.TickerPrice, error) UpdateTicker(currency pair.CurrencyPair) (ticker.TickerPrice, error) GetOrderbookEx(currency pair.CurrencyPair) (orderbook.OrderbookBase, error) + UpdateOrderbook(currency pair.CurrencyPair) (orderbook.OrderbookBase, error) GetEnabledCurrencies() []pair.CurrencyPair GetExchangeAccountInfo() (AccountInfo, error) GetAuthenticatedAPISupport() bool diff --git a/exchanges/gdax/gdax_wrapper.go b/exchanges/gdax/gdax_wrapper.go index 6895b669..13583d78 100644 --- a/exchanges/gdax/gdax_wrapper.go +++ b/exchanges/gdax/gdax_wrapper.go @@ -10,10 +10,12 @@ import ( "github.com/thrasher-/gocryptotrader/exchanges/ticker" ) +// Start starts the GDAX go routine func (g *GDAX) Start() { go g.Run() } +// 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), GDAX_WEBSOCKET_URL) @@ -42,7 +44,8 @@ func (g *GDAX) Run() { } } -//GetExchangeAccountInfo : Retrieves balances for all enabled currencies for the GDAX exchange +// 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() @@ -61,6 +64,7 @@ func (g *GDAX) GetExchangeAccountInfo() (exchange.AccountInfo, error) { return response, nil } +// UpdateTicker updates and returns the ticker for a currency pair func (g *GDAX) UpdateTicker(p pair.CurrencyPair) (ticker.TickerPrice, error) { var tickerPrice ticker.TickerPrice tick, err := g.GetTicker(exchange.FormatExchangeCurrency(g.Name, p).String()) @@ -83,6 +87,7 @@ func (g *GDAX) UpdateTicker(p pair.CurrencyPair) (ticker.TickerPrice, error) { return tickerPrice, nil } +// GetTickerPrice returns the ticker for a currency pair func (g *GDAX) GetTickerPrice(p pair.CurrencyPair) (ticker.TickerPrice, error) { tickerNew, err := ticker.GetTicker(g.GetName(), p) if err != nil { @@ -91,12 +96,17 @@ func (g *GDAX) GetTickerPrice(p pair.CurrencyPair) (ticker.TickerPrice, error) { return tickerNew, nil } +// GetOrderbookEx returns orderbook base on the currency pair func (g *GDAX) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, error) { ob, err := orderbook.GetOrderbook(g.GetName(), p) if err == nil { - return ob, nil + return g.UpdateOrderbook(p) } + return ob, nil +} +// UpdateOrderbook updates and returns the orderbook for a currency pair +func (g *GDAX) UpdateOrderbook(p pair.CurrencyPair) (orderbook.OrderbookBase, error) { var orderBook orderbook.OrderbookBase orderbookNew, err := g.GetOrderbook(p.Pair().String(), 2) if err != nil { @@ -112,6 +122,7 @@ func (g *GDAX) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, err for x := range obNew.Asks { orderBook.Asks = append(orderBook.Asks, orderbook.OrderbookItem{Amount: obNew.Bids[x].Amount, Price: obNew.Bids[x].Price}) } + orderBook.Pair = p orderbook.ProcessOrderbook(g.GetName(), p, orderBook) return orderBook, nil diff --git a/exchanges/gemini/gemini_wrapper.go b/exchanges/gemini/gemini_wrapper.go index 653979fe..b948ce05 100644 --- a/exchanges/gemini/gemini_wrapper.go +++ b/exchanges/gemini/gemini_wrapper.go @@ -10,10 +10,12 @@ import ( "github.com/thrasher-/gocryptotrader/exchanges/ticker" ) +// Start starts the Gemini go routine func (g *Gemini) Start() { go g.Run() } +// Run implements the Gemini wrapper func (g *Gemini) Run() { if g.Verbose { log.Printf("%s polling delay: %ds.\n", g.GetName(), g.RESTPollingDelay) @@ -31,11 +33,12 @@ func (g *Gemini) Run() { } } -//GetExchangeAccountInfo : Retrieves balances for all enabled currencies for the Gemini exchange -func (e *Gemini) GetExchangeAccountInfo() (exchange.AccountInfo, error) { +// GetExchangeAccountInfo Retrieves balances for all enabled currencies for the +// Gemini exchange +func (g *Gemini) GetExchangeAccountInfo() (exchange.AccountInfo, error) { var response exchange.AccountInfo - response.ExchangeName = e.GetName() - accountBalance, err := e.GetBalances() + response.ExchangeName = g.GetName() + accountBalance, err := g.GetBalances() if err != nil { return response, err } @@ -49,6 +52,7 @@ func (e *Gemini) GetExchangeAccountInfo() (exchange.AccountInfo, error) { return response, nil } +// UpdateTicker updates and returns the ticker for a currency pair func (g *Gemini) UpdateTicker(p pair.CurrencyPair) (ticker.TickerPrice, error) { var tickerPrice ticker.TickerPrice tick, err := g.GetTicker(p.Pair().String()) @@ -64,6 +68,7 @@ func (g *Gemini) UpdateTicker(p pair.CurrencyPair) (ticker.TickerPrice, error) { return tickerPrice, nil } +// GetTickerPrice returns the ticker for a currency pair func (g *Gemini) GetTickerPrice(p pair.CurrencyPair) (ticker.TickerPrice, error) { tickerNew, err := ticker.GetTicker(g.GetName(), p) if err != nil { @@ -72,12 +77,17 @@ func (g *Gemini) GetTickerPrice(p pair.CurrencyPair) (ticker.TickerPrice, error) return tickerNew, nil } +// GetOrderbookEx returns orderbook base on the currency pair func (g *Gemini) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, error) { ob, err := orderbook.GetOrderbook(g.GetName(), p) if err == nil { - return ob, nil + return g.UpdateOrderbook(p) } + return ob, nil +} +// UpdateOrderbook updates and returns the orderbook for a currency pair +func (g *Gemini) UpdateOrderbook(p pair.CurrencyPair) (orderbook.OrderbookBase, error) { var orderBook orderbook.OrderbookBase orderbookNew, err := g.GetOrderbook(p.Pair().String(), url.Values{}) if err != nil { diff --git a/exchanges/huobi/huobi_wrapper.go b/exchanges/huobi/huobi_wrapper.go index 09432302..07d36619 100644 --- a/exchanges/huobi/huobi_wrapper.go +++ b/exchanges/huobi/huobi_wrapper.go @@ -10,10 +10,12 @@ import ( "github.com/thrasher-/gocryptotrader/exchanges/ticker" ) +// Start starts the HUOBI go routine func (h *HUOBI) Start() { go h.Run() } +// Run implements the HUOBI wrapper func (h *HUOBI) Run() { if h.Verbose { log.Printf("%s Websocket: %s (url: %s).\n", h.GetName(), common.IsEnabled(h.Websocket), HUOBI_SOCKETIO_ADDRESS) @@ -26,6 +28,7 @@ func (h *HUOBI) Run() { } } +// UpdateTicker updates and returns the ticker for a currency pair func (h *HUOBI) UpdateTicker(p pair.CurrencyPair) (ticker.TickerPrice, error) { var tickerPrice ticker.TickerPrice tick, err := h.GetTicker(p.GetFirstCurrency().Lower().String()) @@ -43,6 +46,7 @@ func (h *HUOBI) UpdateTicker(p pair.CurrencyPair) (ticker.TickerPrice, error) { return tickerPrice, nil } +// GetTickerPrice returns the ticker for a currency pair func (h *HUOBI) GetTickerPrice(p pair.CurrencyPair) (ticker.TickerPrice, error) { tickerNew, err := ticker.GetTicker(h.GetName(), p) if err != nil { @@ -51,12 +55,17 @@ func (h *HUOBI) GetTickerPrice(p pair.CurrencyPair) (ticker.TickerPrice, error) return tickerNew, nil } +// GetOrderbookEx returns orderbook base on the currency pair func (h *HUOBI) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, error) { ob, err := orderbook.GetOrderbook(h.GetName(), p) if err == nil { - return ob, nil + return h.UpdateOrderbook(p) } + return ob, nil +} +// UpdateOrderbook updates and returns the orderbook for a currency pair +func (h *HUOBI) UpdateOrderbook(p pair.CurrencyPair) (orderbook.OrderbookBase, error) { var orderBook orderbook.OrderbookBase orderbookNew, err := h.GetOrderBook(p.GetFirstCurrency().Lower().String()) if err != nil { @@ -72,15 +81,16 @@ func (h *HUOBI) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, er data := orderbookNew.Asks[x] orderBook.Asks = append(orderBook.Asks, orderbook.OrderbookItem{Amount: data[1], Price: data[0]}) } + orderBook.Pair = p orderbook.ProcessOrderbook(h.GetName(), p, orderBook) return orderBook, nil } -//TODO: retrieve HUOBI balance info -//GetExchangeAccountInfo : Retrieves balances for all enabled currencies for the HUOBI exchange -func (e *HUOBI) GetExchangeAccountInfo() (exchange.AccountInfo, error) { +//GetExchangeAccountInfo retrieves balances for all enabled currencies for the +// HUOBI exchange - to-do +func (h *HUOBI) GetExchangeAccountInfo() (exchange.AccountInfo, error) { var response exchange.AccountInfo - response.ExchangeName = e.GetName() + response.ExchangeName = h.GetName() return response, nil } diff --git a/exchanges/itbit/itbit_wrapper.go b/exchanges/itbit/itbit_wrapper.go index 702a75e2..445abc61 100644 --- a/exchanges/itbit/itbit_wrapper.go +++ b/exchanges/itbit/itbit_wrapper.go @@ -10,9 +10,12 @@ import ( "github.com/thrasher-/gocryptotrader/exchanges/ticker" ) +// Start starts the ItBit go routine func (i *ItBit) Start() { go i.Run() } + +// Run implements the ItBit wrapper func (i *ItBit) Run() { if i.Verbose { log.Printf("%s polling delay: %ds.\n", i.GetName(), i.RESTPollingDelay) @@ -20,6 +23,7 @@ func (i *ItBit) Run() { } } +// UpdateTicker updates and returns the ticker for a currency pair func (i *ItBit) UpdateTicker(p pair.CurrencyPair) (ticker.TickerPrice, error) { var tickerPrice ticker.TickerPrice tick, err := i.GetTicker(p.Pair().String()) @@ -38,6 +42,7 @@ func (i *ItBit) UpdateTicker(p pair.CurrencyPair) (ticker.TickerPrice, error) { return tickerPrice, nil } +// GetTickerPrice returns the ticker for a currency pair func (i *ItBit) GetTickerPrice(p pair.CurrencyPair) (ticker.TickerPrice, error) { tickerNew, err := ticker.GetTicker(i.GetName(), p) if err != nil { @@ -46,12 +51,17 @@ func (i *ItBit) GetTickerPrice(p pair.CurrencyPair) (ticker.TickerPrice, error) return tickerNew, nil } +// GetOrderbookEx returns orderbook base on the currency pair func (i *ItBit) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, error) { ob, err := orderbook.GetOrderbook(i.GetName(), p) if err == nil { - return ob, nil + return i.UpdateOrderbook(p) } + return ob, nil +} +// UpdateOrderbook updates and returns the orderbook for a currency pair +func (i *ItBit) UpdateOrderbook(p pair.CurrencyPair) (orderbook.OrderbookBase, error) { var orderBook orderbook.OrderbookBase orderbookNew, err := i.GetOrderbook(p.Pair().String()) if err != nil { @@ -83,13 +93,14 @@ func (i *ItBit) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, er } orderBook.Asks = append(orderBook.Asks, orderbook.OrderbookItem{Amount: amount, Price: price}) } + orderBook.Pair = p orderbook.ProcessOrderbook(i.GetName(), p, orderBook) return orderBook, nil } -//TODO Get current holdings from ItBit -//GetExchangeAccountInfo : Retrieves balances for all enabled currencies for the ItBit exchange +// GetExchangeAccountInfo retrieves balances for all enabled currencies for the +//ItBit exchange - to-do func (i *ItBit) GetExchangeAccountInfo() (exchange.AccountInfo, error) { var response exchange.AccountInfo response.ExchangeName = i.GetName() diff --git a/exchanges/kraken/kraken.go b/exchanges/kraken/kraken.go index ee99b2b4..cb90144e 100644 --- a/exchanges/kraken/kraken.go +++ b/exchanges/kraken/kraken.go @@ -187,20 +187,62 @@ func (k *Kraken) GetOHLC(symbol string) error { return nil } -func (k *Kraken) GetDepth(symbol string) error { +// GetDepth returns the orderbook for a particular currency +func (k *Kraken) GetDepth(symbol string) (Orderbook, error) { values := url.Values{} values.Set("pair", symbol) var result interface{} + var ob Orderbook path := fmt.Sprintf("%s/%s/public/%s?%s", KRAKEN_API_URL, KRAKEN_API_VERSION, KRAKEN_DEPTH, values.Encode()) err := common.SendHTTPGetRequest(path, true, &result) if err != nil { - return err + return ob, err } - log.Println(result) - return nil + data := result.(map[string]interface{}) + orderbookData := data["result"].(map[string]interface{}) + + var bidsData []interface{} + var asksData []interface{} + for _, y := range orderbookData { + yData := y.(map[string]interface{}) + bidsData = yData["bids"].([]interface{}) + asksData = yData["asks"].([]interface{}) + } + + processOrderbook := func(data []interface{}) ([]OrderbookBase, error) { + var result []OrderbookBase + for x := range data { + entry := data[x].([]interface{}) + + price, err := strconv.ParseFloat(entry[0].(string), 64) + if err != nil { + return nil, err + } + + amount, err := strconv.ParseFloat(entry[1].(string), 64) + if err != nil { + return nil, err + } + + result = append(result, OrderbookBase{Price: price, Amount: amount}) + } + return result, nil + } + + ob.Bids, err = processOrderbook(bidsData) + if err != nil { + return ob, err + } + + ob.Asks, err = processOrderbook(asksData) + if err != nil { + return ob, err + } + + return ob, nil } func (k *Kraken) GetTrades(symbol string) error { diff --git a/exchanges/kraken/kraken_types.go b/exchanges/kraken/kraken_types.go index f1ec460d..007009fc 100644 --- a/exchanges/kraken/kraken_types.go +++ b/exchanges/kraken/kraken_types.go @@ -31,6 +31,18 @@ type KrakenTicker struct { Open float64 } +// OrderbookBase stores the orderbook price and amount data +type OrderbookBase struct { + Price float64 + Amount float64 +} + +// Orderbook stores the bids and asks orderbook data +type Orderbook struct { + Bids []OrderbookBase + Asks []OrderbookBase +} + type KrakenTickerResponse struct { Ask []string `json:"a"` Bid []string `json:"b"` diff --git a/exchanges/kraken/kraken_wrapper.go b/exchanges/kraken/kraken_wrapper.go index 3b27b576..c05e69c8 100644 --- a/exchanges/kraken/kraken_wrapper.go +++ b/exchanges/kraken/kraken_wrapper.go @@ -9,10 +9,12 @@ import ( "github.com/thrasher-/gocryptotrader/exchanges/ticker" ) +// Start starts the Kraken go routine func (k *Kraken) Start() { go k.Run() } +// Run implements the Kraken wrapper func (k *Kraken) Run() { if k.Verbose { log.Printf("%s polling delay: %ds.\n", k.GetName(), k.RESTPollingDelay) @@ -34,9 +36,9 @@ func (k *Kraken) Run() { } } +// UpdateTicker updates and returns the ticker for a currency pair func (k *Kraken) UpdateTicker(p pair.CurrencyPair) (ticker.TickerPrice, error) { var tickerPrice ticker.TickerPrice - pairs := k.GetEnabledCurrencies() pairsCollated, err := exchange.GetAndFormatExchangeCurrencies(k.Name, pairs) if err != nil { @@ -66,7 +68,7 @@ func (k *Kraken) UpdateTicker(p pair.CurrencyPair) (ticker.TickerPrice, error) { return ticker.GetTicker(k.GetName(), p) } -//This will return the TickerPrice struct when tickers are completed here.. +// GetTickerPrice returns the ticker for a currency pair func (k *Kraken) GetTickerPrice(p pair.CurrencyPair) (ticker.TickerPrice, error) { tickerNew, err := ticker.GetTicker(k.GetName(), p) if err != nil { @@ -75,14 +77,40 @@ func (k *Kraken) GetTickerPrice(p pair.CurrencyPair) (ticker.TickerPrice, error) return tickerNew, nil } +// GetOrderbookEx returns orderbook base on the currency pair func (k *Kraken) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, error) { - return orderbook.OrderbookBase{}, nil + ob, err := orderbook.GetOrderbook(k.GetName(), p) + if err == nil { + return k.UpdateOrderbook(p) + } + return ob, nil } -//TODO: Retrieve Kraken info -//GetExchangeAccountInfo : Retrieves balances for all enabled currencies for the Kraken exchange -func (e *Kraken) GetExchangeAccountInfo() (exchange.AccountInfo, error) { +// UpdateOrderbook updates and returns the orderbook for a currency pair +func (k *Kraken) UpdateOrderbook(p pair.CurrencyPair) (orderbook.OrderbookBase, error) { + var orderBook orderbook.OrderbookBase + 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.OrderbookItem{Amount: orderbookNew.Bids[x].Amount, Price: orderbookNew.Bids[x].Price}) + } + + for x := range orderbookNew.Asks { + orderBook.Asks = append(orderBook.Asks, orderbook.OrderbookItem{Amount: orderbookNew.Asks[x].Amount, Price: orderbookNew.Asks[x].Price}) + } + + orderBook.Pair = p + orderbook.ProcessOrderbook(k.GetName(), p, orderBook) + return orderBook, nil +} + +// GetExchangeAccountInfo retrieves balances for all enabled currencies for the +// Kraken exchange - to-do +func (k *Kraken) GetExchangeAccountInfo() (exchange.AccountInfo, error) { var response exchange.AccountInfo - response.ExchangeName = e.GetName() + response.ExchangeName = k.GetName() return response, nil } diff --git a/exchanges/lakebtc/lakebtc_wrapper.go b/exchanges/lakebtc/lakebtc_wrapper.go index 12bb72c9..e9b463ab 100644 --- a/exchanges/lakebtc/lakebtc_wrapper.go +++ b/exchanges/lakebtc/lakebtc_wrapper.go @@ -11,9 +11,12 @@ import ( "github.com/thrasher-/gocryptotrader/exchanges/ticker" ) +// Start starts the LakeBTC go routine func (l *LakeBTC) Start() { go l.Run() } + +// Run implements the LakeBTC wrapper func (l *LakeBTC) Run() { if l.Verbose { log.Printf("%s polling delay: %ds.\n", l.GetName(), l.RESTPollingDelay) @@ -21,6 +24,7 @@ func (l *LakeBTC) Run() { } } +// UpdateTicker updates and returns the ticker for a currency pair func (l *LakeBTC) UpdateTicker(p pair.CurrencyPair) (ticker.TickerPrice, error) { tick, err := l.GetTicker() if err != nil { @@ -44,6 +48,7 @@ func (l *LakeBTC) UpdateTicker(p pair.CurrencyPair) (ticker.TickerPrice, error) return tickerPrice, nil } +// GetTickerPrice returns the ticker for a currency pair func (l *LakeBTC) GetTickerPrice(p pair.CurrencyPair) (ticker.TickerPrice, error) { tickerNew, err := ticker.GetTicker(l.GetName(), p) if err != nil { @@ -52,12 +57,17 @@ func (l *LakeBTC) GetTickerPrice(p pair.CurrencyPair) (ticker.TickerPrice, error return tickerNew, nil } +// GetOrderbookEx returns orderbook base on the currency pair func (l *LakeBTC) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, error) { ob, err := orderbook.GetOrderbook(l.GetName(), p) if err == nil { - return ob, nil + return l.UpdateOrderbook(p) } + return ob, nil +} +// UpdateOrderbook updates and returns the orderbook for a currency pair +func (l *LakeBTC) UpdateOrderbook(p pair.CurrencyPair) (orderbook.OrderbookBase, error) { var orderBook orderbook.OrderbookBase orderbookNew, err := l.GetOrderBook(p.Pair().String()) if err != nil { @@ -77,6 +87,8 @@ func (l *LakeBTC) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, return orderBook, nil } +// GetExchangeAccountInfo retrieves balances for all enabled currencies for the +// LakeBTC exchange func (l *LakeBTC) GetExchangeAccountInfo() (exchange.AccountInfo, error) { var response exchange.AccountInfo response.ExchangeName = l.GetName() diff --git a/exchanges/liqui/liqui_wrapper.go b/exchanges/liqui/liqui_wrapper.go index 74cd9d43..ae3d5b79 100644 --- a/exchanges/liqui/liqui_wrapper.go +++ b/exchanges/liqui/liqui_wrapper.go @@ -10,10 +10,12 @@ import ( "github.com/thrasher-/gocryptotrader/exchanges/ticker" ) +// Start starts the Liqui go routine func (l *Liqui) Start() { go l.Run() } +// Run implements the Liqui wrapper func (l *Liqui) Run() { if l.Verbose { log.Printf("%s polling delay: %ds.\n", l.GetName(), l.RESTPollingDelay) @@ -33,6 +35,7 @@ func (l *Liqui) Run() { } } +// UpdateTicker updates and returns the ticker for a currency pair func (l *Liqui) UpdateTicker(p pair.CurrencyPair) (ticker.TickerPrice, error) { var tickerPrice ticker.TickerPrice pairsString, err := exchange.GetAndFormatExchangeCurrencies(l.Name, @@ -62,6 +65,7 @@ func (l *Liqui) UpdateTicker(p pair.CurrencyPair) (ticker.TickerPrice, error) { return ticker.GetTicker(l.GetName(), p) } +// GetTickerPrice returns the ticker for a currency pair func (l *Liqui) GetTickerPrice(p pair.CurrencyPair) (ticker.TickerPrice, error) { tickerNew, err := ticker.GetTicker(l.GetName(), p) if err != nil { @@ -70,12 +74,17 @@ func (l *Liqui) GetTickerPrice(p pair.CurrencyPair) (ticker.TickerPrice, error) return tickerNew, nil } +// GetOrderbookEx returns orderbook base on the currency pair func (l *Liqui) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, error) { ob, err := orderbook.GetOrderbook(l.GetName(), p) if err == nil { - return ob, nil + return l.UpdateOrderbook(p) } + return ob, nil +} +// UpdateOrderbook updates and returns the orderbook for a currency pair +func (l *Liqui) UpdateOrderbook(p pair.CurrencyPair) (orderbook.OrderbookBase, error) { var orderBook orderbook.OrderbookBase orderbookNew, err := l.GetDepth(exchange.FormatExchangeCurrency(l.Name, p).String()) if err != nil { @@ -91,16 +100,18 @@ func (l *Liqui) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, er data := orderbookNew.Asks[x] orderBook.Asks = append(orderBook.Asks, orderbook.OrderbookItem{Amount: data[1], Price: data[0]}) } + orderBook.Pair = p orderbook.ProcessOrderbook(l.GetName(), p, orderBook) return orderBook, nil } -//GetExchangeAccountInfo : Retrieves balances for all enabled currencies for the Liqui exchange -func (e *Liqui) GetExchangeAccountInfo() (exchange.AccountInfo, error) { +// GetExchangeAccountInfo retrieves balances for all enabled currencies for the +// Liqui exchange +func (l *Liqui) GetExchangeAccountInfo() (exchange.AccountInfo, error) { var response exchange.AccountInfo - response.ExchangeName = e.GetName() - accountBalance, err := e.GetAccountInfo() + response.ExchangeName = l.GetName() + accountBalance, err := l.GetAccountInfo() if err != nil { return response, err } diff --git a/exchanges/localbitcoins/localbitcoins_wrapper.go b/exchanges/localbitcoins/localbitcoins_wrapper.go index 3712389d..5a980492 100644 --- a/exchanges/localbitcoins/localbitcoins_wrapper.go +++ b/exchanges/localbitcoins/localbitcoins_wrapper.go @@ -11,10 +11,12 @@ import ( "github.com/thrasher-/gocryptotrader/exchanges/ticker" ) +// Start starts the LocalBitcoins go routine func (l *LocalBitcoins) Start() { go l.Run() } +// Run implements the LocalBitcoins wrapper func (l *LocalBitcoins) Run() { if l.Verbose { log.Printf("%s polling delay: %ds.\n", l.GetName(), l.RESTPollingDelay) @@ -22,6 +24,7 @@ func (l *LocalBitcoins) Run() { } } +// UpdateTicker updates and returns the ticker for a currency pair func (l *LocalBitcoins) UpdateTicker(p pair.CurrencyPair) (ticker.TickerPrice, error) { var tickerPrice ticker.TickerPrice tick, err := l.GetTicker() @@ -41,6 +44,7 @@ func (l *LocalBitcoins) UpdateTicker(p pair.CurrencyPair) (ticker.TickerPrice, e return ticker.GetTicker(l.GetName(), p) } +// GetTickerPrice returns the ticker for a currency pair func (l *LocalBitcoins) GetTickerPrice(p pair.CurrencyPair) (ticker.TickerPrice, error) { tickerNew, err := ticker.GetTicker(l.GetName(), p) if err == nil { @@ -49,15 +53,44 @@ func (l *LocalBitcoins) GetTickerPrice(p pair.CurrencyPair) (ticker.TickerPrice, return tickerNew, nil } +// GetOrderbookEx returns orderbook base on the currency pair func (l *LocalBitcoins) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, error) { - return orderbook.OrderbookBase{}, nil + ob, err := orderbook.GetOrderbook(l.GetName(), p) + if err == nil { + return l.UpdateOrderbook(p) + } + return ob, nil } -//GetExchangeAccountInfo : Retrieves balances for all enabled currencies for the LocalBitcoins exchange -func (e *LocalBitcoins) GetExchangeAccountInfo() (exchange.AccountInfo, error) { +// UpdateOrderbook updates and returns the orderbook for a currency pair +func (l *LocalBitcoins) UpdateOrderbook(p pair.CurrencyPair) (orderbook.OrderbookBase, error) { + var orderBook orderbook.OrderbookBase + orderbookNew, err := l.GetOrderbook(p.GetSecondCurrency().String()) + if err != nil { + return orderBook, err + } + + for x := range orderbookNew.Bids { + data := orderbookNew.Bids[x] + orderBook.Bids = append(orderBook.Bids, orderbook.OrderbookItem{Amount: data.Amount, Price: data.Price}) + } + + for x := range orderbookNew.Asks { + data := orderbookNew.Asks[x] + orderBook.Bids = append(orderBook.Asks, orderbook.OrderbookItem{Amount: data.Amount, Price: data.Price}) + } + + orderBook.Pair = p + orderbook.ProcessOrderbook(l.GetName(), p, orderBook) + return orderBook, nil +} + +// GetExchangeAccountInfo retrieves balances for all enabled currencies for the +// LocalBitcoins exchange +func (l *LocalBitcoins) GetExchangeAccountInfo() (exchange.AccountInfo, error) { var response exchange.AccountInfo - response.ExchangeName = e.GetName() - accountBalance, err := e.GetWalletBalance() + response.ExchangeName = l.GetName() + accountBalance, err := l.GetWalletBalance() if err != nil { return response, err } diff --git a/exchanges/okcoin/okcoin_wrapper.go b/exchanges/okcoin/okcoin_wrapper.go index ae05ffde..937ed9aa 100644 --- a/exchanges/okcoin/okcoin_wrapper.go +++ b/exchanges/okcoin/okcoin_wrapper.go @@ -12,10 +12,12 @@ import ( "github.com/thrasher-/gocryptotrader/exchanges/ticker" ) +// Start starts the OKCoin go routine func (o *OKCoin) Start() { go o.Run() } +// Run implements the OKCoin wrapper func (o *OKCoin) Run() { if o.Verbose { log.Printf("%s Websocket: %s. (url: %s).\n", o.GetName(), common.IsEnabled(o.Websocket), o.WebsocketURL) @@ -50,6 +52,7 @@ func (o *OKCoin) Run() { } } +// UpdateTicker updates and returns the ticker for a currency pair func (o *OKCoin) UpdateTicker(currency pair.CurrencyPair) (ticker.TickerPrice, error) { var tickerPrice ticker.TickerPrice tick, err := o.GetTicker(exchange.FormatExchangeCurrency(o.Name, currency).String()) @@ -67,6 +70,7 @@ func (o *OKCoin) UpdateTicker(currency pair.CurrencyPair) (ticker.TickerPrice, e return tickerPrice, nil } +// GetTickerPrice returns the ticker for a currency pair func (o *OKCoin) GetTickerPrice(currency pair.CurrencyPair) (ticker.TickerPrice, error) { tickerNew, err := ticker.GetTicker(o.GetName(), currency) if err != nil { @@ -75,12 +79,17 @@ func (o *OKCoin) GetTickerPrice(currency pair.CurrencyPair) (ticker.TickerPrice, return tickerNew, nil } +// GetOrderbookEx returns orderbook base on the currency pair func (o *OKCoin) GetOrderbookEx(currency pair.CurrencyPair) (orderbook.OrderbookBase, error) { ob, err := orderbook.GetOrderbook(o.GetName(), currency) if err == nil { - return ob, nil + return o.UpdateOrderbook(currency) } + return ob, nil +} +// UpdateOrderbook updates and returns the orderbook for a currency pair +func (o *OKCoin) UpdateOrderbook(currency pair.CurrencyPair) (orderbook.OrderbookBase, error) { var orderBook orderbook.OrderbookBase orderbookNew, err := o.GetOrderBook(exchange.FormatExchangeCurrency(o.Name, currency).String(), 200, false) if err != nil { @@ -96,15 +105,18 @@ func (o *OKCoin) GetOrderbookEx(currency pair.CurrencyPair) (orderbook.Orderbook data := orderbookNew.Asks[x] orderBook.Asks = append(orderBook.Asks, orderbook.OrderbookItem{Amount: data[1], Price: data[0]}) } + orderBook.Pair = currency orderbook.ProcessOrderbook(o.GetName(), currency, orderBook) return orderBook, nil } -func (e *OKCoin) GetExchangeAccountInfo() (exchange.AccountInfo, error) { +// GetExchangeAccountInfo retrieves balances for all enabled currencies for the +// OKCoin exchange +func (o *OKCoin) GetExchangeAccountInfo() (exchange.AccountInfo, error) { var response exchange.AccountInfo - response.ExchangeName = e.GetName() - assets, err := e.GetUserInfo() + response.ExchangeName = o.GetName() + assets, err := o.GetUserInfo() if err != nil { return response, err } diff --git a/exchanges/poloniex/poloniex_wrapper.go b/exchanges/poloniex/poloniex_wrapper.go index 1e308de0..e779f4b8 100644 --- a/exchanges/poloniex/poloniex_wrapper.go +++ b/exchanges/poloniex/poloniex_wrapper.go @@ -10,10 +10,12 @@ import ( "github.com/thrasher-/gocryptotrader/exchanges/ticker" ) +// Start starts the Poloniex go routine func (p *Poloniex) Start() { go p.Run() } +// Run implements the Poloniex wrapper func (p *Poloniex) Run() { if p.Verbose { log.Printf("%s Websocket: %s (url: %s).\n", p.GetName(), common.IsEnabled(p.Websocket), POLONIEX_WEBSOCKET_ADDRESS) @@ -26,8 +28,9 @@ func (p *Poloniex) Run() { } } +// UpdateTicker updates and returns the ticker for a currency pair func (p *Poloniex) UpdateTicker(currencyPair pair.CurrencyPair) (ticker.TickerPrice, error) { - currency := currencyPair.Pair().String() + currency := exchange.FormatCurrency(currencyPair).String() var tickerPrice ticker.TickerPrice tick, err := p.GetTicker() if err != nil { @@ -45,6 +48,7 @@ func (p *Poloniex) UpdateTicker(currencyPair pair.CurrencyPair) (ticker.TickerPr return tickerPrice, nil } +// GetTickerPrice returns the ticker for a currency pair func (p *Poloniex) GetTickerPrice(currencyPair pair.CurrencyPair) (ticker.TickerPrice, error) { tickerNew, err := ticker.GetTicker(p.GetName(), currencyPair) if err != nil { @@ -53,15 +57,19 @@ func (p *Poloniex) GetTickerPrice(currencyPair pair.CurrencyPair) (ticker.Ticker return tickerNew, nil } +// GetOrderbookEx returns orderbook base on the currency pair func (p *Poloniex) GetOrderbookEx(currencyPair pair.CurrencyPair) (orderbook.OrderbookBase, error) { - currency := currencyPair.Pair().String() ob, err := orderbook.GetOrderbook(p.GetName(), currencyPair) if err == nil { - return ob, nil + return p.UpdateOrderbook(currencyPair) } + return ob, nil +} +// UpdateOrderbook updates and returns the orderbook for a currency pair +func (p *Poloniex) UpdateOrderbook(currencyPair pair.CurrencyPair) (orderbook.OrderbookBase, error) { var orderBook orderbook.OrderbookBase - orderbookNew, err := p.GetOrderbook(currency, 1000) + orderbookNew, err := p.GetOrderbook(exchange.FormatCurrency(currencyPair).String(), 1000) if err != nil { return orderBook, err } @@ -80,11 +88,12 @@ func (p *Poloniex) GetOrderbookEx(currencyPair pair.CurrencyPair) (orderbook.Ord return orderBook, nil } -//GetExchangeAccountInfo : Retrieves balances for all enabled currencies for the Poloniex exchange -func (e *Poloniex) GetExchangeAccountInfo() (exchange.AccountInfo, error) { +// GetExchangeAccountInfo retrieves balances for all enabled currencies for the +// Poloniex exchange +func (p *Poloniex) GetExchangeAccountInfo() (exchange.AccountInfo, error) { var response exchange.AccountInfo - response.ExchangeName = e.GetName() - accountBalance, err := e.GetBalances() + response.ExchangeName = p.GetName() + accountBalance, err := p.GetBalances() if err != nil { return response, err } diff --git a/main.go b/main.go index 5a20e936..08a6b9d8 100644 --- a/main.go +++ b/main.go @@ -191,6 +191,7 @@ func main() { go WebsocketHandler() go TickerUpdaterRoutine() + go OrderbookUpdaterRoutine() if bot.config.Webserver.Enabled { err := bot.config.CheckWebserverConfigValues() diff --git a/routines.go b/routines.go index 52afd11a..51cbd657 100644 --- a/routines.go +++ b/routines.go @@ -19,7 +19,7 @@ func TickerUpdaterRoutine() { currency := enabledCurrencies[y] result, err := bot.exchanges[x].UpdateTicker(currency) if err != nil { - log.Printf("failed to get %s currency", currency.Pair().String()) + log.Printf("failed to get %s ticker", currency.Pair().String()) continue } @@ -45,3 +45,37 @@ func TickerUpdaterRoutine() { time.Sleep(time.Second * 10) } } + +func OrderbookUpdaterRoutine() { + log.Println("Starting orderbook updater routine") + for { + for x := range bot.exchanges { + if bot.exchanges[x].IsEnabled() { + exchangeName := bot.exchanges[x].GetName() + enabledCurrencies := bot.exchanges[x].GetEnabledCurrencies() + + for y := range enabledCurrencies { + currency := enabledCurrencies[y] + result, err := bot.exchanges[x].UpdateOrderbook(currency) + if err != nil { + log.Printf("failed to get %s orderbook", currency.Pair().String()) + continue + } + + log.Printf("%s %s %v", + exchangeName, + exchange.FormatCurrency(currency).String(), + result) + + evt := WebsocketEvent{ + Data: result, + Event: "orderbook_update", + Exchange: exchangeName, + } + BroadcastWebsocketMessage(evt) + } + } + } + time.Sleep(time.Second * 10) + } +}