mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-14 07:26:47 +00:00
* Establishes new websocket functionality. Begins the creation of the websocket request * Adding a wrapper over gorilla websocket connect,send,receive to handle ID messages. Doesn't work * Successfully moved exchange_websocket into its own wshandler namespace. oof * Sets up ZB to use a round trip WS request system * Adds Kraken ID support to subscriptions. Renames duplicate func name UnsubscribeToChannels to RemoveSubscribedChannels. Adds some helper methods in the WebsocketConn to reduce duplicate code. Cleans up ZB implementation * Fixes double locking which caused no websocket data to be read. Fixes requestid for kraken subscriptions * Completes Huobi and Hadax implementation. Extends ZB error handling. Adds GZip support for reading messages * Adds HitBTC support. Adds GetCurrencies, GetSymbols, GetTrades WS funcs. Adds super fun new parameter to GenerateMessageID for Unix and UnixNano * Adds GateIO id support * Adds Coinut support. Prevents nil reference error in constatus when there isnt one * Standardises all Exchange websockets to use the wshandler websocket. Removes the wsRequestMtx as wshandler handles that now. Makes the Dialer a dialer, its not externally referenced that I can see. * Fixes issue with coinut implementation. Updates bitmex currencies. Removes redundant log messages which are used to log messages * Starts testing. Renames files * Adds tests for websocket connection * Reverts request.go change * Linting everything * Fixes rebase issue * Final changes. Fixes variable names, removes log.Debug, removes lines, rearranges test types, removes order correlation websocket type * Final final commit, fixing ZB issues. * Adds traffic alerts where missed. Changes empty struct pointer addresses to nil instead. Removes empty lines * Fixed string conversion * Fixes issue with ZB not sending success codes * Fixes issue with coinut processing due to nonce handling with subscriptions * Fixes issue where ZB test failure was not caught. Removes unnecessary error handling from other ZB tests * Removes unused interface * Renames wshandler.Init() to wshandler.Run() * Updates template file * Capitalises cryptocurrencies in struct. Moves websocketResponseCheckTimeout and websocketResponseMaxLimit into config options. Moves connection configuration to main exchange Setup (where appropriate). Reverts currencylastupdated ticks. Improves reader close error checking * Fixes two inconsistent websocket delay times * Creates a default variable for websocket ResponseMaxLimit and ResponseCheckTimeout, then applies it to setdefaults and all tests * Updates exchange template to set and use default websocket response limits
426 lines
13 KiB
Go
426 lines
13 KiB
Go
package bitmex
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"math"
|
|
"strings"
|
|
"sync"
|
|
|
|
"github.com/thrasher-/gocryptotrader/common"
|
|
"github.com/thrasher-/gocryptotrader/currency"
|
|
exchange "github.com/thrasher-/gocryptotrader/exchanges"
|
|
"github.com/thrasher-/gocryptotrader/exchanges/orderbook"
|
|
"github.com/thrasher-/gocryptotrader/exchanges/ticker"
|
|
"github.com/thrasher-/gocryptotrader/exchanges/wshandler"
|
|
log "github.com/thrasher-/gocryptotrader/logger"
|
|
)
|
|
|
|
// Start starts the Bitmex go routine
|
|
func (b *Bitmex) Start(wg *sync.WaitGroup) {
|
|
wg.Add(1)
|
|
go func() {
|
|
b.Run()
|
|
wg.Done()
|
|
}()
|
|
}
|
|
|
|
// Run implements the Bitmex wrapper
|
|
func (b *Bitmex) Run() {
|
|
if b.Verbose {
|
|
log.Debugf("%s Websocket: %s. (url: %s).\n", b.GetName(), common.IsEnabled(b.Websocket.IsEnabled()), b.WebsocketURL)
|
|
log.Debugf("%s polling delay: %ds.\n", b.GetName(), b.RESTPollingDelay)
|
|
log.Debugf("%s %d currencies enabled: %s.\n", b.GetName(), len(b.EnabledPairs), b.EnabledPairs)
|
|
}
|
|
|
|
marketInfo, err := b.GetActiveInstruments(&GenericRequestParams{})
|
|
if err != nil {
|
|
log.Errorf("%s Failed to get available symbols.\n", b.GetName())
|
|
|
|
} else {
|
|
var exchangeProducts []string
|
|
for i := range marketInfo {
|
|
exchangeProducts = append(exchangeProducts, marketInfo[i].Symbol)
|
|
}
|
|
|
|
var NewExchangeProducts currency.Pairs
|
|
for _, p := range exchangeProducts {
|
|
NewExchangeProducts = append(NewExchangeProducts,
|
|
currency.NewPairFromString(p))
|
|
}
|
|
|
|
err = b.UpdateCurrencies(NewExchangeProducts, false, false)
|
|
if err != nil {
|
|
log.Errorf("%s Failed to update available currencies.\n", b.GetName())
|
|
}
|
|
}
|
|
}
|
|
|
|
// UpdateTicker updates and returns the ticker for a currency pair
|
|
func (b *Bitmex) UpdateTicker(p currency.Pair, assetType string) (ticker.Price, error) {
|
|
var tickerPrice ticker.Price
|
|
currency := exchange.FormatExchangeCurrency(b.Name, p)
|
|
|
|
tick, err := b.GetTrade(&GenericRequestParams{
|
|
Symbol: currency.String(),
|
|
Reverse: true,
|
|
Count: 1})
|
|
if err != nil {
|
|
return tickerPrice, err
|
|
}
|
|
|
|
if len(tick) == 0 {
|
|
return tickerPrice, fmt.Errorf("%s REST error: no ticker return", b.Name)
|
|
}
|
|
|
|
tickerPrice.Pair = p
|
|
tickerPrice.Last = tick[0].Price
|
|
tickerPrice.Volume = float64(tick[0].Size)
|
|
return tickerPrice, ticker.ProcessTicker(b.Name, &tickerPrice, assetType)
|
|
}
|
|
|
|
// GetTickerPrice returns the ticker for a currency pair
|
|
func (b *Bitmex) GetTickerPrice(p currency.Pair, assetType string) (ticker.Price, error) {
|
|
tickerNew, err := ticker.GetTicker(b.GetName(), p, assetType)
|
|
if err != nil {
|
|
return b.UpdateTicker(p, assetType)
|
|
}
|
|
return tickerNew, nil
|
|
}
|
|
|
|
// GetOrderbookEx returns orderbook base on the currency pair
|
|
func (b *Bitmex) GetOrderbookEx(currency currency.Pair, assetType string) (orderbook.Base, error) {
|
|
ob, err := orderbook.Get(b.GetName(), currency, assetType)
|
|
if err != nil {
|
|
return b.UpdateOrderbook(currency, assetType)
|
|
}
|
|
return ob, nil
|
|
}
|
|
|
|
// UpdateOrderbook updates and returns the orderbook for a currency pair
|
|
func (b *Bitmex) UpdateOrderbook(p currency.Pair, assetType string) (orderbook.Base, error) {
|
|
var orderBook orderbook.Base
|
|
|
|
orderbookNew, err := b.GetOrderbook(OrderBookGetL2Params{
|
|
Symbol: exchange.FormatExchangeCurrency(b.Name, p).String(),
|
|
Depth: 500})
|
|
if err != nil {
|
|
return orderBook, err
|
|
}
|
|
|
|
for _, ob := range orderbookNew {
|
|
if strings.EqualFold(ob.Side, exchange.SellOrderSide.ToString()) {
|
|
orderBook.Asks = append(orderBook.Asks,
|
|
orderbook.Item{Amount: float64(ob.Size), Price: ob.Price})
|
|
continue
|
|
}
|
|
if strings.EqualFold(ob.Side, exchange.BuyOrderSide.ToString()) {
|
|
orderBook.Bids = append(orderBook.Bids,
|
|
orderbook.Item{Amount: float64(ob.Size), Price: ob.Price})
|
|
continue
|
|
}
|
|
}
|
|
|
|
orderBook.Pair = p
|
|
orderBook.ExchangeName = b.GetName()
|
|
orderBook.AssetType = assetType
|
|
|
|
err = orderBook.Process()
|
|
if err != nil {
|
|
return orderBook, err
|
|
}
|
|
|
|
return orderbook.Get(b.Name, p, assetType)
|
|
}
|
|
|
|
// GetAccountInfo retrieves balances for all enabled currencies for the
|
|
// Bitmex exchange
|
|
func (b *Bitmex) GetAccountInfo() (exchange.AccountInfo, error) {
|
|
var info exchange.AccountInfo
|
|
|
|
bal, err := b.GetAllUserMargin()
|
|
if err != nil {
|
|
return info, err
|
|
}
|
|
|
|
// Need to update to add Margin/Liquidity availibilty
|
|
var balances []exchange.AccountCurrencyInfo
|
|
for i := range bal {
|
|
balances = append(balances, exchange.AccountCurrencyInfo{
|
|
CurrencyName: currency.NewCode(bal[i].Currency),
|
|
TotalValue: float64(bal[i].WalletBalance),
|
|
})
|
|
}
|
|
|
|
info.Exchange = b.GetName()
|
|
info.Accounts = append(info.Accounts, exchange.Account{
|
|
Currencies: balances,
|
|
})
|
|
|
|
return info, nil
|
|
}
|
|
|
|
// GetFundingHistory returns funding history, deposits and
|
|
// withdrawals
|
|
func (b *Bitmex) GetFundingHistory() ([]exchange.FundHistory, error) {
|
|
return nil, common.ErrNotYetImplemented
|
|
}
|
|
|
|
// GetExchangeHistory returns historic trade data since exchange opening.
|
|
func (b *Bitmex) GetExchangeHistory(p currency.Pair, assetType string) ([]exchange.TradeHistory, error) {
|
|
var resp []exchange.TradeHistory
|
|
|
|
return resp, common.ErrNotYetImplemented
|
|
}
|
|
|
|
// SubmitOrder submits a new order
|
|
func (b *Bitmex) SubmitOrder(p currency.Pair, side exchange.OrderSide, orderType exchange.OrderType, amount, price float64, _ string) (exchange.SubmitOrderResponse, error) {
|
|
var submitOrderResponse exchange.SubmitOrderResponse
|
|
|
|
if math.Mod(amount, 1) != 0 {
|
|
return submitOrderResponse,
|
|
errors.New("contract amount can not have decimals")
|
|
}
|
|
|
|
var orderNewParams = OrderNewParams{
|
|
OrdType: side.ToString(),
|
|
Symbol: p.String(),
|
|
OrderQty: amount,
|
|
Side: side.ToString(),
|
|
}
|
|
|
|
if orderType == exchange.LimitOrderType {
|
|
orderNewParams.Price = price
|
|
}
|
|
|
|
response, err := b.CreateOrder(&orderNewParams)
|
|
if response.OrderID != "" {
|
|
submitOrderResponse.OrderID = response.OrderID
|
|
}
|
|
|
|
if err == nil {
|
|
submitOrderResponse.IsOrderPlaced = true
|
|
}
|
|
|
|
return submitOrderResponse, err
|
|
}
|
|
|
|
// ModifyOrder will allow of changing orderbook placement and limit to
|
|
// market conversion
|
|
func (b *Bitmex) ModifyOrder(action *exchange.ModifyOrder) (string, error) {
|
|
var params OrderAmendParams
|
|
|
|
if math.Mod(action.Amount, 1) != 0 {
|
|
return "", errors.New("contract amount can not have decimals")
|
|
}
|
|
|
|
params.OrderID = action.OrderID
|
|
params.OrderQty = int32(action.Amount)
|
|
params.Price = action.Price
|
|
|
|
order, err := b.AmendOrder(¶ms)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return order.OrderID, nil
|
|
}
|
|
|
|
// CancelOrder cancels an order by its corresponding ID number
|
|
func (b *Bitmex) CancelOrder(order *exchange.OrderCancellation) error {
|
|
var params = OrderCancelParams{
|
|
OrderID: order.OrderID,
|
|
}
|
|
_, err := b.CancelOrders(¶ms)
|
|
|
|
return err
|
|
}
|
|
|
|
// CancelAllOrders cancels all orders associated with a currency pair
|
|
func (b *Bitmex) CancelAllOrders(_ *exchange.OrderCancellation) (exchange.CancelAllOrdersResponse, error) {
|
|
cancelAllOrdersResponse := exchange.CancelAllOrdersResponse{
|
|
OrderStatus: make(map[string]string),
|
|
}
|
|
var emptyParams OrderCancelAllParams
|
|
orders, err := b.CancelAllExistingOrders(emptyParams)
|
|
if err != nil {
|
|
return cancelAllOrdersResponse, err
|
|
}
|
|
|
|
for i := range orders {
|
|
if orders[i].OrdRejReason != "" {
|
|
cancelAllOrdersResponse.OrderStatus[orders[i].OrderID] = orders[i].OrdRejReason
|
|
}
|
|
}
|
|
|
|
return cancelAllOrdersResponse, nil
|
|
}
|
|
|
|
// GetOrderInfo returns information on a current open order
|
|
func (b *Bitmex) GetOrderInfo(orderID string) (exchange.OrderDetail, error) {
|
|
var orderDetail exchange.OrderDetail
|
|
return orderDetail, common.ErrNotYetImplemented
|
|
}
|
|
|
|
// GetDepositAddress returns a deposit address for a specified currency
|
|
func (b *Bitmex) GetDepositAddress(cryptocurrency currency.Code, _ string) (string, error) {
|
|
return b.GetCryptoDepositAddress(cryptocurrency.String())
|
|
}
|
|
|
|
// WithdrawCryptocurrencyFunds returns a withdrawal ID when a withdrawal is
|
|
// submitted
|
|
func (b *Bitmex) WithdrawCryptocurrencyFunds(withdrawRequest *exchange.WithdrawRequest) (string, error) {
|
|
var request = UserRequestWithdrawalParams{
|
|
Address: withdrawRequest.Address,
|
|
Amount: withdrawRequest.Amount,
|
|
Currency: withdrawRequest.Currency.String(),
|
|
OtpToken: withdrawRequest.OneTimePassword,
|
|
}
|
|
if withdrawRequest.FeeAmount > 0 {
|
|
request.Fee = withdrawRequest.FeeAmount
|
|
}
|
|
|
|
resp, err := b.UserRequestWithdrawal(request)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return resp.TransactID, nil
|
|
}
|
|
|
|
// WithdrawFiatFunds returns a withdrawal ID when a withdrawal is
|
|
// submitted
|
|
func (b *Bitmex) WithdrawFiatFunds(withdrawRequest *exchange.WithdrawRequest) (string, error) {
|
|
return "", common.ErrFunctionNotSupported
|
|
}
|
|
|
|
// WithdrawFiatFundsToInternationalBank returns a withdrawal ID when a withdrawal is
|
|
// submitted
|
|
func (b *Bitmex) WithdrawFiatFundsToInternationalBank(withdrawRequest *exchange.WithdrawRequest) (string, error) {
|
|
return "", common.ErrFunctionNotSupported
|
|
}
|
|
|
|
// GetWebsocket returns a pointer to the exchange websocket
|
|
func (b *Bitmex) GetWebsocket() (*wshandler.Websocket, error) {
|
|
return b.Websocket, nil
|
|
}
|
|
|
|
// GetFeeByType returns an estimate of fee based on type of transaction
|
|
func (b *Bitmex) GetFeeByType(feeBuilder *exchange.FeeBuilder) (float64, error) {
|
|
if (b.APIKey == "" || b.APISecret == "") && // Todo check connection status
|
|
feeBuilder.FeeType == exchange.CryptocurrencyTradeFee {
|
|
feeBuilder.FeeType = exchange.OfflineTradeFee
|
|
}
|
|
return b.GetFee(feeBuilder)
|
|
}
|
|
|
|
// GetActiveOrders retrieves any orders that are active/open
|
|
// This function is not concurrency safe due to orderSide/orderType maps
|
|
func (b *Bitmex) GetActiveOrders(getOrdersRequest *exchange.GetOrdersRequest) ([]exchange.OrderDetail, error) {
|
|
var orders []exchange.OrderDetail
|
|
params := OrdersRequest{}
|
|
params.Filter = "{\"open\":true}"
|
|
|
|
resp, err := b.GetOrders(¶ms)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for i := range resp {
|
|
orderSide := orderSideMap[resp[i].Side]
|
|
orderType := orderTypeMap[resp[i].OrdType]
|
|
if orderType == "" {
|
|
orderType = exchange.UnknownOrderType
|
|
}
|
|
|
|
orderDetail := exchange.OrderDetail{
|
|
Price: resp[i].Price,
|
|
Amount: float64(resp[i].OrderQty),
|
|
Exchange: b.Name,
|
|
ID: resp[i].OrderID,
|
|
OrderSide: orderSide,
|
|
OrderType: orderType,
|
|
Status: resp[i].OrdStatus,
|
|
CurrencyPair: currency.NewPairWithDelimiter(resp[i].Symbol,
|
|
resp[i].SettlCurrency,
|
|
b.ConfigCurrencyPairFormat.Delimiter),
|
|
}
|
|
|
|
orders = append(orders, orderDetail)
|
|
}
|
|
|
|
exchange.FilterOrdersBySide(&orders, getOrdersRequest.OrderSide)
|
|
exchange.FilterOrdersByType(&orders, getOrdersRequest.OrderType)
|
|
exchange.FilterOrdersByTickRange(&orders, getOrdersRequest.StartTicks,
|
|
getOrdersRequest.EndTicks)
|
|
exchange.FilterOrdersByCurrencies(&orders, getOrdersRequest.Currencies)
|
|
|
|
return orders, nil
|
|
}
|
|
|
|
// GetOrderHistory retrieves account order information
|
|
// Can Limit response to specific order status
|
|
// This function is not concurrency safe due to orderSide/orderType maps
|
|
func (b *Bitmex) GetOrderHistory(getOrdersRequest *exchange.GetOrdersRequest) ([]exchange.OrderDetail, error) {
|
|
var orders []exchange.OrderDetail
|
|
params := OrdersRequest{}
|
|
resp, err := b.GetOrders(¶ms)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for i := range resp {
|
|
orderSide := orderSideMap[resp[i].Side]
|
|
orderType := orderTypeMap[resp[i].OrdType]
|
|
if orderType == "" {
|
|
orderType = exchange.UnknownOrderType
|
|
}
|
|
|
|
orderDetail := exchange.OrderDetail{
|
|
Price: resp[i].Price,
|
|
Amount: float64(resp[i].OrderQty),
|
|
Exchange: b.Name,
|
|
ID: resp[i].OrderID,
|
|
OrderSide: orderSide,
|
|
OrderType: orderType,
|
|
Status: resp[i].OrdStatus,
|
|
CurrencyPair: currency.NewPairWithDelimiter(resp[i].Symbol,
|
|
resp[i].SettlCurrency,
|
|
b.ConfigCurrencyPairFormat.Delimiter),
|
|
}
|
|
|
|
orders = append(orders, orderDetail)
|
|
}
|
|
|
|
exchange.FilterOrdersBySide(&orders, getOrdersRequest.OrderSide)
|
|
exchange.FilterOrdersByType(&orders, getOrdersRequest.OrderType)
|
|
exchange.FilterOrdersByTickRange(&orders, getOrdersRequest.StartTicks, getOrdersRequest.EndTicks)
|
|
exchange.FilterOrdersByCurrencies(&orders, getOrdersRequest.Currencies)
|
|
|
|
return orders, nil
|
|
}
|
|
|
|
// SubscribeToWebsocketChannels appends to ChannelsToSubscribe
|
|
// which lets websocket.manageSubscriptions handle subscribing
|
|
func (b *Bitmex) SubscribeToWebsocketChannels(channels []wshandler.WebsocketChannelSubscription) error {
|
|
b.Websocket.SubscribeToChannels(channels)
|
|
return nil
|
|
}
|
|
|
|
// UnsubscribeToWebsocketChannels removes from ChannelsToSubscribe
|
|
// which lets websocket.manageSubscriptions handle unsubscribing
|
|
func (b *Bitmex) UnsubscribeToWebsocketChannels(channels []wshandler.WebsocketChannelSubscription) error {
|
|
b.Websocket.RemoveSubscribedChannels(channels)
|
|
return nil
|
|
}
|
|
|
|
// GetSubscriptions returns a copied list of subscriptions
|
|
func (b *Bitmex) GetSubscriptions() ([]wshandler.WebsocketChannelSubscription, error) {
|
|
return b.Websocket.GetSubscriptions(), nil
|
|
}
|
|
|
|
// AuthenticateWebsocket sends an authentication message to the websocket
|
|
func (b *Bitmex) AuthenticateWebsocket() error {
|
|
return b.websocketSendAuth()
|
|
}
|