Skip to content

Commit

Permalink
orjson, unittest, ...
Browse files Browse the repository at this point in the history
  • Loading branch information
oliver-zehentleitner committed Nov 13, 2024
1 parent d0eaa53 commit 866277f
Show file tree
Hide file tree
Showing 10 changed files with 177 additions and 61 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
- New `Websocket API Spot` functions:
- `manager.api.spot.cancel_and_replace_order()`
- `manager.api.spot.get_aggregate_trades()`
- `manager.api.spot.get_current_average_price()`
- `manager.api.spot.get_historical_trades()`
- `manager.api.spot.get_klines()`
- `manager.api.spot.get_ui_klines()`
Expand All @@ -24,6 +25,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
- ujson has been replaced by orjson
- Websocket API functions are no longer available under `manager.api` but under `manager.api.spot`. In addition, there
is now also `manager.api.futures`.
- No more use of deepcopy in ws api (faster!)

## 2.9.0
### Added
Expand Down
10 changes: 7 additions & 3 deletions binance_websocket_api_spot.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@ async def handle_socket_message(stream_id=None):
data = await ubwa.get_stream_data_from_asyncio_queue(stream_id=stream_id)
print(f"Received data:\r\n{data}\r\n")

print(f"Starting Stream:")
print(f"Starting the stream:")
api_stream = ubwa.create_stream(api=True,
api_key=os.getenv('BINANCE_API_KEY'),
api_secret=os.getenv('BINANCE_API_SECRET'),
stream_label="Bobs Spot Websocket API",
process_asyncio_queue=handle_socket_message)
print(f"Commands")

print(f"Executing API requests:")
current_average_price = ubwa.api.spot.get_current_average_price(stream_id=api_stream, symbol=market, return_response=True)
print(f"current_average_price: {current_average_price}\r\n")

ubwa.api.spot.get_listen_key(stream_id=api_stream)

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

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

ubwa.api.spot.get_order(stream_id=api_stream, symbol=market, orig_client_order_id=orig_client_order_id)
ubwa.api.spot.get_order(stream_id=api_stream, symbol=market, orig_client_order_id=replaced_client_order_id)

print(f"Finished! Waiting for responses:")
await asyncio.sleep(5)
Expand Down
178 changes: 141 additions & 37 deletions unicorn_binance_websocket_api/api/spot.py

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions unicorn_binance_websocket_api/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,13 @@

from .exceptions import *
from urllib.parse import urlparse

import asyncio
import copy
import logging
import socks # PySocks https://pypi.org/project/PySocks/
import sys
import websockets


__logger__: logging.getLogger = logging.getLogger("unicorn_binance_websocket_api")

logger = __logger__
Expand Down
2 changes: 0 additions & 2 deletions unicorn_binance_websocket_api/licensing_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
from .licensing_exceptions import NoValidatedLucitLicense
except ModuleNotFoundError:
from unicorn_binance_websocket_api.licensing_exceptions import NoValidatedLucitLicense

import cython
import hashlib
import hmac
Expand All @@ -41,7 +40,6 @@
import time
import uuid


__logger__: logging.getLogger = logging.getLogger("lucit_licensing_python")

logger = __logger__
Expand Down
10 changes: 1 addition & 9 deletions unicorn_binance_websocket_api/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,7 @@
from flask import Flask, redirect
from flask_restful import Api
from operator import itemgetter
from typing import Optional, Union, Callable, List, Set
try:
# Todo: Remove!
# python <=3.7 support
from typing import Literal
except ImportError:
from typing_extensions import Literal

from typing import Optional, Union, Callable, List, Set, Literal
import asyncio
import colorama
import copy
Expand All @@ -63,7 +56,6 @@
import orjson
import websockets


__app_name__: str = "unicorn-binance-websocket-api"
__version__: str = "2.8.1.dev"
__logger__: logging.getLogger = logging.getLogger("unicorn_binance_websocket_api")
Expand Down
2 changes: 0 additions & 2 deletions unicorn_binance_websocket_api/restclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@

from unicorn_binance_rest_api import BinanceRestApiManager
from typing import Optional, Union, Tuple

import logging
import requests
import threading
import time


__logger__: logging.getLogger = logging.getLogger("unicorn_binance_websocket_api")

logger = __logger__
Expand Down
2 changes: 0 additions & 2 deletions unicorn_binance_websocket_api/restserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@
# All rights reserved.

from flask_restful import Resource

import logging


__logger__: logging.getLogger = logging.getLogger("unicorn_binance_websocket_api")

logger = __logger__
Expand Down
2 changes: 0 additions & 2 deletions unicorn_binance_websocket_api/sockets.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@
from .connection import BinanceWebSocketApiConnection
from .exceptions import *
from unicorn_fy.unicorn_fy import UnicornFy

import asyncio
import orjson
import logging


__logger__: logging.getLogger = logging.getLogger("unicorn_binance_websocket_api")

logger = __logger__
Expand Down
28 changes: 26 additions & 2 deletions unittest_binance_websocket_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,13 +622,32 @@ def test_lucitlicmgr(self):

def test_live_api_ws(self):
print(f"Test Websocket API ...")
market = "BUSDUSDT"
ubwam = BinanceWebSocketApiManager(exchange='binance.com-testnet')
api_stream = ubwam.create_stream(api=True,
api_key=BINANCE_COM_TESTNET_API_KEY,
api_secret=BINANCE_COM_TESTNET_API_SECRET,
stream_label="Bobs Websocket API",
process_stream_data=handle_socket_message)
time.sleep(5)
current_average_price = ubwam.api.spot.get_current_average_price(stream_id=api_stream, symbol=market,
return_response=True)
print(f"current_average_price: {current_average_price}\r\n")
order_book = ubwam.api.spot.get_order_book(stream_id=api_stream, symbol=market, limit=2, return_response=True)
print(f"Orderbook, lastUpdateId={order_book['result']['lastUpdateId']}: {order_book['result']['asks']}, "
f"{order_book['result']['bids']}\r\n")
aggregate_trades = ubwam.api.spot.get_aggregate_trades(stream_id=api_stream, symbol=market, return_response=True)
print(f"aggregate_trades: {aggregate_trades['result'][:5]}\r\n")
historical_trades = ubwam.api.spot.get_historical_trades(stream_id=api_stream, symbol=market,
return_response=True)
print(f"historical_trades: {historical_trades['result'][:5]}\r\n")
recent_trades = ubwam.api.spot.get_recent_trades(stream_id=api_stream, symbol=market, return_response=True)
print(f"recent_trades: {recent_trades['result'][:5]}\r\n")
klines = ubwam.api.spot.get_klines(stream_id=api_stream, symbol=market, interval="1m", return_response=True)
print(f"A few klines: {klines['result'][:5]}\r\n")
ui_klines = ubwam.api.spot.get_ui_klines(stream_id=api_stream, symbol=market, interval="1d",
return_response=True)
print(f"A few ui_klines: {ui_klines['result'][:5]}\r\n")
ubwam.api.spot.get_listen_key(stream_id=api_stream)
ubwam.api.spot.get_server_time(stream_id=api_stream)
ubwam.api.spot.get_account_status(stream_id=api_stream)
Expand All @@ -639,11 +658,16 @@ def test_live_api_ws(self):
ubwam.api.spot.ping(stream_id=api_stream)
ubwam.api.spot.get_exchange_info(stream_id=api_stream, symbols=['BUSDUSDT'])
ubwam.api.spot.get_order_book(stream_id=api_stream, symbol="BUSDUSDT", limit=2)
ubwam.api.spot.cancel_order(stream_id=api_stream, symbol="BUSDUSDT", orig_client_order_id=orig_client_order_id)
replaced_client_order_id = ubwam.api.spot.cancel_and_replace_order(stream_id=api_stream, price=1.1,
order_type="LIMIT",
quantity=15.0, side="SELL", symbol=market,
cancel_orig_client_order_id=orig_client_order_id)
ubwam.api.spot.cancel_order(stream_id=api_stream, symbol="BUSDUSDT",
orig_client_order_id=replaced_client_order_id)
ubwam.api.spot.get_open_orders(stream_id=api_stream, symbol="BUSDUSDT")
ubwam.api.spot.get_open_orders(stream_id=api_stream)
ubwam.api.spot.cancel_open_orders(stream_id=api_stream, symbol="BUSDUSDT")
ubwam.api.spot.get_order(stream_id=api_stream, symbol="BUSDUSDT", orig_client_order_id=orig_client_order_id)
ubwam.api.spot.get_order(stream_id=api_stream, symbol="BUSDUSDT", orig_client_order_id=replaced_client_order_id)
time.sleep(5)
ubwam.stop_manager()

Expand Down

0 comments on commit 866277f

Please sign in to comment.