File size: 2,790 Bytes
1e72f5f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/bin/bash
# Compact one extracted day into effective-price change rows, partitioned by kickoff day.
#   side: A = home/over, B = away/under.  line: 0 (ML), home spread number, or total number.
#   eff:  bettable decimal price, NULL if pulled (odds null) or maxBetSize reported < $100.
#   del:  isDeleted row that still carries odds (feed refresh cycle; expires later if not re-added).
#   ts/ts_last: first and last sighting of a run of identical prices (polled books re-log unchanged prices).
#   contract: Kalshi nativeMarketId (team-specific contracts), '' for every other book.
D=$1
IN=/home/stats/cfb_cross/cache/day=$D.parquet
duckdb -init /home/stats/.duckdbrc_noview -c "
COPY (
  WITH b AS (
    SELECT matchKey, market, bookmaker AS book,
           CASE WHEN bookmaker='kalshi' THEN nativeMarketId ELSE '' END AS contract,
           CASE WHEN selectionLine IN ('hometeam','over') THEN 'A' ELSE 'B' END AS side,
           CASE market WHEN 'Moneyline' THEN 0.0
                       WHEN 'Spread' THEN CASE WHEN selectionLine='hometeam' THEN selectionPoints ELSE -selectionPoints END
                       ELSE selectionPoints END AS line,
           timestamp AS ts, start_ms, homeCompetitor AS home, awayCompetitor AS away,
           CASE WHEN odds IS NULL OR odds <= 1 THEN NULL
                WHEN maxBetSize IS NOT NULL AND maxBetSize < 100 THEN NULL
                ELSE odds END AS eff,
           (isDeleted AND odds IS NOT NULL) AS del,
           maxBetSize AS mx,
           strftime(make_timestamp((start_ms - 8*3600000)*1000), '%Y-%m-%d') AS kick_day
    FROM read_parquet('$IN')
    WHERE selectionLine IN ('hometeam','awayteam','over','under')
      AND (market = 'Moneyline' OR selectionPoints IS NOT NULL)
  ),
  d AS (
    SELECT *, CASE WHEN eff IS NOT DISTINCT FROM lag(eff) OVER w AND del IS NOT DISTINCT FROM lag(del) OVER w
                   THEN 0 ELSE 1 END AS chg
    FROM b WINDOW w AS (PARTITION BY matchKey, market, line, side, book, contract ORDER BY ts)
  ),
  r AS (
    SELECT *, sum(chg) OVER (PARTITION BY matchKey, market, line, side, book, contract ORDER BY ts
                             ROWS UNBOUNDED PRECEDING) AS run
    FROM d
  )
  -- One row per run of identical (eff, del): ts = first sighting, ts_last = last re-log of that price.
  SELECT matchKey, market, book, contract, side, line, min(ts) AS ts, max(ts) AS ts_last,
         any_value(start_ms) start_ms, any_value(home) home, any_value(away) away,
         any_value(eff) eff, any_value(del) del, arg_min(mx, ts) mx, any_value(kick_day) kick_day
  FROM r GROUP BY matchKey, market, book, contract, side, line, run
) TO '/home/stats/cfb_cross/stage1' (FORMAT parquet, COMPRESSION zstd, PARTITION_BY (kick_day), FILENAME_PATTERN 'src_${D}_{i}', OVERWRITE_OR_IGNORE);
"