# Portfolio Action Report - Markdown Template

Format the JSON data into a markdown report saved to `sandbox/`.

**Filename**: `ib_portfolio_action_report_{ACCOUNT}_{YYYY-MM-DD}_{HHmm}.md`
- Use first account ID if single account, "multi" if multiple

## JSON Structure

```
{
  "generated": "YYYY-MM-DD HH:MM",
  "accounts": ["ACC1", "ACC2"],
  "summary": { "red_count": N, "yellow_count": N, "green_count": N },
  "today_earnings_symbols": ["SYM1"],
  "spreads": [
    {
      "symbol": "AAPL",
      "account": "ACC1",
      "long": { "strike": 150, "expiry": "YYYYMMDD", "right": "C", "quantity": 1, "avg_cost": 5.00, "days_to_exp": 30 },
      "short": { ... },
      "underlying_price": 155.50,
      "earnings_date": "2026-03-15",
      "risk_level": "red|yellow|green",
      "risk_emoji": "🔴|🟡|🟢",
      "recommendation": "text",
      "min_days_to_exp": 7,
      "urgency": "expiring_2_days|expiring_1_week|expiring_2_weeks|longer_dated",
      "earnings_urgency": "this_week|next_week" (optional)
    }
  ],
  "earnings": { "AAPL": "2026-03-15", ... },
  "technicals": { "AAPL": { "rsi": 65.2, "trend": "bullish", "sma20": 150.5, "above_sma20": true, ... } },
  "prices": { "AAPL": 155.50 },
  "earnings_calendar": [ { "date": "2026-03-15", "symbol": "AAPL", "accounts": ["ACC1"], "position_types": ["Spread"] } ],
  "account_summary": [ { "account": "ACC1", "position_count": 10, "spread_count": 5, "red_count": 1, ... } ]
}
```

## Report Sections

### 1. Header
```markdown
# IB Portfolio Action Report - All Accounts
**Generated:** {generated}
**Market Status:** Earnings day (SYM1, SYM2 reporting today)  ← only if today_earnings_symbols non-empty
```

### 2. Critical Summary

| Status | Count | Description |
|--------|-------|-------------|
| 🔴 **RED** | {red_count} | Immediate action required |
| 🟡 **YELLOW** | {yellow_count} | Warning - earnings/expiration within 2 weeks |
| 🟢 **GREEN** | {green_count} | Monitor - no immediate action needed |

### 3. Urgency Sections

Group spreads by `urgency` field. For each group, render by account:

- **expiring_2_days**: `## 🔴 IMMEDIATE ACTION REQUIRED (Expiring ~{date} - 2 DAYS)`
- **expiring_1_week**: `## 🔴 URGENT - Expiring Within 1 Week` (include earnings column)
- **expiring_2_weeks**: `## 🟡 EXPIRING IN 2 WEEKS` (include earnings column)
- **longer_dated**: `## 🟢 LONGER-DATED POSITIONS (No Immediate Action)`

Table columns for short positions:
| Symbol | Position | Strike | Exp | Avg Cost | Underlying | [Earnings] | Action |

For longer-dated, use spread summary format:
| Spread | Long | Short | Underlying | Status |

### 4. Earnings Sections

Group spreads by `earnings_urgency`:
- **this_week**: `## 🔴 CRITICAL EARNINGS ALERT - This Week`
- **next_week**: `## 🟡 EARNINGS NEXT WEEK`

Show by symbol with position details and OTM/ITM status.

### 5. Top Priority Actions

Red spreads: `### 🔴 DO TODAY ({date})`
Yellow spreads: `### 🟡 MONITOR THIS WEEK`

Numbered list, top 5 each.

### 6. Position Size Summary

Use `account_summary` array:

| Account | Positions | Spreads | 🔴 Red | 🟡 Yellow | 🟢 Green |

### 7. Earnings Calendar

Use `earnings_calendar` array:

| Date | Symbol | Account | Position Type |

### 8. Technical Analysis Summary

Use `technicals` and `prices`:

| Symbol | Price | RSI | Trend | SMA20 | SMA50 | MACD | ADX |

Formatting:
- RSI >70: bold + 🔴; RSI <30: bold + 🟢
- Trend: bullish 📈, bearish 📉, neutral ➡️
- SMA: show price + ✓/✗ for above/below
- ADX >25: bold (strong trend)

### 9. Footer
```markdown
*Report generated by Trading Skills on {generated}*
```

## Traffic Light Indicators

- 🔴 **RED**: Immediate action (expiring soon, ITM short, earnings gap risk)
- 🟡 **YELLOW**: Warning zone (earnings within 2 weeks, monitor)
- 🟢 **GREEN**: Safe, no immediate action

## Expiry Formatting

Convert YYYYMMDD to "Mon DD" (e.g., "Feb 14").
