stock-bot/database/postgres/init/11-simulation-execution-ledger.sql

22 lines
1.2 KiB
SQL

-- 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)
);