quantpylib.hft.oms
OMS
__init__(gateway, exchanges=None, orders_mirrored_exchanges=None, positions_mirrored_exchanges=None, fills_mirrored_exchanges=None, pending_timeout=5000, refresh_orders_snapshot=60, refresh_positions_snapshot=60, refresh_fills_snapshot=30, logger=None, orders_cls_init=None, positions_cls_init=None, fills_cls_init=None, contract_specs_kwargs=None, track_mids=False)
Intialize the Order Management System object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gateway
|
Gateway
|
Initialized gateway object. |
required |
exchanges
|
list
|
The list of exchanges to subscribe to. If None, use the clients in the gateway. |
None
|
orders_mirrored_exchanges
|
list
|
The list of exchanges to mirror orders. If None, defaults to gateway exchanges. |
None
|
positions_mirrored_exchanges
|
list
|
The list of exchanges to mirror positions. If None, defaults to gateway exchanges. |
None
|
fills_mirrored_exchanges
|
list
|
The list of exchanges to mirror account fills. If None, defaults to gateway exchanges. |
None
|
pending_timeout
|
int
|
The timeout in milliseconds for pending orders. Defaults to 5000. |
5000
|
refresh_orders_snapshot
|
int
|
The refresh interval in seconds to check orders snapshot against mirrored orders. Defaults to 60. |
60
|
refresh_positions_snapshot
|
int
|
The refresh interval in seconds to check positions snapshot against mirrored positions. Defaults to 60. |
60
|
refresh_fills_snapshot
|
int
|
The refresh interval in seconds to recover account fill gaps. Defaults to 30. |
30
|
track_mids
|
bool
|
Initialize and periodically refresh the
live mid-price cache exposed by |
False
|
contract_specs_kwargs
|
dict
|
Per-exchange keyword arguments forwarded to gateway.exchange.contract_specifications(exc=exc) on every contract-spec fetch (both the full rebuild and the single-exchange refresh). Keyed by exchange alias, e.g. {"binance": {"contract_types": ("PERPETUAL", "TRADIFI_PERPETUAL")}} to admit Binance TradFi perpetuals. Exchanges absent from the mapping use wrapper defaults. |
None
|
account_fills_get(exc, **kwargs)
async
Get an authoritative account-fill range using REST.
account_fills_mirror(exc, on_update=None, on_delta=None)
async
Mirror account fills for a given exchange. Uses socket.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exc
|
str
|
The exchange. |
required |
on_update
|
coroutine
|
The update handler receives fill page snapshots. Defaults to None. |
None
|
on_delta
|
coroutine
|
The delta handler receives accepted fills. Defaults to None. |
None
|
add_clock_callback(callback, name='CLOCK', interval_ms=500)
async
Add a callback to the event loop that is triggered every interval_ms. Can be used for clock-based trading agents. Callback is shutdown when OMS is cleaned up.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
callback
|
coroutine
|
The callback to trigger. |
required |
name
|
str
|
The name of the callback. Defaults to 'CLOCK'. |
'CLOCK'
|
interval_ms
|
int
|
The interval in milliseconds. Defaults to 500. |
500
|
cached_live_mids(exc, ticker)
Get a cached live mid price when track_mids=True.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exc
|
str
|
The exchange. |
required |
ticker
|
str
|
The ticker. |
required |
cancel_order(exc, ticker=None, oid=None, cloid=None)
async
Cancel an order by order id or client order id. If both oid and cloid are None, cancel all open orders for given ticker.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exc
|
str
|
The exchange. |
required |
ticker
|
str
|
The ticker. Defaults to None. |
None
|
oid
|
str
|
The order id. Defaults to None. |
None
|
cloid
|
str
|
The client order id. Defaults to None. |
None
|
cleanup()
async
Clean up the OMS object.
cloid_query(exc, cloid)
Query order information by client order id.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exc
|
str
|
The exchange. |
required |
cloid
|
str
|
The client order id. |
required |
common_lot_precision(ex1, ticker1, ex2, ticker2)
Get the lot precision for trading on two different exchanges.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ex1
|
str
|
The first exchange. |
required |
ticker1
|
str
|
The first ticker. |
required |
ex2
|
str
|
The second exchange. |
required |
ticker2
|
str
|
The second ticker. |
required |
common_min_notional(ex1, ticker1, ex2, ticker2)
Get the minimum notional trade on two different exchanges.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ex1
|
str
|
The first exchange. |
required |
ticker1
|
str
|
The first ticker. |
required |
ex2
|
str
|
The second exchange. |
required |
ticker2
|
str
|
The second ticker. |
required |
common_price_precision(ex1, ticker1, ex2, ticker2)
Get the price precision for trading on two different exchanges.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ex1
|
str
|
The first exchange. |
required |
ticker1
|
str
|
The first ticker. |
required |
ex2
|
str
|
The second exchange. |
required |
ticker2
|
str
|
The second ticker. |
required |
contract_specs(exc, ticker)
Get the contract specifications for a given exchange and ticker.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exc
|
str
|
The exchange. |
required |
ticker
|
str
|
The ticker. |
required |
fills_peek(exc)
Return the quantpylib.standards.portfolio.Fills object maintained for a given exchange.
get_all_balances()
async
Get the balances for all exchanges. Uses HTTP request.
get_all_equities()
async
Get the equity for all exchanges. Uses HTTP request.
get_balance(exc)
async
Get the balance for a given exchange. Uses HTTP request.
get_base_asset(exc, ticker)
Get the base asset for a given exchange and ticker. For example, the base asset for BTCUSDT is BTC.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exc
|
str
|
The exchange. |
required |
ticker
|
str
|
The ticker. |
required |
get_funding_info(exc, ticker)
Get the funding information for a given exchange and ticker.
Only available when init(include_funding=True) was called.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exc
|
str
|
The exchange. |
required |
ticker
|
str
|
The ticker. |
required |
get_position(exc, ticker)
Get the position for a given exchange and ticker. Uses socket.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exc
|
str
|
The exchange. |
required |
ticker
|
str
|
The ticker. |
required |
get_quote_asset(exc, ticker)
Get the quote asset for a given exchange and ticker. For example, the quote asset for BTCUSDT is USDT.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exc
|
str
|
The exchange. |
required |
ticker
|
str
|
The ticker. |
required |
init(include_funding=False)
async
Initialize the OMS object with information such as exchange contract trading specifications and metadata. To be called after the OMS has been created.
limit_order(**kwargs)
async
Place a limit order. Updates the orders page maintained by the OMS with a CREATE_PENDING status.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
The order parameters. Parameter usage is same as the |
{}
|
lot_precision(exc, ticker)
Get the lot precision for a given exchange and ticker.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exc
|
str
|
The exchange. |
required |
ticker
|
str
|
The ticker. |
required |
market_order(round_to_specs=False, **kwargs)
async
Place a market order. If round_to_specs is True, round the order amount to the lot precision. Updates the orders page maintained by the OMS with a CREATE_PENDING status.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
round_to_specs
|
bool
|
Round the order amount to the lot precision. Defaults to False. |
False
|
**kwargs
|
The order parameters. Parameter usage is same as the |
{}
|
min_notional(exc, ticker)
Get the minimum notional required to trade for a given exchange and ticker.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exc
|
str
|
The exchange. |
required |
ticker
|
str
|
The ticker. |
required |
oid_query(exc, oid)
Query order information by order id.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exc
|
str
|
The exchange. |
required |
oid
|
str
|
The order id. |
required |
orders_get(exc, **kwargs)
async
Get the orders for a given exchange. Uses HTTP request.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exc
|
str
|
The exchange. |
required |
orders_get_all()
async
Get the orders for all exchanges.Uses HTTP request.
orders_mirror(exc, on_update=None, on_delta=None)
async
Mirror the orders for a given exchange. Uses socket.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exc
|
str
|
The exchange. |
required |
on_update
|
coroutine
|
The update handler receives order page snapshots. Defaults to None. |
None
|
on_delta
|
coroutine
|
The delta handler receives order page changes. Defaults to None. |
None
|
orders_peek(exc)
Return the quantpylib.standards.portfolio.Orders object maintained for a given exchange.
positions_get(exc, **kwargs)
async
Get the positions for a given exchange. Uses HTTP request.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exc
|
str
|
The exchange. |
required |
positions_get_all()
async
Get the positions for all exchanges. Uses HTTP request.
Returns:
| Name | Type | Description |
|---|---|---|
dict |
Dictionary of positions for all exchanges. |
positions_get_by_base()
async
Gets mapping from base asset to exchange positions for all exchanges using the mirrored positions.
positions_get_net_base(base_exchange_positions=None)
async
Gets the net positions for each base asset across all exchanges using the mirrored positions.
positions_mirror(exc, on_update=None, on_delta=None)
async
Mirror the positions for a given exchange. Uses socket.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exc
|
str
|
The exchange. |
required |
on_update
|
coroutine
|
The update handler. Defaults to None. |
None
|
on_delta
|
coroutine
|
The delta handler. Defaults to None. |
None
|
positions_peek(exc)
Return the quantpylib.standards.portfolio.Positions object maintained for a given exchange.
price_precision(exc, ticker)
Get the price precision for a given exchange and ticker.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exc
|
str
|
The exchange. |
required |
ticker
|
str
|
The ticker. |
required |
rand_cloid(exc, **kwargs)
Generate a client order id.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exc
|
str
|
The exchange. |
required |
**kwargs
|
Exchange wrapper specific keyword arguments. |
{}
|
rounded_ceil_price(exc, ticker, price, **kwargs)
Round a price up to the price precision for a given exchange and ticker.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exc
|
str
|
The exchange. |
required |
ticker
|
str
|
The ticker. |
required |
price
|
Decimal
|
The price to round. |
required |
rounded_floor_price(exc, ticker, price, **kwargs)
Round a price down to the price precision for a given exchange and ticker.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exc
|
str
|
The exchange. |
required |
ticker
|
str
|
The ticker. |
required |
price
|
Decimal
|
The price to round. |
required |
rounded_lots(exc, ticker, amount, **kwargs)
Round an amount to the lot precision for a given exchange and ticker.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exc
|
str
|
The exchange. |
required |
ticker
|
str
|
The ticker. |
required |
amount
|
Decimal
|
The amount to round. |
required |
rounded_price(exc, ticker, price, **kwargs)
Round a price to the price precision for a given exchange and ticker.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exc
|
str
|
The exchange. |
required |
ticker
|
str
|
The ticker. |
required |
price
|
Decimal
|
The price to round. |
required |
update_exc_orders(exc, **kwargs)
async
Reconcile mirrored orders with exchange snapshot. 1. get all oids from snapshot. get all oids from mirror. 2. for oids in snapshot but not mirror, upsert (mirror may not have because behind OR ahead and complete) 3. for oids in both mirror and snapshot, upsert 4. for oids in mirror but not snapshot, a. if pending not timed out: ignore b. otherwise, query and upsert. (snapshot may be behind OR 2) ahead and complete, to remove) Args: exc (str): Exchange identifier **kwargs: Additional arguments passed to orders_get
update_exchange_contracts(exc=None)
async
Refresh exchange contract trading specifications and metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exc
|
str
|
If provided, refresh only this exchange's contract specifications, updating just that exchange's slice across the contract-spec maps while preserving the entries for every other exchange. If None (default), refresh all exchanges with a full rebuild. The single-exchange refresh is idempotent and safe to call mid-session while orders and positions are live, so a caller can refresh and retry after a missing-contract lookup (for example a newly-listed market) instead of waiting for the periodic full refresh. |
None
|