package okcoin import ( "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 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) 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) } if o.Websocket { go o.WebsocketClient() } } // UpdateTicker updates and returns the ticker for a currency pair func (o *OKCoin) UpdateTicker(p pair.CurrencyPair, assetType string) (ticker.Price, error) { currency := exchange.FormatExchangeCurrency(o.Name, p).String() var tickerPrice ticker.Price if assetType != ticker.Spot && o.APIUrl == OKCOIN_API_URL { tick, err := o.GetFuturesTicker(currency, assetType) if err != nil { return tickerPrice, err } tickerPrice.Pair = p tickerPrice.Ask = tick.Sell tickerPrice.Bid = tick.Buy tickerPrice.Low = tick.Low tickerPrice.Last = tick.Last tickerPrice.Volume = tick.Vol tickerPrice.High = tick.High ticker.ProcessTicker(o.GetName(), p, tickerPrice, assetType) } else { tick, err := o.GetTicker(currency) if err != nil { return tickerPrice, err } tickerPrice.Pair = p tickerPrice.Ask = tick.Sell tickerPrice.Bid = tick.Buy tickerPrice.Low = tick.Low tickerPrice.Last = tick.Last tickerPrice.Volume = tick.Vol tickerPrice.High = tick.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 *OKCoin) 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 *OKCoin) 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 *OKCoin) UpdateOrderbook(currency pair.CurrencyPair, assetType string) (orderbook.Base, error) { var orderBook orderbook.Base orderbookNew, err := o.GetOrderBook(exchange.FormatExchangeCurrency(o.Name, currency).String(), 200, false) if err != nil { return orderBook, err } for x := range orderbookNew.Bids { data := orderbookNew.Bids[x] orderBook.Bids = append(orderBook.Bids, orderbook.Item{Amount: data[1], Price: data[0]}) } for x := range orderbookNew.Asks { data := orderbookNew.Asks[x] orderBook.Asks = append(orderBook.Asks, orderbook.Item{Amount: data[1], Price: data[0]}) } orderbook.ProcessOrderbook(o.GetName(), currency, orderBook, assetType) return orderbook.GetOrderbook(o.Name, currency, assetType) } // GetExchangeAccountInfo retrieves balances for all enabled currencies for the // OKCoin exchange func (o *OKCoin) GetExchangeAccountInfo() (exchange.AccountInfo, error) { var response exchange.AccountInfo response.ExchangeName = o.GetName() assets, err := o.GetUserInfo() if err != nil { return response, err } response.Currencies = append(response.Currencies, exchange.AccountCurrencyInfo{ CurrencyName: "BTC", TotalValue: assets.Info.Funds.Free.BTC, Hold: assets.Info.Funds.Freezed.BTC, }) response.Currencies = append(response.Currencies, exchange.AccountCurrencyInfo{ CurrencyName: "LTC", TotalValue: assets.Info.Funds.Free.LTC, Hold: assets.Info.Funds.Freezed.LTC, }) response.Currencies = append(response.Currencies, exchange.AccountCurrencyInfo{ CurrencyName: "USD", TotalValue: assets.Info.Funds.Free.USD, Hold: assets.Info.Funds.Freezed.USD, }) response.Currencies = append(response.Currencies, exchange.AccountCurrencyInfo{ CurrencyName: "CNY", TotalValue: assets.Info.Funds.Free.CNY, Hold: assets.Info.Funds.Freezed.CNY, }) return response, nil }