-
Notifications
You must be signed in to change notification settings - Fork 0
/
dev_test_cex_full.py
executable file
·221 lines (191 loc) · 11.6 KB
/
dev_test_cex_full.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# File: dev_test_cex_full.py
#
# Part of ‘UNICORN Binance WebSocket API’
# Project website: https://github.com/oliver-zehentleitner/unicorn-binance-websocket-api
# Documentation: https://oliver-zehentleitner.github.io/unicorn-binance-websocket-api
# PyPI: https://pypi.org/project/unicorn-binance-websocket-api/
#
# Author: Oliver Zehentleitner
# https://about.me/oliver-zehentleitner
#
# Copyright (c) 2019-2021, Oliver Zehentleitner
# All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish, dis-
# tribute, sublicense, and/or sell copies of the Software, and to permit
# persons to whom the Software is furnished to do so, subject to the fol-
# lowing conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
from unicorn_binance_websocket_api.unicorn_binance_websocket_api_manager import BinanceWebSocketApiManager
import logging
import os
import time
import threading
# https://docs.python.org/3/library/logging.html#logging-levels
logging.basicConfig(level=logging.DEBUG,
filename=os.path.basename(__file__) + '.log',
format="{asctime} [{levelname:8}] {process} {thread} {module}: {message}",
style="{")
def print_stream_data_from_stream_buffer(binance_websocket_api_manager):
while True:
if binance_websocket_api_manager.is_manager_stopping():
exit(0)
oldest_stream_data_from_stream_buffer = binance_websocket_api_manager.pop_stream_data_from_stream_buffer()
if oldest_stream_data_from_stream_buffer is False:
time.sleep(0.01)
else:
try:
print(oldest_stream_data_from_stream_buffer)
except Exception:
# not able to process the data? write it back to the stream_buffer
binance_websocket_api_manager.add_to_stream_buffer(oldest_stream_data_from_stream_buffer)
# create instance of BinanceWebSocketApiManager and provide the function for stream processing
binance_websocket_api_manager = BinanceWebSocketApiManager()
# start a worker process to process to move the received stream_data from the stream_buffer to a print function
worker_thread = threading.Thread(target=print_stream_data_from_stream_buffer, args=(binance_websocket_api_manager,))
worker_thread.start()
print("testing ws/ single streams")
print("\r\n========================================== Starting ticker all ========================================\r\n")
ticker_all_stream_id = binance_websocket_api_manager.create_stream(["arr"], ["!ticker"])
time.sleep(6)
binance_websocket_api_manager.stop_stream(ticker_all_stream_id)
time.sleep(2)
print("\r\n=========================================== Stopped ticker all ========================================\r\n")
print("\r\n========================================= Starting !miniticker ========================================\r\n")
miniticker_stream_id = binance_websocket_api_manager.create_stream(["arr"], ["!miniTicker"])
time.sleep(5)
binance_websocket_api_manager.stop_stream(miniticker_stream_id)
time.sleep(2)
print("\r\n======================================== Stopped !miniticker =========================================\r\n")
print("\r\n========================================== Starting !userData ========================================\r\n")
userdata_stream_id = binance_websocket_api_manager.create_stream(["arr"], ["!userData"])
time.sleep(5)
binance_websocket_api_manager.stop_stream(userdata_stream_id)
time.sleep(2)
print("\r\n========================================== Stopped !userData ==========================================\r\n")
print("\r\n\r\nTesting multi streams with just one market")
print("\r\n========================================== Starting aggTrade ==========================================\r\n")
markets = {'bnbbtc'}
aggtrade_stream_id = binance_websocket_api_manager.create_stream(["aggTrade"], markets)
time.sleep(5)
binance_websocket_api_manager.stop_stream(aggtrade_stream_id)
time.sleep(2)
print("\r\n=========================================== Stopped aggTrade ==========================================\r\n")
print("\r\n====================================== Starting trade and kline_1m ====================================\r\n")
trade_stream_id = binance_websocket_api_manager.create_stream(["trade"], markets)
kline_1m_stream_id = binance_websocket_api_manager.create_stream(["kline_1m"], markets)
time.sleep(5)
binance_websocket_api_manager.stop_stream(trade_stream_id)
binance_websocket_api_manager.stop_stream(kline_1m_stream_id)
time.sleep(2)
print("\r\n====================================== Stopped trade and kline_1m =====================================\r\n")
print("\r\n======================================== Starting ticker BNB/BTC ======================================\r\n")
ticker_bnbbtc_stream_id = binance_websocket_api_manager.create_stream(["ticker"], markets)
time.sleep(5)
binance_websocket_api_manager.stop_stream(ticker_bnbbtc_stream_id)
time.sleep(2)
print("\r\n======================================== Stopped ticker BNB/BTC =======================================\r\n")
print("\r\n========================================== Starting miniticker ========================================\r\n")
miniticker_stream_id = binance_websocket_api_manager.create_stream(["miniTicker"], markets)
time.sleep(5)
binance_websocket_api_manager.stop_stream(miniticker_stream_id)
time.sleep(2)
print("\r\n========================================= Stopped miniticker =========================================\r\n")
print("\r\n========================================== Starting kline_5m ==========================================\r\n")
kline_5m_stream_id = binance_websocket_api_manager.create_stream(["kline_5m"], markets)
time.sleep(5)
binance_websocket_api_manager.stop_stream(kline_5m_stream_id)
time.sleep(2)
print("\r\n========================================= Stopped kline_5m ===========================================\r\n")
print("\r\n=========================================== Starting depth10 ===========================================\r\n")
depth5_stream_id = binance_websocket_api_manager.create_stream(["depth10"], markets)
time.sleep(5)
binance_websocket_api_manager.stop_stream(depth5_stream_id)
time.sleep(2)
print("\r\n========================================== Stopped depth5 ============================================\r\n")
print("\r\n========================================== Starting depth =============================================\r\n")
depth_stream_id = binance_websocket_api_manager.create_stream(["depth"], markets)
time.sleep(4)
binance_websocket_api_manager.stop_stream(depth_stream_id)
time.sleep(2)
print("\r\n============================================ Stopped depth ===========================================\r\n")
print("\r\n\r\nTesting multi markets with one stream_type")
markets = {'xrpusdt', 'rvnbtc', 'ltcusdt', 'adausdt', 'eosusdt', 'neousdt', 'btcusdt', 'bnbusdt'}
print("\r\n========================================== Starting aggTrade ==========================================\r\n")
aggtrade_stream_id = binance_websocket_api_manager.create_stream(["aggTrade"], markets)
time.sleep(3)
binance_websocket_api_manager.stop_stream(aggtrade_stream_id)
time.sleep(2)
print("\r\n=========================================== Stopped aggTrade ==========================================\r\n")
print("\r\n====================================== Starting trade and kline_1m ====================================\r\n")
trade_stream_id = binance_websocket_api_manager.create_stream(["trade"], markets)
kline_1m_stream_id = binance_websocket_api_manager.create_stream(["kline_1m"], markets)
time.sleep(3)
binance_websocket_api_manager.stop_stream(trade_stream_id)
binance_websocket_api_manager.stop_stream(kline_1m_stream_id)
time.sleep(2)
print("\r\n====================================== Stopped trade and kline_1m =====================================\r\n")
print("\r\n======================================= Starting ticker ===============================================\r\n")
ticker_bnbbtc_stream_id = binance_websocket_api_manager.create_stream(["ticker"], markets)
time.sleep(4)
binance_websocket_api_manager.stop_stream(ticker_bnbbtc_stream_id)
time.sleep(2)
print("\r\n========================================= Stopped ticker ==============================================\r\n")
print("\r\n========================================== Starting miniticker ========================================\r\n")
miniticker_stream_id = binance_websocket_api_manager.create_stream(["miniTicker"], markets)
time.sleep(4)
binance_websocket_api_manager.stop_stream(miniticker_stream_id)
time.sleep(2)
print("\r\n========================================== Stopped miniticker ========================================\r\n")
print("\r\n========================================== Starting kline_5m ==========================================\r\n")
kline_5m_stream_id = binance_websocket_api_manager.create_stream(["kline_5m"], markets)
time.sleep(4)
binance_websocket_api_manager.stop_stream(kline_5m_stream_id)
time.sleep(2)
print("\r\n========================================= Stopped kline_5m ===========================================\r\n")
print("\r\n=========================================== Starting depth5 ===========================================\r\n")
depth5_stream_id = binance_websocket_api_manager.create_stream(["depth5"], markets)
time.sleep(4)
binance_websocket_api_manager.stop_stream(depth5_stream_id)
time.sleep(2)
print("\r\n========================================== Stopped depth5 ============================================\r\n")
print("\r\n========================================== Starting depth =============================================\r\n")
depth_stream_id = binance_websocket_api_manager.create_stream(["depth"], markets)
time.sleep(4)
binance_websocket_api_manager.stop_stream(depth_stream_id)
time.sleep(2)
print("\r\n============================================ Stopped depth ===========================================\r\n")
print("\r\n=================================== Starting multi multi socket =======================================\r\n")
channels = {'trade', 'kline_1', 'kline_5', 'kline_15', 'kline_30', 'kline_1h', 'kline_12h', 'kline_1w',
'miniTicker', 'depth20', '!miniTicker', '!ticker'}
multi_multi_stream_id = binance_websocket_api_manager.create_stream(channels, markets)
time.sleep(4)
binance_websocket_api_manager.stop_stream(multi_multi_stream_id)
time.sleep(2)
print("\r\n================================== Stopped multi multi socket ========================================\r\n")
# print summary
binance_websocket_api_manager.print_summary()
print("\r\n=============================== Stopping BinanceWebSocketManager ======================================\r\n")
binance_websocket_api_manager.stop_manager_with_all_streams()
time.sleep(7)
stream_list = binance_websocket_api_manager.get_stream_list()
for stream_id in stream_list:
binance_websocket_api_manager.print_stream_info(stream_id)
time.sleep(3.5)
print("finished!")