Skip to content

Commit af3d128

Browse files
2.6.0 RC1
1 parent ca3fb55 commit af3d128

File tree

1 file changed

+75
-34
lines changed

1 file changed

+75
-34
lines changed

meta.yaml

Lines changed: 75 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ about:
7474
in a easy, fast, flexible, robust and fully-featured way.
7575
description: |
7676
[![Get a UNICORN Binance Suite License](https://raw.githubusercontent.com/LUCIT-Systems-and-Development/unicorn-binance-suite/master/images/logo/LUCIT-UBS-License-Offer.png)](https://shop.lucit.services)
77-
7877
[![GitHub Release](https://img.shields.io/github/release/LUCIT-Systems-and-Development/unicorn-binance-websocket-api.svg?label=github)](https://github.com/LUCIT-Systems-and-Development/unicorn-binance-websocket-api/releases)
7978
[![GitHub Downloads](https://img.shields.io/github/downloads/LUCIT-Systems-and-Development/unicorn-binance-websocket-api/total?color=blue)](https://github.com/LUCIT-Systems-and-Development/unicorn-binance-websocket-api/releases)
8079
![Anaconda Release](https://img.shields.io/conda/v/lucit/unicorn-binance-websocket-api?color=blue)
@@ -122,7 +121,7 @@ about:
122121
123122
## Receive Data from Binance WebSockets
124123
125-
### [Create a multiplex websocket connection](https://unicorn-binance-websocket-api.docs.lucit.tech/unicorn_binance_websocket_api.html#unicorn_binance_websocket_api.manager.BinanceWebSocketApiManager.create_stream) to Binance with a [`stream_buffer`](https://github.com/LUCIT-Systems-and-Development/unicorn-binance-websocket-api/wiki/%60stream_buffer%60) with just 3 lines of code:
124+
### [Create a multiplex websocket connection](https://unicorn-binance-websocket-api.docs.lucit.tech/unicorn_binance_websocket_api.html#unicorn_binance_websocket_api.manager.BinanceWebSocketApiManager.create_stream) to Binance with a [`stream_buffer`](https://github.com/LUCIT-Systems-and-Development/unicorn-binance-websocket-api/wiki/%60stream_buffer%60) with just 3 lines of code
126125
127126
```
128127
from unicorn_binance_websocket_api import BinanceWebSocketApiManager
@@ -131,15 +130,17 @@ about:
131130
ubwa.create_stream(channels=['trade', 'kline_1m'], markets=['btcusdt', 'bnbbtc', 'ethbtc'])
132131
```
133132
134-
### And 4 more lines to print out the data:
133+
### And 4 more lines to print out the data
134+
135135
```
136136
while True:
137137
oldest_data_from_stream_buffer = ubwa.pop_stream_data_from_stream_buffer()
138138
if oldest_data_from_stream_buffer:
139139
print(oldest_data_from_stream_buffer)
140140
```
141141
142-
### Or with a [callback function](https://unicorn-binance-websocket-api.docs.lucit.tech/unicorn_binance_websocket_api.html?highlight=process_stream_data#unicorn_binance_websocket_api.manager.BinanceWebSocketApiManager.create_stream) just do:
142+
### Or with a [callback function](https://unicorn-binance-websocket-api.docs.lucit.tech/unicorn_binance_websocket_api.html?highlight=process_stream_data#unicorn_binance_websocket_api.manager.BinanceWebSocketApiManager.create_stream) just do
143+
143144
```
144145
from unicorn_binance_websocket_api import BinanceWebSocketApiManager
145146
@@ -154,8 +155,10 @@ about:
154155
process_stream_data=process_new_receives)
155156
```
156157
157-
### Or await the webstream data in an asyncio task:
158+
### Or await the webstream data in an asyncio coroutine
159+
158160
This is the recommended method for processing data from web streams.
161+
159162
```
160163
from unicorn_binance_websocket_api import BinanceWebSocketApiManager
161164
import asyncio
@@ -178,17 +181,17 @@ about:
178181
try:
179182
asyncio.run(main())
180183
except KeyboardInterrupt:
181-
print("Gracefully stopping ...")
184+
print("\r\nGracefully stopping ...")
182185
except Exception as error_msg:
183-
print(f"\r\nERROR: {error_msg}")
184-
print("Gracefully stopping ...")
186+
print(f"\r\nERROR: {error_msg}\r\nGracefully stopping ...")
185187
```
186188
187189
Basically that's it, but there are more options.
188190
189-
### Convert received raw webstream data into well-formed Python dictionaries with [UnicornFy](https://www.lucit.tech/unicorn-fy.html):
191+
## Convert received stream data into well-formed Python dictionaries with [UnicornFy](https://www.lucit.tech/unicorn-fy.html)
192+
190193
```
191-
unicorn_fied_stream_data = UnicornFy.binance_com_websocket(oldest_data_from_stream_buffer)
194+
unicorn_fied_stream_data = UnicornFy.binance_com_websocket(data)
192195
```
193196
194197
or
@@ -197,7 +200,8 @@ about:
197200
ubwa.create_stream(['trade'], ['btcusdt'], output="UnicornFy")
198201
```
199202
200-
### [Subscribe](https://unicorn-binance-websocket-api.docs.lucit.tech/unicorn_binance_websocket_api.html#unicorn_binance_websocket_api.manager.BinanceWebSocketApiManager.subscribe_to_stream) / [unsubscribe](https://unicorn-binance-websocket-api.docs.lucit.tech/unicorn_binance_websocket_api.html#unicorn_binance_websocket_api.manager.BinanceWebSocketApiManager.unsubscribe_from_stream) new markets and channels:
203+
## [Subscribe](https://unicorn-binance-websocket-api.docs.lucit.tech/unicorn_binance_websocket_api.html#unicorn_binance_websocket_api.manager.BinanceWebSocketApiManager.subscribe_to_stream) / [unsubscribe](https://unicorn-binance-websocket-api.docs.lucit.tech/unicorn_binance_websocket_api.html#unicorn_binance_websocket_api.manager.BinanceWebSocketApiManager.unsubscribe_from_stream) new markets and channels
204+
201205
```
202206
markets = ['engbtc', 'zileth']
203207
channels = ['kline_5m', 'kline_15m', 'kline_30m', 'kline_1h', 'kline_12h', 'depth5']
@@ -210,35 +214,37 @@ about:
210214
```
211215
212216
## Send Requests to Binance WebSocket API
213-
### [Place orders](https://unicorn-binance-websocket-api.docs.lucit.tech/unicorn_binance_websocket_api.html#unicorn_binance_websocket_api.api.BinanceWebSocketApiApi.create_order), [cancel orders](https://unicorn-binance-websocket-api.docs.lucit.tech/unicorn_binance_websocket_api.html#unicorn_binance_websocket_api.api.BinanceWebSocketApiApi.cancel_order) or [send other requests](https://unicorn-binance-websocket-api.docs.lucit.tech/unicorn_binance_websocket_api.html#module-unicorn_binance_websocket_api.api) via WebSocket:
217+
### [Place orders](https://unicorn-binance-websocket-api.docs.lucit.tech/unicorn_binance_websocket_api.html#unicorn_binance_websocket_api.api.BinanceWebSocketApiApi.create_order), [cancel orders](https://unicorn-binance-websocket-api.docs.lucit.tech/unicorn_binance_websocket_api.html#unicorn_binance_websocket_api.api.BinanceWebSocketApiApi.cancel_order) or [send other requests](https://unicorn-binance-websocket-api.docs.lucit.tech/unicorn_binance_websocket_api.html#module-unicorn_binance_websocket_api.api) via WebSocket
218+
214219
```
215220
from unicorn_binance_websocket_api import BinanceWebSocketApiManager
216221
217-
218-
def process_api_responses(stream_data):
219-
print(str(stream_data))
220-
221-
222222
api_key = "YOUR_BINANCE_API_KEY"
223223
api_secret = "YOUR_BINANCE_API_SECRET"
224224
225+
async def process_api_responses(stream_id=None):
226+
while ubwa.is_stop_request(stream_id=stream_id) is False:
227+
data = await ubwa.get_stream_data_from_asyncio_queue(stream_id=stream_id)
228+
print(data)
229+
ubwa.asyncio_queue_task_done(stream_id=stream_id)
225230
226231
ubwa = BinanceWebSocketApiManager(exchange="binance.com")
227-
api_stream = ubwa.create_stream(api=True,
228-
api_key=api_key,
232+
api_stream = ubwa.create_stream(api=True,
233+
api_key=api_key,
229234
api_secret=api_secret,
230-
process_stream_data=process_api_responses)
231-
232-
response = ubwa.api.get_listen_key(return_response=True))
233-
print(response['result']['listenKey'])
234-
235-
orig_client_order_id = ubwa.api.create_order(order_type="LIMIT",
236-
price=1.1,
237-
quantity=15.0,
238-
side="SELL",
239-
symbol="BUSDUSDT")
240-
241-
ubwa.api.cancel_order(orig_client_order_id=orig_client_order_id, symbol="BUSDUSDT")
235+
output="UnicornFy",
236+
process_asyncio_queue=process_api_responses)
237+
238+
response = ubwa.api.get_server_time(return_response=True)
239+
print(f"Binance serverTime: {response['result']['serverTime']}")
240+
241+
orig_client_order_id = ubwa.api.create_order(order_type="LIMIT",
242+
price = 1.1,
243+
quantity = 15.0,
244+
side = "SELL",
245+
symbol = "BUSDUSDT")
246+
247+
ubwa.api.cancel_order(orig_client_order_id=orig_client_order_id, symbol="BUSDUSDT")
242248
```
243249
244250
[Here](https://medium.lucit.tech/create-and-cancel-orders-via-websocket-on-binance-7f828831404) you can find a complete
@@ -247,13 +253,48 @@ about:
247253
248254
## Stop `ubwa` after usage to avoid memory leaks
249255
256+
When you instantiate UBWA with `with`, `ubwa.stop_manager()` is automatically executed upon exiting the `with`-block.
257+
258+
```
259+
with BinanceWebSocketApiManager() as ubwa:
260+
ubwa.create_stream(channels="trade", markets="btcusdt", stream_label="TRADES")
261+
```
262+
263+
Without `with`, you must explicitly execute `ubwa.stop_manager()` yourself.
264+
250265
```
251266
ubwa.stop_manager()
252267
```
253268
254-
[Discover even more possibilities](https://unicorn-binance-websocket-api.docs.lucit.tech/unicorn_binance_websocket_api.html)
255-
or [use this script](https://github.com/LUCIT-Systems-and-Development/unicorn-binance-websocket-api/blob/master/example_stream_everything.py)
256-
to stream everything from "binance.com".
269+
## `stream_signals` - know the state of your streams
270+
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
271+
currently "blind" and you may want to close open positions to be on the safe side, know that indicators will now provide
272+
incorrect values or that you have to reload the missing data via REST as an alternative.
273+
274+
For this purpose, the UNICORN Binance WebSocket API provides so-called
275+
[`stream_signals`](https://github.com/LUCIT-Systems-and-Development/unicorn-binance-websocket-api/wiki/%60stream_signals%60)
276+
, which are used to tell your code in real time when a stream is connected, when it received its first data record, when
277+
it was disconnected and stopped, and when the stream cannot be restored.
278+
279+
```
280+
from unicorn_binance_websocket_api import BinanceWebSocketApiManager
281+
import time
282+
283+
def process_stream_signals(signal_type=None, stream_id=None, data_record=None, error_msg=None):
284+
print(f"Received stream_signal for stream '{ubwa.get_stream_label(stream_id=stream_id)}': "
285+
f"{signal_type} - {stream_id} - {data_record} - {error_msg}")
286+
287+
with BinanceWebSocketApiManager(process_stream_signals=process_stream_signals) as ubwa:
288+
ubwa.create_stream(channels="trade", markets="btcusdt", stream_label="TRADES")
289+
time.sleep(2)
290+
print(f"Waiting 5 seconds and then stop the stream ...")
291+
time.sleep(5)
292+
```
293+
294+
## More?
295+
296+
[Discover even more possibilities](https://unicorn-binance-websocket-api.docs.lucit.tech/unicorn_binance_websocket_api.html), [use this script](https://github.com/LUCIT-Systems-and-Development/unicorn-binance-websocket-api/blob/master/example_stream_everything.py)
297+
to stream everything from "binance.com" or try our [examples](#examples)!
257298
258299
This should be known by everyone using this lib:
259300

0 commit comments

Comments
 (0)