create table if not exists trading_plans ( id bigserial primary key, instrument_id bigint not null references instruments(id), plan_date date not null, status text not null, narrative text not null, primary_draw jsonb not null default '{}'::jsonb, secondary_draw jsonb not null default '{}'::jsonb, bias text not null, allowed_sessions jsonb not null default '[]'::jsonb, invalidations jsonb not null default '[]'::jsonb, no_trade_conditions jsonb not null default '[]'::jsonb, inputs jsonb not null default '{}'::jsonb, quality jsonb not null default '{}'::jsonb, timeframe_set jsonb not null default '{}'::jsonb, created_ts timestamptz not null, activated_ts timestamptz, created_at timestamptz not null default now(), updated_at timestamptz not null default now(), unique (instrument_id, plan_date) ); create index if not exists idx_trading_plans_instr_date_desc on trading_plans (instrument_id, plan_date desc); create index if not exists idx_trading_plans_instr_status_date_desc on trading_plans (instrument_id, status, plan_date desc);