@@ -177,6 +177,18 @@ ubwa.api.cancel_order(orig_client_order_id=orig_client_order_id, symbol="BUSDUSD
177
177
guide on
178
178
[ how to process requests via the Binance WebSocket API] ( https://medium.lucit.tech/create-and-cancel-orders-via-websocket-on-binance-7f828831404 ) !
179
179
180
+ ## Stop ` ubwa ` after usage to avoid memory leaks
181
+ When you instantiate UBWA with ` with ` , ` ubwa.stop_manager() ` is automatically executed upon exiting the ` with ` -block.
182
+ ```
183
+ with BinanceWebSocketApiManager() as ubwa:
184
+ ubwa.create_stream(channels="trade", markets="btcusdt", stream_label="TRADES")
185
+ ```
186
+
187
+ Without ` with ` , you must explicitly execute ` ubwa.stop_manager() ` yourself.
188
+ ```
189
+ ubwa.stop_manager()
190
+ ```
191
+
180
192
## STREAM SIGNALS - know the state of your streams
181
193
Usually you want to know when a stream is working and when it is not. This can be useful to know that your own system is
182
194
currently "blind" and you may want to close open positions to be on the safe side, know that indicators will now provide
@@ -187,10 +199,19 @@ For this purpose, the UNICORN Binance WebSocket API provides so-called
187
199
, which are used to tell your code in real time when a stream is connected, when it received its first data record, when
188
200
it was disconnected and stopped, and when the stream cannot be restored.
189
201
190
- ## Stop ` ubwa ` after usage to avoid memory leaks
191
-
192
202
```
193
- ubwa.stop_manager()
203
+ from unicorn_binance_websocket_api import BinanceWebSocketApiManager
204
+ import time
205
+
206
+ def process_stream_signals(signal_type=None, stream_id=None, data_record=None, error_msg=None):
207
+ print(f"Received stream_signal for stream '{ubwa.get_stream_label(stream_id=stream_id)}': "
208
+ f"{signal_type} - {stream_id} - {data_record} - {error_msg}")
209
+
210
+ with BinanceWebSocketApiManager(process_stream_signals=process_stream_signals) as ubwa:
211
+ ubwa.create_stream(channels="trade", markets="btcusdt", stream_label="TRADES")
212
+ time.sleep(2)
213
+ print(f"Waiting 5 seconds and then stop the stream ...")
214
+ time.sleep(5)
194
215
```
195
216
196
217
[ Discover even more possibilities] ( https://unicorn-binance-websocket-api.docs.lucit.tech/unicorn_binance_websocket_api.html )
0 commit comments