Files
gocryptotrader/exchanges/itbit/itbit_wrapper.go
Scott 74d5bca03b engine: Ticker REST/Websocket improvements (#345)
* Adds extra properties to Websocket ticker. Adds new properties to binance and bitfinex, but doesn't test anything yet. Changes names and properties of ticker package to make more streamlined

* Adds support for coinbasepro, coinut, gateio, gitbtc, huobi, hadax, kraken, okex, okcoin. Adds quoteVolume

* Adds poloniex and ZB ticker datas

* Updates ANX, Binance, Bitfinex, Bistamp, Bittrex, BTCMarkets, BTSE, CoinbasePRo, Coinut, Exmo tickers. It looks like a whole bunch of stuff is wrong in how tickers are done though :/

* Updates tickers everywhere. Will revert batch ones

* Re-Preseves ticker batching

* Minor fixes to ticks and removal of comment

* Logging errors instead of returning mid loop. Adds bitfinex batch ticker processing. Fixes unrelated okgroup wallet bug

* Removes bad code I wrote preventing function from running if feature not enabled

* Fixes issue with bitmex and rebase issues

* Fixes bitmex iterator error, splits hitbtc ticker requests

* Fixes okgroup currency pair formatting. Updates okgroup to use ticker batching. Fixes okgroup ticker issues due to assetTypes. Fixes okgroup ws pinging. Fixes Kraken's currency pairs formatting. Reverts ANXs auto parsing back to strings because ANX json makes me cry. Minor property improvements for coinut, coinbasepro, btse, exmo. Protects wshandler manageSubscriptions() from running and returning an error when feature not supported

* Updates config example to reflect the underscore dash situation

* Fixes a config delimiter oopsie. Simplifies ANX wrapper ticker parsing. Fixes bittrex date parsing. Simplifies okcoin switch to if.

* Fixes super fun issue where kraken has updated their currency pair format and must add new delimiter support. Fixes super fun issue where okex has updated their currency pair format and must add new delimiter support. Fixes super fun issue where okcoin has updated their currency pair format and must add new delimiter support.

* Updates config example for kraken

* Adds lbank batch ticker support. Adds details to errors

* Updates FetchTradablePairs to use the config delimiter to prevent issues if the delimiter ever changes

* Fixes nil reference bug. Uses NAme not GetNAme

* Fixes hardcoded delimiter in Binance.  Expands bitfinex websocket ticker fields. Updates Bitstamp rate limits. Expands BTSE ticker data fields. Fixes typo in coibasepro. Expands Coinut ticker data. Renames currency to curr as it conflicts with package name. Expands GateIO websocket ticker data. Fixes ticker data implementation for huobi and huobi hadax. Reverts ticker map to string instead of assetType. Fixes real stupid bug I introduced which preveted subscriptions from running :glitch_crab: Adds Price ATH to ws ticker type. Adds quotevolume to yobit ticker. Uses delimiter in ZB rather than hardcoded field.

* Fixes bug in syncer where if the websocket is already connected, then UsingWebsocket is not set

* Updates broken tests

* Simplifies poloniex frozen check

* Updates configtest.json with new delimiters

* Renames shorthand properties of structs. Fixes delimiter referencing

* Fixes some bugs and nits around variable declaration, currency pairs and config upscaling

* Adds config upgrade path for okcoin and okex. Reverts configtest.json.

* Fixes okex futures currency formatting by no longer using global currency format. updates Run code to adapt. Removes BTSE value

* Adds ":" as a delimiter for when a delimiter only shows up SOMETIMES

* Adds support for optional delimiter
2019-09-02 18:05:09 +10:00

525 lines
16 KiB
Go

package itbit
import (
"fmt"
"net/url"
"strconv"
"strings"
"sync"
"time"
"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/config"
"github.com/thrasher-corp/gocryptotrader/currency"
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/orderbook"
"github.com/thrasher-corp/gocryptotrader/exchanges/request"
"github.com/thrasher-corp/gocryptotrader/exchanges/ticker"
"github.com/thrasher-corp/gocryptotrader/exchanges/websocket/wshandler"
log "github.com/thrasher-corp/gocryptotrader/logger"
)
// GetDefaultConfig returns a default exchange config
func (i *ItBit) GetDefaultConfig() (*config.ExchangeConfig, error) {
i.SetDefaults()
exchCfg := new(config.ExchangeConfig)
exchCfg.Name = i.Name
exchCfg.HTTPTimeout = exchange.DefaultHTTPTimeout
exchCfg.BaseCurrencies = i.BaseCurrencies
err := i.SetupDefaults(exchCfg)
if err != nil {
return nil, err
}
if i.Features.Supports.RESTCapabilities.AutoPairUpdates {
err = i.UpdateTradablePairs(true)
if err != nil {
return nil, err
}
}
return exchCfg, nil
}
// SetDefaults sets the defaults for the exchange
func (i *ItBit) SetDefaults() {
i.Name = "ITBIT"
i.Enabled = true
i.Verbose = true
i.API.CredentialsValidator.RequiresClientID = true
i.API.CredentialsValidator.RequiresSecret = true
i.CurrencyPairs = currency.PairsManager{
AssetTypes: asset.Items{
asset.Spot,
},
UseGlobalFormat: true,
RequestFormat: &currency.PairFormat{
Uppercase: true,
},
ConfigFormat: &currency.PairFormat{
Uppercase: true,
},
}
i.Features = exchange.Features{
Supports: exchange.FeaturesSupported{
REST: true,
Websocket: false,
RESTCapabilities: exchange.ProtocolFeatures{
AutoPairUpdates: false,
TickerBatching: false,
},
WithdrawPermissions: exchange.WithdrawCryptoViaWebsiteOnly |
exchange.WithdrawFiatViaWebsiteOnly,
},
Enabled: exchange.FeaturesEnabled{
AutoPairUpdates: false,
},
}
i.Requester = request.New(i.Name,
request.NewRateLimit(time.Second, itbitAuthRate),
request.NewRateLimit(time.Second, itbitUnauthRate),
common.NewHTTPClientWithTimeout(exchange.DefaultHTTPTimeout))
i.API.Endpoints.URLDefault = itbitAPIURL
i.API.Endpoints.URL = i.API.Endpoints.URLDefault
}
// Setup sets the exchange parameters from exchange config
func (i *ItBit) Setup(exch *config.ExchangeConfig) error {
if !exch.Enabled {
i.SetEnabled(false)
return nil
}
return i.SetupDefaults(exch)
}
// Start starts the ItBit go routine
func (i *ItBit) Start(wg *sync.WaitGroup) {
wg.Add(1)
go func() {
i.Run()
wg.Done()
}()
}
// Run implements the ItBit wrapper
func (i *ItBit) Run() {
if i.Verbose {
i.PrintEnabledPairs()
}
}
// FetchTradablePairs returns a list of the exchanges tradable pairs
func (i *ItBit) FetchTradablePairs(asset asset.Item) ([]string, error) {
return nil, common.ErrFunctionNotSupported
}
// UpdateTradablePairs updates the exchanges available pairs and stores
// them in the exchanges config
func (i *ItBit) UpdateTradablePairs(forceUpdate bool) error {
return common.ErrFunctionNotSupported
}
// UpdateTicker updates and returns the ticker for a currency pair
func (i *ItBit) UpdateTicker(p currency.Pair, assetType asset.Item) (ticker.Price, error) {
var tickerPrice ticker.Price
tick, err := i.GetTicker(i.FormatExchangeCurrency(p, assetType).String())
if err != nil {
return tickerPrice, err
}
tickerPrice = ticker.Price{
Last: tick.LastPrice,
High: tick.High24h,
Low: tick.Low24h,
Bid: tick.Bid,
Ask: tick.Ask,
Volume: tick.Volume24h,
Open: tick.OpenToday,
Pair: p,
LastUpdated: tick.ServertimeUTC,
}
err = ticker.ProcessTicker(i.GetName(), &tickerPrice, assetType)
if err != nil {
return tickerPrice, err
}
return ticker.GetTicker(i.Name, p, assetType)
}
// FetchTicker returns the ticker for a currency pair
func (i *ItBit) FetchTicker(p currency.Pair, assetType asset.Item) (ticker.Price, error) {
tickerNew, err := ticker.GetTicker(i.GetName(), p, assetType)
if err != nil {
return i.UpdateTicker(p, assetType)
}
return tickerNew, nil
}
// FetchOrderbook returns orderbook base on the currency pair
func (i *ItBit) FetchOrderbook(p currency.Pair, assetType asset.Item) (orderbook.Base, error) {
ob, err := orderbook.Get(i.GetName(), p, assetType)
if err != nil {
return i.UpdateOrderbook(p, assetType)
}
return ob, nil
}
// UpdateOrderbook updates and returns the orderbook for a currency pair
func (i *ItBit) UpdateOrderbook(p currency.Pair, assetType asset.Item) (orderbook.Base, error) {
var orderBook orderbook.Base
orderbookNew, err := i.GetOrderbook(i.FormatExchangeCurrency(p, assetType).String())
if err != nil {
return orderBook, err
}
for x := range orderbookNew.Bids {
data := orderbookNew.Bids[x]
var price, amount float64
price, err = strconv.ParseFloat(data[0], 64)
if err != nil {
return orderBook, err
}
amount, err = strconv.ParseFloat(data[1], 64)
if err != nil {
return orderBook, err
}
orderBook.Bids = append(orderBook.Bids, orderbook.Item{Amount: amount, Price: price})
}
for x := range orderbookNew.Asks {
data := orderbookNew.Asks[x]
var price, amount float64
price, err = strconv.ParseFloat(data[0], 64)
if err != nil {
return orderBook, err
}
amount, err = strconv.ParseFloat(data[1], 64)
if err != nil {
return orderBook, err
}
orderBook.Asks = append(orderBook.Asks, orderbook.Item{Amount: amount, Price: price})
}
orderBook.Pair = p
orderBook.ExchangeName = i.GetName()
orderBook.AssetType = assetType
err = orderBook.Process()
if err != nil {
return orderBook, err
}
return orderbook.Get(i.Name, p, assetType)
}
// GetAccountInfo retrieves balances for all enabled currencies
func (i *ItBit) GetAccountInfo() (exchange.AccountInfo, error) {
var info exchange.AccountInfo
info.Exchange = i.GetName()
wallets, err := i.GetWallets(url.Values{})
if err != nil {
return info, err
}
type balance struct {
TotalValue float64
Hold float64
}
var amounts = make(map[string]*balance)
for _, wallet := range wallets {
for _, cb := range wallet.Balances {
if _, ok := amounts[cb.Currency]; !ok {
amounts[cb.Currency] = &balance{}
}
amounts[cb.Currency].TotalValue += cb.TotalBalance
amounts[cb.Currency].Hold += cb.TotalBalance - cb.AvailableBalance
}
}
var fullBalance []exchange.AccountCurrencyInfo
for key, data := range amounts {
fullBalance = append(fullBalance, exchange.AccountCurrencyInfo{
CurrencyName: currency.NewCode(key),
TotalValue: data.TotalValue,
Hold: data.Hold,
})
}
info.Accounts = append(info.Accounts, exchange.Account{
Currencies: fullBalance,
})
return info, nil
}
// GetFundingHistory returns funding history, deposits and
// withdrawals
func (i *ItBit) GetFundingHistory() ([]exchange.FundHistory, error) {
var fundHistory []exchange.FundHistory
return fundHistory, common.ErrFunctionNotSupported
}
// GetExchangeHistory returns historic trade data since exchange opening.
func (i *ItBit) GetExchangeHistory(p currency.Pair, assetType asset.Item) ([]exchange.TradeHistory, error) {
return nil, common.ErrNotYetImplemented
}
// SubmitOrder submits a new order
func (i *ItBit) SubmitOrder(order *exchange.OrderSubmission) (exchange.SubmitOrderResponse, error) {
var submitOrderResponse exchange.SubmitOrderResponse
if order == nil {
return submitOrderResponse, exchange.ErrOrderSubmissionIsNil
}
if err := order.Validate(); err != nil {
return submitOrderResponse, err
}
var wallet string
wallets, err := i.GetWallets(url.Values{})
if err != nil {
return submitOrderResponse, err
}
// Determine what wallet ID to use if there is any actual available currency to make the trade!
for _, i := range wallets {
for j := range i.Balances {
if i.Balances[j].Currency == order.Pair.Base.String() &&
i.Balances[j].AvailableBalance >= order.Amount {
wallet = i.ID
}
}
}
if wallet == "" {
return submitOrderResponse,
fmt.Errorf("no wallet found with currency: %s with amount >= %v",
order.Pair.Base,
order.Amount)
}
response, err := i.PlaceOrder(wallet,
order.OrderSide.ToString(),
order.OrderType.ToString(),
order.Pair.Base.String(),
order.Amount,
order.Price,
order.Pair.String(),
"")
if response.ID != "" {
submitOrderResponse.OrderID = response.ID
}
if err == nil {
submitOrderResponse.IsOrderPlaced = true
}
return submitOrderResponse, err
}
// ModifyOrder will allow of changing orderbook placement and limit to
// market conversion
func (i *ItBit) ModifyOrder(action *exchange.ModifyOrder) (string, error) {
return "", common.ErrFunctionNotSupported
}
// CancelOrder cancels an order by its corresponding ID number
func (i *ItBit) CancelOrder(order *exchange.OrderCancellation) error {
return i.CancelExistingOrder(order.WalletAddress, order.OrderID)
}
// CancelAllOrders cancels all orders associated with a currency pair
func (i *ItBit) CancelAllOrders(orderCancellation *exchange.OrderCancellation) (exchange.CancelAllOrdersResponse, error) {
cancelAllOrdersResponse := exchange.CancelAllOrdersResponse{
OrderStatus: make(map[string]string),
}
openOrders, err := i.GetOrders(orderCancellation.WalletAddress, "", "open", 0, 0)
if err != nil {
return cancelAllOrdersResponse, err
}
for j := range openOrders {
err = i.CancelExistingOrder(orderCancellation.WalletAddress, openOrders[j].ID)
if err != nil {
cancelAllOrdersResponse.OrderStatus[openOrders[j].ID] = err.Error()
}
}
return cancelAllOrdersResponse, nil
}
// GetOrderInfo returns information on a current open order
func (i *ItBit) GetOrderInfo(orderID string) (exchange.OrderDetail, error) {
var orderDetail exchange.OrderDetail
return orderDetail, common.ErrNotYetImplemented
}
// GetDepositAddress returns a deposit address for a specified currency
// NOTE: This has not been implemented due to the fact you need to generate a
// a specific wallet ID and they restrict the amount of deposit address you can
// request limiting them to 2.
func (i *ItBit) GetDepositAddress(cryptocurrency currency.Code, accountID string) (string, error) {
return "", common.ErrNotYetImplemented
}
// WithdrawCryptocurrencyFunds returns a withdrawal ID when a withdrawal is
// submitted
func (i *ItBit) WithdrawCryptocurrencyFunds(withdrawRequest *exchange.CryptoWithdrawRequest) (string, error) {
return "", common.ErrFunctionNotSupported
}
// WithdrawFiatFunds returns a withdrawal ID when a
// withdrawal is submitted
func (i *ItBit) WithdrawFiatFunds(withdrawRequest *exchange.FiatWithdrawRequest) (string, error) {
return "", common.ErrFunctionNotSupported
}
// WithdrawFiatFundsToInternationalBank returns a withdrawal ID when a
// withdrawal is submitted
func (i *ItBit) WithdrawFiatFundsToInternationalBank(withdrawRequest *exchange.FiatWithdrawRequest) (string, error) {
return "", common.ErrFunctionNotSupported
}
// GetWebsocket returns a pointer to the exchange websocket
func (i *ItBit) GetWebsocket() (*wshandler.Websocket, error) {
return nil, common.ErrFunctionNotSupported
}
// GetFeeByType returns an estimate of fee based on type of transaction
func (i *ItBit) GetFeeByType(feeBuilder *exchange.FeeBuilder) (float64, error) {
if !i.AllowAuthenticatedRequest() && // Todo check connection status
feeBuilder.FeeType == exchange.CryptocurrencyTradeFee {
feeBuilder.FeeType = exchange.OfflineTradeFee
}
return i.GetFee(feeBuilder)
}
// GetActiveOrders retrieves any orders that are active/open
func (i *ItBit) GetActiveOrders(getOrdersRequest *exchange.GetOrdersRequest) ([]exchange.OrderDetail, error) {
wallets, err := i.GetWallets(url.Values{})
if err != nil {
return nil, err
}
var allOrders []Order
for _, wallet := range wallets {
resp, err := i.GetOrders(wallet.ID, "", "open", 0, 0)
if err != nil {
return nil, err
}
allOrders = append(allOrders, resp...)
}
var orders []exchange.OrderDetail
for j := range allOrders {
symbol := currency.NewPairDelimiter(allOrders[j].Instrument,
i.GetPairFormat(asset.Spot, false).Delimiter)
side := exchange.OrderSide(strings.ToUpper(allOrders[j].Side))
orderDate, err := time.Parse(time.RFC3339, allOrders[j].CreatedTime)
if err != nil {
log.Warnf(log.ExchangeSys, "Exchange %v Func %v Order %v Could not parse date to unix with value of %v",
i.Name, "GetActiveOrders", allOrders[j].ID, allOrders[j].CreatedTime)
}
orders = append(orders, exchange.OrderDetail{
ID: allOrders[j].ID,
OrderSide: side,
Amount: allOrders[j].Amount,
ExecutedAmount: allOrders[j].AmountFilled,
RemainingAmount: (allOrders[j].Amount - allOrders[j].AmountFilled),
Exchange: i.Name,
OrderDate: orderDate,
CurrencyPair: symbol,
})
}
exchange.FilterOrdersByTickRange(&orders, getOrdersRequest.StartTicks,
getOrdersRequest.EndTicks)
exchange.FilterOrdersBySide(&orders, getOrdersRequest.OrderSide)
exchange.FilterOrdersByCurrencies(&orders, getOrdersRequest.Currencies)
return orders, nil
}
// GetOrderHistory retrieves account order information
// Can Limit response to specific order status
func (i *ItBit) GetOrderHistory(getOrdersRequest *exchange.GetOrdersRequest) ([]exchange.OrderDetail, error) {
wallets, err := i.GetWallets(url.Values{})
if err != nil {
return nil, err
}
var allOrders []Order
for _, wallet := range wallets {
resp, err := i.GetOrders(wallet.ID, "", "", 0, 0)
if err != nil {
return nil, err
}
allOrders = append(allOrders, resp...)
}
var orders []exchange.OrderDetail
for j := range allOrders {
if allOrders[j].Type == "open" {
continue
}
symbol := currency.NewPairDelimiter(allOrders[j].Instrument,
i.GetPairFormat(asset.Spot, false).Delimiter)
side := exchange.OrderSide(strings.ToUpper(allOrders[j].Side))
orderDate, err := time.Parse(time.RFC3339, allOrders[j].CreatedTime)
if err != nil {
log.Warnf(log.ExchangeSys, "Exchange %v Func %v Order %v Could not parse date to unix with value of %v",
i.Name, "GetActiveOrders", allOrders[j].ID, allOrders[j].CreatedTime)
}
orders = append(orders, exchange.OrderDetail{
ID: allOrders[j].ID,
OrderSide: side,
Amount: allOrders[j].Amount,
ExecutedAmount: allOrders[j].AmountFilled,
RemainingAmount: (allOrders[j].Amount - allOrders[j].AmountFilled),
Exchange: i.Name,
OrderDate: orderDate,
CurrencyPair: symbol,
})
}
exchange.FilterOrdersByTickRange(&orders, getOrdersRequest.StartTicks,
getOrdersRequest.EndTicks)
exchange.FilterOrdersBySide(&orders, getOrdersRequest.OrderSide)
exchange.FilterOrdersByCurrencies(&orders, getOrdersRequest.Currencies)
return orders, nil
}
// SubscribeToWebsocketChannels appends to ChannelsToSubscribe
// which lets websocket.manageSubscriptions handle subscribing
func (i *ItBit) SubscribeToWebsocketChannels(channels []wshandler.WebsocketChannelSubscription) error {
return common.ErrFunctionNotSupported
}
// UnsubscribeToWebsocketChannels removes from ChannelsToSubscribe
// which lets websocket.manageSubscriptions handle unsubscribing
func (i *ItBit) UnsubscribeToWebsocketChannels(channels []wshandler.WebsocketChannelSubscription) error {
return common.ErrFunctionNotSupported
}
// GetSubscriptions returns a copied list of subscriptions
func (i *ItBit) GetSubscriptions() ([]wshandler.WebsocketChannelSubscription, error) {
return nil, common.ErrFunctionNotSupported
}
// AuthenticateWebsocket sends an authentication message to the websocket
func (i *ItBit) AuthenticateWebsocket() error {
return common.ErrFunctionNotSupported
}