adding backtest table / pages
This commit is contained in:
parent
38a6e73ad5
commit
a876f3c35b
19 changed files with 1058 additions and 69 deletions
56
database/postgres/init/06-create-backtest-tables.sql
Normal file
56
database/postgres/init/06-create-backtest-tables.sql
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
-- Create enum for backtest status (if not exists)
|
||||
DO $$ BEGIN
|
||||
CREATE TYPE backtest_status AS ENUM ('pending', 'running', 'completed', 'failed', 'cancelled');
|
||||
EXCEPTION
|
||||
WHEN duplicate_object THEN null;
|
||||
END $$;
|
||||
|
||||
-- Create backtests table
|
||||
CREATE TABLE IF NOT EXISTS backtests (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
name TEXT NOT NULL,
|
||||
strategy TEXT NOT NULL,
|
||||
symbols JSONB NOT NULL,
|
||||
start_date TIMESTAMP WITH TIME ZONE NOT NULL,
|
||||
end_date TIMESTAMP WITH TIME ZONE NOT NULL,
|
||||
initial_capital NUMERIC(20, 2) NOT NULL,
|
||||
config JSONB DEFAULT '{}',
|
||||
status backtest_status NOT NULL DEFAULT 'pending',
|
||||
error TEXT,
|
||||
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
|
||||
updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
|
||||
);
|
||||
|
||||
-- Create backtest_results table
|
||||
CREATE TABLE IF NOT EXISTS backtest_results (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
backtest_id UUID NOT NULL REFERENCES backtests(id) ON DELETE CASCADE,
|
||||
completed_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
|
||||
metrics JSONB NOT NULL,
|
||||
equity_curve JSONB NOT NULL,
|
||||
ohlc_data JSONB NOT NULL,
|
||||
trades JSONB NOT NULL,
|
||||
positions JSONB NOT NULL,
|
||||
analytics JSONB NOT NULL,
|
||||
execution_time INTEGER NOT NULL,
|
||||
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
|
||||
);
|
||||
|
||||
-- Create indexes for better query performance
|
||||
CREATE INDEX idx_backtests_status ON backtests(status);
|
||||
CREATE INDEX idx_backtests_created_at ON backtests(created_at DESC);
|
||||
CREATE INDEX idx_backtests_strategy ON backtests(strategy);
|
||||
CREATE INDEX idx_backtest_results_backtest_id ON backtest_results(backtest_id);
|
||||
|
||||
-- Create updated_at trigger
|
||||
CREATE OR REPLACE FUNCTION update_updated_at_column()
|
||||
RETURNS TRIGGER AS $$
|
||||
BEGIN
|
||||
NEW.updated_at = NOW();
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$ language 'plpgsql';
|
||||
|
||||
DROP TRIGGER IF EXISTS update_backtests_updated_at ON backtests;
|
||||
CREATE TRIGGER update_backtests_updated_at BEFORE UPDATE
|
||||
ON backtests FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
|
||||
4
database/postgres/init/07-fix-execution-time-type.sql
Normal file
4
database/postgres/init/07-fix-execution-time-type.sql
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
-- Fix execution_time column type to handle larger values
|
||||
-- The execution_time might be a timestamp or a large millisecond value
|
||||
ALTER TABLE backtest_results
|
||||
ALTER COLUMN execution_time TYPE BIGINT;
|
||||
Loading…
Add table
Add a link
Reference in a new issue