feat(research): add strategy catalog and versioned backtest setup
This commit is contained in:
parent
8cca0ee588
commit
d929db707a
44 changed files with 4198 additions and 209 deletions
44
database/postgres/init/18-backtest-strategy-versions.sql
Normal file
44
database/postgres/init/18-backtest-strategy-versions.sql
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
-- A catalog selection is a saved draft, never a legacy strategy alias.
|
||||
ALTER TABLE backtests ADD COLUMN IF NOT EXISTS strategy_version_id UUID
|
||||
REFERENCES strategy_versions(id) ON DELETE RESTRICT;
|
||||
ALTER TABLE backtests ADD COLUMN IF NOT EXISTS strategy_parameters JSONB;
|
||||
DO $$ BEGIN
|
||||
IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname='backtests_catalog_selection' AND conrelid='backtests'::regclass) THEN
|
||||
ALTER TABLE backtests ADD CONSTRAINT backtests_catalog_selection CHECK (
|
||||
(strategy_version_id IS NULL AND strategy_parameters IS NULL)
|
||||
OR (strategy_version_id IS NOT NULL AND strategy_parameters IS NOT NULL
|
||||
AND jsonb_typeof(strategy_parameters)='object' AND strategy='catalog')
|
||||
);
|
||||
END IF;
|
||||
END $$;
|
||||
|
||||
CREATE OR REPLACE FUNCTION protect_backtest_strategy_mode() RETURNS trigger
|
||||
LANGUAGE plpgsql AS $$ BEGIN
|
||||
IF (OLD.strategy_version_id IS NULL) IS DISTINCT FROM (NEW.strategy_version_id IS NULL) THEN
|
||||
RAISE EXCEPTION 'Create a separate template to switch between legacy execution and catalog drafts'
|
||||
USING ERRCODE='23514';
|
||||
END IF;
|
||||
RETURN NEW;
|
||||
END $$;
|
||||
DROP TRIGGER IF EXISTS backtests_strategy_mode ON backtests;
|
||||
CREATE TRIGGER backtests_strategy_mode BEFORE UPDATE ON backtests
|
||||
FOR EACH ROW EXECUTE FUNCTION protect_backtest_strategy_mode();
|
||||
|
||||
-- Retain the same parent lock and archive rule from17. Existing admitted runs only
|
||||
-- UPDATE their own rows, so their progress/recovery remains independent of drafts.
|
||||
CREATE OR REPLACE FUNCTION require_active_backtest_for_new_run() RETURNS trigger
|
||||
LANGUAGE plpgsql AS $$
|
||||
DECLARE parent RECORD;
|
||||
BEGIN
|
||||
SELECT archived_at,strategy_version_id,strategy INTO parent
|
||||
FROM backtests WHERE id=NEW.backtest_id FOR UPDATE;
|
||||
IF FOUND THEN
|
||||
IF parent.archived_at IS NOT NULL THEN
|
||||
RAISE EXCEPTION 'Backtest is archived; restore it before submitting a new run' USING ERRCODE='23514';
|
||||
END IF;
|
||||
IF parent.strategy_version_id IS NOT NULL OR parent.strategy='catalog' THEN
|
||||
RAISE EXCEPTION 'Catalog strategy execution is unavailable until canonical runner integration (R05)' USING ERRCODE='23514';
|
||||
END IF;
|
||||
END IF;
|
||||
RETURN NEW;
|
||||
END $$;
|
||||
Loading…
Add table
Add a link
Reference in a new issue