本人自学python水平有限,耗费3天时间调试成功了这个websocket接口。实在是官方文档没有python实现的demo,网络上又根本搜索不到相关信息,自己硬生生琢磨出来的。全当抛砖引玉,请各位看官指点。
以下内容属于最小可行性版本,功能逐步扩展:
import gzip
import json
import websocket
import ssl
import base64
class order_maker:
def __init__(self):
# 初始化Websocket
# 墙内外修改这里
depth_address = "wss://push.bibox365.com/" # 墙内地址
# depth_address = "wss://push.bibox.com/" # 墙外地址
websocket.enableTrace(True)
self.ws = websocket.WebSocketApp(depth_address,
on_message=self.on_message,
on_error=self.on_error,
on_close=self.on_close)
self.ws.on_open = self.on_open
# 初始化bibox接口
self.bibox_service = websocket.WebSocketApp('access key',
'secret key',
'https://api.bibox365.com')
# 启动ws
def run(self):
# 墙内外修改这里
# self.ws.run_forever() # 直接连接
self.ws.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE}) # sslopt={"cert_reqs": ssl.CERT_NONE}禁用ssl证书验证
# self.ws.run_forever(http_proxy_host='xxx.xxx.xxx.xx', http_proxy_port=xxxx) # 设置墙外代理ip地址和端口
# WebSocket回调函数
# ---------------------------------------------------------------------
def on_message(self, message):
msg = json.loads(message[1:-2]) # str转换成dict
msg = base64.b64decode(msg['data']) # 解码BASE64
msg = gzip.decompress(msg).decode("utf-8") # gzip解码
print(msg)
if 'kline' in message:
print('订阅历史数据')
# 自己实现功能
# ……
if 'depth' in message:
print('订阅市场深度行情')
# 自己实现功能
# ……
def on_error(self, error):
print("Websocket连接错误,%s" % (error))
def on_close(self):
print("Websocket连接关闭,5秒后重新连接")
def on_open(self):
print("bibox行情Websocket连接成功")
self.subscribe_trade('ETH_BTC') # 订阅历史数据
self.subscribe_depth('ETH_BTC') # 订阅市场深度行情
# bibox订阅
# ---------------------------------------------------------------------
# 订阅历史数据
def subscribe_trade(self, symbol):
tradeStr = '{"event": "addChannel","channel": "bibox_sub_spot_%s_kline_1min"}' % (symbol)
self.ws.send(tradeStr)
# 订阅市场深度行情
def subscribe_depth(self, symbol):
tradeStr = '{"event": "addChannel","channel": "bibox_sub_spot_%s_depth"}' % (symbol)
self.ws.send(tradeStr)
# 主函数
if __name__ == "__main__":
cf = order_maker()
cf.run()
运行代码后,会收到交易所自动推送的数据
--- request header ---
GET / HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: push.bibox365.com
Origin: http://push.bibox365.com
Sec-WebSocket-Key: urPNXKgvds2Di2j2woGF4g==
Sec-WebSocket-Version: 13
-----------------------
--- response header ---
HTTP/1.1 101 Switching Protocols
Date: Mon, 10 Jun 2019 09:13:01 GMT
Connection: upgrade
Set-Cookie: __cfduid=d252f2523002e861dfe0afa9db4d9e1dc1560157981; expires=Tue, 09-Jun-20 09:13:01 GMT; path=/; domain=.bibox365.com; HttpOnly; Secure
Upgrade: websocket
Sec-WebSocket-Accept: r6p3L/m/xEYMXZzPDATvZGUqkSU=
Set-Cookie: __cflb=3426105834; Secure; path=/; expires=Tue, 11-Jun-19 08:13:01 GMT; HttpOnly
Strict-Transport-Security: max-age=15552000; includeSubDomains; preload
X-Content-Type-Options: nosniff
Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
Server: cloudflare
CF-RAY: 4e4a3a167f8e6bc0-SJC
-----------------------
bibox行情Websocket连接成功
send: b"\x81\xc6\xa4\xe5\x89K\xdf\xc7\xec=\xc1\x8b\xfdi\x9e\xc5\xab*\xc0\x81\xca#\xc5\x8b\xe7.\xc8\xc7\xa5i\xc7\x8d\xe8%\xca\x80\xe5i\x9e\xc5\xab)\xcd\x87\xe63\xfb\x96\xfc)\xfb\x96\xf9$\xd0\xba\xdc\x1e\xf1\xba\xcb\x1f\xe7\xba\xe2'\xcd\x8b\xec\x14\x95\x88\xe0%\x86\x98"
send: b'\x81\xc1l\x16\xc1/\x174\xa4Y\tx\xb5\rV6\xe3N\x08r\x82G\rx\xafJ\x004\xed\r\x0f~\xa0A\x02s\xad\rV6\xe3M\x05t\xaeW3e\xb4M3e\xb1@\x18I\x94z9I\x83{/I\xa5J\x1cb\xa9\r\x11'
[{"time":1560157980000,"open":"0.00000019","high":"0.00000019","low":"0.00000019","close":"0.00000019","vol":"0.00000000"},{"time":1560158040000,"open":"0.00000019","high":"0.00000019","low":"0.00000019","close":"0.00000019","vol":"0.00000000"}]
订阅历史数据
{"update_time":1560158542038,"seq":14709,
"asks":
[{"volume":"869028.3959","price":"0.00000019"},{"volume":"20000.0000","price":"0.00000020"},{"volume":"96114.0241","price":"0.00000021"},{"volume":"38820.9916","price":"0.00000022"},{"volume":"144892.1200","price":"0.00000023"},{"volume":"206661.3907","price":"0.00000024"},{"volume":"313451.9123","price":"0.00000025"},{"volume":"264606.8265","price":"0.00000026"},{"volume":"286350.1487","price":"0.00000027"},{"volume":"88230.0321","price":"0.00000028"},{"volume":"663583.8935","price":"0.00000029"},{"volume":"2570936.5313","price":"0.00000030"},{"volume":"2008961.5000","price":"0.00000031"},{"volume":"100000.0000","price":"0.00000032"},{"volume":"1268720.7938","price":"0.00000033"},{"volume":"1453576.3437","price":"0.00000034"},{"volume":"10000.0000","price":"0.00000035"},{"volume":"641250.0000","price":"0.00000036"},{"volume":"707682.2578","price":"0.00000037"},{"volume":"110000.0000","price":"0.00000038"},{"volume":"1279663.5000","price":"0.00000040"},{"volume":"2972825.6728","price":"0.00000041"},{"volume":"20000.0000","price":"0.00000042"},{"volume":"2530761.6933","price":"0.00000043"},{"volume":"15843.5870","price":"0.00000044"},{"volume":"110000.0000","price":"0.00000045"},{"volume":"2908071.2700","price":"0.00000046"},{"volume":"12343.8898","price":"0.00000047"},{"volume":"595.0000","price":"0.00000048"},{"volume":"2306303.4525","price":"0.00000049"},{"volume":"20408.7732","price":"0.00000050"},{"volume":"2630165.0894","price":"0.00000053"},{"volume":"78504.1385","price":"0.00000054"},{"volume":"131481.2499","price":"0.00000056"},{"volume":"972623.8170","price":"0.00000057"},{"volume":"223313.1886","price":"0.00000058"},{"volume":"60266.5935","price":"0.00000060"},{"volume":"10000.0000","price":"0.00000065"},{"volume":"5000.0000","price":"0.00000068"},{"volume":"20408.7733","price":"0.00000070"},{"volume":"2164539.3220","price":"0.00000083"},{"volume":"5000.0000","price":"0.00000085"},{"volume":"443870.6473","price":"0.00000091"},{"volume":"197409.7084","price":"0.00000099"},{"volume":"2092250.3230","price":"0.00000100"},{"volume":"5000.0000","price":"0.00000130"},{"volume":"5000.0000","price":"0.00000150"},{"volume":"69899.9449","price":"0.00000177"},{"volume":"5000.0000","price":"0.00000180"},{"volume":"51603.7636","price":"0.00000200"},{"volume":"333000.0221","price":"0.00000350"},{"volume":"348356.6223","price":"0.00000450"},{"volume":"348356.6222","price":"0.00000490"},{"volume":"232237.7481","price":"0.00000500"},{"volume":"298.0000","price":"0.00000700"},{"volume":"13778.8421","price":"0.00005000"},{"volume":"1933.5483","price":"0.00800000"}],
"bids":
[{"volume":"669610.7777","price":"0.00000018"},{"volume":"6622707.9053","price":"0.00000017"},{"volume":"3725968.6396","price":"0.00000016"},{"volume":"2134237.6861","price":"0.00000015"},{"volume":"4231170.6427","price":"0.00000014"},{"volume":"1325850.6152","price":"0.00000013"},{"volume":"1232314.8331","price":"0.00000012"},{"volume":"657813.6363","price":"0.00000011"},{"volume":"531615.2000","price":"0.00000010"},{"volume":"1566785.0870","price":"0.00000009"},{"volume":"1328040.2141","price":"0.00000008"},{"volume":"586629.3365","price":"0.00000007"},{"volume":"540429.8727","price":"0.00000006"},{"volume":"271267.0000","price":"0.00000005"},{"volume":"4000000.0000","price":"0.00000001"}],"pair":"UUU_BTC"}
订阅市场深度行情
前面是请求和响应的内容,后面是推送的数据。
这只是一个开始,我的最终目标是做量化策略开发,实盘运行赚钱。