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"]
}
}
@@ -0,0 +1,37 @@
{
"code": "0",
"data": [
{
"acctLv": "2",
"acctStpMode": "cancel_maker",
"autoLoan": false,
"ctIsoMode": "automatic",
"enableSpotBorrow": false,
"greeksType": "PA",
"feeType": "0",
"ip": "",
"type": "0",
"kycLv": "3",
"label": "v5 test",
"level": "Lv1",
"levelTmp": "",
"liquidationGear": "-1",
"mainUid": "44705892343619584",
"mgnIsoMode": "automatic",
"opAuth": "1",
"perm": "read_only,withdraw,trade",
"posMode": "long_short_mode",
"roleType": "0",
"spotBorrowAutoRepay": false,
"spotOffsetType": "",
"spotRoleType": "0",
"spotTraderInsts": [],
"stgyType": "0",
"traderInsts": [],
"uid": "44705892343619584",
"settleCcy": "USDC",
"settleCcyList": ["USD", "USDC", "USDG"]
}
],
"msg": ""
}
@@ -0,0 +1,14 @@
{
"code": "0",
"data": [
{
"algoClOrdId": "",
"algoId": "1836489397437468672",
"clOrdId": "",
"sCode": "0",
"sMsg": "",
"tag": ""
}
],
"msg": ""
}
@@ -0,0 +1,14 @@
{
"code": "0",
"data": [
{
"algoClOrdId": "order1234",
"algoId": "1836487817828872192",
"clOrdId": "",
"sCode": "0",
"sMsg": "",
"tag": ""
}
],
"msg": ""
}
@@ -0,0 +1,65 @@
{
"code": "0",
"data": [
{
"activePx": "",
"actualPx": "",
"actualSide": "",
"actualSz": "0",
"algoClOrdId": "",
"algoId": "1753184812254216192",
"amendPxOnTriggerType": "0",
"attachAlgoOrds": [],
"cTime": "1724751378980",
"callbackRatio": "",
"callbackSpread": "",
"ccy": "",
"chaseType": "",
"chaseVal": "",
"clOrdId": "",
"closeFraction": "",
"failCode": "0",
"instId": "BTC-USDT",
"instType": "SPOT",
"isTradeBorrowMode": "",
"last": "62916.5",
"lever": "",
"linkedOrd": {
"ordId": ""
},
"maxChaseType": "",
"maxChaseVal": "",
"moveTriggerPx": "",
"ordId": "",
"ordIdList": [],
"ordPx": "",
"ordType": "conditional",
"posSide": "net",
"pxLimit": "",
"pxSpread": "",
"pxVar": "",
"quickMgnType": "",
"reduceOnly": "false",
"side": "buy",
"slOrdPx": "",
"slTriggerPx": "",
"slTriggerPxType": "",
"state": "live",
"sz": "10",
"szLimit": "",
"tag": "",
"tdMode": "cash",
"tgtCcy": "quote_ccy",
"timeInterval": "",
"tpOrdPx": "-1",
"tpTriggerPx": "10000",
"tpTriggerPxType": "last",
"triggerPx": "",
"triggerPxType": "",
"triggerTime": "",
"tradeQuoteCcy": "USDT",
"uTime": "1724751378980"
}
],
"msg": ""
}
@@ -0,0 +1,79 @@
{
"code": "0",
"data": [
{
"adjEq": "55415.624719833286",
"availEq": "55415.624719833286",
"borrowFroz": "0",
"delta": "0",
"deltaLever": "0",
"deltaNeutralStatus": "0",
"details": [
{
"autoLendStatus": "off",
"autoLendMtAmt": "0",
"availBal": "4834.317093622894",
"availEq": "4834.3170936228935",
"borrowFroz": "0",
"cashBal": "4850.435693622894",
"ccy": "USDT",
"crossLiab": "0",
"colRes": "0",
"collateralEnabled": false,
"collateralRestrict": false,
"colBorrAutoConversion": "0",
"disEq": "4991.542013297616",
"eq": "4992.890093622894",
"eqUsd": "4991.542013297616",
"smtSyncEq": "0",
"spotCopyTradingEq": "0",
"fixedBal": "0",
"frozenBal": "158.573",
"frpType": "0",
"imr": "",
"interest": "0",
"isoEq": "0",
"isoLiab": "0",
"isoUpl": "0",
"liab": "0",
"maxLoan": "0",
"mgnRatio": "",
"mmr": "",
"notionalLever": "",
"ordFrozen": "0",
"rewardBal": "0",
"spotInUseAmt": "",
"clSpotInUseAmt": "",
"maxSpotInUse": "",
"spotIsoBal": "0",
"stgyEq": "150",
"twap": "0",
"uTime": "1705449605015",
"upl": "-7.545600000000006",
"uplLiab": "0",
"spotBal": "",
"openAvgPx": "",
"accAvgPx": "",
"spotUpl": "",
"spotUplRatio": "",
"totalPnl": "",
"totalPnlRatio": ""
}
],
"imr": "0",
"isoEq": "0",
"mgnRatio": "",
"mmr": "0",
"notionalUsd": "0",
"notionalUsdForBorrow": "0",
"notionalUsdForFutures": "0",
"notionalUsdForOption": "0",
"notionalUsdForSwap": "0",
"ordFroz": "",
"totalEq": "55837.43556134779",
"uTime": "1705474164160",
"upl": "0"
}
],
"msg": ""
}
@@ -0,0 +1,34 @@
{
"code": "0",
"data": [
{
"side": "buy",
"fillSz": "0.00192834",
"fillPx": "51858",
"fillPxVol": "",
"fillFwdPx": "",
"fee": "-0.00000192834",
"fillPnl": "0",
"ordId": "680800019749904384",
"feeRate": "-0.001",
"instType": "SPOT",
"fillPxUsd": "",
"instId": "BTC-USDT",
"clOrdId": "",
"posSide": "net",
"billId": "680800019754098688",
"subType": "1",
"fillMarkVol": "",
"tag": "",
"fillTime": "1708587373361",
"execType": "T",
"fillIdxPx": "",
"tradeId": "744876980",
"fillMarkPx": "",
"feeCcy": "BTC",
"ts": "1708587373362",
"tradeQuoteCcy": "USDT"
}
],
"msg": ""
}
@@ -0,0 +1,15 @@
{
"code":"0",
"msg":"",
"data":[
{
"clOrdId":"oktswap6",
"ordId":"12345689",
"ts":"1695190491421",
"sCode":"0",
"sMsg":""
}
],
"inTime": "1695190491421339",
"outTime": "1695190491423240"
}
@@ -0,0 +1,18 @@
{
"id": "1512",
"op": "order",
"data": [
{
"clOrdId": "",
"ordId": "",
"tag": "",
"ts":"1695190491421",
"sCode": "5XXXX",
"sMsg": "not exist"
}
],
"code": "1",
"msg": "",
"inTime": "1695190491421339",
"outTime": "1695190491423240"
}
@@ -0,0 +1,62 @@
{
"code": "0",
"data": [
{
"accFillSz": "0.00192834",
"algoClOrdId": "",
"algoId": "",
"attachAlgoClOrdId": "",
"attachAlgoOrds": [],
"avgPx": "51858",
"cTime": "1708587373361",
"cancelSource": "",
"cancelSourceReason": "",
"category": "normal",
"ccy": "",
"clOrdId": "",
"fee": "-0.00000192834",
"feeCcy": "BTC",
"fillPx": "51858",
"fillSz": "0.00192834",
"fillTime": "1708587373361",
"instId": "BTC-USDT",
"instType": "SPOT",
"isTpLimit": "false",
"lever": "",
"linkedAlgoOrd": {
"algoId": ""
},
"ordId": "680800019749904384",
"ordType": "market",
"pnl": "0",
"posSide": "net",
"px": "",
"pxType": "",
"pxUsd": "",
"pxVol": "",
"quickMgnType": "",
"rebate": "0",
"rebateCcy": "USDT",
"reduceOnly": "false",
"side": "buy",
"slOrdPx": "",
"slTriggerPx": "",
"slTriggerPxType": "",
"source": "",
"state": "filled",
"stpId": "",
"stpMode": "",
"sz": "100",
"tag": "",
"tdMode": "cash",
"tgtCcy": "quote_ccy",
"tpOrdPx": "",
"tpTriggerPx": "",
"tpTriggerPxType": "",
"tradeId": "744876980",
"tradeQuoteCcy": "USDT",
"uTime": "1708587373362"
}
],
"msg": ""
}
@@ -0,0 +1,17 @@
{
"code": "0",
"msg": "",
"data": [
{
"clOrdId": "oktswap6",
"ordId": "312269865356374016",
"tag": "",
"ts":"1695190491421",
"sCode": "0",
"sMsg": "",
"subCode": ""
}
],
"inTime": "1695190491421339",
"outTime": "1695190491423240"
}
@@ -0,0 +1,62 @@
{
"code": "0",
"data": [
{
"accFillSz": "0",
"algoClOrdId": "",
"algoId": "",
"attachAlgoClOrdId": "",
"attachAlgoOrds": [],
"avgPx": "",
"cTime": "1724733617998",
"cancelSource": "",
"cancelSourceReason": "",
"category": "normal",
"ccy": "",
"clOrdId": "",
"fee": "0",
"feeCcy": "BTC",
"fillPx": "",
"fillSz": "0",
"fillTime": "",
"instId": "BTC-USDT",
"instType": "SPOT",
"isTpLimit": "false",
"lever": "",
"linkedAlgoOrd": {
"algoId": ""
},
"ordId": "1752588852617379840",
"ordType": "post_only",
"pnl": "0",
"posSide": "net",
"px": "13013.5",
"pxType": "",
"pxUsd": "",
"pxVol": "",
"quickMgnType": "",
"rebate": "0",
"rebateCcy": "USDT",
"reduceOnly": "false",
"side": "buy",
"slOrdPx": "",
"slTriggerPx": "",
"slTriggerPxType": "",
"source": "",
"state": "live",
"stpId": "",
"stpMode": "cancel_maker",
"sz": "0.001",
"tag": "",
"tdMode": "cash",
"tgtCcy": "",
"tpOrdPx": "",
"tpTriggerPx": "",
"tpTriggerPxType": "",
"tradeId": "",
"tradeQuoteCcy": "USDT",
"uTime": "1724733617998"
}
],
"msg": ""
}
@@ -0,0 +1,31 @@
{
"code": "0",
"data": [
{
"category": "1",
"delivery": "",
"exercise": "",
"feeGroup": [
{
"elpMaker": "-0.0008",
"rpiMaker": "-0.0008",
"groupId": "1",
"maker": "-0.0008",
"taker": "-0.001"
}
],
"fiat": [],
"instType": "SPOT",
"level": "Lv1",
"maker": "-0.0008",
"makerU": "",
"makerUSDC": "",
"ruleType": "normal",
"taker": "-0.001",
"takerU": "",
"takerUSDC": "",
"ts": "1763979985847"
}
],
"msg": ""
}
@@ -0,0 +1,56 @@
{
"arg": {
"channel": "orders-algo",
"uid": "77982378738415879",
"instType": "FUTURES",
"instId": "BTC-USD-200329"
},
"data": [{
"actualPx": "0",
"actualSide": "",
"actualSz": "0",
"algoClOrdId": "",
"algoId": "581878926302093312",
"attachAlgoOrds": [],
"amendResult": "",
"cTime": "1685002746818",
"uTime": "1708679675245",
"ccy": "",
"clOrdId": "",
"closeFraction": "",
"failCode": "",
"instId": "BTC-USDC",
"instType": "SPOT",
"last": "26174.8",
"lever": "0",
"notionalUsd": "11.0",
"ordId": "",
"ordIdList": [],
"ordPx": "",
"ordType": "conditional",
"posSide": "",
"quickMgnType": "",
"reduceOnly": "false",
"reqId": "",
"side": "buy",
"slOrdPx": "",
"slTriggerPx": "",
"slTriggerPxType": "",
"state": "live",
"sz": "11",
"tag": "",
"tdMode": "cross",
"tgtCcy": "quote_ccy",
"tpOrdPx": "-1",
"tpTriggerPx": "1",
"tpTriggerPxType": "last",
"triggerPx": "",
"triggerTime": "",
"tradeQuoteCcy": "USDT",
"amendPxOnTriggerType": "0",
"linkedOrd":{
"ordId":"98192973880283"
},
"isTradeBorrowMode": ""
}]
}
@@ -0,0 +1,6 @@
{
"event": "login",
"code": "0",
"msg": "",
"connId": "a4d3ae55"
}
@@ -0,0 +1,84 @@
{
"arg": {
"channel": "orders",
"instType": "SPOT",
"instId": "BTC-USDT",
"uid": "614488474791936"
},
"data": [
{
"accFillSz": "0.001",
"algoClOrdId": "",
"algoId": "",
"amendResult": "",
"amendSource": "",
"avgPx": "31527.1",
"cancelSource": "",
"category": "normal",
"ccy": "",
"clOrdId": "",
"code": "0",
"cTime": "1654084334977",
"execType": "M",
"fee": "-0.02522168",
"feeCcy": "USDT",
"fillFee": "-0.02522168",
"fillFeeCcy": "USDT",
"fillNotionalUsd": "31.50818374",
"fillPx": "31527.1",
"fillSz": "0.001",
"fillPnl": "0.01",
"fillTime": "1654084353263",
"fillPxVol": "",
"fillPxUsd": "",
"fillMarkVol": "",
"fillFwdPx": "",
"fillMarkPx": "",
"fillIdxPx": "",
"instId": "BTC-USDT",
"instType": "SPOT",
"lever": "0",
"msg": "",
"notionalUsd": "31.50818374",
"ordId": "452197707845865472",
"ordType": "limit",
"pnl": "0",
"posSide": "",
"px": "31527.1",
"pxUsd":"",
"pxVol":"",
"pxType":"",
"quickMgnType": "",
"rebate": "0",
"rebateCcy": "BTC",
"reduceOnly": "false",
"reqId": "",
"side": "sell",
"attachAlgoClOrdId": "",
"slOrdPx": "",
"slTriggerPx": "",
"slTriggerPxType": "last",
"source": "",
"state": "filled",
"stpId": "",
"stpMode": "",
"sz": "0.001",
"tag": "",
"tdMode": "cash",
"tgtCcy": "",
"tpOrdPx": "",
"tpTriggerPx": "",
"tpTriggerPxType": "last",
"attachAlgoOrds": [],
"tradeId": "242589207",
"tradeQuoteCcy": "USDT",
"lastPx": "38892.2",
"uTime": "1654084353264",
"isTpLimit": "false",
"riskBypassResult": "",
"linkedAlgoOrd": {
"algoId": ""
}
}
]
}
@@ -0,0 +1,10 @@
{
"id": "1512",
"event": "subscribe",
"arg": {
"channel": "orders",
"instType": "FUTURES",
"instId": "BTC-USD-200329"
},
"connId": "a4d3ae55"
}