trading_agent.risk.kill_switch
¶
The kill switch: stops every desk and closes every position in the account.
It calls tradingmachine's Account.flatten, which halts UBI's order engine, cancels every open order at every broker and closes every position. tradingmachine warns that a flatten that times out may still have happened, and that calling it twice can close positions twice, so the halt is written to the ledger before UBI is called, and the switch never fires again while that halt is in force. In dry_run mode the flatten is a dry run: UBI reports what it would cancel and close, and sends nothing.
Typical usage example:
kill_switch = KillSwitch(reader, ledger, event_bus, 'dry_run') await kill_switch.trigger('Daily loss limit reached', until_date, now)
KillSwitch
¶
Halts trading and flattens the account.
Creates the switch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reader
|
BrokerReader
|
The shared reader, whose client the flatten goes through. |
required |
ledger
|
RiskLedger
|
Where the halt is recorded. |
required |
event_bus
|
EventBus
|
Where the switch's firing and its outcome are published. |
required |
mode
|
str
|
"dry_run" for a flatten that sends nothing, "live" for a real one. |
required |
Source code in src/trading_agent/risk/kill_switch.py
trigger
async
¶
Halts the account and flattens it, unless a halt is already in force.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reason
|
str
|
Why, for the ledger, the journal and the desks. |
required |
until_date
|
date
|
The first day trading is allowed again. |
required |
now
|
datetime
|
The current time. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
UBI's answer to the flatten, an error description, or a note that the switch had already fired. |