Model sports events and market rules and persist trade price history

This commit is contained in:
2026-12-03 03:30:07 +08:00
parent ca45f83038
commit 9708d701c2
9 changed files with 386 additions and 1 deletions
+21
View File
@@ -35,3 +35,24 @@ CREATE TRIGGER IF NOT EXISTS book_update AFTER UPDATE OF remaining,status ON ord
INSERT OR REPLACE INTO book_changes(seq, revision)
SELECT NEW.seq, COALESCE(MAX(revision), 0) + 1 FROM book_changes;
END;
CREATE TABLE IF NOT EXISTS events (
id TEXT PRIMARY KEY, title TEXT NOT NULL, sport TEXT NOT NULL, competition TEXT NOT NULL,
home TEXT NOT NULL, away TEXT NOT NULL, scheduled_at_ms INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS sports_markets (
market TEXT PRIMARY KEY REFERENCES markets, event TEXT NOT NULL REFERENCES events,
question TEXT NOT NULL, yes_label TEXT NOT NULL, no_label TEXT NOT NULL,
terms TEXT NOT NULL, source TEXT NOT NULL, resolver TEXT NOT NULL, rules_version INTEGER NOT NULL
);
CREATE INDEX IF NOT EXISTS event_markets ON sports_markets(event,market);
CREATE TABLE IF NOT EXISTS trade_prices (
fill INTEGER PRIMARY KEY REFERENCES fills, market TEXT NOT NULL REFERENCES markets,
outcome TEXT NOT NULL, at_ms INTEGER NOT NULL, price_micros INTEGER NOT NULL, quantity INTEGER NOT NULL
);
CREATE INDEX IF NOT EXISTS trade_series ON trade_prices(market,outcome,at_ms,fill);
CREATE TRIGGER IF NOT EXISTS record_trade_price AFTER INSERT ON fills BEGIN
INSERT INTO trade_prices(fill,market,outcome,at_ms,price_micros,quantity)
SELECT NEW.seq,market,outcome,CAST(unixepoch('subsec')*1000 AS INTEGER),
NEW.price*10000,NEW.quantity FROM orders WHERE seq=NEW.bid;
END;