/* Serving mart: COT positioning (combined futures+options) for Coffee C futures. */ /* Same analytics as serving.cot_positioning, but filtered to the combined */ /* report variant (FutOnly_or_Combined = 'Combined'). Positions include */ /* options delta-equivalent exposure, showing total directional market bet. */ /* Grain: one row per report_date for Coffee C futures. */ /* Latest revision per date: MAX(ingest_date) used to deduplicate CFTC corrections. */
MODEL (
  name serving.cot_positioning_combined,
  kind INCREMENTAL_BY_TIME_RANGE (
    time_column report_date
  ),
  grain (
    report_date
  ),
  start '2006-06-13',
  cron '@daily'
);

WITH latest_revision AS (
  /* Pick the most recently ingested row when CFTC issues corrections */
  SELECT
    f.*
  FROM foundation.fct_cot_positioning AS f
  INNER JOIN foundation.dim_commodity AS d
    ON f.cftc_commodity_code = d.cftc_commodity_code
  WHERE
    d.commodity_name = 'Coffee, Green'
    AND f.report_type = 'Combined'
    AND f.report_date BETWEEN @start_ds AND @end_ds
  QUALIFY
    ROW_NUMBER() OVER (
      PARTITION BY f.report_date, f.cftc_contract_market_code
      ORDER BY f.ingest_date DESC
    ) = 1
), with_derived AS (
  SELECT
    report_date,
    market_and_exchange_name,
    cftc_commodity_code,
    cftc_contract_market_code,
    contract_units,
    ingest_date,
    open_interest, /* Absolute positions (contracts, delta-equivalent for options) */
    managed_money_long,
    managed_money_short,
    managed_money_spread,
    managed_money_net,
    prod_merc_long,
    prod_merc_short,
    prod_merc_net,
    swap_long,
    swap_short,
    swap_spread,
    swap_net,
    other_reportable_long,
    other_reportable_short,
    other_reportable_spread,
    other_reportable_net,
    nonreportable_long,
    nonreportable_short,
    nonreportable_net,
    ROUND(managed_money_net::REAL / NULLIF(open_interest, 0) * 100, 2) AS managed_money_net_pct_of_oi, /* Normalized: managed money net as % of open interest */ /* Removes size effects and makes cross-period comparison meaningful */
    ROUND(managed_money_long::REAL / NULLIF(managed_money_short, 0), 3) AS managed_money_long_short_ratio, /* Long/short ratio: >1 = more bulls than bears in managed money */
    change_open_interest, /* Weekly changes */
    change_managed_money_long,
    change_managed_money_short,
    change_managed_money_net,
    change_prod_merc_long,
    change_prod_merc_short,
    managed_money_net /* Week-over-week momentum in managed money net (via LAG) */ - LAG(managed_money_net, 1) OVER (ORDER BY report_date) AS managed_money_net_wow,
    concentration_top4_long_pct, /* Concentration */
    concentration_top4_short_pct,
    concentration_top8_long_pct,
    concentration_top8_short_pct,
    traders_total, /* Trader counts */
    traders_managed_money_long,
    traders_managed_money_short,
    traders_managed_money_spread,
    CASE
      WHEN MAX(managed_money_net) OVER w26 = MIN(managed_money_net) OVER w26
      THEN 50.0
      ELSE ROUND(
        (
          managed_money_net - MIN(managed_money_net) OVER w26
        )::REAL / (
          MAX(managed_money_net) OVER w26 - MIN(managed_money_net) OVER w26
        ) * 100,
        1
      )
    END AS cot_index_26w, /* COT Index (26-week): where is current net vs. trailing 26 weeks? */ /* 0 = most bearish extreme, 100 = most bullish extreme */ /* Includes options delta-equivalent exposure */
    CASE
      WHEN MAX(managed_money_net) OVER w52 = MIN(managed_money_net) OVER w52
      THEN 50.0
      ELSE ROUND(
        (
          managed_money_net - MIN(managed_money_net) OVER w52
        )::REAL / (
          MAX(managed_money_net) OVER w52 - MIN(managed_money_net) OVER w52
        ) * 100,
        1
      )
    END AS cot_index_52w /* COT Index (52-week): longer-term positioning context */
  FROM latest_revision
  WINDOW w26 AS (ORDER BY report_date ROWS BETWEEN 25 PRECEDING AND CURRENT ROW), w52 AS (ORDER BY report_date ROWS BETWEEN 51 PRECEDING AND CURRENT ROW)
)
SELECT
  report_date,
  market_and_exchange_name,
  cftc_commodity_code,
  cftc_contract_market_code,
  contract_units,
  ingest_date,
  open_interest,
  managed_money_long,
  managed_money_short,
  managed_money_spread,
  managed_money_net,
  prod_merc_long,
  prod_merc_short,
  prod_merc_net,
  swap_long,
  swap_short,
  swap_spread,
  swap_net,
  other_reportable_long,
  other_reportable_short,
  other_reportable_spread,
  other_reportable_net,
  nonreportable_long,
  nonreportable_short,
  nonreportable_net,
  managed_money_net_pct_of_oi,
  managed_money_long_short_ratio,
  change_open_interest,
  change_managed_money_long,
  change_managed_money_short,
  change_managed_money_net,
  change_prod_merc_long,
  change_prod_merc_short,
  managed_money_net_wow,
  concentration_top4_long_pct,
  concentration_top4_short_pct,
  concentration_top8_long_pct,
  concentration_top8_short_pct,
  traders_total,
  traders_managed_money_long,
  traders_managed_money_short,
  traders_managed_money_spread,
  cot_index_26w,
  cot_index_52w
FROM with_derived
ORDER BY
  report_date
