Skip to content

Commit 497ed6a

Browse files
futues WS API
1 parent adea2bc commit 497ed6a

File tree

14 files changed

+215
-22
lines changed

14 files changed

+215
-22
lines changed

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,14 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
99

1010
[How to upgrade to the latest version!](https://unicorn-binance-websocket-api.docs.lucit.tech/readme.html#installation-and-upgrade)
1111

12-
## 2.8.1.dev (development stage/unreleased/unstable)
12+
## 2.9.0.dev (development stage/unreleased/unstable)
13+
14+
## 2.9.0
15+
### Added
16+
- Support for 'binance.com-futures' WebSocket API.
17+
- Error handling if someone tries to use the WebSocket API Feature with an unsupported exchange.
18+
### Fixed
19+
- Type Error of returned value of `api.create_order()` and `api.create_test_order()`.
1320

1421
## 2.8.1
1522
### Changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,8 @@ Use the [UNICORN Binance REST API](https://www.lucit.tech/unicorn-binance-rest-a
290290
| [Binance Margin Testnet](https://testnet.binance.vision/) | `binance.com-margin-testnet` | ![yes](https://raw.githubusercontent.com/lucit-systems-and-development/unicorn-binance-websocket-api/master/images/misc/ok-icon.png) | ![no](https://raw.githubusercontent.com/lucit-systems-and-development/unicorn-binance-websocket-api/master/images/misc/x-icon.png) |
291291
| [Binance Isolated Margin](https://www.binance.com) | `binance.com-isolated_margin` | ![yes](https://raw.githubusercontent.com/lucit-systems-and-development/unicorn-binance-websocket-api/master/images/misc/ok-icon.png) | ![no](https://raw.githubusercontent.com/lucit-systems-and-development/unicorn-binance-websocket-api/master/images/misc/x-icon.png) |
292292
| [Binance Isolated Margin Testnet](https://testnet.binance.vision/) | `binance.com-isolated_margin-testnet` | ![yes](https://raw.githubusercontent.com/lucit-systems-and-development/unicorn-binance-websocket-api/master/images/misc/ok-icon.png) | ![no](https://raw.githubusercontent.com/lucit-systems-and-development/unicorn-binance-websocket-api/master/images/misc/x-icon.png) |
293-
| [Binance USD-M Futures](https://www.binance.com) | `binance.com-futures` | ![yes](https://raw.githubusercontent.com/lucit-systems-and-development/unicorn-binance-websocket-api/master/images/misc/ok-icon.png) | ![no](https://raw.githubusercontent.com/lucit-systems-and-development/unicorn-binance-websocket-api/master/images/misc/x-icon.png) |
294-
| [Binance USD-M Futures Testnet](https://testnet.binancefuture.com) | `binance.com-futures-testnet` | ![yes](https://raw.githubusercontent.com/lucit-systems-and-development/unicorn-binance-websocket-api/master/images/misc/ok-icon.png) | ![no](https://raw.githubusercontent.com/lucit-systems-and-development/unicorn-binance-websocket-api/master/images/misc/x-icon.png) |
293+
| [Binance USD-M Futures](https://www.binance.com) | `binance.com-futures` | ![yes](https://raw.githubusercontent.com/lucit-systems-and-development/unicorn-binance-websocket-api/master/images/misc/ok-icon.png) | ![yes](https://raw.githubusercontent.com/lucit-systems-and-development/unicorn-binance-websocket-api/master/images/misc/ok-icon.png) |
294+
| [Binance USD-M Futures Testnet](https://testnet.binancefuture.com) | `binance.com-futures-testnet` | ![yes](https://raw.githubusercontent.com/lucit-systems-and-development/unicorn-binance-websocket-api/master/images/misc/ok-icon.png) | ![yes](https://raw.githubusercontent.com/lucit-systems-and-development/unicorn-binance-websocket-api/master/images/misc/ok-icon.png) |
295295
| [Binance Coin-M Futures](https://www.binance.com) | `binance.com-coin_futures` | ![yes](https://raw.githubusercontent.com/lucit-systems-and-development/unicorn-binance-websocket-api/master/images/misc/ok-icon.png) | ![no](https://raw.githubusercontent.com/lucit-systems-and-development/unicorn-binance-websocket-api/master/images/misc/x-icon.png) |
296296
| [Binance US](https://www.binance.us) | `binance.us` | ![yes](https://raw.githubusercontent.com/lucit-systems-and-development/unicorn-binance-websocket-api/master/images/misc/ok-icon.png) | ![no](https://raw.githubusercontent.com/lucit-systems-and-development/unicorn-binance-websocket-api/master/images/misc/x-icon.png) |
297297
| [Binance TR](https://www.trbinance.com) | `trbinance.com` | ![yes](https://raw.githubusercontent.com/lucit-systems-and-development/unicorn-binance-websocket-api/master/images/misc/ok-icon.png) | ![no](https://raw.githubusercontent.com/lucit-systems-and-development/unicorn-binance-websocket-api/master/images/misc/x-icon.png) |

binance_websocket_api_futures.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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()

dev/test_websocket_api_orders.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33
#
4-
# File: example_websocket_api.py
4+
# File: binance_websocket_api_futures.py
55
#
66
# Part of ‘UNICORN Binance WebSocket API’
77
# Project website: https://www.lucit.tech/unicorn-binance-websocket-api.html

dev/test_websocket_api_speed.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33
#
4-
# File: example_websocket_api.py
4+
# File: binance_websocket_api_futures.py
55
#
66
# Part of ‘UNICORN Binance WebSocket API’
77
# Project website: https://www.lucit.tech/unicorn-binance-websocket-api.html

examples/_archive/example_websocket_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33
#
4-
# File: example_websocket_api.py
4+
# File: binance_websocket_api_futures.py
55
#
66
# Part of ‘UNICORN Binance WebSocket API’
77
# Project website: https://www.lucit.tech/unicorn-binance-websocket-api.html
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Binance WebSocket API
2+
## Overview
3+
A best practice example for websockets to Binance in Python.
4+
5+
## Prerequisites
6+
Ensure you have Python 3.7+ installed on your system.
7+
8+
Before running the provided script, install the required Python packages:
9+
```bash
10+
pip install -r requirements.txt
11+
```
12+
## Get a UNICORN Binance Suite License
13+
To run modules of the *UNICORN Binance Suite* you need a [valid license](https://shop.lucit.services)!
14+
15+
## Usage
16+
### Running the Script:
17+
```bash
18+
python binance_chain_websocket_best_practice.py
19+
```
20+
21+
### Graceful Shutdown:
22+
The script is designed to handle a graceful shutdown upon receiving a KeyboardInterrupt (e.g., Ctrl+C) or encountering
23+
an unexpected exception.
24+
25+
## Logging
26+
The script employs logging to provide insights into its operation and to assist in troubleshooting. Logs are saved to a
27+
file named after the script with a .log extension.
28+
29+
For further assistance or to report issues, please [contact our support team](https://www.lucit.tech/get-support.html)
30+
or [visit our GitHub repository](https://github.com/LUCIT-Systems-and-Development/unicorn-binance-websocket-api).
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
unicorn-binance-websocket-api
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Binance WebSocket API
2+
## Overview
3+
A best practice example for websockets to Binance in Python.
4+
5+
## Prerequisites
6+
Ensure you have Python 3.7+ installed on your system.
7+
8+
Before running the provided script, install the required Python packages:
9+
```bash
10+
pip install -r requirements.txt
11+
```
12+
## Get a UNICORN Binance Suite License
13+
To run modules of the *UNICORN Binance Suite* you need a [valid license](https://shop.lucit.services)!
14+
15+
## Usage
16+
### Running the Script:
17+
```bash
18+
python binance_chain_websocket_best_practice.py
19+
```
20+
21+
### Graceful Shutdown:
22+
The script is designed to handle a graceful shutdown upon receiving a KeyboardInterrupt (e.g., Ctrl+C) or encountering
23+
an unexpected exception.
24+
25+
## Logging
26+
The script employs logging to provide insights into its operation and to assist in troubleshooting. Logs are saved to a
27+
file named after the script with a .log extension.
28+
29+
For further assistance or to report issues, please [contact our support team](https://www.lucit.tech/get-support.html)
30+
or [visit our GitHub repository](https://github.com/LUCIT-Systems-and-Development/unicorn-binance-websocket-api).
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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')
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

Comments
 (0)