|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# ¯\_(ツ)_/¯ |
| 4 | + |
| 5 | +from unicorn_binance_websocket_api import BinanceWebSocketApiManager |
| 6 | +import asyncio |
| 7 | +import logging |
| 8 | +import os |
| 9 | + |
| 10 | +api_key = "" |
| 11 | +api_secret = "" |
| 12 | + |
| 13 | + |
| 14 | +async def binance_stream(ubwa): |
| 15 | + async def handle_socket_message(stream_id=None): |
| 16 | + while ubwa.is_stop_request(stream_id=stream_id) is False: |
| 17 | + data = await ubwa.get_stream_data_from_asyncio_queue(stream_id=stream_id) |
| 18 | + print(f"received data:\r\n{data}\r\n") |
| 19 | + |
| 20 | + api_stream = ubwa.create_stream(api=True, api_key=api_key, api_secret=api_secret, |
| 21 | + stream_label="Bobs Websocket API", |
| 22 | + process_asyncio_queue=handle_socket_message) |
| 23 | + print(f"Start:") |
| 24 | + ubwa.api.get_listen_key(stream_id=api_stream) |
| 25 | + ubwa.api.get_server_time(stream_id=api_stream) |
| 26 | + ubwa.api.get_account_status(stream_id=api_stream) |
| 27 | + orig_client_order_id = ubwa.api.create_order(stream_id=api_stream, price=1.0, order_type="LIMIT", |
| 28 | + quantity=15.0, side="SELL", symbol="BUSDUSDT") |
| 29 | + ubwa.api.create_test_order(stream_id=api_stream, price=1.2, order_type="LIMIT", |
| 30 | + quantity=12.0, side="SELL", symbol="BUSDUSDT") |
| 31 | + ubwa.api.ping(stream_id=api_stream) |
| 32 | + ubwa.api.get_exchange_info(stream_id=api_stream, symbols=['BUSDUSDT']) |
| 33 | + ubwa.api.get_order_book(stream_id=api_stream, symbol="BUSDUSDT", limit=2) |
| 34 | + ubwa.api.cancel_order(stream_id=api_stream, symbol="BUSDUSDT", orig_client_order_id=orig_client_order_id) |
| 35 | + ubwa.api.get_open_orders(stream_id=api_stream, symbol="BUSDUSDT") |
| 36 | + ubwa.api.get_open_orders(stream_id=api_stream) |
| 37 | + ubwa.api.cancel_open_orders(stream_id=api_stream, symbol="BUSDUSDT") |
| 38 | + ubwa.api.get_order(stream_id=api_stream, symbol="BUSDUSDT", orig_client_order_id=orig_client_order_id) |
| 39 | + |
| 40 | + print(f"Finished! Waiting for responses:") |
| 41 | + await asyncio.sleep(5) |
| 42 | + |
| 43 | + print(f"Stopping!") |
| 44 | + ubwa.stop_manager() |
| 45 | + |
| 46 | +if __name__ == "__main__": |
| 47 | + logging.basicConfig(level=logging.DEBUG, |
| 48 | + filename=os.path.basename(__file__) + '.log', |
| 49 | + format="{asctime} [{levelname:8}] {process} {thread} {module}: {message}", |
| 50 | + style="{") |
| 51 | + |
| 52 | + # To use this library you need a valid UNICORN Binance Suite License: |
| 53 | + # https://shop.lucit.services |
| 54 | + ubwa = BinanceWebSocketApiManager(exchange='binance.com-futures') |
| 55 | + try: |
| 56 | + asyncio.run(binance_stream(ubwa)) |
| 57 | + except KeyboardInterrupt: |
| 58 | + print("\r\nGracefully stopping the websocket manager...") |
| 59 | + ubwa.stop_manager() |
0 commit comments