mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-17 15:09:59 +00:00
General fixes for CancelAllOrders exchange wrapper function (#313)
* General fixes for CancelAllOrders * Fix error shadowing issue * Initialise order status map and rm reduntant initialisations
This commit is contained in:
committed by
Adrian Gallagher
parent
416fbbd5ae
commit
a80acb16de
@@ -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
|
||||
|
||||
@@ -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(),
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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, "_")
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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++
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user