-- dolphin_violet DDL — consolidated from the LIVE dolphin_pink schema
-- (post-ALTER: includes maras_tp + provenance columns). Generated 2026-06-12.
-- Apply via apply_violet_ddl.py (one statement per HTTP POST).

CREATE TABLE IF NOT EXISTS dolphin_violet.trade_exit_legs
(
    `ts` DateTime64(6, 'UTC'),
    `ts_day` Date MATERIALIZED toDate(ts),
    `date` Date,
    `strategy` LowCardinality(String),
    `trade_id` String,
    `chain_root_trade_id` String DEFAULT '',
    `chain_head_leg_id` String DEFAULT '',
    `chain_prev_leg_id` String DEFAULT '',
    `chain_seq` UInt32 DEFAULT 0,
    `chain_token` String DEFAULT '',
    `chain_mode` LowCardinality(String) DEFAULT '',
    `exit_leg_id` String,
    `exit_seq` UInt32,
    `command_id` String DEFAULT '',
    `source` LowCardinality(String),
    `reason` LowCardinality(String),
    `asset` LowCardinality(String),
    `side` LowCardinality(String),
    `entry_price` Float64,
    `exit_price` Float64,
    `fraction` Float32,
    `exit_notional` Float64,
    `remaining_notional` Float64,
    `remaining_qty` Float64,
    `pnl_pct_leg` Float32,
    `pnl_leg` Float64,
    `pnl_realized_total` Float64,
    `bars_held` UInt16,
    `fee_leg` Float64 DEFAULT 0,
    `fee_source` LowCardinality(String) DEFAULT '',
    `is_maker` UInt8 DEFAULT 0,
    `slippage_bps` Float32 DEFAULT 0,
    `pnl_source` LowCardinality(String) DEFAULT ''
)
ENGINE = MergeTree
PARTITION BY toYYYYMM(ts)
ORDER BY (ts_day, trade_id, exit_seq, ts)
TTL ts_day + toIntervalDay(180)
SETTINGS index_granularity = 8192;
