From 784e9e48f56230b97b21b0b47194aa7647fb46f0 Mon Sep 17 00:00:00 2001 From: Adrian Gallagher Date: Mon, 29 Jan 2018 20:52:00 +1100 Subject: [PATCH] Link up OKEX and fix config test --- README.md | 4 +- config/config_test.go | 4 +- exchange.go | 3 + exchanges/okex/okex.go | 2 +- exchanges/okex/okex_wrapper.go | 130 +++++++++++++++++++++++++++++++++ 5 files changed, 139 insertions(+), 4 deletions(-) create mode 100644 exchanges/okex/okex_wrapper.go diff --git a/README.md b/README.md index 34306893..ef896c28 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,9 @@ Join our slack to discuss all things related to GoCryptoTrader! [GoCryptoTrader | LakeBTC | Yes | No | NA | | Liqui | Yes | No | NA | | LocalBitcoins | Yes | NA | NA | -| OKCoin (both) | Yes | Yes | No | +| OKCoin China | Yes | Yes | No | +| OKCoin International | Yes | Yes | No | +| OKEX | Yes | No | No | | Poloniex | Yes | Yes | NA | | WEX | Yes | NA | NA | diff --git a/config/config_test.go b/config/config_test.go index 9ae21814..7637c469 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -89,7 +89,7 @@ func TestGetEnabledExchanges(t *testing.T) { } exchanges := cfg.GetEnabledExchanges() - if len(exchanges) != 19 { + if len(exchanges) != 20 { t.Error( "Test failed. TestGetEnabledExchanges. Enabled exchanges value mismatch", ) @@ -141,7 +141,7 @@ func TestGetDisabledExchanges(t *testing.T) { } func TestCountEnabledExchanges(t *testing.T) { - defaultEnabledExchanges := 19 + defaultEnabledExchanges := 20 GetConfigEnabledExchanges := GetConfig() err := GetConfigEnabledExchanges.LoadConfig(ConfigTestFile) if err != nil { diff --git a/exchange.go b/exchange.go index 50a71e27..fcb7239b 100644 --- a/exchange.go +++ b/exchange.go @@ -22,6 +22,7 @@ import ( "github.com/thrasher-/gocryptotrader/exchanges/liqui" "github.com/thrasher-/gocryptotrader/exchanges/localbitcoins" "github.com/thrasher-/gocryptotrader/exchanges/okcoin" + "github.com/thrasher-/gocryptotrader/exchanges/okex" "github.com/thrasher-/gocryptotrader/exchanges/poloniex" "github.com/thrasher-/gocryptotrader/exchanges/wex" ) @@ -158,6 +159,8 @@ func LoadExchange(name string) error { exch = new(okcoin.OKCoin) case "okcoin international": exch = new(okcoin.OKCoin) + case "okex": + exch = new(okex.OKEX) case "poloniex": exch = new(poloniex.Poloniex) case "wex": diff --git a/exchanges/okex/okex.go b/exchanges/okex/okex.go index d5269770..d509ac93 100644 --- a/exchanges/okex/okex.go +++ b/exchanges/okex/okex.go @@ -614,7 +614,7 @@ func (o *OKEX) GetSpotTicker(symbol string) (SpotPrice, error) { } //GetSpotMarketDepth returns Market Depth -func (o *OKEX) GetSpotMarketDepth(symbol, size string) (interface{}, error) { +func (o *OKEX) GetSpotMarketDepth(symbol, size string) (ActualSpotDepth, error) { resp := SpotDepth{} fullDepth := ActualSpotDepth{} diff --git a/exchanges/okex/okex_wrapper.go b/exchanges/okex/okex_wrapper.go new file mode 100644 index 00000000..23fdafdf --- /dev/null +++ b/exchanges/okex/okex_wrapper.go @@ -0,0 +1,130 @@ +package okex + +import ( + "errors" + "log" + + "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 OKEX go routine +func (o *OKEX) Start() { + go o.Run() +} + +// Run implements the OKEX wrapper +func (o *OKEX) Run() { + if o.Verbose { + log.Printf("%s Websocket: %s. (url: %s).\n", o.GetName(), common.IsEnabled(o.Websocket), o.WebsocketURL) + log.Printf("%s polling delay: %ds.\n", o.GetName(), o.RESTPollingDelay) + log.Printf("%s %d currencies enabled: %s.\n", o.GetName(), len(o.EnabledPairs), o.EnabledPairs) + } +} + +// UpdateTicker updates and returns the ticker for a currency pair +func (o *OKEX) UpdateTicker(p pair.CurrencyPair, assetType string) (ticker.Price, error) { + currency := exchange.FormatExchangeCurrency(o.Name, p).String() + var tickerPrice ticker.Price + + if assetType != ticker.Spot { + tick, err := o.GetContractPrice(currency, assetType) + if err != nil { + return tickerPrice, err + } + + tickerPrice.Pair = p + tickerPrice.Ask = tick.Ticker.Sell + tickerPrice.Bid = tick.Ticker.Buy + tickerPrice.Low = tick.Ticker.Low + tickerPrice.Last = tick.Ticker.Last + tickerPrice.Volume = tick.Ticker.Vol + tickerPrice.High = tick.Ticker.High + ticker.ProcessTicker(o.GetName(), p, tickerPrice, assetType) + } else { + tick, err := o.GetSpotTicker(currency) + if err != nil { + return tickerPrice, err + } + tickerPrice.Pair = p + tickerPrice.Ask = tick.Ticker.Sell + tickerPrice.Bid = tick.Ticker.Buy + tickerPrice.Low = tick.Ticker.Low + tickerPrice.Last = tick.Ticker.Last + tickerPrice.Volume = tick.Ticker.Vol + tickerPrice.High = tick.Ticker.High + ticker.ProcessTicker(o.GetName(), p, tickerPrice, ticker.Spot) + + } + return ticker.GetTicker(o.Name, p, assetType) +} + +// GetTickerPrice returns the ticker for a currency pair +func (o *OKEX) GetTickerPrice(p pair.CurrencyPair, assetType string) (ticker.Price, error) { + tickerNew, err := ticker.GetTicker(o.GetName(), p, assetType) + if err != nil { + return o.UpdateTicker(p, assetType) + } + return tickerNew, nil +} + +// GetOrderbookEx returns orderbook base on the currency pair +func (o *OKEX) GetOrderbookEx(currency pair.CurrencyPair, assetType string) (orderbook.Base, error) { + ob, err := orderbook.GetOrderbook(o.GetName(), currency, assetType) + if err != nil { + return o.UpdateOrderbook(currency, assetType) + } + return ob, nil +} + +// UpdateOrderbook updates and returns the orderbook for a currency pair +func (o *OKEX) UpdateOrderbook(p pair.CurrencyPair, assetType string) (orderbook.Base, error) { + var orderBook orderbook.Base + currency := exchange.FormatExchangeCurrency(o.Name, p).String() + + if assetType != ticker.Spot { + orderbookNew, err := o.GetContractMarketDepth(currency, assetType) + if err != nil { + return orderBook, err + } + + for x := range orderbookNew.Bids { + data := orderbookNew.Bids[x] + orderBook.Bids = append(orderBook.Bids, orderbook.Item{Amount: data.Volume, Price: data.Price}) + } + + for x := range orderbookNew.Asks { + data := orderbookNew.Asks[x] + orderBook.Asks = append(orderBook.Asks, orderbook.Item{Amount: data.Volume, Price: data.Price}) + } + + } else { + orderbookNew, err := o.GetSpotMarketDepth(currency, "200") + if err != nil { + return orderBook, err + } + + for x := range orderbookNew.Bids { + data := orderbookNew.Bids[x] + orderBook.Bids = append(orderBook.Bids, orderbook.Item{Amount: data.Volume, Price: data.Price}) + } + + for x := range orderbookNew.Asks { + data := orderbookNew.Asks[x] + orderBook.Asks = append(orderBook.Asks, orderbook.Item{Amount: data.Volume, Price: data.Price}) + } + } + + orderbook.ProcessOrderbook(o.GetName(), p, orderBook, assetType) + return orderbook.GetOrderbook(o.Name, p, assetType) +} + +// GetExchangeAccountInfo retrieves balances for all enabled currencies for the +// OKEX exchange +func (o *OKEX) GetExchangeAccountInfo() (exchange.AccountInfo, error) { + var response exchange.AccountInfo + return response, errors.New("not implemented") +}