-- OHLCV candle data fetched from exchange REST APIs.
-- Enables candle-based backtesting with ~500x less data than tick replay.
-- All standard intervals for all instruments fit in ~245 MB (vs 94 GB for trades).
CREATE TABLE market_candles (
    id            BIGINT           PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
    instrument_id INTEGER          NOT NULL REFERENCES instruments(id),
    interval      TEXT             NOT NULL,  -- '1m', '5m', '15m', '1h', '4h', '1d'
    open_time     TIMESTAMPTZ      NOT NULL,
    close_time    TIMESTAMPTZ      NOT NULL,
    open          DOUBLE PRECISION NOT NULL,
    high          DOUBLE PRECISION NOT NULL,
    low           DOUBLE PRECISION NOT NULL,
    close         DOUBLE PRECISION NOT NULL,
    volume        DOUBLE PRECISION NOT NULL,
    trade_count   INTEGER          NOT NULL,
    UNIQUE (instrument_id, interval, open_time)
);

CREATE INDEX idx_market_candles_lookup
    ON market_candles(instrument_id, interval, open_time);
