ALTER TABLE paper_trades
    ADD COLUMN IF NOT EXISTS margin_usdt DOUBLE PRECISION DEFAULT 0;

ALTER TABLE paper_trades
    ADD COLUMN IF NOT EXISTS leverage DOUBLE PRECISION DEFAULT 5;

UPDATE paper_trades
SET leverage = 5
WHERE leverage IS NULL OR leverage <= 0;

UPDATE paper_trades
SET margin_usdt = CASE
    WHEN margin_usdt IS NULL OR margin_usdt <= 0 THEN COALESCE(notional_usdt, 0) / NULLIF(leverage, 0)
    ELSE margin_usdt
END;
