From a80acb16dea7d110663de5b2b4de317cae3b0b45 Mon Sep 17 00:00:00 2001 From: Ryan O'Hara-Reid Date: Fri, 31 May 2019 16:06:10 +1000 Subject: [PATCH] General fixes for CancelAllOrders exchange wrapper function (#313) * General fixes for CancelAllOrders * Fix error shadowing issue * Initialise order status map and rm reduntant initialisations --- exchanges/alphapoint/alphapoint_wrapper.go | 3 ++- exchanges/bithumb/bithumb_wrapper.go | 2 +- exchanges/bitmex/bitmex_wrapper.go | 4 +++- exchanges/bitstamp/bitstamp_wrapper.go | 7 +++++-- exchanges/btse/btse_wrapper.go | 4 ++-- exchanges/exmo/exmo_wrapper.go | 8 ++++++-- exchanges/gateio/gateio_wrapper.go | 8 ++++---- exchanges/gemini/gemini.go | 2 +- exchanges/hitbtc/hitbtc_wrapper.go | 8 +++++++- exchanges/huobi/huobi_wrapper.go | 4 +--- exchanges/huobihadax/huobihadax_wrapper.go | 4 +--- exchanges/kraken/kraken_wrapper.go | 10 ++++------ exchanges/lakebtc/lakebtc_wrapper.go | 7 ++----- exchanges/localbitcoins/localbitcoins_wrapper.go | 2 +- exchanges/okgroup/okgroup_wrapper.go | 10 ++++++---- exchanges/poloniex/poloniex.go | 9 ++++----- exchanges/poloniex/poloniex_wrapper.go | 7 ++----- exchanges/yobit/yobit_wrapper.go | 3 ++- exchanges/zb/zb_wrapper.go | 2 -- 19 files changed, 54 insertions(+), 50 deletions(-) diff --git a/exchanges/alphapoint/alphapoint_wrapper.go b/exchanges/alphapoint/alphapoint_wrapper.go index 204cf01c..954e9ffe 100644 --- a/exchanges/alphapoint/alphapoint_wrapper.go +++ b/exchanges/alphapoint/alphapoint_wrapper.go @@ -170,7 +170,8 @@ func (a *Alphapoint) CancelOrder(order *exchange.OrderCancellation) error { // CancelAllOrders cancels all orders for a given account func (a *Alphapoint) CancelAllOrders(orderCancellation *exchange.OrderCancellation) (exchange.CancelAllOrdersResponse, error) { - return exchange.CancelAllOrdersResponse{}, a.CancelAllExistingOrders(orderCancellation.AccountID) + return exchange.CancelAllOrdersResponse{}, + a.CancelAllExistingOrders(orderCancellation.AccountID) } // GetOrderInfo returns information on a current open order diff --git a/exchanges/bithumb/bithumb_wrapper.go b/exchanges/bithumb/bithumb_wrapper.go index 245022d4..6717b52e 100644 --- a/exchanges/bithumb/bithumb_wrapper.go +++ b/exchanges/bithumb/bithumb_wrapper.go @@ -242,8 +242,8 @@ func (b *Bithumb) CancelAllOrders(orderCancellation *exchange.OrderCancellation) cancelAllOrdersResponse := exchange.CancelAllOrdersResponse{ OrderStatus: make(map[string]string), } - var allOrders []OrderData + var allOrders []OrderData for _, currency := range b.GetEnabledCurrencies() { orders, err := b.GetOrders("", orderCancellation.Side.ToString(), diff --git a/exchanges/bitmex/bitmex_wrapper.go b/exchanges/bitmex/bitmex_wrapper.go index b7a06a75..539a0871 100644 --- a/exchanges/bitmex/bitmex_wrapper.go +++ b/exchanges/bitmex/bitmex_wrapper.go @@ -247,7 +247,9 @@ func (b *Bitmex) CancelAllOrders(_ *exchange.OrderCancellation) (exchange.Cancel } for i := range orders { - cancelAllOrdersResponse.OrderStatus[orders[i].OrderID] = orders[i].OrdRejReason + if orders[i].OrdRejReason != "" { + cancelAllOrdersResponse.OrderStatus[orders[i].OrderID] = orders[i].OrdRejReason + } } return cancelAllOrdersResponse, nil diff --git a/exchanges/bitstamp/bitstamp_wrapper.go b/exchanges/bitstamp/bitstamp_wrapper.go index 00183ac3..9526d874 100644 --- a/exchanges/bitstamp/bitstamp_wrapper.go +++ b/exchanges/bitstamp/bitstamp_wrapper.go @@ -232,8 +232,11 @@ func (b *Bitstamp) CancelOrder(order *exchange.OrderCancellation) error { // CancelAllOrders cancels all orders associated with a currency pair func (b *Bitstamp) CancelAllOrders(_ *exchange.OrderCancellation) (exchange.CancelAllOrdersResponse, error) { - isCancelAllSuccessful, err := b.CancelAllExistingOrders() - if !isCancelAllSuccessful { + success, err := b.CancelAllExistingOrders() + if err != nil { + return exchange.CancelAllOrdersResponse{}, err + } + if !success { err = errors.New("cancel all orders failed. Bitstamp provides no further information. Check order status to verify") } diff --git a/exchanges/btse/btse_wrapper.go b/exchanges/btse/btse_wrapper.go index 3cd625e0..22d1be50 100644 --- a/exchanges/btse/btse_wrapper.go +++ b/exchanges/btse/btse_wrapper.go @@ -187,13 +187,13 @@ func (b *BTSE) CancelOrder(order *exchange.OrderCancellation) error { // If product ID is sent, all orders of that specified market will be cancelled // If not specified, all orders of all markets will be cancelled func (b *BTSE) CancelAllOrders(orderCancellation *exchange.OrderCancellation) (exchange.CancelAllOrdersResponse, error) { + var resp exchange.CancelAllOrdersResponse r, err := b.CancelOrders(exchange.FormatExchangeCurrency(b.Name, orderCancellation.CurrencyPair).String()) if err != nil { - return exchange.CancelAllOrdersResponse{}, err + return resp, err } - var resp exchange.CancelAllOrdersResponse switch r.Code { case -1: return resp, errors.New("order cancellation unsuccessful") diff --git a/exchanges/exmo/exmo_wrapper.go b/exchanges/exmo/exmo_wrapper.go index 0f1d8cc0..a97e01ae 100644 --- a/exchanges/exmo/exmo_wrapper.go +++ b/exchanges/exmo/exmo_wrapper.go @@ -240,7 +240,6 @@ func (e *EXMO) ModifyOrder(action *exchange.ModifyOrder) (string, error) { // CancelOrder cancels an order by its corresponding ID number func (e *EXMO) CancelOrder(order *exchange.OrderCancellation) error { orderIDInt, err := strconv.ParseInt(order.OrderID, 10, 64) - if err != nil { return err } @@ -253,6 +252,7 @@ func (e *EXMO) CancelAllOrders(_ *exchange.OrderCancellation) (exchange.CancelAl cancelAllOrdersResponse := exchange.CancelAllOrdersResponse{ OrderStatus: make(map[string]string), } + openOrders, err := e.GetOpenOrders() if err != nil { return cancelAllOrdersResponse, err @@ -292,7 +292,10 @@ func (e *EXMO) GetDepositAddress(cryptocurrency currency.Code, _ string) (string // WithdrawCryptocurrencyFunds returns a withdrawal ID when a withdrawal is // submitted func (e *EXMO) WithdrawCryptocurrencyFunds(withdrawRequest *exchange.WithdrawRequest) (string, error) { - resp, err := e.WithdrawCryptocurrency(withdrawRequest.Currency.String(), withdrawRequest.Address, withdrawRequest.AddressTag, withdrawRequest.Amount) + resp, err := e.WithdrawCryptocurrency(withdrawRequest.Currency.String(), + withdrawRequest.Address, + withdrawRequest.AddressTag, + withdrawRequest.Amount) return fmt.Sprintf("%v", resp), err } @@ -329,6 +332,7 @@ func (e *EXMO) GetActiveOrders(getOrdersRequest *exchange.GetOrdersRequest) ([]e if err != nil { return nil, err } + var orders []exchange.OrderDetail for _, order := range resp { symbol := currency.NewPairDelimiter(order.Pair, "_") diff --git a/exchanges/gateio/gateio_wrapper.go b/exchanges/gateio/gateio_wrapper.go index b7e91baa..f05d2c4e 100644 --- a/exchanges/gateio/gateio_wrapper.go +++ b/exchanges/gateio/gateio_wrapper.go @@ -266,15 +266,15 @@ func (g *Gateio) CancelAllOrders(_ *exchange.OrderCancellation) (exchange.Cancel return cancelAllOrdersResponse, err } - uniqueSymbols := make(map[string]string) + uniqueSymbols := make(map[string]int) for i := range openOrders.Orders { - uniqueSymbols[openOrders.Orders[i].CurrencyPair] = openOrders.Orders[i].CurrencyPair + uniqueSymbols[openOrders.Orders[i].CurrencyPair]++ } for unique := range uniqueSymbols { - err = g.CancelAllExistingOrders(-1, uniqueSymbols[unique]) + err = g.CancelAllExistingOrders(-1, unique) if err != nil { - return cancelAllOrdersResponse, err + cancelAllOrdersResponse.OrderStatus[unique] = err.Error() } } diff --git a/exchanges/gemini/gemini.go b/exchanges/gemini/gemini.go index cc9a0811..e2c61ccb 100644 --- a/exchanges/gemini/gemini.go +++ b/exchanges/gemini/gemini.go @@ -342,12 +342,12 @@ func (g *Gemini) CancelExistingOrder(orderID int64) (Order, error) { // the UI. If sessions = true will only cancel the order that is called on this // session asssociated with the APIKEY func (g *Gemini) CancelExistingOrders(cancelBySession bool) (OrderResult, error) { - response := OrderResult{} path := geminiOrderCancelAll if cancelBySession { path = geminiOrderCancelSession } + var response OrderResult err := g.SendAuthenticatedHTTPRequest(http.MethodPost, path, nil, &response) if err != nil { return response, err diff --git a/exchanges/hitbtc/hitbtc_wrapper.go b/exchanges/hitbtc/hitbtc_wrapper.go index ec149444..9460ef1f 100644 --- a/exchanges/hitbtc/hitbtc_wrapper.go +++ b/exchanges/hitbtc/hitbtc_wrapper.go @@ -231,13 +231,19 @@ func (h *HitBTC) CancelAllOrders(_ *exchange.OrderCancellation) (exchange.Cancel cancelAllOrdersResponse := exchange.CancelAllOrdersResponse{ OrderStatus: make(map[string]string), } + resp, err := h.CancelAllExistingOrders() if err != nil { return cancelAllOrdersResponse, err } for i := range resp { - cancelAllOrdersResponse.OrderStatus[strconv.FormatInt(resp[i].ID, 10)] = fmt.Sprintf("Could not cancel order %v. Status: %v", resp[i].ID, resp[i].Status) + if resp[i].Status != "canceled" { + cancelAllOrdersResponse.OrderStatus[strconv.FormatInt(resp[i].ID, 10)] = + fmt.Sprintf("Could not cancel order %v. Status: %v", + resp[i].ID, + resp[i].Status) + } } return cancelAllOrdersResponse, nil diff --git a/exchanges/huobi/huobi_wrapper.go b/exchanges/huobi/huobi_wrapper.go index 79b9eca2..ce5739d2 100644 --- a/exchanges/huobi/huobi_wrapper.go +++ b/exchanges/huobi/huobi_wrapper.go @@ -335,9 +335,7 @@ func (h *HUOBI) CancelOrder(order *exchange.OrderCancellation) error { // CancelAllOrders cancels all orders associated with a currency pair func (h *HUOBI) CancelAllOrders(orderCancellation *exchange.OrderCancellation) (exchange.CancelAllOrdersResponse, error) { - cancelAllOrdersResponse := exchange.CancelAllOrdersResponse{ - OrderStatus: make(map[string]string), - } + var cancelAllOrdersResponse exchange.CancelAllOrdersResponse for _, currency := range h.GetEnabledCurrencies() { resp, err := h.CancelOpenOrdersBatch(orderCancellation.AccountID, exchange.FormatExchangeCurrency(h.Name, currency).String()) if err != nil { diff --git a/exchanges/huobihadax/huobihadax_wrapper.go b/exchanges/huobihadax/huobihadax_wrapper.go index 190ac046..59ea1c10 100644 --- a/exchanges/huobihadax/huobihadax_wrapper.go +++ b/exchanges/huobihadax/huobihadax_wrapper.go @@ -291,9 +291,7 @@ func (h *HUOBIHADAX) CancelOrder(order *exchange.OrderCancellation) error { // CancelAllOrders cancels all orders associated with a currency pair func (h *HUOBIHADAX) CancelAllOrders(orderCancellation *exchange.OrderCancellation) (exchange.CancelAllOrdersResponse, error) { - cancelAllOrdersResponse := exchange.CancelAllOrdersResponse{ - OrderStatus: make(map[string]string), - } + var cancelAllOrdersResponse exchange.CancelAllOrdersResponse for _, currency := range h.GetEnabledCurrencies() { resp, err := h.CancelOpenOrdersBatch(orderCancellation.AccountID, exchange.FormatExchangeCurrency(h.Name, currency).String()) if err != nil { diff --git a/exchanges/kraken/kraken_wrapper.go b/exchanges/kraken/kraken_wrapper.go index c3f8fd72..c359427e 100644 --- a/exchanges/kraken/kraken_wrapper.go +++ b/exchanges/kraken/kraken_wrapper.go @@ -251,12 +251,10 @@ func (k *Kraken) CancelAllOrders(_ *exchange.OrderCancellation) (exchange.Cancel return cancelAllOrdersResponse, err } - if openOrders.Count > 0 { - for orderID := range openOrders.Open { - _, err = k.CancelExistingOrder(orderID) - if err != nil { - cancelAllOrdersResponse.OrderStatus[orderID] = err.Error() - } + for orderID := range openOrders.Open { + _, err = k.CancelExistingOrder(orderID) + if err != nil { + cancelAllOrdersResponse.OrderStatus[orderID] = err.Error() } } diff --git a/exchanges/lakebtc/lakebtc_wrapper.go b/exchanges/lakebtc/lakebtc_wrapper.go index dac301be..33061c81 100644 --- a/exchanges/lakebtc/lakebtc_wrapper.go +++ b/exchanges/lakebtc/lakebtc_wrapper.go @@ -202,9 +202,7 @@ func (l *LakeBTC) CancelOrder(order *exchange.OrderCancellation) error { // CancelAllOrders cancels all orders associated with a currency pair func (l *LakeBTC) CancelAllOrders(_ *exchange.OrderCancellation) (exchange.CancelAllOrdersResponse, error) { - cancelAllOrdersResponse := exchange.CancelAllOrdersResponse{ - OrderStatus: make(map[string]string), - } + var cancelAllOrdersResponse exchange.CancelAllOrdersResponse openOrders, err := l.GetOpenOrders() if err != nil { return cancelAllOrdersResponse, err @@ -212,8 +210,7 @@ func (l *LakeBTC) CancelAllOrders(_ *exchange.OrderCancellation) (exchange.Cance var ordersToCancel []string for _, order := range openOrders { - orderIDString := strconv.FormatInt(order.ID, 10) - ordersToCancel = append(ordersToCancel, orderIDString) + ordersToCancel = append(ordersToCancel, strconv.FormatInt(order.ID, 10)) } return cancelAllOrdersResponse, l.CancelExistingOrders(ordersToCancel) diff --git a/exchanges/localbitcoins/localbitcoins_wrapper.go b/exchanges/localbitcoins/localbitcoins_wrapper.go index 73b0e534..8f27977a 100644 --- a/exchanges/localbitcoins/localbitcoins_wrapper.go +++ b/exchanges/localbitcoins/localbitcoins_wrapper.go @@ -253,7 +253,7 @@ func (l *LocalBitcoins) CancelAllOrders(_ *exchange.OrderCancellation) (exchange adIDString := strconv.FormatInt(ads.AdList[i].Data.AdID, 10) err = l.DeleteAd(adIDString) if err != nil { - cancelAllOrdersResponse.OrderStatus[strconv.FormatInt(ads.AdList[i].Data.AdID, 10)] = err.Error() + cancelAllOrdersResponse.OrderStatus[adIDString] = err.Error() } } diff --git a/exchanges/okgroup/okgroup_wrapper.go b/exchanges/okgroup/okgroup_wrapper.go index b74cf7fd..b0ceb467 100644 --- a/exchanges/okgroup/okgroup_wrapper.go +++ b/exchanges/okgroup/okgroup_wrapper.go @@ -264,13 +264,15 @@ func (o *OKGroup) CancelOrder(orderCancellation *exchange.OrderCancellation) (er } // CancelAllOrders cancels all orders associated with a currency pair -func (o *OKGroup) CancelAllOrders(orderCancellation *exchange.OrderCancellation) (resp exchange.CancelAllOrdersResponse, _ error) { +func (o *OKGroup) CancelAllOrders(orderCancellation *exchange.OrderCancellation) (resp exchange.CancelAllOrdersResponse, err error) { orderIDs := strings.Split(orderCancellation.OrderID, ",") + resp.OrderStatus = make(map[string]string) var orderIDNumbers []int64 for _, i := range orderIDs { - orderIDNumber, err := strconv.ParseInt(i, 10, 64) - if err != nil { - return resp, err + orderIDNumber, strConvErr := strconv.ParseInt(i, 10, 64) + if strConvErr != nil { + resp.OrderStatus[i] = strConvErr.Error() + continue } orderIDNumbers = append(orderIDNumbers, orderIDNumber) } diff --git a/exchanges/poloniex/poloniex.go b/exchanges/poloniex/poloniex.go index 3d0b8e0d..9a2cf9fa 100644 --- a/exchanges/poloniex/poloniex.go +++ b/exchanges/poloniex/poloniex.go @@ -533,22 +533,21 @@ func (p *Poloniex) PlaceOrder(currency string, rate, amount float64, immediate, } // CancelExistingOrder cancels and order by orderID -func (p *Poloniex) CancelExistingOrder(orderID int64) (bool, error) { +func (p *Poloniex) CancelExistingOrder(orderID int64) error { result := GenericResponse{} values := url.Values{} values.Set("orderNumber", strconv.FormatInt(orderID, 10)) err := p.SendAuthenticatedHTTPRequest(http.MethodPost, poloniexOrderCancel, values, &result) - if err != nil { - return false, err + return err } if result.Success != 1 { - return false, errors.New(result.Error) + return errors.New(result.Error) } - return true, nil + return nil } // MoveOrder moves an order diff --git a/exchanges/poloniex/poloniex_wrapper.go b/exchanges/poloniex/poloniex_wrapper.go index 5e93a95a..11629b61 100644 --- a/exchanges/poloniex/poloniex_wrapper.go +++ b/exchanges/poloniex/poloniex_wrapper.go @@ -230,14 +230,11 @@ func (p *Poloniex) ModifyOrder(action *exchange.ModifyOrder) (string, error) { // CancelOrder cancels an order by its corresponding ID number func (p *Poloniex) CancelOrder(order *exchange.OrderCancellation) error { orderIDInt, err := strconv.ParseInt(order.OrderID, 10, 64) - if err != nil { return err } - _, err = p.CancelExistingOrder(orderIDInt) - - return err + return p.CancelExistingOrder(orderIDInt) } // CancelAllOrders cancels all orders associated with a currency pair @@ -252,7 +249,7 @@ func (p *Poloniex) CancelAllOrders(_ *exchange.OrderCancellation) (exchange.Canc for _, openOrderPerCurrency := range openOrders.Data { for _, openOrder := range openOrderPerCurrency { - _, err = p.CancelExistingOrder(openOrder.OrderNumber) + err = p.CancelExistingOrder(openOrder.OrderNumber) if err != nil { cancelAllOrdersResponse.OrderStatus[strconv.FormatInt(openOrder.OrderNumber, 10)] = err.Error() } diff --git a/exchanges/yobit/yobit_wrapper.go b/exchanges/yobit/yobit_wrapper.go index 6a541c0c..3279a93f 100644 --- a/exchanges/yobit/yobit_wrapper.go +++ b/exchanges/yobit/yobit_wrapper.go @@ -219,7 +219,8 @@ func (y *Yobit) CancelAllOrders(_ *exchange.OrderCancellation) (exchange.CancelA for key := range activeOrders { orderIDInt, err := strconv.ParseInt(key, 10, 64) if err != nil { - return cancelAllOrdersResponse, err + cancelAllOrdersResponse.OrderStatus[key] = err.Error() + continue } _, err = y.CancelExistingOrder(orderIDInt) diff --git a/exchanges/zb/zb_wrapper.go b/exchanges/zb/zb_wrapper.go index cbf1bb9d..0d36862f 100644 --- a/exchanges/zb/zb_wrapper.go +++ b/exchanges/zb/zb_wrapper.go @@ -239,7 +239,6 @@ func (z *ZB) CancelAllOrders(_ *exchange.OrderCancellation) (exchange.CancelAllO } var allOpenOrders []Order for _, currency := range z.GetEnabledCurrencies() { - var pageNumber int64 // Limiting to 10 pages for i := 0; i < 10; i++ { openOrders, err := z.GetUnfinishedOrdersIgnoreTradeType(exchange.FormatExchangeCurrency(z.Name, currency).String(), 1, 10) @@ -252,7 +251,6 @@ func (z *ZB) CancelAllOrders(_ *exchange.OrderCancellation) (exchange.CancelAllO } allOpenOrders = append(allOpenOrders, openOrders...) - pageNumber++ } }