trading_agent.risk.risk_ledger
¶
The risk engine's memory, in a small SQLite database shared by every desk.
It records which desk owns which instrument, which desk owned each instrument on each day (so a desk's profit and loss can be worked out even after it closed a position and gave the instrument up), when each desk's approved orders were sent, each day's profit or loss, and any trading halts, so that all of them survive a restart of the gateway. Only the risk engine and the kill switch write to it; the desks have no tool that can.
Typical usage example:
ledger = RiskLedger(Path('workspace/shared/risk_ledger.db')) owner = ledger.owner(instrument_id)
RiskLedger
¶
The ledger's tables and the questions the risk engine asks of them.
Opens the database, creating it and its tables if needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
The database file. |
required |
Source code in src/trading_agent/risk/risk_ledger.py
owner
¶
The desk that owns an instrument.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instrument_id
|
str
|
The instrument. |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
The desk's name, or None when no desk owns it. |
Source code in src/trading_agent/risk/risk_ledger.py
claim
¶
Gives an instrument to a desk, or renews the desk's claim.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instrument_id
|
str
|
The instrument. |
required |
desk
|
str
|
The desk. |
required |
now
|
datetime
|
When the claim is made. |
required |
Source code in src/trading_agent/risk/risk_ledger.py
release_unheld
¶
Releases claims on instruments that are no longer held.
A claim younger than the grace period is kept even when nothing is held yet, because the desk's order may still be waiting to fill.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
held_instrument_ids
|
set[str]
|
The instruments the account holds now. |
required |
now
|
datetime
|
The current time. |
required |
grace
|
timedelta
|
How long a claim is kept without a position. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
The instruments released. |
Source code in src/trading_agent/risk/risk_ledger.py
day_owners
¶
Which desk owned each instrument on a day, released or not.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trading_date
|
date
|
The India date. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
Desk names keyed by instrument id; the last desk to claim an instrument that day. |
Source code in src/trading_agent/risk/risk_ledger.py
owned_by
¶
The instruments a desk owns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
desk
|
str
|
The desk. |
required |
Returns:
| Type | Description |
|---|---|
set[str]
|
Their ids. |
Source code in src/trading_agent/risk/risk_ledger.py
record_approval
¶
Records that one of a desk's orders was approved.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
desk
|
str
|
The desk. |
required |
now
|
datetime
|
When. |
required |
Source code in src/trading_agent/risk/risk_ledger.py
approvals_since
¶
How many of a desk's orders were approved since a moment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
desk
|
str
|
The desk. |
required |
since
|
datetime
|
The start of the period. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The count. |
Source code in src/trading_agent/risk/risk_ledger.py
record_day_pnl
¶
Stores the latest profit or loss seen for a day, replacing any earlier figure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trading_date
|
date
|
The day, in India time. |
required |
pnl
|
float
|
The profit or loss, negative for a loss. |
required |
Source code in src/trading_agent/risk/risk_ledger.py
week_pnl
¶
The total profit or loss recorded from Monday of a date's week up to that date.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trading_date
|
date
|
A day of the week. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The total, negative for a loss. |
Source code in src/trading_agent/risk/risk_ledger.py
halt
¶
Stops a desk, or the whole account, from opening positions until a date.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scope
|
str
|
ACCOUNT_SCOPE, or a desk's name. |
required |
reason
|
str
|
Why. |
required |
until_date
|
date
|
The first day trading is allowed again. |
required |
now
|
datetime
|
When the halt starts. |
required |
Source code in src/trading_agent/risk/risk_ledger.py
active_halt
¶
The reason for a halt that is still in force.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scope
|
str
|
ACCOUNT_SCOPE, or a desk's name. |
required |
today
|
date
|
Today, in India time. |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
The newest halt's reason, or None when no halt applies today. |
Source code in src/trading_agent/risk/risk_ledger.py
owners
¶
Every instrument a desk owns now.
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
Desk names keyed by instrument id. |
Source code in src/trading_agent/risk/risk_ledger.py
halts_in_force
¶
Every halt still in force, the account's and the desks'.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
today
|
date
|
Today, in India time. |
required |
Returns:
| Type | Description |
|---|---|
list[dict[str, str]]
|
Each halt's scope, reason and the first day trading is allowed again, newest first. |