Cross-Margin Liquidation Engine yenw0d

Simulate a cross-margin derivatives engine: process trades, track account equity, and liquidate undercollateralized accounts as fast as possible.

Accounts are funded with a USD deposit and trade instruments whose prices change over time. When a price update causes an account’s equity to fall below 1% of its total position notional, the account must be liquidated.

Definitions

  • Account equity = balance + Σ(size_i * price_i) - Σ(total_paid_i)
  • Total position notional = Σ(|size_i| * price_i)
  • Total paid for instrument i = signed sum of trade_size * price_at_trade_time across all trades
  • Margin rule: liquidate if equity < total_notional / 100 after any price update

Input

One command per line on STDIN:

  • a <balance> – create account with USD balance (IDs start at 0, incrementing)
  • p <instrument_idx> <price> – set/update instrument price (0-indexed)
  • t <account_idx> <instrument_idx> <size> – trade size (signed) at current price
  • Final line: <account_idx> – query this account and terminate

Output

On each price update, liquidate all accounts violating the margin rule. For each liquidation, print:

liquidate <account_id> <equity> <position_notional>

Liquidation order: largest total position notional first, then account ID descending as tie-breaker. Liquidated accounts have their balance and positions cleared.

For the final query, print <equity> <notional> for the requested account.

Constraints

  • Accounts: <= 100,000
  • Instruments: <= 1,000
  • Price range: 100 to 1,000,000
  • Trade size range: 1 to 10,000

Example

a 100
p 0 100
t 0 0 10
p 0 90
0

Output:

liquidate 0 0 900
0 0
Date AuthorLanguageStatus Score
Oct 12, 2025 23:06WilmerFinleyWilmerFinleyCPPg++13.3.0Success198,759+22.62 RP
Oct 12, 2025 22:57WilmerFinleyWilmerFinleyCPPg++13.3.0Success361,145+27.69 RP
Oct 12, 2025 22:56imotw2imotw2CPPg++13.3.0Error
Oct 12, 2025 22:47imotw2imotw2CPPg++13.3.0Error
Oct 12, 2025 20:33WilmerFinleyWilmerFinleyCPPg++13.3.0Error
Oct 12, 2025 20:00imotw2imotw2CPPg++13.3.0Error
Oct 12, 2025 19:46imotw2imotw2CPPg++13.3.0Success501,795
Oct 12, 2025 19:38imotw2imotw2CPPg++13.3.0Success44,414+6.97 RP
Oct 12, 2025 19:37imotw2imotw2CPPg++13.3.0Success458,939
Oct 12, 2025 19:34imotw2imotw2CPPclang++18.1.3Success45,832+8.86 RP
Oct 12, 2025 19:33imotw2imotw2CPPg++13.3.0Success67,714
Oct 12, 2025 19:33Carl BergmanCarl BergmanRUSTrust-1.90.0Success212,202+35.30 RP
Oct 12, 2025 19:31imotw2imotw2CPPg++13.3.0Error
Oct 12, 2025 19:28Carl BergmanCarl BergmanRUSTrust-1.90.0Success845,355+0.57 RP
Oct 12, 2025 19:26imotw2imotw2CPPg++13.3.0Error
Oct 12, 2025 19:24imotw2imotw2CPPg++13.3.0Success91,375
Oct 12, 2025 19:22Carl BergmanCarl BergmanRUSTrust-1.90.0Success888,125+11.26 RP
Oct 12, 2025 19:17imotw2imotw2CPPg++13.3.0Success56,613
Oct 12, 2025 19:13imotw2imotw2CPPg++13.3.0Success51,918
Oct 12, 2025 19:09imotw2imotw2CPPg++13.3.0Success49,590
Oct 12, 2025 19:03Carl BergmanCarl BergmanRUSTrust-1.90.0Error
Oct 12, 2025 18:56imotw2imotw2CPPg++13.3.0Success47,773+3.23 RP
Oct 12, 2025 18:46imotw2imotw2CPPg++13.3.0Success48,522+5.60 RP
Oct 12, 2025 18:45imotw2imotw2CPPg++13.3.0Success51,777
Oct 12, 2025 18:44imotw2imotw2CPPg++13.3.0Success50,717
Oct 12, 2025 18:32imotw2imotw2CPPg++13.3.0Success49,878+51.25 RP
Oct 12, 2025 17:59imotw2imotw2CPPg++13.3.0Success79,674
Oct 12, 2025 17:53imotw2imotw2CPPg++13.3.0Success67,006+38.42 RP
Oct 12, 2025 17:47imotw2imotw2CPPg++13.3.0Error
Oct 12, 2025 17:43imotw2imotw2CPPg++13.3.0Error
Oct 12, 2025 17:42imotw2imotw2CPPg++13.3.0Error
Oct 12, 2025 17:29imotw2imotw2CPPg++13.3.0Success90,237+44.31 RP
Oct 12, 2025 17:16imotw2imotw2CPPg++13.3.0Success272,544
Oct 12, 2025 17:04imotw2imotw2CPPg++13.3.0Success151,699
Oct 12, 2025 17:03imotw2imotw2CPPg++13.3.0Error
Oct 12, 2025 16:30yenw0dyenw0dRUSTrust-1.90.0Success6,109+18.64 RP
Oct 12, 2025 14:12yenw0dyenw0dRUSTrust-1.90.0Success6,165
Oct 12, 2025 14:10yenw0dyenw0dRUSTrust-1.90.0Success6,353
Oct 12, 2025 14:09yenw0dyenw0dRUSTrust-1.90.0Success6,178
Oct 12, 2025 14:07yenw0dyenw0dRUSTrust-1.90.0Success6,410
Oct 12, 2025 14:04yenw0dyenw0dRUSTrust-1.90.0Success6,300
Oct 12, 2025 11:50Enda CarrollEnda CarrollRUSTrust-1.90.0Success233,148+42.89 RP
Oct 12, 2025 04:35Carl BergmanCarl BergmanRUSTrust-1.90.0Error
Oct 12, 2025 04:33Carl BergmanCarl BergmanRUSTrust-1.90.0Error
Oct 11, 2025 23:47imotw2imotw2CPPg++13.3.0Success156,941
Oct 11, 2025 23:47imotw2imotw2CPPg++13.3.0Success150,350+0.53 RP
Oct 11, 2025 23:44imotw2imotw2CPPg++13.3.0Success178,548
Oct 11, 2025 23:42imotw2imotw2CPPg++13.3.0Success155,643
Oct 11, 2025 23:38imotw2imotw2CPPg++13.3.0Success224,891
Oct 11, 2025 23:30imotw2imotw2CPPg++13.3.0Error
Oct 11, 2025 23:22imotw2imotw2CPPg++13.3.0Success172,466
Oct 11, 2025 23:20imotw2imotw2CPPg++13.3.0Error
Oct 11, 2025 23:09imotw2imotw2CPPg++13.3.0Success158,018
Oct 11, 2025 23:07imotw2imotw2CPPg++13.3.0Error
Oct 11, 2025 23:06imotw2imotw2CPPg++13.3.0Success179,793
Oct 11, 2025 23:04imotw2imotw2CPPg++13.3.0Success151,557+12.25 RP
Oct 11, 2025 23:03imotw2imotw2CPPg++13.3.0Success239,668
Oct 11, 2025 22:58imotw2imotw2CPPg++13.3.0Error
Oct 11, 2025 22:50imotw2imotw2CPPg++13.3.0Error
Oct 11, 2025 22:45Ludvig SjöströmLudvig SjöströmCPPg++13.3.0Success167,102+6.33 RP
Oct 11, 2025 22:36imotw2imotw2CPPg++13.3.0Success186,103+11.96 RP
Oct 11, 2025 22:34imotw2imotw2CPPg++13.3.0Success260,980
Oct 11, 2025 22:27imotw2imotw2CPPg++13.3.0Success239,389+41.77 RP
Oct 11, 2025 22:22imotw2imotw2CPPg++13.3.0Error
Oct 11, 2025 22:12imotw2imotw2CPPg++13.3.0Error
Oct 11, 2025 22:05Ludvig SjöströmLudvig SjöströmCPPg++13.3.0Error
Oct 11, 2025 21:47Ludvig SjöströmLudvig SjöströmCPPg++13.3.0Success186,882+0.09 RP
Oct 11, 2025 21:46Ludvig SjöströmLudvig SjöströmCPPg++13.3.0Success191,957
Oct 11, 2025 21:41Ludvig SjöströmLudvig SjöströmCPPg++13.3.0Success187,201+31.89 RP
Oct 11, 2025 21:19Ludvig SjöströmLudvig SjöströmCPPg++13.3.0Success464,505+21.53 RP
Oct 10, 2025 22:16yenw0dyenw0dRUSTrust-1.90.0Success6,567
Oct 10, 2025 21:07yenw0dyenw0dRUSTrust-1.90.0Success6,299
Oct 10, 2025 21:04yenw0dyenw0dRUSTrust-1.90.0Success7,576
Oct 10, 2025 21:03yenw0dyenw0dRUSTrust-1.90.0Success7,601
Oct 10, 2025 20:59yenw0dyenw0dRUSTrust-1.90.0Success7,525
Oct 10, 2025 20:55yenw0dyenw0dRUSTrust-1.90.0Success7,552
Oct 10, 2025 20:52yenw0dyenw0dRUSTrust-1.90.0Success7,648
Oct 10, 2025 20:30yenw0dyenw0dRUSTrust-1.90.0Success6,144+243.16 RP
Oct 10, 2025 20:28yenw0dyenw0dRUSTrust-1.90.0Success7,541
Oct 10, 2025 20:27yenw0dyenw0dRUSTrust-1.90.0Success7,755
Oct 9, 2025 19:54KeefeKeefeCPPg++13.3.0Success229,424
Oct 9, 2025 19:44KeefeKeefeCPPg++13.3.0Success194,488
Oct 9, 2025 19:37KeefeKeefeCPPg++13.3.0Success541,573
Oct 9, 2025 19:28KeefeKeefeCPPg++13.3.0Error
Oct 9, 2025 15:48Yuriy LyfenkoYuriy LyfenkoCPPclang++18.1.3Success6,734
Oct 9, 2025 15:47Yuriy LyfenkoYuriy LyfenkoCPPclang++18.1.3Success6,655
Oct 9, 2025 15:45Yuriy LyfenkoYuriy LyfenkoCPPg++13.3.0Success6,930
Oct 9, 2025 15:44Yuriy LyfenkoYuriy LyfenkoCPPclang++18.1.3Success5,990+62.36 RP
Oct 9, 2025 15:34Yuriy LyfenkoYuriy LyfenkoCPPclang++18.1.3Success6,244
Oct 9, 2025 15:31Yuriy LyfenkoYuriy LyfenkoCPPclang++18.1.3Success6,104+258.12 RP
Oct 9, 2025 15:20Yuriy LyfenkoYuriy LyfenkoCPPclang++18.1.3Success6,658
Oct 9, 2025 15:19Yuriy LyfenkoYuriy LyfenkoCPPg++13.3.0Success7,237
Oct 9, 2025 15:18Yuriy LyfenkoYuriy LyfenkoCPPclang++18.1.3Success6,626+259.04 RP
Oct 9, 2025 14:58Yuriy LyfenkoYuriy LyfenkoCPPclang++18.1.3Success7,248+374.18 RP
Oct 9, 2025 14:57Yuriy LyfenkoYuriy LyfenkoCPPg++13.3.0Success8,409
Oct 9, 2025 14:56Yuriy LyfenkoYuriy LyfenkoCPPg++13.3.0Success8,385+2'074.06 RP
Oct 8, 2025 20:11Yuriy LyfenkoYuriy LyfenkoCPPclang++18.1.3Success225,271
Oct 8, 2025 18:41Yuriy LyfenkoYuriy LyfenkoCPPclang++18.1.3Error
Oct 8, 2025 18:17Yuriy LyfenkoYuriy LyfenkoCPPclang++18.1.3Error
Oct 8, 2025 17:49Yuriy LyfenkoYuriy LyfenkoCPPclang++18.1.3Success82,282