Add the trading core: ledger, simulator, order manager, reconciliation (refs #26 #27 #28 #29 #30)

Phase 3 needs books the bot can trust before any strategy trades. This adds:
- schema 0003: executions, order events, trades, residues, account status,
  reconciliations, capital changes, applied corporate actions, simulator books;
- the broker interface with the internal simulator and the Alpaca paper and
  OKX demo adapters, all behind the live guard (SPEC 2.2);
- the append-only ledger with lots, trades from zero to zero, residues,
  dividends, splits and reversals; capital entrusted per broker and equity
  photographs with time-weighted return and FX effect;
- the order manager (chasing limit, pacing, sells never abandoned, server
  stops, breached-stop rule) and the reconciler with prudent mode;
- the trading service wired into the host, off in demo mode and in tests.

Ids made with one clock in the same millisecond now carry a counter (RFC 9562
method 1): two lots of one order kept a random order, which broke LIFO.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-10 20:29:02 +01:00
1 parent 74d4c4fa6f
commit 1419457fe4
50 files changed
+7327 -5

No files matched your search

@@ -0,0 +1,39 @@
{
"account_blocked": false,
"account_number": "PALPACA_123",
"accrued_fees": "0",
"admin_configurations": {},
"balance_asof": "2023-09-27",
"buying_power": "245432.61",
"cash": "122086.5",
"created_at": "2023-01-01T18:20:20.272275Z",
"crypto_status": "ACTIVE",
"crypto_tier": 1,
"currency": "USD",
"effective_buying_power": "245432.61",
"equity": "123346.11",
"id": "1d9eed04-be39-4e01-9b84-a48ac5bbafcf",
"initial_margin": "629.8",
"intraday_adjustments": "0",
"last_equity": "122011.09751111286868",
"last_maintenance_margin": "480.73",
"long_market_value": "1259.61",
"maintenance_margin": "377.88",
"multiplier": "2",
"non_marginable_buying_power": "122086.5",
"options_buying_power": "122716.305",
"options_trading_level": 2,
"pending_reg_taf_fees": "0",
"pending_transfer_in": "0",
"portfolio_value": "123346.11",
"position_market_value": "1259.61",
"regt_buying_power": "245432.61",
"short_market_value": "0",
"shorting_enabled": true,
"sma": "123369.74",
"status": "ACTIVE",
"trade_suspended_by_user": false,
"trading_blocked": false,
"transfers_blocked": false,
"user_configurations": null
}
@@ -0,0 +1,10 @@
{
"activity_type": "DIV",
"id": "20190801011955195::5f596936-6f23-4cef-bdf1-3806aae57dbf",
"date": "2019-08-01",
"net_amount": "1.02",
"symbol": "T",
"cusip": "C00206R102",
"qty": "2",
"per_share_amount": "0.51"
}
@@ -0,0 +1,13 @@
{
"activity_type": "FILL",
"cum_qty": "1",
"id": "20190524113406977::8efc7b9a-8b2b-4000-9955-d36e7db0df74",
"leaves_qty": "0",
"price": "1.63",
"qty": "1",
"side": "buy",
"symbol": "LPCN",
"transaction_time": "2019-05-24T15:34:06.977Z",
"order_id": "904837e3-3b76-47ec-b432-046db621571b",
"type": "fill"
}
@@ -0,0 +1,4 @@
{
"code": 42210000,
"message": "invalid limit_price 290.123. sub-penny increment does not fulfill minimum pricing criteria"
}
@@ -0,0 +1,36 @@
{
"asset_class": "crypto",
"asset_id": "a1733398-6acc-4e92-af24-0d0667f78713",
"canceled_at": null,
"client_order_id": "5b5d3d67-06ad-4ffa-af65-a117d0fc5a59",
"created_at": "2023-12-12T22:36:51.337711497Z",
"expired_at": null,
"extended_hours": false,
"failed_at": null,
"filled_at": null,
"filled_avg_price": null,
"filled_qty": "0",
"hwm": null,
"id": "38e482f3-79a8-4f75-a057-f07a1ec6a397",
"legs": null,
"limit_price": "2100",
"notional": null,
"order_class": "",
"order_type": "limit",
"qty": "0.02",
"replaced_at": null,
"replaced_by": null,
"replaces": null,
"side": "buy",
"source": null,
"status": "pending_new",
"stop_price": null,
"submitted_at": "2023-12-12T22:36:51.313261061Z",
"subtag": null,
"symbol": "ETH/USD",
"time_in_force": "gtc",
"trail_percent": null,
"trail_price": null,
"type": "limit",
"updated_at": "2023-12-12T22:36:51.337754768Z"
}
@@ -0,0 +1,36 @@
{
"asset_class": "us_equity",
"asset_id": "b0b6dd9d-8b9b-48a9-ba46-b9d54906e415",
"canceled_at": null,
"client_order_id": "5680c4bc-9ac1-4a12-a44c-df427ba53032",
"created_at": "2023-12-12T22:31:24.668464435Z",
"expired_at": null,
"extended_hours": false,
"failed_at": null,
"filled_at": null,
"filled_avg_price": null,
"filled_qty": "0",
"hwm": null,
"id": "7b08df51-c1ac-453c-99f9-323a5f075f0d",
"legs": null,
"limit_price": "150",
"notional": null,
"order_class": "",
"order_type": "limit",
"qty": "2",
"replaced_at": null,
"replaced_by": null,
"replaces": null,
"side": "buy",
"source": null,
"status": "accepted",
"stop_price": null,
"submitted_at": "2023-12-12T22:31:24.577215743Z",
"subtag": null,
"symbol": "AAPL",
"time_in_force": "gtc",
"trail_percent": null,
"trail_price": null,
"type": "limit",
"updated_at": "2023-12-12T22:31:24.668464435Z"
}
@@ -0,0 +1,20 @@
{
"asset_class": "us_equity",
"asset_id": "904837e3-3b76-47ec-b432-046db621571b",
"asset_marginable": true,
"avg_entry_price": "100.0",
"change_today": "0.0084",
"cost_basis": "500.0",
"current_price": "120.0",
"exchange": "NASDAQ",
"lastday_price": "119.0",
"market_value": "600.0",
"qty": "5",
"qty_available": "4",
"side": "long",
"symbol": "AAPL",
"unrealized_intraday_pl": "10.0",
"unrealized_intraday_plpc": "0.0084",
"unrealized_pl": "100.0",
"unrealized_plpc": "0.20"
}
@@ -0,0 +1,7 @@
{
"stream": "authorization",
"data": {
"status": "authorized",
"action": "authenticate"
}
}
@@ -0,0 +1,46 @@
{
"stream": "trade_updates",
"data": {
"event": "fill",
"execution_id": "2f63ea93-423d-4169-b3f6-3fdafc10c418",
"order": {
"asset_class": "crypto",
"asset_id": "1cf35270-99ee-44e2-a77f-6fab902c7f80",
"cancel_requested_at": null,
"canceled_at": null,
"client_order_id": "4642fd68-d59a-47d7-a9ac-e22f536828d1",
"created_at": "2022-04-19T13:45:04.981350886-04:00",
"expired_at": null,
"extended_hours": false,
"failed_at": null,
"filled_at": "2022-04-19T17:45:05.024916716Z",
"filled_avg_price": "105.8988475",
"filled_qty": "1790.86",
"hwm": null,
"id": "a5be8f5e-fdfa-41f5-a644-7a74fe947a8f",
"legs": null,
"limit_price": null,
"notional": null,
"order_class": "",
"order_type": "market",
"qty": "1790.86",
"replaced_at": null,
"replaced_by": null,
"replaces": null,
"side": "sell",
"status": "filled",
"stop_price": null,
"submitted_at": "2022-04-19T13:45:04.980944666-04:00",
"symbol": "SOLUSD",
"time_in_force": "gtc",
"trail_percent": null,
"trail_price": null,
"type": "market",
"updated_at": "2022-04-19T13:45:05.027690731-04:00"
},
"position_qty": "0",
"price": "105.8988475",
"qty": "1790.86",
"timestamp": "2022-04-19T17:45:05.024916716Z"
}
}
@@ -0,0 +1,6 @@
{
"stream": "listening",
"data": {
"streams": ["trade_updates"]
}
}