-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCoinbase Pro WebSocket.py
32 lines (26 loc) · 983 Bytes
/
Coinbase Pro WebSocket.py
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
29
30
31
32
import time, cbpro
# Simple WebSocket
wsc = cbpro.WebsocketClient(url="wss://ws-feed.pro.coinbase.com",
products="ADA-USD",
channels=["ticker"])
# WebSocket Class Example
class myWebsocketClient(cbpro.WebsocketClient):
def on_open(self):
self.url = "wss://ws-feed.pro.coinbase.com/"
self.products = ["ETH-USDT"]
self.channels=["ticker"]
self.message_count = 0
def on_message(self, msg):
self.message_count += 1
if 'price' in msg and 'type' in msg:
print ("Message type:", msg["type"],
"\t@ {:.3f}".format(float(msg["price"])))
def on_close(self):
print("Closing")
wsClient = myWebsocketClient()
wsClient.start()
print(wsClient.url, wsClient.products, wsClient.channels)
while (wsClient.message_count < 50):
print ("\nmessage_count =", "{} \n".format(wsClient.message_count))
time.sleep(1)
wsClient.close()