ohenrik / bitfinex

A Python client for the Bitfinex API
MIT License
58 stars 32 forks source link

Error creating new order #29

Closed fccoelho closed 5 years ago

fccoelho commented 5 years ago

Hi I am getting this error when trying to communicate with Bitfinex using the WSClient:

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/IPython/core/interactiveshell.py", line 3267, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-4-aa43adbe623c>", line 1, in <module>
    create_order('tBTCUSD',0,4000)
  File "/home/fccoelho/Documentos/Projects_software/Trading/tradeboard/bitfinex_client.py", line 46, in create_order
    price=price,
  File "/usr/local/lib/python3.6/dist-packages/bitfinex/websockets/client.py", line 657, in new_order
    self.factories["auth"].protocol_instance.sendMessage(payload, isBinary=False)
AttributeError: 'NoneType' object has no attribute 'sendMessage'
fccoelho commented 5 years ago

The same error happens when I call client.ping()

ohenrik commented 5 years ago

Are you sure that you have authenticated the client? If you are trying to send messages over the auth channel before it has finished setting it up you might not actually have the connection object.

If you share the code (or a simplified version of it) it will be easier to see whats going wrong.

fccoelho commented 5 years ago

I have had no problems with the authentication through the REST v2 interface. My example is the same as the one in the documentation. I have tried it from the docs:

trade_client = WssClient(settings.PUBLIC, settings.PRIVATE)
trade_client.authenticate(print)
trade_client.start()
trade_client.ping()

by the way, the authenticate call is not printing anything, should it? Should I call time.sleep(5) between authenticate and start()?

dantimofte commented 5 years ago

I have had no problems with the authentication through the REST v2 interface. My example is the same as the one in the documentation. I have tried it from the docs:

trade_client = WssClient(settings.PUBLIC, settings.PRIVATE)
trade_client.authenticate(print)
trade_client.start()
trade_client.ping()

by the way, the authenticate call is not printing anything, should it? Should I call time.sleep(5) between authenticate and start()?

You should put that sleep after start(). Please check this example on how to use the websockets : https://github.com/ohenrik/bitfinex/blob/master/examples/wss_example.py

fccoelho commented 5 years ago

well I ran the commands in an Ipython shell and I waited way more than 5 seconds after start before issuing the ping() and still got the same error. I am using python 3.6

dantimofte commented 5 years ago

I have tested with python 3.7.1 and python 3.6.0 and it worked ok:

`bitfinex$ python Python 3.6.0 (default, Jan 18 2019, 22:28:09) [GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.10.44.4)] on darwin Type "help", "copyright", "credits" or "license" for more information.

import os import threading import logging from time import sleep from bitfinex.websockets.client import WssClient KEY = os.environ.get("API_KEY") SECRET = os.environ.get("API_SECRET") trade_client = WssClient(key=KEY, secret=SECRET) trade_client.authenticate(print) trade_client.start() {'event': 'info', 'version': 2, 'serverId': '04f6624b-b93c-4002-80c2-2fc423d944d6', 'platform': {'status': 1}} {'event': 'auth', 'status': 'OK', 'chanId': 0, ........} [0, 'ps', []] [0, 'ws', [...]] [0, 'os', [...]] [0, 'fos', []] [0, 'fcs', []] [0, 'fls', []] trade_client.ping() 1547837635647762 [0, 'hb'] {'event': 'pong', 'cid': 1547837635647762, 'ts': 1547844846153} [0, 'hb'] [0, 'hb'] [0, 'hb'] [0, 'hb'] [0, 'hb'] trade_client.new_order("EXCHANGE LIMIT", "tIOTUSD", "-1000", "30") 1547838013106443 [0, 'hb'] [0, 'n', [1547845216131, 'on-req', None, None, [None, None, 1547838013106443, 'tIOTUSD', None, None, -1000, None, 'EXCHANGE LIMIT', None, None, None, 0, None, None, None, 30, None, 0, 0, None, None, None, 0, 0, None, None, None, None, None, None, None], None, 'ERROR', 'api_key: permission invalid']] `

fccoelho commented 5 years ago

Tried again from another computer and it worked as it should. I don't know what may have caused the problem. Thanks for the reassurances. I think this issue can be closed now.

andrebrener commented 5 years ago

I'm getting the same error and cannot find the solution.

Here's my code

trade_client = WssClient(BFX_KEY, BFX_SECRET)
trade_client.authenticate(callback=handle_account_messages)

trade_client.start()

order_operation = trade_client.new_order_op(
    order_type='FOK',
    symbol='BTCUSD',
    amount=0.004,
    price=1000.0
)

trade_client.multi_order(
    operations=[order_operation]
)
dantimofte commented 5 years ago

what version of python are you using ? can you paste the error output ?

andrebrener commented 5 years ago

I think I solve it by adding the sleep(5) after the trade_client.start().

I'm using python 3.7.4 in OSX

Now I'm getting this error:


  File "/Users/andre/git-repos/work/crypto_trader/python/test.py", line 22, in <module>
    trade_client.multi_order(operations=[order_operation])
  File "/usr/local/lib/python3.7/site-packages/bitfinex/websockets/client.py", line 719, in multi_order
    return [order[1].get("cid", None) for order in operations]
  File "/usr/local/lib/python3.7/site-packages/bitfinex/websockets/client.py", line 719, in <listcomp>
    return [order[1].get("cid", None) for order in operations]
KeyError: 1
[0, 'hb']
[0, 'n', [1566490238285, 'ox_multi-req', None, None, None, None, 'ERROR', 'input: invalid']]```
dantimofte commented 5 years ago

it appears that you are getting the error because the new order parameters are not ok for bitfinex. You will get rid of the error if you correct the parameters. This situation should be treated so it does not raise that error. please open a new issue for this.

andrebrener commented 5 years ago

I've opened a new issue.

By correcting the parameters you mean these ones?

    symbol='BTCUSD',
    amount=0.004,
    price=1000.0

I tried with different ones and got the same error. Order type = 'FOK' or 'EXCHANGE LIMIT' symbol = 'BTCUSD' or 'tBTCUSD' amount = 0.004 or -0.004 for selling price = 1000, 15000, 2000