basana.external.ccxt.exchange

class basana.external.ccxt.exchange.Exchange(dispatcher, exchange_id, api_key=None, api_secret=None, session=None, config=None)

A client for crypto currency exchanges through CCXT.

This class wraps a subset of the unified CCXT API, exposing the most common trading operations with an interface that is consistent with the rest of the exchanges in Basana. Any other CCXT functionality can still be accessed through the underlying exchange instance, available via ccxt.

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

  • exchange_id (str) – The CCXT exchange id.

  • api_key (str | None) – An optional api key.

  • api_secret (str | None) – An optional api secret.

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

  • config (dict | None) – An optional dictionary for overriding CCXT config settings.

async cancel_order(pair, order_id=None, client_order_id=None, **kwargs)

Cancels an order.

If the order can’t be canceled CCXT will raise an exception.

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

  • order_id (str | None) – The order id.

  • client_order_id (str | None) – The client order id.

  • kwargs (Any) – Additional keyword arguments that will be forwarded.

Return type:

CanceledOrder

Note

  • Either order_id or client_order_id should be set, but not both.

property ccxt

The underlying CCXT exchange instance.

Use this to call any CCXT method not wrapped by this class.

async close()

Closes the underlying CCXT exchange.

async create_limit_order(operation, pair, amount, limit_price, client_order_id=None, **kwargs)

Creates a limit order.

If the order can’t be created CCXT will raise an exception.

Parameters:
  • operation (OrderOperation) – The order operation.

  • pair (Pair) – The pair to trade.

  • amount (Decimal) – The amount to buy/sell in base units.

  • limit_price (Decimal) – The limit price.

  • client_order_id (str | None) – A client order id.

  • kwargs (Any) – Additional keyword arguments that will be forwarded.

Return type:

CreatedOrder

async create_market_order(operation, pair, amount, client_order_id=None, **kwargs)

Creates a market order.

If the order can’t be created CCXT will raise an exception.

Parameters:
  • operation (OrderOperation) – The order operation.

  • pair (Pair) – The pair to trade.

  • amount (Decimal) – The amount to buy/sell in base units.

  • client_order_id (str | None) – A client order id.

  • kwargs (Any) – Additional keyword arguments that will be forwarded.

Return type:

CreatedOrder

async create_stop_limit_order(operation, pair, amount, stop_price, limit_price, client_order_id=None, **kwargs)

Creates a stop limit order.

If the order can’t be created CCXT will raise an exception.

Parameters:
  • operation (OrderOperation) – The order operation.

  • pair (Pair) – The pair to trade.

  • amount (Decimal) – The amount to buy/sell in base units.

  • stop_price (Decimal) – The stop price.

  • limit_price (Decimal) – The limit price.

  • client_order_id (str | None) – A client order id.

  • kwargs (Any) – Additional keyword arguments that will be forwarded.

Return type:

CreatedOrder

async create_stop_order(operation, pair, amount, stop_price, client_order_id=None, **kwargs)

Creates a stop order.

If the order can’t be created CCXT will raise an exception.

Parameters:
  • operation (OrderOperation) – The order operation.

  • pair (Pair) – The pair to trade.

  • amount (Decimal) – The amount to buy/sell in base units.

  • stop_price (Decimal) – The stop price.

  • client_order_id (str | None) – A client order id.

  • kwargs (Any) – Additional keyword arguments that will be forwarded.

Return type:

CreatedOrder

async get_balance(symbol)

Returns the balance for a specific currency/symbol/etc..

Parameters:

symbol (str) – The currency/symbol/etc..

Return type:

Balance

async get_balances()

Returns all balances.

Return type:

Dict[str, Balance]

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_info(pair, order_id=None, client_order_id=None, **kwargs)

Returns information about an order.

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

  • order_id (str | None) – The order id.

  • client_order_id (str | None) – The client order id.

  • kwargs (Any) – Additional keyword arguments that will be forwarded.

Return type:

OrderInfo

Note

  • Either order_id or client_order_id should be set, but not both.

async get_pair_info(pair)

Returns information about a trading pair.

Parameters:

pair (Pair) – The trading pair.

Return type:

PairInfo

subscribe_to_bar_events(pair, bar_duration, event_handler, **kwargs)

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

Uses CCXT Pro’s watchOHLCV websocket method. Only closed candles are reported.

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

  • bar_duration (str) – The bar duration as a CCXT timeframe (e.g. 1m, 5m, 1h).

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

  • kwargs (Any) – Additional keyword arguments that will be forwarded to CCXT.

Note

  • The target exchange must support watchOHLCV. Check exchange.ccxt.has['watchOHLCV'] after loading markets.

subscribe_to_order_book_events(pair, event_handler, depth=None, **kwargs)

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

Uses CCXT Pro’s watchOrderBook websocket method.

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

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

  • depth (int | None) – The order book depth. If not set, CCXT’s default depth is used.

  • kwargs (Any) – Additional keyword arguments that will be forwarded to CCXT.

Note

  • The target exchange must support watchOrderBook. Check exchange.ccxt.has['watchOrderBook'] after loading markets.

subscribe_to_private_order_events(pair, event_handler, **kwargs)

Registers an async callable that will be called when your own orders are updated.

Uses CCXT Pro’s watchOrders websocket method.

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

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

  • kwargs (Any) – Additional keyword arguments that will be forwarded to CCXT.

Note

  • The target exchange must support watchOrders. Check exchange.ccxt.has['watchOrders'] after loading markets.

  • API credentials must be configured when creating the exchange.

subscribe_to_public_trade_events(pair, event_handler, **kwargs)

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

Uses CCXT Pro’s watchTrades websocket method.

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

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

  • kwargs (Any) – Additional keyword arguments that will be forwarded to CCXT.

Note

  • The target exchange must support watchTrades. Check exchange.ccxt.has['watchTrades'] after loading markets.

class basana.external.ccxt.exchange.Balance(balance, symbol)
Parameters:
  • balance (dict)

  • symbol (str)

property available: Decimal

The available balance.

property locked: Decimal

The locked balance.

property total: Decimal

The total balance (available + locked).

class basana.external.ccxt.exchange.CreatedOrder(json)
Parameters:

json (dict)

property amount: Decimal

The amount.

property client_order_id: str | None

The client order id.

property datetime: datetime

The creation datetime.

property id: str

The order id.

property limit_price: Decimal | None

The limit price.

property operation: OrderOperation

The operation.

property stop_price: Decimal | None

The stop price.

class basana.external.ccxt.exchange.OrderInfo(json)
Parameters:

json (dict)

property amount: Decimal

The amount.

property amount_filled: Decimal

The amount filled.

property amount_remaining: Decimal

The amount remaining to be filled.

property client_order_id: str | None

The client order id.

property fill_price: Decimal | None

The average fill price.

Uses the exchange-reported average price when available; otherwise it is computed as quote_amount_filled / amount_filled (a volume-weighted average), which may not be exact due to rounding.

property id: str

The order id.

property is_open: bool

True if the order is open, False otherwise.

property limit_price: Decimal | None

The limit price.

property operation: OrderOperation

The operation.

property quote_amount_filled: Decimal

The amount filled in quote units.

property stop_price: Decimal | None

The stop price.

class basana.external.ccxt.exchange.CanceledOrder(json)
Parameters:

json (dict)

property amount: Decimal

The amount.

property client_order_id: str | None

The client order id.

property id: str

The order id.

property limit_price: Decimal | None

The limit price.

property operation: OrderOperation

The operation.