Skip to content

Commit 866277f

Browse files
orjson, unittest, ...
1 parent d0eaa53 commit 866277f

File tree

10 files changed

+177
-61
lines changed

10 files changed

+177
-61
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
1414
- New `Websocket API Spot` functions:
1515
- `manager.api.spot.cancel_and_replace_order()`
1616
- `manager.api.spot.get_aggregate_trades()`
17+
- `manager.api.spot.get_current_average_price()`
1718
- `manager.api.spot.get_historical_trades()`
1819
- `manager.api.spot.get_klines()`
1920
- `manager.api.spot.get_ui_klines()`
@@ -24,6 +25,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
2425
- ujson has been replaced by orjson
2526
- Websocket API functions are no longer available under `manager.api` but under `manager.api.spot`. In addition, there
2627
is now also `manager.api.futures`.
28+
- No more use of deepcopy in ws api (faster!)
2729

2830
## 2.9.0
2931
### Added

binance_websocket_api_spot.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,17 @@ async def handle_socket_message(stream_id=None):
1616
data = await ubwa.get_stream_data_from_asyncio_queue(stream_id=stream_id)
1717
print(f"Received data:\r\n{data}\r\n")
1818

19-
print(f"Starting Stream:")
19+
print(f"Starting the stream:")
2020
api_stream = ubwa.create_stream(api=True,
2121
api_key=os.getenv('BINANCE_API_KEY'),
2222
api_secret=os.getenv('BINANCE_API_SECRET'),
2323
stream_label="Bobs Spot Websocket API",
2424
process_asyncio_queue=handle_socket_message)
25-
print(f"Commands")
25+
26+
print(f"Executing API requests:")
27+
current_average_price = ubwa.api.spot.get_current_average_price(stream_id=api_stream, symbol=market, return_response=True)
28+
print(f"current_average_price: {current_average_price}\r\n")
29+
2630
ubwa.api.spot.get_listen_key(stream_id=api_stream)
2731

2832
ubwa.api.spot.get_server_time(stream_id=api_stream)
@@ -76,7 +80,7 @@ async def handle_socket_message(stream_id=None):
7680

7781
ubwa.api.spot.cancel_open_orders(stream_id=api_stream, symbol=market)
7882

79-
ubwa.api.spot.get_order(stream_id=api_stream, symbol=market, orig_client_order_id=orig_client_order_id)
83+
ubwa.api.spot.get_order(stream_id=api_stream, symbol=market, orig_client_order_id=replaced_client_order_id)
8084

8185
print(f"Finished! Waiting for responses:")
8286
await asyncio.sleep(5)

unicorn_binance_websocket_api/api/spot.py

Lines changed: 141 additions & 37 deletions
Large diffs are not rendered by default.

unicorn_binance_websocket_api/connection.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,13 @@
2121

2222
from .exceptions import *
2323
from urllib.parse import urlparse
24-
2524
import asyncio
2625
import copy
2726
import logging
2827
import socks # PySocks https://pypi.org/project/PySocks/
2928
import sys
3029
import websockets
3130

32-
3331
__logger__: logging.getLogger = logging.getLogger("unicorn_binance_websocket_api")
3432

3533
logger = __logger__

unicorn_binance_websocket_api/licensing_manager.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
from .licensing_exceptions import NoValidatedLucitLicense
3030
except ModuleNotFoundError:
3131
from unicorn_binance_websocket_api.licensing_exceptions import NoValidatedLucitLicense
32-
3332
import cython
3433
import hashlib
3534
import hmac
@@ -41,7 +40,6 @@
4140
import time
4241
import uuid
4342

44-
4543
__logger__: logging.getLogger = logging.getLogger("lucit_licensing_python")
4644

4745
logger = __logger__

unicorn_binance_websocket_api/manager.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,7 @@
3434
from flask import Flask, redirect
3535
from flask_restful import Api
3636
from operator import itemgetter
37-
from typing import Optional, Union, Callable, List, Set
38-
try:
39-
# Todo: Remove!
40-
# python <=3.7 support
41-
from typing import Literal
42-
except ImportError:
43-
from typing_extensions import Literal
44-
37+
from typing import Optional, Union, Callable, List, Set, Literal
4538
import asyncio
4639
import colorama
4740
import copy
@@ -63,7 +56,6 @@
6356
import orjson
6457
import websockets
6558

66-
6759
__app_name__: str = "unicorn-binance-websocket-api"
6860
__version__: str = "2.8.1.dev"
6961
__logger__: logging.getLogger = logging.getLogger("unicorn_binance_websocket_api")

unicorn_binance_websocket_api/restclient.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,11 @@
2121

2222
from unicorn_binance_rest_api import BinanceRestApiManager
2323
from typing import Optional, Union, Tuple
24-
2524
import logging
2625
import requests
2726
import threading
2827
import time
2928

30-
3129
__logger__: logging.getLogger = logging.getLogger("unicorn_binance_websocket_api")
3230

3331
logger = __logger__

unicorn_binance_websocket_api/restserver.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@
2020
# All rights reserved.
2121

2222
from flask_restful import Resource
23-
2423
import logging
2524

26-
2725
__logger__: logging.getLogger = logging.getLogger("unicorn_binance_websocket_api")
2826

2927
logger = __logger__

unicorn_binance_websocket_api/sockets.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,10 @@
2222
from .connection import BinanceWebSocketApiConnection
2323
from .exceptions import *
2424
from unicorn_fy.unicorn_fy import UnicornFy
25-
2625
import asyncio
2726
import orjson
2827
import logging
2928

30-
3129
__logger__: logging.getLogger = logging.getLogger("unicorn_binance_websocket_api")
3230

3331
logger = __logger__

unittest_binance_websocket_api.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -622,13 +622,32 @@ def test_lucitlicmgr(self):
622622

623623
def test_live_api_ws(self):
624624
print(f"Test Websocket API ...")
625+
market = "BUSDUSDT"
625626
ubwam = BinanceWebSocketApiManager(exchange='binance.com-testnet')
626627
api_stream = ubwam.create_stream(api=True,
627628
api_key=BINANCE_COM_TESTNET_API_KEY,
628629
api_secret=BINANCE_COM_TESTNET_API_SECRET,
629630
stream_label="Bobs Websocket API",
630631
process_stream_data=handle_socket_message)
631632
time.sleep(5)
633+
current_average_price = ubwam.api.spot.get_current_average_price(stream_id=api_stream, symbol=market,
634+
return_response=True)
635+
print(f"current_average_price: {current_average_price}\r\n")
636+
order_book = ubwam.api.spot.get_order_book(stream_id=api_stream, symbol=market, limit=2, return_response=True)
637+
print(f"Orderbook, lastUpdateId={order_book['result']['lastUpdateId']}: {order_book['result']['asks']}, "
638+
f"{order_book['result']['bids']}\r\n")
639+
aggregate_trades = ubwam.api.spot.get_aggregate_trades(stream_id=api_stream, symbol=market, return_response=True)
640+
print(f"aggregate_trades: {aggregate_trades['result'][:5]}\r\n")
641+
historical_trades = ubwam.api.spot.get_historical_trades(stream_id=api_stream, symbol=market,
642+
return_response=True)
643+
print(f"historical_trades: {historical_trades['result'][:5]}\r\n")
644+
recent_trades = ubwam.api.spot.get_recent_trades(stream_id=api_stream, symbol=market, return_response=True)
645+
print(f"recent_trades: {recent_trades['result'][:5]}\r\n")
646+
klines = ubwam.api.spot.get_klines(stream_id=api_stream, symbol=market, interval="1m", return_response=True)
647+
print(f"A few klines: {klines['result'][:5]}\r\n")
648+
ui_klines = ubwam.api.spot.get_ui_klines(stream_id=api_stream, symbol=market, interval="1d",
649+
return_response=True)
650+
print(f"A few ui_klines: {ui_klines['result'][:5]}\r\n")
632651
ubwam.api.spot.get_listen_key(stream_id=api_stream)
633652
ubwam.api.spot.get_server_time(stream_id=api_stream)
634653
ubwam.api.spot.get_account_status(stream_id=api_stream)
@@ -639,11 +658,16 @@ def test_live_api_ws(self):
639658
ubwam.api.spot.ping(stream_id=api_stream)
640659
ubwam.api.spot.get_exchange_info(stream_id=api_stream, symbols=['BUSDUSDT'])
641660
ubwam.api.spot.get_order_book(stream_id=api_stream, symbol="BUSDUSDT", limit=2)
642-
ubwam.api.spot.cancel_order(stream_id=api_stream, symbol="BUSDUSDT", orig_client_order_id=orig_client_order_id)
661+
replaced_client_order_id = ubwam.api.spot.cancel_and_replace_order(stream_id=api_stream, price=1.1,
662+
order_type="LIMIT",
663+
quantity=15.0, side="SELL", symbol=market,
664+
cancel_orig_client_order_id=orig_client_order_id)
665+
ubwam.api.spot.cancel_order(stream_id=api_stream, symbol="BUSDUSDT",
666+
orig_client_order_id=replaced_client_order_id)
643667
ubwam.api.spot.get_open_orders(stream_id=api_stream, symbol="BUSDUSDT")
644668
ubwam.api.spot.get_open_orders(stream_id=api_stream)
645669
ubwam.api.spot.cancel_open_orders(stream_id=api_stream, symbol="BUSDUSDT")
646-
ubwam.api.spot.get_order(stream_id=api_stream, symbol="BUSDUSDT", orig_client_order_id=orig_client_order_id)
670+
ubwam.api.spot.get_order(stream_id=api_stream, symbol="BUSDUSDT", orig_client_order_id=replaced_client_order_id)
647671
time.sleep(5)
648672
ubwam.stop_manager()
649673

0 commit comments

Comments
 (0)