alt/services/worker/internal/storage/postgres/queries/queries.sql
toki 38b68315fc feat: backtest engine baseline implementation
- Add backtest proto definitions and generated code
- Update domain types for backtest results and fixtures
- Add PostgreSQL migrations for backtest tables
- Implement storage layer for backtest result persistence
- Add backtest job definitions and execution pipeline
- Remove obsolete agent-task documents for completed items
2026-05-30 12:13:45 +09:00

69 lines
2.5 KiB
SQL

-- name: UpsertInstrument :exec
INSERT INTO instruments (id, market, venue, symbol, name, currency, provider_symbols)
VALUES ($1, $2, $3, $4, $5, $6, $7)
ON CONFLICT (id) DO UPDATE SET
market = EXCLUDED.market,
venue = EXCLUDED.venue,
symbol = EXCLUDED.symbol,
name = EXCLUDED.name,
currency = EXCLUDED.currency,
provider_symbols = EXCLUDED.provider_symbols;
-- name: GetInstrument :one
SELECT id, market, venue, symbol, name, currency, provider_symbols
FROM instruments
WHERE id = $1;
-- name: ListInstruments :many
SELECT id, market, venue, symbol, name, currency, provider_symbols
FROM instruments;
-- name: UpsertBar :exec
INSERT INTO bars (instrument_id, timeframe, timestamp, open, high, low, close, volume)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
ON CONFLICT (instrument_id, timeframe, timestamp) DO UPDATE SET
open = EXCLUDED.open,
high = EXCLUDED.high,
low = EXCLUDED.low,
close = EXCLUDED.close,
volume = EXCLUDED.volume;
-- name: GetBars :many
SELECT instrument_id, timeframe, timestamp, open, high, low, close, volume
FROM bars
WHERE instrument_id = $1 AND timeframe = $2 AND timestamp >= $3 AND timestamp <= $4
ORDER BY timestamp ASC;
-- name: UpsertRun :exec
INSERT INTO backtest_runs (id, strategy_id, market, timeframe, from_time, to_time, status, created_at, updated_at)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
ON CONFLICT (id) DO UPDATE SET
strategy_id = EXCLUDED.strategy_id,
market = EXCLUDED.market,
timeframe = EXCLUDED.timeframe,
from_time = EXCLUDED.from_time,
to_time = EXCLUDED.to_time,
status = EXCLUDED.status,
created_at = EXCLUDED.created_at,
updated_at = EXCLUDED.updated_at;
-- name: GetRun :one
SELECT id, strategy_id, market, timeframe, from_time, to_time, status, created_at, updated_at
FROM backtest_runs
WHERE id = $1;
-- name: UpsertResult :exec
INSERT INTO backtest_results (run_id, starting_cash_currency, starting_cash_amount, ending_equity_currency, ending_equity_amount, trades, positions)
VALUES ($1, $2, $3, $4, $5, $6, $7)
ON CONFLICT (run_id) DO UPDATE SET
starting_cash_currency = EXCLUDED.starting_cash_currency,
starting_cash_amount = EXCLUDED.starting_cash_amount,
ending_equity_currency = EXCLUDED.ending_equity_currency,
ending_equity_amount = EXCLUDED.ending_equity_amount,
trades = EXCLUDED.trades,
positions = EXCLUDED.positions;
-- name: GetResult :one
SELECT run_id, starting_cash_currency, starting_cash_amount, ending_equity_currency, ending_equity_amount, trades, positions
FROM backtest_results
WHERE run_id = $1;