Skip to content

Add new methods to Binance API class #373

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions binance/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,20 @@ def _handle_exception(self, response):
status_code, err["code"], err["msg"], response.headers, error_data
)
raise ServerError(status_code, response.text)

def get_server_time(self):
"""Fetch the server time from the Binance API"""
url_path = "/api/v3/time"
return self.query(url_path)

def get_exchange_info(self):
"""Fetch exchange information from the Binance API"""
url_path = "/api/v3/exchangeInfo"
return self.query(url_path)

def get_order_book(self, symbol, limit=100):
"""Fetch the order book for a specific symbol from the Binance API"""
check_required_parameter(symbol, "symbol")
url_path = "/api/v3/depth"
payload = {"symbol": symbol, "limit": limit}
return self.query(url_path, payload)
10 changes: 10 additions & 0 deletions binance/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,13 @@ def __init__(self, error_message):

def __str__(self):
return self.error_message


class ExchangeError(Error):
def __init__(self, message):
self.message = message


class OrderBookError(Error):
def __init__(self, message):
self.message = message
18 changes: 18 additions & 0 deletions binance/lib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,21 @@ def parse_proxies(proxies: dict):
else None
),
}


def validate_symbol(symbol):
"""Validate the format of a trading symbol"""
if not isinstance(symbol, str) or not symbol.isalnum():
raise ValueError("Invalid symbol format")
return True


def parse_order_book(order_book_data):
"""Parse the order book data returned by the Binance API"""
if not isinstance(order_book_data, dict):
raise ValueError("Invalid order book data format")
return {
"lastUpdateId": order_book_data.get("lastUpdateId"),
"bids": order_book_data.get("bids", []),
"asks": order_book_data.get("asks", []),
}
15 changes: 15 additions & 0 deletions binance/websocket/binance_socket_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,18 @@ def _handle_exception(self, e):
self.on_error(self, e)
else:
raise e

def subscribe_to_agg_trades(self, symbol):
"""Subscribe to aggregate trade streams"""
stream_name = f"{symbol.lower()}@aggTrade"
self.send_message(json.dumps({"method": "SUBSCRIBE", "params": [stream_name], "id": 1}))

def subscribe_to_kline(self, symbol, interval):
"""Subscribe to kline/candlestick streams"""
stream_name = f"{symbol.lower()}@kline_{interval}"
self.send_message(json.dumps({"method": "SUBSCRIBE", "params": [stream_name], "id": 1}))

def subscribe_to_order_book(self, symbol, level=5):
"""Subscribe to order book streams"""
stream_name = f"{symbol.lower()}@depth{level}"
self.send_message(json.dumps({"method": "SUBSCRIBE", "params": [stream_name], "id": 1}))