-
Notifications
You must be signed in to change notification settings - Fork 204
/
main.py
31 lines (26 loc) · 1.21 KB
/
main.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
import time
from ccapi import EventHandler, SessionOptions, SessionConfigs, Session, Subscription, Event
class MyEventHandler(EventHandler):
def __init__(self):
super().__init__()
def processEvent(self, event: Event, session: Session) -> bool:
if event.getType() == Event.Type_SUBSCRIPTION_STATUS:
print(f"Received an event of type SUBSCRIPTION_STATUS:\n{event.toStringPretty(2, 2)}")
elif event.getType() == Event.Type_SUBSCRIPTION_DATA:
for message in event.getMessageList():
print(f"Best bid and ask at {message.getTimeISO()} are:")
for element in message.getElementList():
elementNameValueMap = element.getNameValueMap()
for name, value in elementNameValueMap.items():
print(f" {name} = {value}")
return True # This line is needed.
if __name__ == "__main__":
eventHandler = MyEventHandler()
option = SessionOptions()
config = SessionConfigs()
session = Session(option, config, eventHandler)
subscription = Subscription("okx", "BTC-USDT", "MARKET_DEPTH")
session.subscribe(subscription)
time.sleep(10)
session.stop()
print("Bye")