quantpylib.hft.oms
OMS
__init__(gateway, exchanges=None, orders_mirrored_exchanges=None, positions_mirrored_exchanges=None, pending_timeout=5000, refresh_orders_snapshot=60, refresh_positions_snapshot=60, orders_cls_init=None)
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
|
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
|
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
|
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 |
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 |
hedge_order(maker_order, hedge_order, trigger_after_ms=0)
async
Place a maker-taker order. The maker order is the order that is being hedged. The taker order is the order that
hedges the maker order - taker order has no amount specified, size executed is contingent on the size of maker fills.
Each order are dictionaries - and are passed as named arguments (**order) into the gateway
as per the quantpylib.gateway.executor.limit_order and quantpylib.gateway.executor.market_order methods respectively.
hedge_portfolio_delta(hedge_on='', default_on='', by=markets.MARGIN_TOTAL, ord_type=markets.ORDER_TYPE_MARKET, **kwargs)
async
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.
lagged_price(exc, ticker)
Get the lagged price for a given exchange and ticker.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exc
|
str
|
The exchange. |
required |
ticker
|
str
|
The ticker. |
required |
limit_order(round_to_specs=False, **kwargs)
async
Place a limit 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 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 |
{}
|
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 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 |
order_delta_hedge()
Hedge order logic. Fill sizes on maker exchange that are not compatible with trading rules on taker exchange are hedged as much as possible, and size differences are submitted to a hedge balance that waits for future arriving fills to be hedged.
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