stock-bot/apps/stock/data-pipeline
2026-11-27 19:40:37 -05:00
..
src T-69: Bug sweep (3 bugs: T-64, T-67, T-71) 2026-11-27 19:40:37 -05:00
package.json refactor: pre-M0 cleanup, core library hardening, and isolated dev/QA Docker stack 2026-11-27 00:11:50 -05:00
README.md Clear M1 milestone bugs (T-37, T-39, T-41, T-42, T-44, T-45, T-46, T-48, T-49, T-51, T-52) 2026-11-27 16:11:57 -05:00
tsconfig.json first refactor 2026-10-20 12:39:38 -04:00

@stock-bot/data-pipeline

Normalizes raw provider collections from MongoDB (stock) into canonical PostgreSQL security-master records and curated research datasets.

Safety & Connection Model

  • Port: 2002 (PIPELINE_SERVICE_PORT, default 2002 in docker-compose.yml / docker-compose.dev.yml).
  • Read-Only MongoDB Enforcement: Bootstraps its DI container with mongoReadOnly: true, connecting via MONGODB_READ_URI (studio_stock_bot_read@stock) and blocking all write, index, and collection-mutation methods at the client wrapper layer.
  • Dev vs QA Targets:
    • In development, reads QA MongoDB read-only and writes only to isolated dev PostgreSQL (localhost:5433/trading_bot_dev). assertEnvironmentSafety() hard-blocks writable connections to QA MongoDB (27017/stock) or QA PostgreSQL (5432/trading_bot).
    • Destructive sync flags (?clear=true, POST /sync/clear) are restricted in non-test environments; never run destructive clears against trading_bot.

Current Runtime Scope

  • MongoDB Inputs: exchanges, eodExchanges, ibExchanges, qmExchanges, eodSymbols, ibSymbols, qmSymbols.
  • PostgreSQL Tables (database/postgres/init/02-master-schema.sql):
    • exchanges & provider_exchange_mappings
    • symbols & provider_mappings
    • sync_status
  • HTTP Endpoints (src/routes/):
    • GET /health — liveness & database readiness
    • GET /sync/status & GET /sync/status/enhanced — sync state and counts
    • POST /sync/exchanges/all — normalize exchange reference catalog
    • POST /sync/symbols/:provider — sync qm, eod, or ib symbol mappings

Target Architecture (Milestone M1 & M2)

Full specifications live in:

  • DATA-CONTRACTS.md — Empirical 31-collection rules, bi-temporal security master (exchange, exchange_source_map, instrument, instrument_identifier_history, listing, corporate_action_continuity, listing_symbol_history, provider_mapping, resolution_conflict, override, golden_field_audit), Hive Parquet + DuckDB schemas (bars_1d, fundamentals_pit, earnings_events_pit, short_interest_pit, macro_pit, fx_rates_1d), and QuestDB 10.2.0 intraday schema.
  • MILESTONES.md — Step-by-step M0 (read-only wrapper hardening), M1 (US + Canada security master & bars_1d + fx_rates_1d curation), and M2 (qmFilings-backed PIT fundamentals, earnings, short interest, and macro curation).
  • phase1-investigation.md — Initial provider exchange/symbol investigation notes.

Commands

Run from the repository root:

# Start pipeline in dev mode (targets localhost:5433/trading_bot_dev)
bun run --cwd apps/stock/data-pipeline dev

# Typecheck & build
bun run typecheck
bun run build

# Run PostgreSQL environment & read-only MongoDB safety tests
bun --env-file=/home/boki/.studio/test-env/stock-bot.env test test/postgres/pipeline-safety.test.ts

Pass 1 Exchange Seed Generator

The committed baseline seed in database/postgres/init/35-security-master-exchange-seed.sql covers the 11 US+CA ISO 10383 MICs and the 17 documented explicit provider mappings. To regenerate a full reviewable candidate seed and unresolved-codes report from the read-only QA MongoDB collections (eodExchanges, ibExchanges, qmExchanges, ceoExchanges) on the host:

MONGODB_READ_URI="mongodb://studio_stock_bot_read:<password>@localhost:27017/stock?authSource=stock" \
  bun run apps/stock/data-pipeline/src/security-master/generate-exchange-seed.ts \
  --sql-out ./tmp/35-security-master-exchange-seed.generated.sql \
  --report-out ./tmp/pass1-unresolved-exchanges.md
  • Read-only guarantee: The CLI initializes config with mongoReadOnly: true, connects via MONGODB_READ_URI with readOnly: true, performs zero MongoDB writes, and performs zero PostgreSQL writes.
  • Review and upgrade workflow: Keep database/postgres/init/35-security-master-exchange-seed.sql immutable as the reproducible Pass 1 baseline—runPostgresMigrations records applied filenames in migrations and skips any file it has already run, so editing migration 35 in place would not reach databases that already applied it. Instead, inspect ./tmp/pass1-unresolved-exchanges.md for unresolved codes (unknown MICs, multi-valued OperatingMIC, missing MICs), add approved additions or corrections as a new numbered migration under database/postgres/init/ (for example 36-security-master-exchange-overrides.sql, setting reviewed_by on director-approved rows) and register it in MASTER_FILES in libs/data/postgres/src/migrations.ts (also updating EXPLICIT_EXCHANGE_SOURCE_RULES if future generator runs should classify those codes explicitly). Verify the upgrade against a schema where migration 35 has already run—not only a fresh schema—confirming the new migration applies cleanly and that re-applying the baseline never overwrites rows where reviewed_by IS NOT NULL.