basana.external.binance.isolated_margin

class basana.external.binance.isolated_margin.Account(cli, ws_mgr)

Isolated margin account.

Parameters:
  • cli (IsolatedMarginAccount)

  • ws_mgr (WebsocketManager)

async cancel_oco_order(pair, order_list_id=None, client_order_list_id=None)

Cancels an OCO order.

If the order can’t be canceled a basana.external.binance.exchange.Error will be raised.

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

  • order_list_id (str | None) – The order list id.

  • client_order_list_id (str | None) – A client id for the order list.

Return type:

CanceledOCOOrder

Note

  • Either order_list_id or client_order_list_id should be set, but not both.

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

Cancels an order.

If the order can’t be canceled a basana.external.binance.exchange.Error will be raised.

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

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

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

Return type:

CanceledOrder

Note

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

async create_limit_order(operation, pair, amount, limit_price, side_effect_type='NO_SIDE_EFFECT', time_in_force='GTC', client_order_id=None, **kwargs)

Creates a limit order.

Check https://binance-docs.github.io/apidocs/spot/en/#margin-account-new-order-trade for more information. If the order can’t be created a basana.external.binance.exchange.Error will be raised.

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.

  • side_effect_type (str) – One of NO_SIDE_EFFECT, MARGIN_BUY or AUTO_REPAY.

  • time_in_force (str) – The time in force.

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

  • kwargs (Dict[str, Any]) – Additional keyword arguments that will be forwarded.

Return type:

CreatedOrder

async create_market_order(operation, pair, amount=None, quote_amount=None, client_order_id=None, side_effect_type='NO_SIDE_EFFECT', **kwargs)

Creates a market order.

Check https://binance-docs.github.io/apidocs/spot/en/#margin-account-new-order-trade for more information. If the order can’t be created a basana.external.binance.exchange.Error will be raised.

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

  • pair (Pair) – The pair to trade.

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

  • quote_amount (Decimal | None) – The amount to buy/sell in quote units.

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

  • side_effect_type (str) – One of NO_SIDE_EFFECT, MARGIN_BUY or AUTO_REPAY.

  • kwargs (Dict[str, Any]) – Additional keyword arguments that will be forwarded.

Return type:

CreatedOrder

Note

  • Either amount or quote_amount should be set, but not both.

async create_oco_order(operation, pair, amount, limit_price, stop_price, stop_limit_price=None, side_effect_type='NO_SIDE_EFFECT', stop_limit_time_in_force='GTC', list_client_order_id=None, limit_client_order_id=None, stop_client_order_id=None, **kwargs)

Creates an OCO order.

Check https://binance-docs.github.io/apidocs/spot/en/#margin-account-new-oco-trade for more information. If the order can’t be created a basana.external.binance.exchange.Error will be raised.

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.

  • stop_price (Decimal) – The stop price.

  • stop_limit_price (Decimal | None) – The stop limit price.

  • side_effect_type (str) – One of NO_SIDE_EFFECT, MARGIN_BUY or AUTO_REPAY.

  • stop_limit_time_in_force (str) – The time in force for the stop limit order.

  • list_client_order_id (str | None) – A client id for the order list.

  • limit_client_order_id (str | None) – A client id for the limit order.

  • stop_client_order_id (str | None) – A client id for the stop order.

  • kwargs (Dict[str, Any]) – Additional keyword arguments that will be forwarded.

Return type:

CreatedOCOOrder

async create_stop_limit_order(operation, pair, amount, stop_price, limit_price, side_effect_type='NO_SIDE_EFFECT', time_in_force='GTC', client_order_id=None, **kwargs)

Creates a stop limit order.

Check https://binance-docs.github.io/apidocs/spot/en/#margin-account-new-order-trade for more information. If the order can’t be created a basana.external.binance.exchange.Error will be raised.

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.

  • side_effect_type (str) – One of NO_SIDE_EFFECT, MARGIN_BUY or AUTO_REPAY.

  • time_in_force (str) – The time in force.

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

  • kwargs (Dict[str, Any]) – Additional keyword arguments that will be forwarded.

Return type:

CreatedOrder

async get_balances()

Returns all balances.

Return type:

Dict[Pair, IsolatedBalance]

async get_oco_order_info(order_list_id=None, client_order_list_id=None)

Returns information about an OCO order.

Parameters:
  • order_list_id (str | None) – The order list id.

  • client_order_list_id (str | None) – A client id for the order list.

Return type:

OCOOrderInfo

Note

  • Either order_list_id or client_order_list_id should be set, but not both.

async get_open_orders(pair=None)

Returns open orders.

Parameters:

pair (Pair | None) – If set, only open orders matching this pair will be returned, otherwise all open orders will be returned.

Return type:

List[OpenOrder]

async get_order_info(pair, order_id=None, client_order_id=None, include_trades=True)

Returns information about an order.

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

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

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

  • include_trades (bool)

Return type:

OrderInfo

Note

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

  • Including trades requires making an extra request to Binance.

subscribe_to_order_events(pair, event_handler)

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

Works as defined in https://developers.binance.com/docs/margin_trading/trade-data-stream/Event-Order-Update.

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

  • event_handler (Callable[[OrderEvent], Awaitable[Any]]) – The event handler.

subscribe_to_user_data_events(pair, event_handler)

Registers an async callable that will be called for every new user data event.

Works as defined in https://developers.binance.com/docs/margin_trading/trade-data-stream.

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

  • event_handler (Callable[[Event], Awaitable[Any]]) – The event handler.

async transfer_from_spot_account(asset, pair, amount)

Transfer balances from the spot account to the isolated margin account.

If the transfer can’t be completed a basana.external.binance.exchange.Error will be raised.

Parameters:
  • asset (str) – The asset to transfer.

  • pair (Pair) – The trading pair.

  • amount (Decimal) – The amount to transfer.

Return type:

dict

async transfer_to_spot_account(asset, pair, amount)

Transfer balances from the isolated margin account to the spot account.

If the transfer can’t be completed a basana.external.binance.exchange.Error will be raised.

Parameters:
  • asset (str) – The asset to transfer.

  • pair (Pair) – The trading pair.

  • amount (Decimal) – The amount to transfer.

Return type:

dict

class basana.external.binance.isolated_margin.IsolatedBalance(json)
Parameters:

json (dict)

property base_asset: str

The base asset.

property base_asset_balance: Balance

The base asset balance.

property quote_asset: str

The quote asset.

property quote_asset_balance: Balance

The quote asset balance.