trading_agent.risk.account_snapshot
¶
The state of the account that the risk engine needs, read fresh from UBI before each order.
Typical usage example:
snapshot = await AccountSnapshotReader(broker_reader).read() snapshot.net_quantity('f310e1b0-...')
PositionRow
dataclass
¶
One open position, in one instrument and product, merged across brokers.
Attributes:
| Name | Type | Description |
|---|---|---|
instrument_id |
str
|
UBI's id for the instrument. |
symbol |
str
|
The instrument's symbol. |
segment |
str
|
UBI's segment. |
product |
str
|
UBI's position product, such as intraday or delivery. |
quantity |
int
|
Positive when long, negative when short. |
last_price |
float
|
The latest price. |
AccountSnapshot
dataclass
¶
AccountSnapshot(total_balance, margin_utilized, day_pnl, positions, held_quantities=dict(), held_symbols=frozenset(), day_pnl_by_instrument=dict())
Balances, today's profit and loss, and open positions.
Attributes:
| Name | Type | Description |
|---|---|---|
total_balance |
float
|
The total balance across every broker: available balance plus margin in use. |
margin_utilized |
float
|
Margin already blocked across every broker. |
day_pnl |
float
|
Today's realised plus unrealised profit or loss, negative for a loss. |
positions |
tuple[PositionRow, ...]
|
The open positions. |
held_quantities |
dict[str, int]
|
The quantity held as delivery holdings, by instrument id. |
held_symbols |
frozenset[str]
|
The upper-case symbols of every delivery holding, including holdings UBI gives no instrument id. |
day_pnl_by_instrument |
dict[str, float]
|
Today's profit or loss of each position in UBI's day view, closed ones included, by instrument id. |
net_quantity
¶
The account's total position in one instrument, across products.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instrument_id
|
str
|
UBI's id for the instrument. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Positive when long, negative when short, zero when flat. |
Source code in src/trading_agent/risk/account_snapshot.py
held_instrument_ids
¶
The instruments with a non-zero position or a holding.
Returns:
| Type | Description |
|---|---|
set[str]
|
Their ids. |
Source code in src/trading_agent/risk/account_snapshot.py
AccountSnapshotReader
¶
Reads an AccountSnapshot from UBI.
Creates the reader.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reader
|
BrokerReader
|
The shared reader of UBI. |
required |
Source code in src/trading_agent/risk/account_snapshot.py
read
async
¶
Reads funds and positions.
Returns:
| Type | Description |
|---|---|
AccountSnapshot
|
The account now. |
Raises:
| Type | Description |
|---|---|
UnifiedBrokerInterfaceError
|
UBI could not answer. |