ccxt / ccxt

A JavaScript / TypeScript / Python / C# / PHP cryptocurrency trading API with support for more than 100 bitcoin/altcoin exchanges
https://docs.ccxt.com
MIT License
31.86k stars 7.36k forks source link

kraken: fetch_closed_orders fails #22886

Open joergkiesewetter opened 4 days ago

joergkiesewetter commented 4 days ago

Operating System

mac

Programming Languages

Python

CCXT Version

4.3.51

Description

querying fetch_closed_orders leads to an exception, when there is a cancelled order with insufficient funds.

ccxt.base.errors.InsufficientFunds
File "./provider/kraken.py", line 85, in get_transactions
    results = self._client.fetch_closed_orders(since=since, params={'ofs': offset})
  File "./venv/lib/python3.8/site-packages/ccxt/kraken.py", line 2126, in fetch_closed_orders
    response = self.privatePostClosedOrders(self.extend(request, query))
  File "./venv/lib/python3.8/site-packages/ccxt/base/types.py", line 35, in unbound_method
    return _self.request(self.path, self.api, self.method, params, config=self.config)
  File "./venv/lib/python3.8/site-packages/ccxt/base/exchange.py", line 3740, in request
    return self.fetch2(path, api, method, params, headers, body, config)
  File "./venv/lib/python3.8/site-packages/ccxt/base/exchange.py", line 3737, in fetch2
    return self.fetch(request['url'], request['method'], request['headers'], request['body'])
  File "./venv/lib/python3.8/site-packages/ccxt/base/exchange.py", line 695, in fetch
    self.handle_errors(http_status_code, http_status_text, url, method, headers, http_response, json_response, request_headers, request_body)
  File "./venv/lib/python3.8/site-packages/ccxt/kraken.py", line 2846, in handle_errors
    raise InsufficientFunds(self.id + ' ' + body)

The included order which leads to the exception is:

"XXX":{
            "refid":null,
            "userref":0,
            "status":"canceled",
            "opentm":1718981982.8230925,
            "starttm":0,
            "expiretm":0,
            "descr":{
               "pair":"USDCUSD",
               "type":"buy",
               "ordertype":"limit",
               "price":"0.9999",
               "price2":"0",
               "leverage":"none",
               "order":"xxx",
               "close":""
            },
            "vol":"xxx",
            "vol_exec":"xxx",
            "cost":"xxx",
            "fee":"0.00000000",
            "price":"0.9998",
            "stopprice":"0.00000000",
            "limitprice":"0.00000000",
            "misc":"partial",
            "oflags":"fciq",
            "reason":"Insufficient funds",
            "closetm":1718993508.4183657
         },

The function handle_errors scans for specific strings and when the test "Insufficient funds" is in the response body, an exception is thrown. But the expected behaviour would be, that the closed orders are just returned.

Code

  results = []
    offset = 0
    since = time.time()
    since = since - timedelta(days=365).total_seconds()
    since = round(since * 1000)
    while True:

        try:
            results = self._client.fetch_closed_orders(since=since, params={'ofs': offset})
            trades += results

            print(f'fetched orders: {len(trades)}')

            if len(results) > 0:
                offset += len(results)
        except:
            traceback.print_exc()

        if len(results) < 50:
            break