ccxt icon indicating copy to clipboard operation
ccxt copied to clipboard

ccxt.base.errors.OrderNotFound: binance {"code":-2011,"msg":"Unknown order sent."} when calling binance.cancel_all_orders without any open orders

Open m-nez opened this issue 3 years ago • 1 comments

Calling binance.cancel_all_orders raises an error when there are no open orders. It should return an empty list of closed orders.

  • OS: MANJARO
  • Programming Language version: Python 3.10.4
  • CCXT version: 1.93.43
import ccxt
binance = ccxt.binance(credentials)
binance.cancel_all_orders("BTC/USDT")
Traceback (most recent call last):
  File "/REDACTED/lib/python3.10/site-packages/ccxt/base/exchange.py", line 620, in fetch
    response.raise_for_status()
  File "/REDACTED/lib/python3.10/site-packages/requests/models.py", line 1021, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://api.binance.com/api/v3/openOrders?timestamp=REDACTED&symbol=BTCUSDT&recvWindow=10000&signature=REDACTED

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/REDACTED/lib/python3.10/site-packages/ccxt/binance.py", line 3216, in cancel_all_orders
    response = getattr(self, method)(self.extend(request, query))
  File "/REDACTED/lib/python3.10/site-packages/ccxt/base/exchange.py", line 500, in inner
    return entry(_self, **inner_kwargs)
  File "/REDACTED/lib/python3.10/site-packages/ccxt/binance.py", line 5558, in request
    response = self.fetch2(path, api, method, params, headers, body, config, context)
  File "/REDACTED/lib/python3.10/site-packages/ccxt/base/exchange.py", line 2607, in fetch2
    return self.fetch(request['url'], request['method'], request['headers'], request['body'])
  File "/REDACTED/lib/python3.10/site-packages/ccxt/base/exchange.py", line 636, in fetch
    skip_further_error_handling = self.handle_errors(http_status_code, http_status_text, url, method, headers, http_response, json_response, request_headers, request_body)
  File "/REDACTED/lib/python3.10/site-packages/ccxt/binance.py", line 5536, in handle_errors
    self.throw_exactly_matched_exception(self.exceptions['exact'], error, feedback)
  File "/REDACTED/lib/python3.10/site-packages/ccxt/base/exchange.py", line 2853, in throw_exactly_matched_exception
    raise exact[string](message)
ccxt.base.errors.OrderNotFound: binance {"code":-2011,"msg":"Unknown order sent."}

m-nez avatar Sep 14 '22 20:09 m-nez

Hello @m-nez, that depends on how the exchanges handle it, some exchanges will return a successful response + empty array as suggested, but some decided to consider this as an "error response". For the latter, we just wrap the original around a unified exception that users can easily catch.

You can emulate that behavior by doing this:

Example

result = []
    try:
       # your code here
        result = exchange.cancel_all_orders(....)
    except ccxt.OrderNotFound as e:
      # handle exception here

# remaining logic here

carlosmiei avatar Sep 15 '22 11:09 carlosmiei

Maybe you need to specify the type of account, something like this: exchange.cancel_all_orders('BTCUSDT',{'type': 'margin','marginMode': 'cross'})

mrjamesbruno avatar Sep 29 '22 11:09 mrjamesbruno