-
-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
adea2bc
commit 497ed6a
Showing
14 changed files
with
215 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
# ¯\_(ツ)_/¯ | ||
|
||
from unicorn_binance_websocket_api import BinanceWebSocketApiManager | ||
import asyncio | ||
import logging | ||
import os | ||
|
||
api_key = "" | ||
api_secret = "" | ||
|
||
|
||
async def binance_stream(ubwa): | ||
async def handle_socket_message(stream_id=None): | ||
while ubwa.is_stop_request(stream_id=stream_id) is False: | ||
data = await ubwa.get_stream_data_from_asyncio_queue(stream_id=stream_id) | ||
print(f"received data:\r\n{data}\r\n") | ||
|
||
api_stream = ubwa.create_stream(api=True, api_key=api_key, api_secret=api_secret, | ||
stream_label="Bobs Websocket API", | ||
process_asyncio_queue=handle_socket_message) | ||
print(f"Start:") | ||
ubwa.api.get_listen_key(stream_id=api_stream) | ||
ubwa.api.get_server_time(stream_id=api_stream) | ||
ubwa.api.get_account_status(stream_id=api_stream) | ||
orig_client_order_id = ubwa.api.create_order(stream_id=api_stream, price=1.0, order_type="LIMIT", | ||
quantity=15.0, side="SELL", symbol="BUSDUSDT") | ||
ubwa.api.create_test_order(stream_id=api_stream, price=1.2, order_type="LIMIT", | ||
quantity=12.0, side="SELL", symbol="BUSDUSDT") | ||
ubwa.api.ping(stream_id=api_stream) | ||
ubwa.api.get_exchange_info(stream_id=api_stream, symbols=['BUSDUSDT']) | ||
ubwa.api.get_order_book(stream_id=api_stream, symbol="BUSDUSDT", limit=2) | ||
ubwa.api.cancel_order(stream_id=api_stream, symbol="BUSDUSDT", orig_client_order_id=orig_client_order_id) | ||
ubwa.api.get_open_orders(stream_id=api_stream, symbol="BUSDUSDT") | ||
ubwa.api.get_open_orders(stream_id=api_stream) | ||
ubwa.api.cancel_open_orders(stream_id=api_stream, symbol="BUSDUSDT") | ||
ubwa.api.get_order(stream_id=api_stream, symbol="BUSDUSDT", orig_client_order_id=orig_client_order_id) | ||
|
||
print(f"Finished! Waiting for responses:") | ||
await asyncio.sleep(5) | ||
|
||
print(f"Stopping!") | ||
ubwa.stop_manager() | ||
|
||
if __name__ == "__main__": | ||
logging.basicConfig(level=logging.DEBUG, | ||
filename=os.path.basename(__file__) + '.log', | ||
format="{asctime} [{levelname:8}] {process} {thread} {module}: {message}", | ||
style="{") | ||
|
||
# To use this library you need a valid UNICORN Binance Suite License: | ||
# https://shop.lucit.services | ||
ubwa = BinanceWebSocketApiManager(exchange='binance.com-futures') | ||
try: | ||
asyncio.run(binance_stream(ubwa)) | ||
except KeyboardInterrupt: | ||
print("\r\nGracefully stopping the websocket manager...") | ||
ubwa.stop_manager() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Binance WebSocket API | ||
## Overview | ||
A best practice example for websockets to Binance in Python. | ||
|
||
## Prerequisites | ||
Ensure you have Python 3.7+ installed on your system. | ||
|
||
Before running the provided script, install the required Python packages: | ||
```bash | ||
pip install -r requirements.txt | ||
``` | ||
## Get a UNICORN Binance Suite License | ||
To run modules of the *UNICORN Binance Suite* you need a [valid license](https://shop.lucit.services)! | ||
|
||
## Usage | ||
### Running the Script: | ||
```bash | ||
python binance_chain_websocket_best_practice.py | ||
``` | ||
|
||
### Graceful Shutdown: | ||
The script is designed to handle a graceful shutdown upon receiving a KeyboardInterrupt (e.g., Ctrl+C) or encountering | ||
an unexpected exception. | ||
|
||
## Logging | ||
The script employs logging to provide insights into its operation and to assist in troubleshooting. Logs are saved to a | ||
file named after the script with a .log extension. | ||
|
||
For further assistance or to report issues, please [contact our support team](https://www.lucit.tech/get-support.html) | ||
or [visit our GitHub repository](https://github.com/LUCIT-Systems-and-Development/unicorn-binance-websocket-api). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
unicorn-binance-websocket-api |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Binance WebSocket API | ||
## Overview | ||
A best practice example for websockets to Binance in Python. | ||
|
||
## Prerequisites | ||
Ensure you have Python 3.7+ installed on your system. | ||
|
||
Before running the provided script, install the required Python packages: | ||
```bash | ||
pip install -r requirements.txt | ||
``` | ||
## Get a UNICORN Binance Suite License | ||
To run modules of the *UNICORN Binance Suite* you need a [valid license](https://shop.lucit.services)! | ||
|
||
## Usage | ||
### Running the Script: | ||
```bash | ||
python binance_chain_websocket_best_practice.py | ||
``` | ||
|
||
### Graceful Shutdown: | ||
The script is designed to handle a graceful shutdown upon receiving a KeyboardInterrupt (e.g., Ctrl+C) or encountering | ||
an unexpected exception. | ||
|
||
## Logging | ||
The script employs logging to provide insights into its operation and to assist in troubleshooting. Logs are saved to a | ||
file named after the script with a .log extension. | ||
|
||
For further assistance or to report issues, please [contact our support team](https://www.lucit.tech/get-support.html) | ||
or [visit our GitHub repository](https://github.com/LUCIT-Systems-and-Development/unicorn-binance-websocket-api). |
59 changes: 59 additions & 0 deletions
59
examples/binance_websocket_api_spot/binance_websocket_api_spot.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
# ¯\_(ツ)_/¯ | ||
|
||
from unicorn_binance_websocket_api import BinanceWebSocketApiManager | ||
import asyncio | ||
import logging | ||
import os | ||
|
||
api_key = "" | ||
api_secret = "" | ||
|
||
|
||
async def binance_stream(ubwa): | ||
async def handle_socket_message(stream_id=None): | ||
while ubwa.is_stop_request(stream_id=stream_id) is False: | ||
data = await ubwa.get_stream_data_from_asyncio_queue(stream_id=stream_id) | ||
print(f"received data:\r\n{data}\r\n") | ||
|
||
api_stream = ubwa.create_stream(api=True, api_key=api_key, api_secret=api_secret, | ||
stream_label="Bobs Websocket API", | ||
process_asyncio_queue=handle_socket_message) | ||
print(f"Start:") | ||
ubwa.api.get_listen_key(stream_id=api_stream) | ||
ubwa.api.get_server_time(stream_id=api_stream) | ||
ubwa.api.get_account_status(stream_id=api_stream) | ||
orig_client_order_id = ubwa.api.create_order(stream_id=api_stream, price=1.0, order_type="LIMIT", | ||
quantity=15.0, side="SELL", symbol="BUSDUSDT") | ||
ubwa.api.create_test_order(stream_id=api_stream, price=1.2, order_type="LIMIT", | ||
quantity=12.0, side="SELL", symbol="BUSDUSDT") | ||
ubwa.api.ping(stream_id=api_stream) | ||
ubwa.api.get_exchange_info(stream_id=api_stream, symbols=['BUSDUSDT']) | ||
ubwa.api.get_order_book(stream_id=api_stream, symbol="BUSDUSDT", limit=2) | ||
ubwa.api.cancel_order(stream_id=api_stream, symbol="BUSDUSDT", orig_client_order_id=orig_client_order_id) | ||
ubwa.api.get_open_orders(stream_id=api_stream, symbol="BUSDUSDT") | ||
ubwa.api.get_open_orders(stream_id=api_stream) | ||
ubwa.api.cancel_open_orders(stream_id=api_stream, symbol="BUSDUSDT") | ||
ubwa.api.get_order(stream_id=api_stream, symbol="BUSDUSDT", orig_client_order_id=orig_client_order_id) | ||
|
||
print(f"Finished! Waiting for responses:") | ||
await asyncio.sleep(5) | ||
|
||
print(f"Stopping!") | ||
ubwa.stop_manager() | ||
|
||
if __name__ == "__main__": | ||
logging.basicConfig(level=logging.DEBUG, | ||
filename=os.path.basename(__file__) + '.log', | ||
format="{asctime} [{levelname:8}] {process} {thread} {module}: {message}", | ||
style="{") | ||
|
||
# To use this library you need a valid UNICORN Binance Suite License: | ||
# https://shop.lucit.services | ||
ubwa = BinanceWebSocketApiManager(exchange='binance.com') | ||
try: | ||
asyncio.run(binance_stream(ubwa)) | ||
except KeyboardInterrupt: | ||
print("\r\nGracefully stopping the websocket manager...") | ||
ubwa.stop_manager() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
unicorn-binance-websocket-api |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters