Finalize trading doc alignment

This commit is contained in:
Codex
2026-05-06 21:11:46 +08:00
parent 241990af84
commit ad5f4802ba
18 changed files with 641 additions and 108 deletions
@@ -0,0 +1,117 @@
alter table if exists trade_plan_candidates
add column if not exists session_code text,
add column if not exists candidate_state text,
add column if not exists quality_score numeric(10,4),
add column if not exists entry_low numeric(20,10),
add column if not exists entry_high numeric(20,10),
add column if not exists stop_loss numeric(20,10),
add column if not exists tp1 numeric(20,10),
add column if not exists tp2 numeric(20,10),
add column if not exists rr_tp1 numeric(8,4),
add column if not exists rr_tp2 numeric(8,4),
add column if not exists expiry_ts timestamptz,
add column if not exists management_rules jsonb not null default '[]'::jsonb,
add column if not exists ai_explanation text;
update trade_plan_candidates
set
session_code = coalesce(session_code, meta->>'session_code'),
candidate_state = coalesce(candidate_state, status),
quality_score = coalesce(quality_score, nullif(meta->>'quality_score', '')::numeric),
entry_low = coalesce(entry_low, nullif(entry->>'entry_low', '')::numeric),
entry_high = coalesce(entry_high, nullif(entry->>'entry_high', '')::numeric),
stop_loss = coalesce(stop_loss, nullif(entry->>'stop_loss', '')::numeric),
tp1 = coalesce(tp1, nullif(entry->>'tp1', '')::numeric),
tp2 = coalesce(tp2, nullif(entry->>'tp2', '')::numeric),
rr_tp1 = coalesce(rr_tp1, nullif(entry->>'rr_tp1', '')::numeric),
rr_tp2 = coalesce(rr_tp2, nullif(entry->>'rr_tp2', '')::numeric),
expiry_ts = coalesce(expiry_ts, expires_ts),
management_rules = case
when management_rules <> '[]'::jsonb then management_rules
when entry ? 'management_rules' then entry->'management_rules'
when meta ? 'management_rules' then meta->'management_rules'
else '[]'::jsonb
end,
ai_explanation = coalesce(ai_explanation, meta->>'ai_explanation', narrative)
where true;
alter table if exists trade_signals
add column if not exists trading_plan_id bigint references trading_plans(id),
add column if not exists candidate_id bigint references trade_plan_candidates(id),
add column if not exists primary_draw jsonb not null default '{}'::jsonb,
add column if not exists target_plan jsonb not null default '{}'::jsonb,
add column if not exists risk_governor_decision jsonb not null default '{}'::jsonb,
add column if not exists confirmation_quality jsonb not null default '{}'::jsonb,
add column if not exists quality_score numeric(10,4),
add column if not exists ai_explanation text;
update trade_signals
set
trading_plan_id = coalesce(trading_plan_id, nullif(meta->>'trading_plan_id', '')::bigint),
candidate_id = coalesce(candidate_id, nullif(meta->>'candidate_id', '')::bigint),
primary_draw = case
when primary_draw <> '{}'::jsonb then primary_draw
when meta ? 'primary_draw' then meta->'primary_draw'
else '{}'::jsonb
end,
target_plan = case
when target_plan <> '{}'::jsonb then target_plan
when meta ? 'target_plan' then meta->'target_plan'
else '{}'::jsonb
end,
risk_governor_decision = case
when risk_governor_decision <> '{}'::jsonb then risk_governor_decision
when meta ? 'risk_governor_decision' then meta->'risk_governor_decision'
else '{}'::jsonb
end,
confirmation_quality = case
when confirmation_quality <> '{}'::jsonb then confirmation_quality
when meta ? 'confirmation_quality' then meta->'confirmation_quality'
when meta ? 'evidence_quality' then meta->'evidence_quality'
else '{}'::jsonb
end,
quality_score = coalesce(quality_score, nullif(meta->>'quality_score', '')::numeric),
ai_explanation = coalesce(ai_explanation, meta->>'ai_explanation', meta->>'structure_quality')
where true;
alter table if exists trade_executions
add column if not exists execution_ticket_id bigint references execution_tickets(id),
add column if not exists confirmation_required boolean not null default false,
add column if not exists confirmed_by text,
add column if not exists confirmed_at timestamptz,
add column if not exists broker_order_ids jsonb not null default '[]'::jsonb,
add column if not exists bracket_state jsonb not null default '{}'::jsonb,
add column if not exists fill_state text,
add column if not exists position_state text,
add column if not exists close_reason text,
add column if not exists reconciliation_state jsonb not null default '{}'::jsonb,
add column if not exists quality_score numeric(10,4),
add column if not exists ai_explanation text;
update trade_executions
set
execution_ticket_id = coalesce(execution_ticket_id, nullif(meta->>'execution_ticket_id', '')::bigint),
confirmation_required = coalesce(confirmation_required, false) or coalesce((meta->>'confirmation_required')::boolean, (meta->>'requires_confirmation')::boolean, false),
confirmed_by = coalesce(confirmed_by, meta->>'confirmed_by'),
confirmed_at = coalesce(confirmed_at, nullif(meta->>'confirmed_at', '')::timestamptz),
broker_order_ids = case
when broker_order_ids <> '[]'::jsonb then broker_order_ids
when meta ? 'broker_order_ids' then meta->'broker_order_ids'
else '[]'::jsonb
end,
bracket_state = case
when bracket_state <> '{}'::jsonb then bracket_state
when meta ? 'bracket_state' then meta->'bracket_state'
else '{}'::jsonb
end,
fill_state = coalesce(fill_state, meta->>'fill_state'),
position_state = coalesce(position_state, meta->>'position_state'),
close_reason = coalesce(close_reason, meta->>'close_reason'),
reconciliation_state = case
when reconciliation_state <> '{}'::jsonb then reconciliation_state
when meta ? 'reconciliation_state' then meta->'reconciliation_state'
else '{}'::jsonb
end,
quality_score = coalesce(quality_score, nullif(meta->>'quality_score', '')::numeric),
ai_explanation = coalesce(ai_explanation, meta->>'ai_explanation')
where true;