Bitfinex Websockets API - 无法下新订单

时间:2017-11-16 11:00:10

标签: python websocket

我已尝试使用带有Python的websocket API向书中提交新订单。连接工作正常,我得到了我订阅的频道的持续响应流。但是,Bitfinex文档至少可以说是有点轻松,而且我无法弄清楚我这次尝试下新订单的错误。 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()

在建立经过身份验证的连接后,这是否应该发送第二封发送消息?即通过调用与create_connection相同的对象发送?

编辑以包含回复: -

使用' ws.send(json.dumps(neworder))'评论说:

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']'
...

使用' ws.send(json.dumps(neworder))'没有注释掉:

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 个答案:

答案 0 :(得分:0)

这就是您的问题所在:

sleep(3)

你实际上需要等待#autah'发送任何新订单有效负载之前的事件。