Crypto-toolbox/btfxwss

Unable to Subscribe to Candle Channel

Closed this issue · 14 comments

test_is_connected_decorator_works_as_expected (__main__.BtfxWssTests) ... ok
test_subscribing_to_data_works (__main__.BtfxWssTests) ... ERROR

======================================================================
ERROR: test_subscribing_to_data_works (__main__.BtfxWssTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\bxty9\Desktop\client_tests.py", line 27, in test_subscribing_to_data_works
    wss.tickers('BTCUSD').get(block=False)
  File "C:\ProgramData\Anaconda3\lib\site-packages\btfxwss\client.py", line 228, in tickers
    raise KeyError(pair)
KeyError: 'BTCUSD'

----------------------------------------------------------------------
Ran 2 tests in 13.095s

FAILED (errors=1)

Hey @BitbeyHub !
Thanks for contributing! From looking at it, this probably due to a pull request I merged earlier. Good catch!
Unfortunately I'm not near a computer till Sunday - until then a workaround may be to prepend a 't' to the symbol when calling the method.

I've checked the logs and found that Bitfinex doesn't like the way I send the subscription for candles:

DEBUG:btfxwss.client:_subscribe: {'key': 'trade:1m:tBTCUSD', 'event': 'subscribe', 'channel': 'candles'}
DEBUG:btfxwss.connection:_on_message(): Received new message {"channel":"candles","event":"error","msg":"key: invalid","code":10300} at 1506424899.4304948

However, according to their documentation, this is what they want:

// request
{
   event: "subscribe",
   channel: "candles",
   key: "trade:1m:tBTCUSD"
}

Which is, as far as I can tell, the same thing as mine. Will look into this further.

Sorry I am not very familiar with github and ... I found it might be my network reason after I made the script repeat the line of code again and again only if it's in "KeyError" until it's connected successfully. Or so I can say I ... fixed it ?

And I traced back to its importing modules, only to find that the functions finally need to compare elements with the retrieved Queue object. Given my network unconnected, the Queue object to be compared with must be empty. So being empty means nothing to compare. Hence the KeyError occurred.


And there is another question I wanna cast : which branch is kept latest should I download and install if I wanna conduct websocket connect in Python 3.6.2?

@BitbeyHub, the release branch is the currently most up-to-date branch.

I have opened a ticket with the Bitfinex Support Team, hopefully they'll find out what's going on here.
Although I'm quite positive it's something odd on my end, I just haven't been able to spot it.

Fixed here: #64.

If you need a quick fix, you can modify your requirements.txt to replace:
btfxwss==1.1.1
with
-e git://github.com/TapWealth/btfxwss.git#egg=btfxwss

Note, the fix above also makes the code Python2 compatible which was a requirement for my use case. If you want to avoid any side effects of those changes and just want to fix the KeyError issue, here's the change that fixes it:

https://github.com/nlsdfnbch/btfxwss/pull/64/files#diff-fa3139a968f240692f4ad7f331d4ff7dR153

Hello @nlsdfnbch,
Please check below!

In client._subscribe, the payload dictionary is passed to conn.send as separated parameters.
Unfortunately, candles channel has 'key' in payload field.
This is overlapped with original parameter name 'key' in conn.send method, which is used for api key.

A quick fix for this problem is to change parameter name in conn.send, something like below.

def send(self, key=None, secret=None, list_data=None, auth=False, **kwargs): ->
def send(self, api_key=None, api_secret=None, list_data=None, auth=False, **kwargs):

Hope this helps!

@kimms213
You're right! Fixed it that way. Thanks for the help and pointers, people!

I still have this error. I created a python file for collecting data (candles) of one PAIR. Then I created a bash script for running (with nohup) my python file many times with different pairs.

When I run my bash script with lets say 4-5 pairs - its ok. But if I run it with 10 pairs first 4-5 pairs are running ok but next pairs raises an error like [9]- Exit 1 nohup python3 $COLLECTOR $PAIR > $OUT_COLLECTOR

I think it might be because I create many websocket connection for collecting one pair. Is it true or I should look another cause of this error?

That is likely the case, yes - Bitfinex allows a maximum of 4-6 Requests per minute to the Websocket API, if I remember correctly. So You should connect once, and send a channel subscription for each pair on that one connection.

@nlsdfnbch Thank you Nils!
I didn't find any limitation of requests for websocket on official website.
Do you know if there is some limit for channel subscription? I mean how many pairs I can subscribe per one ws connection.

@Closius, I dug through my mails and found a ticket I had opened with them a few months ago, that's where I got the limit from:

The limit is 15 connections per minute. You should hardly ever need to connect more that once or twice per minute. You should connect and keep the connection open for longer periods. You should only need to connect a few time per day, if at all.

Kind regards,

Bjorn
Bitfinex.com

So It's 15 per minute, but still, that might happen pretty quickly if the client is executed multiple times and tries to reconnect.

You can subscribe to every single channel with all kinds of configurations - I haven't hit a limit for this on a single connection yet (and I run about 50 configurations)

@nlsdfnbch Thank you!