This repository has been archived by the owner on Mar 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 171
/
websocket_example.py
143 lines (118 loc) · 3.55 KB
/
websocket_example.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
"""
To see which endpoints and topics are available, check the Bybit API
documentation: https://bybit-exchange.github.io/docs/inverse/#t-websocket
Inverse Perpetual endpoints:
wss://stream-testnet.bybit.com/realtime
wss://stream.bybit.com/realtime
USDT Perpetual endpoints:
wss://stream-testnet.bybit.com/realtime_public
wss://stream-testnet.bybit.com/realtime_private
wss://stream.bybit.com/realtime_public
wss://stream.bybit.com/realtime_private
Spot endpoints:
wss://stream-testnet.bybit.com/spot/quote/ws/v1
wss://stream-testnet.bybit.com/spot/quote/ws/v2
wss://stream-testnet.bybit.com/spot/ws
wss://stream.bybit.com/spot/quote/ws/v1
wss://stream.bybit.com/spot/quote/ws/v2
wss://stream.bybit.com/spot/ws
Futures Public Topics:
orderBookL2_25
orderBookL2-200
trade
insurance
instrument_info
klineV2
Futures Private Topics:
position
execution
order
stop_order
wallet
Spot Topics:
Subscribing to spot topics uses the JSON format to pass the topic name and
filters, as opposed to futures WS where the topic and filters are pass in a
single string. So, it's recommended to pass a JSON or python dict in your
subscriptions, which also be used to fetch the topic's updates. Examples can
be found in the code panel here: https://bybit-exchange.github.io/docs/spot/#t-publictopics
However, as private spot topics do not require a subscription, the following
strings can be used to fetch data:
outboundAccountInfo
executionReport
ticketInfo
"""
# Import the WebSocket object from pybit.
from pybit import WebSocket
"""
We can also import the HTTP object at the same time using:
from pybit import HTTP, WebSocket
Additionally, we can simply import all of pybit and use each
object selectively.
import pybit
client = pybit.HTTP(...)
ws = pybit.WebSocket(...)
"""
# Define your endpoint URLs and subscriptions.
endpoint_public = 'wss://stream.bybit.com/realtime_public'
endpoint_private = 'wss://stream.bybit.com/realtime_private'
subs = [
'orderBookL2_25.BTCUSD',
'instrument_info.100ms.BTCUSD',
'instrument_info.100ms.ETHUSD'
]
# Connect without authentication!
ws_unauth = WebSocket(endpoint_public, subscriptions=subs)
# Connect with authentication!
ws_auth = WebSocket(
endpoint_private,
subscriptions=['position'],
api_key='...',
api_secret='...'
)
# Let's fetch the orderbook for BTCUSD.
print(
ws_unauth.fetch('orderBookL2_25.BTCUSD')
)
# We can also create a dict containing multiple results.
print(
{i: ws_unauth.fetch(i) for i in subs}
)
# Check on your position. Note that no position data is received until a
# change in your position occurs (initially, there will be no data).
print(
ws_auth.fetch('position')
)
"""
Spot websocket sample usage.
As using the spot websocket works slightly differently, separate sample usage is
listed here.
"""
# Define endpoints and subscriptions
endpoint_spot_v1 = "wss://stream.bybit.com/spot/quote/ws/v1"
endpoint_spot_private = "wss://stream.bybit.com/spot/ws"
trade_v1 = """
{
"topic": "trade",
"event": "sub",
"symbol": "BTCUSDT",
"params": {
"binary": false
}
}"""
realtimes_v1 = """
{
"topic": "realtimes",
"event": "sub",
"symbol": "BTCUSDT",
"params": {
"binary": false
}
}"""
subs = [trade_v1, realtimes_v1]
# Connect to the unauth spot websocket
ws_spot_unauth = WebSocket(endpoint=endpoint_spot_v1, subscriptions=subs)
# Fetch this subscription
print(ws_spot_unauth.fetch(trade_v1))
# Connect to the auth spot websocket (subscriptions are not required)
ws_spot_auth = WebSocket(endpoint=endpoint_spot_private)
print(ws_spot_auth.fetch('outboundAccountInfo'))