0%

用 websocket 连接 bitmex 交易所

直接贴一下代码吧。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import websocket


def on_message(ws, message): # 服务器有数据更新时,主动推送过来的数据
print(message)


def on_error(ws, error): # 程序报错时,就会触发on_error事件
print(error)


def on_close(ws):
print("Connection closed ……")


def on_open(ws): # 连接到服务器之后就会触发on_open事件,这里用于send数据
_ = '{"op": "subscribe","args": "instrument:XBTUSD"}'
ws.send(_)


if __name__ == "__main__":
websocket.enableTrace(True)
ws = websocket.WebSocketApp("wss://www.bitmex.com/realtime",
on_message=on_message,
on_error=on_error,
on_close=on_close)
ws.on_open = on_open
ws.run_forever(http_proxy_host='127.0.0.1', http_proxy_port=1087)

里面的端口号 1087 是我 vpn 的端口号,各位可以根据自己的需要换。

请我喝杯咖啡吧~