trading_agent.trading.desk_order_book
¶
The record of which desk placed which order, in a small SQLite database shared by every desk.
A desk may change or cancel only the orders it placed itself, and this record is how the order tools know which those are. Each order is kept under its parent_id, the handle UBI's order engine gives every order, and also under its broker order_id once a broker has accepted it. A dry run reaches no broker and gets no id from UBI, so it is kept under a made-up id that starts with "dry-", which lets a desk practise changing and cancelling it.
Typical usage example:
book = DeskOrderBook(Path('workspace/shared/desk_orders.db')) record = book.find('main', parent_id='...', order_id=None)
OrderRecord
dataclass
¶
OrderRecord(record_id, desk, kind, parent_id, order_id, instrument_id, exchange, segment, symbol, transaction_type, quantity, product, price, dry_run, state, placed_at)
One order a desk placed.
Attributes:
| Name | Type | Description |
|---|---|---|
record_id |
str
|
The key of the record: the parent_id, the order_id, or a made-up "dry-" id for a dry run. |
desk |
str
|
The desk that placed it. |
kind |
str
|
"order" for a plain order, "bracket" for a bracket order, or "close" for a closing order. |
parent_id |
str | None
|
The handle UBI's order engine gave it, or the made-up id for a dry run, or None. |
order_id |
str | None
|
The broker's id, or None until a broker has accepted it. |
instrument_id |
str
|
UBI's id for the instrument. |
exchange |
str
|
The exchange, such as nse. |
segment |
str
|
UBI's segment, such as nse_equities. |
symbol |
str
|
The symbol, or the underlying's for a future or option. |
transaction_type |
str
|
"buy" or "sell". |
quantity |
int
|
How many units. |
product |
str
|
"cnc", "mis" or "nrml". |
price |
float
|
The price the order was valued at. |
dry_run |
bool
|
Whether it was a dry run that reached no broker. |
state |
str
|
"open", or "cancelled" once a desk cancelled it. |
placed_at |
str
|
When it was placed, as ISO text. |
DeskOrderBook
¶
The table of orders placed by the desks, and the questions the order tools ask of it.
Opens the database, creating it and its table if needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
The database file. |
required |
Source code in src/trading_agent/trading/desk_order_book.py
add
¶
Stores a newly placed order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record
|
OrderRecord
|
The order. |
required |
Source code in src/trading_agent/trading/desk_order_book.py
find
¶
Finds one of a desk's orders by its parent_id or its order_id.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
desk
|
str
|
The desk asking; another desk's order is never found. |
required |
parent_id
|
str | None
|
The engine's handle, or None. |
required |
order_id
|
str | None
|
The broker's id, or None. |
required |
Returns:
| Type | Description |
|---|---|
OrderRecord | None
|
The order, or None when this desk placed no such order. |
Source code in src/trading_agent/trading/desk_order_book.py
open_orders
¶
A desk's orders that it has not cancelled.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
desk
|
str
|
The desk. |
required |
Returns:
| Type | Description |
|---|---|
list[OrderRecord]
|
The orders, oldest first. |
Source code in src/trading_agent/trading/desk_order_book.py
change
¶
Records a new quantity and price after an order was modified.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record_id
|
str
|
The order's record id. |
required |
quantity
|
int
|
Its new quantity. |
required |
price
|
float
|
Its new valuation price. |
required |
Source code in src/trading_agent/trading/desk_order_book.py
mark_cancelled
¶
Records that an order was cancelled.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record_id
|
str
|
The order's record id. |
required |
Source code in src/trading_agent/trading/desk_order_book.py
close
¶
now_text
staticmethod
¶
The current time in UTC as ISO text, for placed_at.
Returns:
| Type | Description |
|---|---|
str
|
The time. |