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 983a19d commit d0eaa53
Show file tree
Hide file tree
Showing 13 changed files with 159 additions and 107 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
- Support for `Websocket API Futures`:
- `manager.api.futures.get_aggregate_trades()`
### Changed
- 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`.

Expand Down
3 changes: 2 additions & 1 deletion binance_websocket_api_futures.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ 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:")
api_stream = ubwa.create_stream(api=True,
api_key=os.getenv('BINANCE_API_KEY'),
api_secret=os.getenv('BINANCE_API_SECRET'),
Expand All @@ -28,7 +29,7 @@ async def handle_socket_message(stream_id=None):
orig_client_order_id = ubwa.api.futures.create_order(stream_id=api_stream, price=1.0, order_type="LIMIT",
quantity=15.0, side="SELL", symbol=market)
ubwa.api.futures.ping(stream_id=api_stream)
ubwa.api.futures.get_order_book(stream_id=api_stream, symbol=market, limit=2)
orderbook = ubwa.api.futures.get_order_book(stream_id=api_stream, symbol=market, limit=2, return_response=True)
ubwa.api.futures.cancel_order(stream_id=api_stream, symbol=market, orig_client_order_id=orig_client_order_id)
ubwa.api.futures.get_order(stream_id=api_stream, symbol=market, orig_client_order_id=orig_client_order_id)

Expand Down
59 changes: 48 additions & 11 deletions binance_websocket_api_spot.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ async def binance_api(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")
print(f"Received data:\r\n{data}\r\n")

print(f"Starting Stream:")
api_stream = ubwa.create_stream(api=True,
api_key=os.getenv('BINANCE_API_KEY'),
Expand All @@ -23,24 +24,58 @@ async def handle_socket_message(stream_id=None):
process_asyncio_queue=handle_socket_message)
print(f"Commands")
ubwa.api.spot.get_listen_key(stream_id=api_stream)

ubwa.api.spot.get_server_time(stream_id=api_stream)
ubwa.api.spot.get_account_status(stream_id=api_stream)

server_time = ubwa.api.spot.get_server_time(stream_id=api_stream, return_response=True)
print(f"Server Time: {server_time['result']['serverTime']}\r\n")

account_status = ubwa.api.spot.get_account_status(stream_id=api_stream, return_response=True)
print(f"Status of account_status request: {account_status['status']}\r\n")

orig_client_order_id = ubwa.api.spot.create_order(stream_id=api_stream, price=1.0, order_type="LIMIT",
quantity=15.0, side="SELL", symbol=market)

ubwa.api.spot.create_test_order(stream_id=api_stream, price=1.2, order_type="LIMIT",
quantity=12.0, side="SELL", symbol=market)

ubwa.api.spot.ping(stream_id=api_stream)
ubwa.api.spot.get_exchange_info(stream_id=api_stream, symbols=[market, ])
ubwa.api.spot.get_order_book(stream_id=api_stream, symbol=market, limit=2)
ubwa.api.spot.get_aggregate_trades(stream_id=api_stream, symbol=market)
ubwa.api.spot.get_historical_trades(stream_id=api_stream, symbol=market)
ubwa.api.spot.get_klines(stream_id=api_stream, symbol=market, interval="1m")
ubwa.api.spot.get_ui_klines(stream_id=api_stream, symbol=market, interval="1d")
ubwa.api.spot.get_recent_trades(stream_id=api_stream, symbol=market)
ubwa.api.spot.cancel_order(stream_id=api_stream, symbol=market, orig_client_order_id=orig_client_order_id)

exchange_info = ubwa.api.spot.get_exchange_info(stream_id=api_stream, symbols=[market, ], return_response=True)
print(f"Status of exchange_info request: {exchange_info['status']}\r\n")

order_book = ubwa.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 = ubwa.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 = ubwa.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 = ubwa.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 = ubwa.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 = ubwa.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")

replaced_client_order_id = ubwa.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)

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

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

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

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)

print(f"Finished! Waiting for responses:")
Expand All @@ -54,11 +89,13 @@ async def handle_socket_message(stream_id=None):
filename=os.path.basename(__file__) + '.log',
format="{asctime} [{levelname:8}] {process} {thread} {module}: {message}",
style="{")

# Loading os.getenv() vars from .env
load_dotenv()

# To use this library you need a valid UNICORN Binance Suite License:
# https://shop.lucit.services
with BinanceWebSocketApiManager(exchange='binance.com') as ubwa_manager:
with BinanceWebSocketApiManager(exchange='binance.com', output_default="dict") as ubwa_manager:
try:
asyncio.run(binance_api(ubwa_manager))
except KeyboardInterrupt:
Expand Down
1 change: 0 additions & 1 deletion dev/test_websocket_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import logging
import os
import time
import ujson as json

api_key = ""
api_secret = ""
Expand Down
3 changes: 2 additions & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ dependencies:
- Cython
- flask
- flask-restful
- orjson
- psutil
- PySocks
- requests>=2.31.0
- ujson
- simplejson
- websocket-client
- websockets==11.0.3
- typing_extensions
6 changes: 4 additions & 2 deletions meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ requirements:
- Cython
- flask
- flask-restful
- orjson
- psutil
- PySocks
- requests >=2.31.0
- ujson
- simplejson
- websocket-client
- websockets==11.0.3
- typing_extensions
Expand All @@ -51,10 +52,11 @@ requirements:
- Cython
- flask
- flask-restful
- orjson
- psutil
- PySocks
- requests >=2.31.0
- ujson
- simplejson
- websocket-client
- websockets==11.0.3
- typing_extensions
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ Cython = "*"
flask = "*"
flask_restful = "*"
lucit-licensing-python = ">=1.8.2"
orjson = "*"
psutil = "*"
PySocks = "*"
requests = ">=2.31.0"
ujson = "*"
simplejson = "*"
unicorn-fy = ">=0.14.2"
unicorn-binance-rest-api = ">=2.5.1"
websocket-client = "*"
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ psutil
PySocks
requests>=2.31.0
simplejson
ujson
orjson
unicorn-fy>=0.14.2
unicorn-binance-rest-api>=2.5.1
websocket-client
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
long_description_content_type="text/markdown",
license='LSOSL - LUCIT Synergetic Open Source License',
install_requires=['colorama', 'requests>=2.31.0', 'websocket-client', 'websockets==11.0.3', 'flask_restful',
'cheroot', 'flask', 'lucit-licensing-python>=1.8.2', 'ujson', 'psutil', 'PySocks',
'cheroot', 'flask', 'lucit-licensing-python>=1.8.2', 'orjson', 'psutil', 'PySocks', 'simplejson',
'unicorn-fy>=0.14.2', 'unicorn-binance-rest-api>=2.5.1', 'typing_extensions', 'Cython'],
keywords='binance, asyncio, async, asynchronous, concurrent, websocket-api, webstream-api, '
'binance-websocket, binance-webstream, webstream, websocket, api, binance-dex, '
Expand Down
Loading

0 comments on commit d0eaa53

Please sign in to comment.