Skip to content

quantpylib.wrappers.binance

Binance

account_balance(**kwargs) async

Retrieve balance details of the user, such as equity, margin (total, maintenance) and pnl.

Returns:

Type Description
dict

Balance details.

account_fill_subscribe(handler, **kwargs) async

Subscribe to account fill updates - in Binance, this just calls the order_updates_subscribe method.

account_fill_unsubscribe(**kwargs) async

Unsubscribe to account fill updates - in Binance, this just calls the order_updates_unsubscribe method.

cancel_open_orders(ticker=None, **kwargs) async

Cancel open orders on the exchange.

Parameters:

Name Type Description Default
ticker str

The coin symbol. Defaults to None, which means cancel all open orders.

None
**kwargs

Additional keyword arguments.

{}

Returns:

Name Type Description
Any

The result of the cancellation request. Returns None if no open orders are found or no orders are canceled.

cancel_order(ticker, oid=None, cloid=None, **kwargs) async

Cancel an order.

Parameters:

Name Type Description Default
ticker str

Ticker symbol.

required
oid int

Order ID to cancel.

None
cloid str

Client Order ID to cancel.

None
**kwargs

Exchange wrapper specific keyword arguments.

{}

cleanup() async

Cleans up open sessions with Binance server

contract_specifications(**kwargs) async

Retrieve the contract's trading rules from the exchange.

Parameters:

Name Type Description Default
**kwargs

Additional keyword arguments.

{}

Returns:

Name Type Description
dict

A dictionary containing contract specifications for each asset with key-values: - SYMBOL_PRICE_PRECISION. - SYMBOL_QUANTITY_PRECISION. - SYMBOL_MIN_NOTIONAL - SYMBOL_BASE_ASSET - SYMBOL_QUOTE_ASSET

get_all_marks(**kwargs) async

Retrieve the mark-price for all available tickers.

Parameters:

Name Type Description Default
**kwargs

Additional keyword arguments.

{}

Returns:

Name Type Description
dict

A dictionary with contract symbols as keys and their corresponding mark-prices (Decimal) as values.

get_all_mids(ticker=None, **kwargs) async

Retrieve the mid-price for a specific ticker or all available tickers.

Parameters:

Name Type Description Default
ticker str

The symbol of the specific contract for which to retrieve the mid-price. If not provided, mid-prices for all contracts will be returned. Defaults to None.

None
**kwargs

Additional keyword arguments.

{}

Returns:

Name Type Description
Decimal

The mid-price of the specified ticker if ticker is provided.

dict

A dictionary with contract symbols as keys and their corresponding mid-prices (Decimal) as values if ticker is not provided.

get_trade_bars(ticker, start, end, granularity, granularity_multiplier, kline_close=False, **kwargs) async

Retrieve trade bars data.

Parameters:

Name Type Description Default
ticker str

Ticker symbol for the asset.

required
start datetime

Start datetime for the data retrieval.

required
end datetime

End datetime for the data retrieval.

required
granularity Period

Granularity of the data.

required
granularity_multiplier int

Multiplier for the granularity.

required
**kwargs

Additional keyword arguments.

{}

Returns:

Type Description
DataFrame

DataFrame containing the trade bars data.

init_client() async

Initializes the exchange client.

l2_book_get(ticker, depth=1000, standardize_schema=True, **kwargs) async

Retrieve L2 Order Book data.

Parameters:

Name Type Description Default
ticker str

Ticker symbol.

required
depth int

Depth of order book data to retrieve. Defaults to 1000.

1000
standardize_schema bool

Whether to standardize the schema of the retrieved data. Defaults to True.

True
**kwargs

Exchange wrapper specific keyword arguments.

{}

Returns:

Name Type Description
dict

A dictionary containing the order book data.

l2_book_mirror(ticker, depth=20, buffer_size=100, as_dict=True, on_update=None, refresh_sec=300, speed_ms=500, **kwargs) async

Keep a live, internal L2 Order Book representation using a l2-book subscription.

Parameters:

Name Type Description Default
ticker str

Ticker symbol.

required
depth int

Depth of order book data to retrieve. Defaults to 20.

20
buffer_size int

The size of the buffer to store order book history. Defaults to 100.

100
as_dict (bool, True)

If True, pass state as dictionary, otherwise as a quantpylib.hft.lob.LOB object into handlers.

True
on_update coroutine

A coroutine handler for the order book updates. Defaults to None.

None
refresh_sec int

The time interval in seconds to refresh the order book snapshot. Defaults to 300.

300
speed_ms int

The speed of stream update. Allowed values are [None,100,250,500]. Defaults to 500.

500
**kwargs

Exchange wrapper specific keyword arguments.

{}

l2_book_peek(ticker, as_dict=True, **kwargs)

Retrieve the local mirror copy of the L2 Order Book.

Parameters:

Name Type Description Default
ticker str

Ticker symbol.

required
as_dict bool

Whether to return the order book data as a dictionary. Defaults to True.

True
**kwargs

Exchange wrapper specific keyword arguments.

{}

l2_book_subscribe(ticker, handler, depth=5, speed_ms=None, fut_type=FuturesType.USD_M, standardize_schema=True, **kwargs) async

Subscribe to L2 Order Book updates.

Parameters:

Name Type Description Default
ticker str

Ticker symbol.

required
handler coroutine

A coroutine handler for the message received.

required
depth Optional[int]

Depth of order book data received. Defaults to 5.

5
speed_ms Optional[int]

Speed of stream update. Allowed values are [None,100,250,500].

None
standardize_schema (bool, True)

Processes the incoming message to {ts,b,a}

True
**kwargs

Exchange wrapper specific keyword arguments.

{}

l2_book_subscriptions(fut_type=FuturesType.USD_M, **kwargs)

Retrieve the list of perpetuals with open subscription.

l2_book_unsubscribe(ticker, fut_type=FuturesType.USD_M, **kwargs) async

Unsubscribe from L2 Order Book.

Parameters:

Name Type Description Default
ticker str

Ticker symbol.

required
**kwargs

Exchange wrapper specific keyword arguments.

{}

limit_order(ticker, amount, price=None, price_match=None, tif=markets.TIME_IN_FORCE_GTC, reduce_only=False, cloid=None, **kwargs) async

Submit limit order. If both price and price_match is specified, then the price_match is prioritized.

Parameters:

Name Type Description Default
ticker str

The coin symbol.

required
amount float or Decimal

The signed quantity of contracts to long/short.

required
price Optional[float]

The price at which to execute the order. Defaults to None.

None
price_match Optional[str]

The price matching rule at which to execute the order. Defaults to None. Allowed values are [OPPONENT,OPPONENT_5,OPPONENT_10,OPPONENT_20,QUEUE,QUEUE_5,QUEUE_10]

None
tif str

The time in force. Defaults to "GTC". Allowed values are [GTC,IOC,FOK,GTX,GTD].

TIME_IN_FORCE_GTC
reduce_only bool

Whether the order should reduce an existing position only. Defaults to False.

False
cloid str

Client order ID for order tracking. Defaults to None.

None

Returns:

Name Type Description
Any

The result of the order placement.

market_order(ticker, amount, reduce_only=False, cloid=None, **kwargs) async

Submit market order.

Parameters:

Name Type Description Default
ticker str

Ticker symbol.

required
amount float or Decimal

The positive/negative quantity of contracts to long/short.

required
reduce_only bool

Whether the order should reduce an existing position. Defaults to False.

False
cloid str

Client order ID for custom tracking. Defaults to None.

None
**kwargs

Exchange wrapper specific keyword arguments.

{}

Returns:

Name Type Description
Any

The result of the order placement.

order_query(ticker, oid=None, cloid=None, as_dict=True, **kwargs) async

Get order details using order ID.

Parameters:

Name Type Description Default
ticker str

The trading instrument ticker.

required
oid (str, int)

Order ID in exchange

None
cloid str

Client Order ID

None
as_dict bool

If True, return the order details as a dictionary. Defaults to True.

True
**kwargs

Exchange wrapper specific keyword arguments.

{}

order_updates_subscribe(handler, **kwargs) async

Subscribe to order updates.

Parameters:

Name Type Description Default
handler coroutine

A coroutine handler for the message received.

required
**kwargs

Exchange wrapper specific keyword arguments.

{}

order_updates_unsubscribe(**kwargs) async

Unsubscribe from order updates.

Parameters:

Name Type Description Default
**kwargs

Exchange wrapper specific keyword arguments.

{}

orders_get(**kwargs) async

Get all open order details.

Parameters:

Name Type Description Default
**kwargs

Exchange wrapper specific keyword arguments.

{}

orders_mirror(on_update=None, as_list=True, **kwargs) async

Keeps a local mirror copy of the account open orders.

Parameters:

Name Type Description Default
on_update coroutine

A coroutine handler for orders dictionary on order event.

None
as_list bool

If True, pass state as list, otherwise as quantpylib.standards.Orders object into handlers.

True
**kwargs

Exchange wrapper specific keyword arguments.

{}

orders_peek(as_dict=True, **kwargs)

Retrieves the local mirror copy of the account open orders.

Parameters:

Name Type Description Default
as_dict bool

If True, pass state as dictionary, otherwise as quantpylib.standards.Orders object.

True
**kwargs

Exchange wrapper specific keyword arguments.

{}

positions_get() async

Get all open position details.

Parameters:

Name Type Description Default
**kwargs

Exchange wrapper specific keyword arguments.

required

positions_mirror(on_update=None, as_dict=True, **kwargs) async

Keeps a local mirror copy of the account open orders.

Parameters:

Name Type Description Default
on_update coroutine

A coroutine handler for positions dictionary on fill.

None
as_dict bool

If True, the method returns positions as a dictionary, otherwise as a quantpylib.standards.Positions object. Defaults to True.

True
**kwargs

Exchange wrapper specific keyword arguments.

{}

positions_peek(as_dict=True, **kwargs)

Retrieves the local mirror copy of the account open positions.

Parameters:

Name Type Description Default
as_dict bool

If True, the method returns positions as a dictionary, otherwise as a quantpylib.standards.Positions object. Defaults to True.

True
**kwargs

Exchange wrapper specific keyword arguments.

{}

rand_cloid(start='', end='', **kwargs)

Generate a random string (cloid) consisting of hexadecimal characters.

Parameters:

Name Type Description Default
start str

A string to prepend to the generated random string. Defaults to ''.

''
end str

A string to append to the generated random string. Defaults to ''.

''
**kwargs

Additional keyword arguments.

{}

Returns:

Name Type Description
str

A random hexadecimal string with a total length of 32 characters, including the optional 'start' and 'end' strings.

trades_subscribe(ticker, handler, standardize_schema=True, **kwargs) async

Subscribe to ticker trades.

Parameters:

Name Type Description Default
ticker str

Ticker symbol.

required
handler coroutine

A coroutine handler for the message received.

required
standardize_schema (bool, True)

Processes the incoming message to (ts,price,sz,dir)

True
**kwargs

Exchange wrapper specific keyword arguments.

{}

trades_unsubscribe(ticker, **kwargs) async

Unsubscribe from ticker trades.

Parameters:

Name Type Description Default
ticker str

Ticker symbol.

required
**kwargs

Exchange wrapper specific keyword arguments.

{}