basana.external.binance.exchange

class basana.external.binance.exchange.Exchange(dispatcher, api_key=None, api_secret=None, session=None, tb=None, config_overrides={})

A client for Binance crypto currency exchange.

Parameters:
  • dispatcher (EventDispatcher) – The event dispatcher.

  • api_key (str | None) – An optional api key. If not set only public endpoints can be used.

  • api_secret (str | None) – An optional api secret. If not set only public endpoints can be used.

  • session (aiohttp.ClientSession) – An optional client session, in case you want to reuse connections.

  • tb (TokenBucketLimiter | None) – An optional token bucket limiter, in case you want to throttle requests.

  • config_overrides (dict) – An optional dictionary for overriding config settings.

property cross_margin_account: Account

Returns the cross margin account.

async get_bid_ask(pair)

Returns the current best bid and ask prices.

Parameters:

pair (Pair) – The trading pair.

Return type:

Tuple[Decimal, Decimal]

async get_order_book(pair, limit=None)

Returns the order book for a given trading pair.

Parameters:
  • pair (Pair) – The trading pair.

  • limit (int | None) – The maximum number of levels to return.

Return type:

PartialOrderBook

async get_pair_info(pair)

Returns information about a trading pair.

Parameters:

pair (Pair) – The trading pair.

Return type:

PairInfoEx

property isolated_margin_account: Account

Returns the isolated margin account.

property spot_account: Account

Returns the spot account.

subscribe_to_bar_events(pair, bar_duration, event_handler, skip_first_bar=True, flush_delay=1)

Registers an async callable that will be called when a new bar is available.

Works as defined in https://binance-docs.github.io/apidocs/spot/en/#kline-candlestick-streams but only closed klines are reported.

Parameters:
  • pair (Pair) – The trading pair.

  • bar_duration (str) – The bar duration. One of 1s, 1m, 3m, 5m, 15m, 30m, 1h, 2h, 4h, 6h, 8h, 12h, 1d, 3d, 1w, 1M.

  • event_handler (Callable[[BarEvent], Awaitable[Any]]) – An async callable that receives a BarEvent.

  • skip_first_bar (bool) – Ignored. It will be removed in a future version.

  • flush_delay (float) – Ignored. It will be removed in a future version.

subscribe_to_order_book_diff_events(pair, event_handler, interval=1000)

Registers an async callable that will be called with depth updates.

Works as defined in https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#diff-depth-stream

Parameters:
  • pair (Pair) – The trading pair.

  • event_handler (Callable[[OrderBookDiffEvent], Awaitable[Any]]) – An async callable that receives an OrderBookDiffEvent.

  • interval (int) – The update interval in milliseconds. Valid values are: 1000 (1s), 100 (100ms).

subscribe_to_order_book_events(pair, event_handler, depth=10, interval=1000)

Registers an async callable that will be called with the top bids/asks of the order book.

Works as defined in https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#partial-book-depth-streams

Parameters:
  • pair (Pair) – The trading pair.

  • event_handler (Callable[[PartialOrderBookEvent], Awaitable[Any]]) – An async callable that receives a PartialOrderBookEvent.

  • depth (int) – The order book depth. Valid values are: 5, 10, 20.

  • interval (int) – The update interval in milliseconds. Valid values are: 1000 (1s), 100 (100ms).

subscribe_to_trade_events(pair, event_handler)

Registers an async callable that will be called for every new trade.

Works as defined in https://binance-docs.github.io/apidocs/spot/en/#trade-streams.

Parameters:
  • pair (Pair) – The trading pair.

  • event_handler (Callable[[TradeEvent], Awaitable[Any]]) – An async callable that receives a TradeEvent.

async symbol_to_pair(symbol)

Returns the pair for a given symbol.

Parameters:

symbol (str) – The symbol.

Return type:

Pair

exception basana.external.binance.exchange.Error(msg, code, resp, json_response)

An error returned by the exchange.

Parameters:
  • msg (str) – The error message, if available.

  • code (int | None) – The error code, if available.

  • resp (ClientResponse) – The response.

  • json_response (Any | None) – The response body, if it was a JSON.

code

The error code, if available.

http_reason

The HTTP reason.

http_status

The HTTP status code.

json_response

The response body, if it was a JSON.

msg

The error message.

class basana.external.binance.exchange.PairInfoEx(base_precision, quote_precision, permissions)

Bases: PairInfo

Information about a trading pair.

Parameters:
  • base_precision (int) – The precision for the base symbol.

  • quote_precision (int) – The precision for the quote symbol.

  • permissions (List[str]) – The account and pair permissions.

Check Account and Symbol Permissions in https://binance-docs.github.io/apidocs/spot/en/#public-api-definitions.

permissions: List[str]

The account and pair permissions.