Unify simulation order execution and persist paper accounting

This commit is contained in:
Boki 2026-10-20 21:35:56 -04:00
parent 7dec0be3b4
commit 63c3856797
37 changed files with 3901 additions and 3043 deletions

View file

@ -0,0 +1,22 @@
-- Separate, account-scoped paper ledger. Existing trading/backtest history is untouched.
CREATE TABLE IF NOT EXISTS simulation_accounts (
id TEXT PRIMARY KEY, state JSONB NOT NULL,
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE TABLE IF NOT EXISTS simulation_orders (
id UUID PRIMARY KEY, account_id TEXT NOT NULL REFERENCES simulation_accounts(id),
client_order_id TEXT NOT NULL, status TEXT NOT NULL,
payload JSONB NOT NULL, created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
UNIQUE(account_id,client_order_id), UNIQUE(account_id,id),
CHECK(status IN ('accepted','partiallyFilled','filled','cancelled','rejected'))
);
CREATE INDEX IF NOT EXISTS simulation_open_orders ON simulation_orders(account_id,status);
CREATE TABLE IF NOT EXISTS simulation_fills (
account_id TEXT NOT NULL, id TEXT NOT NULL, order_id UUID NOT NULL, payload JSONB NOT NULL,
PRIMARY KEY(account_id,id),
FOREIGN KEY(account_id,order_id) REFERENCES simulation_orders(account_id,id)
);
CREATE TABLE IF NOT EXISTS simulation_events (
account_id TEXT NOT NULL REFERENCES simulation_accounts(id), id TEXT NOT NULL,
fingerprint TEXT NOT NULL, PRIMARY KEY(account_id,id)
);