Bruin AI Step 4 of 6

Stock Analyst

1. Scaffold the staging views

Prompt the agent:

AI Prompt

In stock-analyst/assets/, create three Bruin SQL assets for the staging layer. All should materialize as views (cheap, always fresh) with schema staging.

staging/fundamentals.sqlstaging.fundamentals

  • Source: raw.fmp_fundamentals
  • Uppercase symbol, cast date to DATE
  • Dedupe on (symbol, date) using QUALIFY ROW_NUMBER() OVER (PARTITION BY symbol, date ORDER BY date DESC) = 1
  • Pivot income-statement / balance-sheet / cash-flow rows into one row per (symbol, fiscal_quarter_end)
  • Compute free_cash_flow = operating_cash_flow - capital_expenditures
  • Keep: symbol, fiscal_quarter_end, revenue, gross_profit, operating_income, net_income, eps_diluted, operating_cash_flow, capital_expenditures, free_cash_flow, total_assets, total_liabilities, shareholders_equity

staging/prices.sqlstaging.prices

  • Source: raw.yahoo_prices
  • Uppercase symbol, cast date to DATE
  • Dedupe on (symbol, date)
  • Compute daily_return = (close - LAG(close) OVER (PARTITION BY symbol ORDER BY date)) / LAG(close) OVER (...)
  • Compute 20-day and 50-day simple moving averages of close
  • Keep: symbol, date, open, high, low, close, adj_close, volume, daily_return, sma_20, sma_50

staging/macro.sqlstaging.macro

  • Source: raw.fred_macro
  • Pivot so each date has columns: fed_funds_rate (FEDFUNDS), cpi (CPIAUCSL), unemployment_rate (UNRATE), yield_spread_10y2y (T10Y2Y), real_gdp (GDPC1)
  • Forward-fill missing values (monthly/quarterly series need daily granularity) using LAST_VALUE(... IGNORE NULLS) OVER (ORDER BY date) or equivalent
  • Add a regime column: CASE WHEN yield_spread_10y2y < 0 THEN 'inverted' ELSE 'normal' END

For each asset, add a top-level description:, column descriptions, and at least one quality check (e.g. not_null on symbol/date, unique on the grain columns).

Show me the SQL for each asset before writing the files. Don't run anything yet - I'll approve first.

Read each SQL file the agent produces. Look specifically for the QUALIFY, the dedup logic, and the pivot structure. If anything looks off, push back - it's cheaper to correct here than downstream.

Sign up to our newsletter

Practical updates on open-source data pipelines, AI analysts, governance, and what we are shipping at Bruin.