Hierarchical Risk Limits
Prerequisites: Order Gateway and Risk Checks, Trade Lifecycle Overview
The position limit in the gateway article caps what one account holds. A firm sets limits at several levels at once: each trader's account rolls up into a desk, each desk into a firm-wide account, and every level has its own cap.
A fill in one trader's AAPL account uses up capacity in that account, in its desk, and in the firm. Build the bookkeeping that reports, after every event, which accounts are over their limits.
- Direct gross exposure: the sum, over the symbols an account holds, of
abs(position) * price. - Aggregate exposure: an account's own direct gross exposure plus the direct gross exposures of all its descendants.
- Breach: an account breaches when its aggregate exposure is strictly greater than its limit.
What to implement
process_events(accounts, prices, events)appends one status line after applying each event.- Row
iofaccountsis[parent, limit]for accounti, and exactly one parent is-1. - Each row of
pricesis[symbol, price], with distinct symbols. Each row ofeventsis one event split on spaces. - On stdin the three lists appear in that order, each as a count line followed by that many rows.
- There are two parts, and Part 2 adds an event type on top of Part 1. One solution must pass all eleven tests.
| Event | What it does |
|---|---|
TRADE account symbol delta | Add delta to the account's position in symbol |
PRICE symbol new_price | Replace the symbol's price (Part 2) |
The account tree
- The accounts form a rooted tree numbered
0toN-1. Each account has a parent (the root's parent is-1) and a non-negative limit. - Any account may hold its own positions, including a desk or the firm itself.
- Positions start at zero, and each symbol has a positive integer price in ticks.
- Positions in different accounts do not net. A long position in one trader's account and a short position in a sibling's both add to the desk's aggregate exposure.
Part 1: Trades at fixed prices
TRADE account symbol deltaaddsdelta, which may be negative, to the account's position insymbol.- Prices keep their starting values.
- A trade can carry a position through zero, from long to short or back. Exposure always uses the absolute value of the resulting position.
Output: one status line after each event.
OK
BREACH k a1 ... ak
OKif no account breaches.- Otherwise
BREACHlists allkbreached accounts in ascending numeric order.
Limits
- Limits, prices, positions, and exposures fit in signed 64-bit integers.
Tests 1–7 cover this part. None of them contains a PRICE event.
Part 2: Reprice a symbol
Market prices now move.
PRICE symbol new_pricereplaces the symbol's price with a new positive price before breach detection.- Positions do not change, but the direct gross exposure of every account with a nonzero position in that symbol changes.
- So does the aggregate exposure of each of that account's ancestors.
Tests 8–11 cover this part.
3 -1 90 0 60 1 30 1 X 10 3 TRADE 2 X 4 TRADE 1 X 3 TRADE 2 X -2
BREACH 1 2 BREACH 2 1 2 OK
Account 0 is the firm with a limit of 90, account 1 is a desk under it with a limit of 60, and account 2 is a trader's account under the desk with a limit of 30.
Buying 4 X at price 10 gives account 2 an exposure of 40, over its limit of 30. The desk and the firm both see an aggregate of 40, within their limits.
The desk's own 3 units add 30, so its aggregate becomes 30 + 40 = 70, over 60. The firm's 70 stays within 90, so accounts 1 and 2 breach.
Selling 2 units in account 2 leaves 2 units worth 20. That puts account 2 at 20, the desk at 50, and the firm at 50, and nothing breaches.