ccxt icon indicating copy to clipboard operation
ccxt copied to clipboard

Kraken futures: fetch_positions

Open Hadevmin opened this issue 2 years ago • 7 comments

4.1.54

Hello,

The fix for the issue #19896 doesn't work, same error:

    |     positions = await exchange.fetch_positions([symbol])
    |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |   File "\ccxt\async_support\krakenfutures.py", line 1850, in fetch_positions
    |     result = self.parse_positions(response)
    |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |   File "\ccxt\async_support\krakenfutures.py", line 1856, in parse_positions
    |     for i in range(0, len(positions)):
    |                       ^^^^^^^^^^^^^^
    | TypeError: object of type 'NoneType' has no len()

I had 3 open positions, and several second before the error raised, I get some errors like:

ExchangeNotAvailable : krakenfutures POST https://futures.kraken.com/derivatives/api/v3/batchorder 405 Method Not Allowed <html>
<head><title>405 Not Allowed</title></head>
<body>
<center><h1>405 Not Allowed</h1></center>
<hr><center>nginx</center>
</body>
</html>

or InsufficientFunds : krakenfutures {"result":"error","error":"Unavailable","serverTime":"2023-11-21T11:30:14.583Z"}

I don't think InsufficientFunds is the good Exception object for this error, maybe ExchangeNotAvailable ?

Hadevmin avatar Nov 21 '23 13:11 Hadevmin

hello @Hadevmin,

I agree that's not the reported error, but I'm not sure how you got an InsufficientFunds exception there. Can you reproduce the issue with the verbose mode on?

carlosmiei avatar Nov 21 '23 15:11 carlosmiei

Hello @carlosmiei ,

The problem for InsufficientFunds : krakenfutures {"result": "error", "error": "Unavailable", "serverTime": "2023-11-21T11:30:14.583Z"} is just a classification enhancement. This error occurs from time to time when interacting with the API on various endpoints.

On the other hand, TypeError: object of type 'NoneType' has no len() is a bug. I have open positions so I shouldn't have positions = None I'm not talking about positions = await exchange.fetch_positions([symbol])(first line of my previous code), which is the call inside my code, but about the positions used in the for loop line 1856 of krakenfutures.py.

I can't reproduce the problem, but the common point between the times this error occurred is that I had open positions and there were problems with service unavailability and error 405 that I received in my other calls with, for example, the create_orders function. But the bug is only related to fetch_positions

Hadevmin avatar Nov 21 '23 16:11 Hadevmin

Hi @Hadevmin, If I understand right you are having two issues:

  1. You see an InsufficientFunds error when calling batchorder when it should be unavailable.
  2. You are seeing a TypeError: object of type 'NoneType' has no len() when trying to call fetchPositions

Is that correct? If so, are you able to provide a verbose output for any of the two?

pcriadoperez avatar Nov 24 '23 01:11 pcriadoperez

Hi @pcriadoperez,

I think I got the same issue with this function (fetchPositions). I am using JavaScript library with ReactJs. If I run the source code in test mode (debug) I will get this issue: TypeError: Cannot read properties of undefined (reading 'length')

But when I build and run the project in product environment, that issue will be gone. I try to debug and I could see that the value of response in your source code : const response = await this.privateGetOpenpositions(request); will return HTML source code of Kraken futures home page.

I hope this information will help.

caesar3x avatar Nov 24 '23 09:11 caesar3x

Hi @pcriadoperez,

  1. Yes, the error is processed, so I can't remember which functions raise it, but it could be fetch_olhcv too. My remark was just to better classify this error, for me it should belong more to ExchangeNotAvailable. Apart from this class change, it's perfectly legitimate to be raised if the exchange returns this error.

  2. I don't have any verbose informations to provide - the error rarely appears. But what @caesar3x says is completely consistent with the deduction I made in my first post where I was getting html code in other calls (that I handle the error unlike TypeError). With all the context I mentioned above, I'll add that before calling fetch_positions, I use asyncio.wait_for to call watch_positions with a timeout. As I did have positions and should therefore have received feedback from kraken's WS, there's an error on kraken's side, as I don't get a response from the WS and I raise a timeout error to then try via fetch_positions and I get the famous error.

Sorry, I don't have a lot of logs to share, just written scripts. If they're not understandable or if you need more information, don't hesitate to ask me a question!

Hadevmin avatar Nov 24 '23 12:11 Hadevmin

Got this error using GunBot (30.0.3). Error only starts appearing after I one time manually close positions (on the exchange). After that, error persists. Doesn't matter if I open or close positions, error persists. Only fix seems to be to open a new account on the exchange, then things are back to normal.

@Hadevmin I think I can reproduce the error (happened to me twice);

  1. Open new Kraken account, add Futures
  2. Setup GunBot, with sgsfutures strat on USD-XBT
  3. Let Gunbot open and close the positions, have it run for a while
  4. Manually close an open position on the kraken exchange, while gunbot is running
  5. From that moment on, GunBot cannot read open positions anymore, the error is below.

I have two incidents logged at Kraken helpdesk for this, numbers 14040597 and 16020749.

Error fetching balance: TypeError: Cannot set property 'position' of undefined
    at /snapshot/enricoranucci/Desktop/GitHub/gb25/ctx/js/krakenFutures.js
    at arrayEach (/snapshot/enricoranucci/Desktop/GitHub/gb25/node_modules/lodash/lodash.js:516:11)
    at Function.forEach (/snapshot/enricoranucci/Desktop/GitHub/gb25/node_modules/lodash/lodash.js:9344:14)
    at Kraken.fetchBalance (/snapshot/enricoranucci/Desktop/GitHub/gb25/ctx/js/krakenFutures.js)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
TypeError: Cannot set property 'position' of undefined
    at /snapshot/enricoranucci/Desktop/GitHub/gb25/ctx/js/krakenFutures.js
    at arrayEach (/snapshot/enricoranucci/Desktop/GitHub/gb25/node_modules/lodash/lodash.js:516:11)
    at Function.forEach (/snapshot/enricoranucci/Desktop/GitHub/gb25/node_modules/lodash/lodash.js:9344:14)
    at Kraken.fetchBalance (/snapshot/enricoranucci/Desktop/GitHub/gb25/ctx/js/krakenFutures.js)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)

I'd like to test with Postman, but I cannot get it working yet for futures api.

kkoenen avatar Mar 02 '25 13:03 kkoenen

Got this error using GunBot (30.0.3). Error only starts appearing after I one time manually close positions (on the exchange). After that, error persists. Doesn't matter if I open or close positions, error persists. Only fix seems to be to open a new account on the exchange, then things are back to normal.

@Hadevmin I think I can reproduce the error (happened to me twice);

  1. Open new Kraken account, add Futures
  2. Setup GunBot, with sgsfutures strat on USD-XBT
  3. Let Gunbot open and close the positions, have it run for a while
  4. Manually close an open position on the kraken exchange, while gunbot is running
  5. From that moment on, GunBot cannot read open positions anymore, the error is below.

I have two incidents logged at Kraken helpdesk for this, numbers 14040597 and 16020749.

Error fetching balance: TypeError: Cannot set property 'position' of undefined
    at /snapshot/enricoranucci/Desktop/GitHub/gb25/ctx/js/krakenFutures.js
    at arrayEach (/snapshot/enricoranucci/Desktop/GitHub/gb25/node_modules/lodash/lodash.js:516:11)
    at Function.forEach (/snapshot/enricoranucci/Desktop/GitHub/gb25/node_modules/lodash/lodash.js:9344:14)
    at Kraken.fetchBalance (/snapshot/enricoranucci/Desktop/GitHub/gb25/ctx/js/krakenFutures.js)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
TypeError: Cannot set property 'position' of undefined
    at /snapshot/enricoranucci/Desktop/GitHub/gb25/ctx/js/krakenFutures.js
    at arrayEach (/snapshot/enricoranucci/Desktop/GitHub/gb25/node_modules/lodash/lodash.js:516:11)
    at Function.forEach (/snapshot/enricoranucci/Desktop/GitHub/gb25/node_modules/lodash/lodash.js:9344:14)
    at Kraken.fetchBalance (/snapshot/enricoranucci/Desktop/GitHub/gb25/ctx/js/krakenFutures.js)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)

I'd like to test with Postman, but I cannot get it working yet for futures api.

I have the exact same issue, didn't realise until I read your post. NOw the bot keeps opening orders without a balance

williehk avatar May 14 '25 23:05 williehk

@williehk did you get it working again?

For me, the workaround (/ fix) was opening a fresh kraken account. However, this week the bot broke again, different error (just not buying and selling, and no trailing indicators plus some weird values in the logs). I opened an issue in the Gunbot issue repo.

Would like to hear if you also had this experience.

kkoenen avatar Jul 09 '25 17:07 kkoenen

@kkoenen I am not sure if its a ccxt issue, i could get it to temporarily work again by removing and readding the strategy in gunbot. idk but i think it might be something to do with the interfacing with kraken futures, could be nonce in kraken futures exchange REST. It just doesnt appear very robust and stable atm (kraken margin + gunbot margin strat). I tried to play around in AppScript with the same nonce generation method it works in some endpoints and not others, it's just weird at the moment.

williehk avatar Jul 09 '25 19:07 williehk