1
votes

I have tried everything to submit a new order to the book using the websocket API with Python. Connecting is working perfectly, and I get a constant response stream for the channels I am subscribed to. However, the Bitfinex documentation is a little light to say the least, and I cannot work out what I'm doing wrong with this attempt to place a new order. https://bitfinex.readme.io/v2/reference#ws-input-order-new

import json
import hmac, hashlib
from time import time, sleep
from websocket import create_connection

KEY = bitfinex_key
SECRET = bitfinex_secret

def _nonce():
    return str(int(time()*1000000))

nonce = _nonce()

signature = hmac.new(SECRET, bytes('AUTH' + nonce,'utf8'), hashlib.sha384).hexdigest()

ws = create_connection("wss://api.bitfinex.com/ws/2")

payload = {
    "apiKey" : KEY,
    "event" : "auth",
    "authPayload" : "AUTH" + nonce,
    "authNonce" : nonce,
    "authSig" : signature,
    'filter' : [
        'trading', #orders, positions, trades 
        'algo', #algorithmic orders
        'os', 
        'ps', 
        'n'
   ]
}

ws.send(json.dumps(payload))

neworder = [
    0,
    "on",
    None,
    {
     "cid": 902368741641276786498451564556,
     "type": "LIMIT",
     "symbol": "tBTGBTC",
     "amount": "0.14",
     "price": "0.01",
     "hidden": 0,
    }
]

sleep(3)
ws.send(json.dumps(neworder))

while True:
    result = ws.recv()
    result = json.loads(result)
    print ("Received '%s'" % result)

ws.close()

Is this even how I'm supposed to send a second send message once an authenticated connection is established? i.e. by invoking send on the same object used for create_connection?

Edit to include responses:-

With the line 'ws.send(json.dumps(neworder))' commented out:

Received '{'event': 'info', 'version': 2}'
Received '{'event': 'auth', 'status': 'OK', 'chanId': 0, 'userId': 1064981, 'auth_id': 'b45ded09-56dc-46bf-9520-6de33c7f6322', 'caps': {'orders': {'read': 1, 'write': 1}, 'account': {'read': 1, 'write': 0}, 'funding': {'read': 1, 'write': 0}, 'history': {'read': 1, 'write': 0}, 'wallets': {'read': 1, 'write': 0}, 'withdraw': {'read': 0, 'write': 0}, 'positions': {'read': 1, 'write': 0}}}'
Received '[0, 'ps', []]'
Received '[0, 'os', []]'
Received '[0, 'ats', []]'
Received '[0, 'hb']'
Received '[0, 'hb']'
Received '[0, 'hb']'
Received '[0, 'hb']'
...

With the line 'ws.send(json.dumps(neworder))' not commented out:

Received '{'event': 'info', 'version': 2}'
Received '{'event': 'auth', 'status': 'OK', 'chanId': 0, 'userId': 1064981, 'auth_id': '1711a269-d59d-452f-a35a-ba959e1a3386', 'caps': {'orders': {'read': 1, 'write': 1}, 'account': {'read': 1, 'write': 0}, 'funding': {'read': 1, 'write': 0}, 'history': {'read': 1, 'write': 0}, 'wallets': {'read': 1, 'write': 0}, 'withdraw': {'read': 0, 'write': 0}, 'positions': {'read': 1, 'write': 0}}}'
Received '[0, 'ps', []]'
Received '[0, 'os', []]'
Received '[0, 'ats', []]'
Received '[0, 'hb']'
Received '[0, 'hb']'
... 
1
What responses do you get from the server? Include those in your question. - mhawke
Nothing, only the periodic updates for the channels I'm subscribed for - sphere
Hi! Could you please share, if issue was resolved somehow? - Konstantin Utkin

1 Answers

0
votes

This is where your problem is:

sleep(3)

You actually need to wait for 'auth' event before sending any new order payloads.