-- View: paper_trade_detail -- Per-order paper trade detail joining orders, fills, and the originating -- recommendation's prediction outcome. Useful for drill-down from the scorecard. -- Requirements: 10.1, 10.3, 10.4 -- Design ref: Section 9.2 (evidence-to-outcome drill-down) CREATE OR REPLACE VIEW lakehouse.stonks.paper_trade_detail AS SELECT o.order_id, o.recommendation_id, o.ticker, o.side, o.order_type, o.quantity, o.limit_price, o.status AS order_status, o.submitted_at, f.fill_id, f.fill_price, f.fill_quantity, f.commission, f.filled_at, -- Slippage: difference between limit and fill price (buys positive = worse) CASE WHEN o.limit_price IS NOT NULL AND o.limit_price > 0 THEN (f.fill_price - o.limit_price) / o.limit_price * 100 ELSE NULL END AS slippage_pct, -- Link back to prediction outcome pvo.predicted_action, pvo.predicted_confidence, pvo.actual_move_pct, pvo.outcome AS prediction_outcome, o.broker_account, o.dt FROM lakehouse.stonks.trade_orders o LEFT JOIN lakehouse.stonks.trade_fills f ON o.order_id = f.order_id AND o.dt = f.dt LEFT JOIN lakehouse.stonks.prediction_vs_outcome pvo ON o.recommendation_id = pvo.recommendation_id AND o.dt = pvo.dt WHERE o.execution_mode = 'paper';