Market router

From lowtidebuild/public-equity-research · View interactive version

Market router is an AI agent skill from the GitHub repository lowtidebuild/public-equity-research. **Role**: Step 2 — Detect MCP availability, determine data mode (Enhanced/Standard), classify company type, identify peers, initialize a run-local artifact root, and write the research plan.

How to use this skill

Skill file

Market Router — SKILL.md

Role: Step 2 — Detect MCP availability, determine data mode (Enhanced/Standard), classify company type, identify peers, initialize a run-local artifact root, and write the research plan.

Triggered by: CLAUDE.md after Step 1 (query interpretation)

Reads: Session state from Step 1, references/company-type-classification.md

Writes: output/runs/{run_id}/{ticker}/research-plan.json

References: company-type-classification.md


Instructions

Step 2.1 — MCP Availability Detection

Always perform this check at session start (cached for the session — do not repeat per ticker).

Test call:

get_current_stock_price("<TICKER>")

| Result | Decision |

|--------|----------|

| Valid price returned | Enhanced Mode available |

| Error / timeout | Retry once after 2 seconds |

| Second failure | Standard Mode (yfinance + targeted web) |

| Korean stock (any market) | Always Standard Mode regardless of MCP |

Korean stocks are ALWAYS Standard Mode — Financial Datasets MCP does not support KRX.

Cache the result as session variable: DATA_MODE = "enhanced" | "standard"

Log:

MCP check: get_current_stock_price("<TICKER>") -> {result}
DATA_MODE: {enhanced/standard}

Step 2.2 — Company Type Classification

Read company-type-classification.md. Classify based on:

  1. Ticker/company name lookup (known companies)
  2. Sector from API (get_financial_metrics sector field, Enhanced Mode only)
  3. Web search: "{ticker}" sector industry classification site:finance.yahoo.com
  4. Keyword matching in company name

Multi-segment rule: if company has 2+ segments with different economics (e.g., cloud + hardware, banking + insurance), classify as the primary revenue segment type, then note the secondary type.

Output classification with confidence:

Step 2.3 — Peer Identification

Identify 3–5 most relevant peer tickers for comparison (used in dashboard peer section):

Enhanced Mode: Use sector from get_financial_metrics + search "{ticker}" peers competitors sector

Standard Mode: Search "{ticker}" competitors peers {sector} → extract ticker symbols from results

Korean stocks: Search "{company}" 동종업계 경쟁사 → map Korean names to 6-digit codes

Peer selection criteria:

Step 2.4 — Plan Tier 1 API Calls (Enhanced Mode Only)

Based on output_mode and company_type, select which API calls to make in Step 3:

| Output Mode | API Bundle |

|-------------|-----------|

| Mode A | Minimum bundle (current_price, financial_metrics, company_news limit=5) |

| Mode B, C, D | Full standard bundle (10 calls) |

From api-endpoints.md:

FMP calls (if available):

Step 2.5 — Plan Tier 2 Web Searches

From us-data-sources.md (US) or kr-data-sources.md (KR), select searches:

Standard Mode — US (structured first, adaptive search):

  1. Plan yfinance_structured_fetch before web search.
  2. Always plan context searches:
  1. Plan Mode C/D-only context search:
  1. Mark these as conditional targeted searches, not default searches:

Enhanced Mode — qualitative supplement (4 searches):

  1. "{ticker}" earnings call transcript guidance {YYYY}
  2. "{company}" industry trends competitive landscape {YYYY}
  3. "{ticker}" recent news developments last 90 days
  4. "{ticker}" vs competitors {peer1} {peer2}

Standard Mode — KR (see kr-data-sources.md for full 8-query list)

Step 2.5a — Macro Factor Determination (Mode B/C/D)

Condition: Execute for output_mode in {"B", "C", "D"}. Mode A skips this step entirely. Mode B uses a light bundle (3-5 series, no sensitivity table); Mode C/D use the full bundle.

2.5a.1 — Mode C/D (full bundle)
  1. Read company_type from the Step 2.2 classification result.
  2. Look up macro risk factors in company-type-classification.md → "Macro Risk Factors by Type" section for the matching company_type.
  3. Build macro search query using the template:

```

"{sector}" macro risk factors economic outlook {YYYY}

```

Substitute {sector} with the company's sector (from classification) and {YYYY} with the current year.

  1. Korean stocks overlay: If market == "KR", also add Korean-specific macro factors:
  1. Set output fields:

If company_type is not found in the macro factors table, use the sector's closest match or default to ["GDP growth", "interest rates", "inflation"].

2.5a.2 — Mode B (light bundle, Phase C / OD-2)

For Mode B (peer comparison), select 3-5 macro series based on the subject company's company_type (the subject is peer_tickers[0] — the first ticker in the comparison set, treated as the analysis primary).

Company type → Light series bundle:

| Company Type | Light Series Bundle (3-5 series) |

|--------------|----------------------------------|

| Tech / Software / Platform | DGS10, USD index (DTWEXBGS), Consumer Sentiment (UMCSENT) |

| Tech - Memory Semiconductor | DGS10, USD/KRW (KR override), Memory ASP (qualitative tag) |

| Bank / Insurance / Financial | DGS10, DGS2 (yield curve), BAA10Y credit spread |

| Energy / Oil / Gas | WTI crude, USD index, Real GDP |

| Consumer / Retail | UMCSENT, CPI YoY, UNRATE |

| Pharma / Biotech | DGS10, FDA approval pipeline (qualitative), healthcare sector ETF (XLV) |

| Default / Other | DGS10, CPI YoY, Real GDP |

Korean stocks: ALWAYS include USD/KRW regardless of company type (overlay rule). Drop the lowest-priority US series if including USD/KRW would push the bundle past 5 series.

Set output fields in research-plan.json:

Mode B contract: Mode B's Analyst inline step writes macro_context_light into analysis-result.json with two fields: key_series[] (3-5 entries with id, label, value, unit, tag) and narrative_per_peer{} (one short paragraph per ticker, highlighting how that peer's macro sensitivity differs from the others — Beta, FX exposure, sector cyclicality, etc.). See references/analysis-framework-comparison.md Step 5b for the full schema and render contract.

Step 2.6 — Write Research Plan

Before writing the research plan, initialize the run-local artifact namespace:

python .claude/skills/data-manager/scripts/artifact-manager.py init --tickers {ticker}

This creates output/runs/{run_id}/run-manifest.json and output/runs/{run_id}/{ticker}/.

Write to output/runs/{run_id}/{ticker}/research-plan.json:

{
  "ticker": "<TICKER>",
  "market": "US",
  "data_mode": "enhanced",
  "company_type": "<COMPANY_TYPE>",
  "company_type_confidence": "HIGH",
  "output_mode": "C",
  "output_language": "en",
  "analysis_date": "<ANALYSIS_DATE>",
  "peer_tickers": ["<PEER_1>", "<PEER_2>", "<PEER_3>", "<PEER_4>"],
  "analysis_framework_path": "references/analysis-framework-dashboard.md",  // Mode A: "references/analysis-framework-briefing.md"
  "tier1_calls": [
    "get_income_statements",
    "get_balance_sheets",
    "get_cash_flow_statements",
    "get_current_stock_price",
    "get_historical_stock_prices",
    "get_financial_metrics",
    "get_analyst_estimates",
    "get_company_news",
    "get_sec_filings",
    "get_insider_trades"
  ],
  "tier1_fmp_calls": ["price_target_summary", "grades_summary", "historical_grades"],
  "tier2_searches": [
    "\"<TICKER>\" latest quarterly earnings revenue EPS <YYYY>",
    "\"<TICKER>\" analyst price target consensus buy hold sell",
    "\"<TICKER>\" news catalyst <YYYY>",
    "\"<TICKER>\" earnings call transcript guidance <YYYY>",
    "\"<TICKER>\" recent news developments last 90 days"
  ],
  "tier2_search_policy": {
    "standard_us_order": "yfinance_structured_fetch_then_missing_field_search",
    "default_context_searches": [
      "earnings",
      "analyst_coverage",
      "news_catalyst",
      "competitors"
    ],
    "conditional_targeted_searches": {
      "price_market_cap": "only_if_missing_after_yfinance",
      "valuation_ratios": "only_if_missing_after_yfinance",
      "sec_filing_financials": "only_if_missing_after_yfinance"
    }
  },
  "tier2_fetches": [
    "<PORTAL_QUOTE_URL>"
  ],
  "macro_search_required": true,
  "macro_search": "\"<SECTOR>\" macro risk factors economic outlook <YYYY>",
  "macro_factors": ["<MACRO_FACTOR_1>", "<MACRO_FACTOR_2>", "<MACRO_FACTOR_3>"],
  "run_context": {
    "run_id": "<RUN_ID>",
    "ticker": "<TICKER>",
    "artifact_root": "output/runs/<RUN_ID>/<TICKER>",
    "reports_dir": "output/reports",
    "compatibility_mirror_enabled": false
  }
}

Multi-ticker Workflow 2: write separate research plans per ticker using output/runs/{run_id}/{ticker}/research-plan.json. Do NOT overwrite a deprecated shared output/research-plan.json.

Step 2.7 — Peer Mini-Fetch (Mode C / Mode D only)

When to run: ONLY when output_mode in {"C", "D"} AND peer_tickers[] is non-empty. Skip for Mode A and Mode B.

Why: The Mode C/D peer comparison table needs symmetric data — when the subject has full filings while peers are [Est] peer reference placeholders, the comparison has no information value (Phase D / OD-1 of the master mode roadmap, <PRIVATE_DOCS>/plans/<ROADMAP_FILENAME>).

Inputs: peer_tickers[] from the research plan; run_id; cache dir output/data/peers-cache/.

Invocation:

python .claude/skills/financial-data-collector/scripts/peer-fetch.py \
  --tickers <PEER_1> <PEER_2> <PEER_3> <PEER_4> \
  --output-dir output/runs/{run_id}/peers/ \
  --cache-dir  output/data/peers-cache/ \
  --cache-ttl-hours 24 \
  --timeout 30

Outputs: one JSON per peer at output/runs/{run_id}/peers/{TICKER}.json with the canonical 8-metric snapshot (current_price, market_cap, pe_forward, ev_ebitda, revenue_growth_yoy, operating_margin, fcf_yield, beta). Cache is mirrored to output/data/peers-cache/{TICKER}.json (24h TTL).

Trust boundary: Each peer file is sanitized through tools/prompt_injection_filter.py before disk write — analyst must refuse files lacking _sanitization.

Failure behavior: One bad peer never aborts the run. Empty peer_tickers[] → skip the call entirely. All-fail → analyst still runs but the dashboard peer table will render the ⚠️ 데이터 미수집 row.

Full skill spec: .claude/skills/financial-data-collector/peer-fetch-SKILL.md.

Step 2.8 — Report Plan Summary

=== Research Plan: {TICKER} ===
Data Mode: {Enhanced/Standard}
Company Type: {type} ({confidence})
Output Mode: {mode}
Language: {en/ko}
Peers: {list}
Tier 1 calls: {N} API calls planned
Tier 2 searches: {N} web searches planned
Macro factors: {Yes - full bundle (N factors) [Mode C/D] / Yes - light bundle (3-5 series) [Mode B] / Skipped (Mode A)}

→ Proceeding to Step 3 (Financial Data Collector) [Enhanced] or Step 4 (Web Researcher) [Standard]

Completion Check

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.