> ## Documentation Index
> Fetch the complete documentation index at: https://calendar-effects.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Inside the streaming job (src/streaming/job.py + signals.py)

> docs/architecture/05-streaming-job.mmd

```mermaid theme={null}
%% Calendar Effects · 5 of 8 · Inside the streaming job (src/streaming/job.py + signals.py) · Mahad, with Anushiya.
%% Paste into Miro: Creation bar > Diagram > Build with code.
flowchart TB
  topic[["Kafka topic or replay files"]]

  read["1 · READ · readStream<br/>a micro-batch = the messages that arrived since the last one<br/>--max-per-trigger 390 = 10 trading days × 39 stocks per batch"]
  parse["2 · PARSE · from_json with the master contract<br/>FAILFAST: a broken message stops the job<br/>instead of silently becoming empty"]
  time["3 · EVENT TIME · timestamp_utc = the trading day at 00:00 UTC<br/>the time the data is ABOUT, not when it arrived"]
  wm["4 · WATERMARK · 7 days<br/>the job's sense of how late a message may be<br/>newest date seen − 7 days; anything older is dropped<br/>and counted by DropCounter"]
  split["5 · SPLIT · SPY rows vs stock rows"]
  joinspy["6 · STREAM-STREAM JOIN<br/>each stock row + SPY's row for the same day<br/>gives mkt_ret, the market's return"]
  group["7 · groupBy ticker + applyInPandasWithState<br/>one call per stock per micro-batch<br/>STATE = that stock's memory, kept between batches<br/>as a JSON string in the checkpoint"]

  subgraph SIGNALS["8 · src/streaming/signals.py · update(), plain Python, tested without Spark"]
    pending["pending · rows wait until the watermark passes their date<br/>so they come out in date order even if they arrived out of order"]
    daily["DAILY ALERT · market model re-fitted every day<br/>OLS on the 120 trading days ending 10 days earlier<br/>ret = alpha + beta × mkt_ret + error<br/>AR, abnormal return = ret − alpha − beta × mkt_ret<br/>z_ar = AR / ar_sd · abnormal_return = 1 if |z_ar| ≥ 1.96"]
    attn["ATTENTION · z_views = views vs the mean and spread<br/>of a 28-trading-day baseline, same 10-day gap<br/>attention_spike = 1 if z_views ≥ 3"]
    window["EVENT WINDOW · opens on day 0<br/>day 0 = first trading day on or after the event date<br/>fits ONE market model on days −125..−6<br/>takes days −5..−1 from history, collects days 0..+5"]
    closewin["on day +5 · CAR = sum of the 11 ARs under that one model<br/>car_z = CAR / (ar_sd × √n_days)<br/>also n_abnormal_days, max_z_views, n_spike_days"]
  end

  timeout["EventTimeTimeout<br/>wakes a stock up when the watermark moves<br/>even if no new rows came for it"]

  sink["9 · WRITE · foreachBatch<br/>each micro-batch to its own batch_id=N folder<br/>overwritten if Spark retries, so results never double<br/>prints alerts and CARs as they happen"]
  out1[("data/stream/signals<br/>one row per stock per day<br/>ret, mkt_ret, alpha, beta, ar, z_ar, abnormal_return,<br/>views, z_views, attention_spike, event_type")]
  out2[("data/stream/event_car<br/>one row per stock per event occurrence<br/>alpha, beta, ar_sd, car, car_z, n_days,<br/>n_abnormal_days, max_z_views, n_spike_days")]
  ckpt[/"checkpoint folder<br/>Kafka offsets + state, so a restart<br/>continues where it stopped"/]

  topic --> read --> parse --> time --> wm --> split --> joinspy --> group --> pending
  pending --> daily
  pending --> attn
  pending --> window --> closewin
  timeout -.-> group
  daily --> sink
  attn --> sink
  closewin --> sink
  sink --> out1
  sink --> out2
  group -.-> ckpt
```

## Guide

```mermaid theme={null}
%% Calendar Effects · Guide 5 · The ideas inside the streaming job · goes next to diagram 05-streaming-job.mmd
%% Miro: paste onto a board, or Creation bar > Diagram > Build with code. Read top to bottom.
flowchart TB
  T["GUIDE 5 · INSIDE THE STREAMING JOB<br/>src/streaming/job.py + signals.py · decision 0004"]

  subgraph S1["1 · SPARK IDEAS"]
    direction TB
    s1["STRUCTURED STREAMING<br/>one query that Spark reruns<br/>on each batch of new data"]
    s2["MICRO-BATCH<br/>at most 390 messages<br/>= 10 days × 39 stocks"]
    s3["TRIGGER<br/>--once: process and stop<br/>otherwise check every 5 s"]
  end

  subgraph S2["2 · TIME"]
    direction TB
    t1["EVENT TIME<br/>the day the data is about<br/>not when it arrived"]
    t2["WATERMARK · 7 DAYS<br/>newest day seen − 7 days<br/>anything older is dropped"]
    t3["EXAMPLE<br/>newest 2024-11-29<br/>→ drop before 2024-11-22"]
  end

  subgraph S3["3 · JOINING AND REMEMBERING"]
    direction TB
    j1["JOIN WITH SPY<br/>adds the market's return<br/>for the same day"]
    j2["STATE<br/>each stock's memory<br/>between micro-batches"]
    j3["PENDING<br/>rows wait for the watermark<br/>→ handled in date order"]
    j4["TIMEOUT<br/>a moving watermark wakes a stock<br/>even with no new rows"]
  end

  subgraph S4["4 · EVERY DAY · the alerts"]
    direction LR
    d1["MARKET MODEL<br/>OLS on 120 days<br/>ending 10 days back"] --> d2["ABNORMAL RETURN<br/>ret − alpha − beta × mkt"] --> d3["ALERT<br/>z beyond ±1.96"]
    d4["ATTENTION BASELINE<br/>views over 28 days<br/>ending 10 days back"] --> d5["SPIKE<br/>views z ≥ 3"]
  end

  subgraph S5["5 · EVERY EVENT · the CAR"]
    direction LR
    e1["DAY 0<br/>first trading day<br/>on or after the event"] --> e2["ONE MODEL<br/>days −125 to −6"] --> e3["COLLECT<br/>days −5 to +5"] --> e4["DAY +5<br/>CAR = sum of 11 ARs"]
  end

  subgraph S6["6 · WHY TWO MODELS?"]
    direction TB
    m1["ALERTS<br/>can't see a window coming<br/>→ model refreshed daily"]
    m2["CARs<br/>must match the batch study<br/>→ one model per event"]
  end

  subgraph S7["7 · SAFETY"]
    direction TB
    f1["FAILFAST<br/>a broken message stops the job"]
    f2["IDEMPOTENT<br/>a retried batch overwrites<br/>its own folder, never doubles"]
    f3["CHECKPOINT<br/>a restart resumes where it stopped"]
    f4["BOUNDED STATE<br/>130 days kept per stock"]
  end

  T --> S1 --> S2 --> S3 --> S4 --> S5 --> S6 --> S7

  classDef head fill:#2C2C2A,stroke:#2C2C2A,color:#FFFFFF
  classDef idea fill:#E6F1FB,stroke:#185FA5,color:#0C447C
  classDef step fill:#E1F5EE,stroke:#0F6E56,color:#085041
  classDef num fill:#FAEEDA,stroke:#854F0B,color:#633806
  classDef win fill:#EAF3DE,stroke:#3B6D11,color:#27500A
  class T head
  class s1,s2,s3,t1,t2,j1,j2,j3,j4,m1,m2 idea
  class t3 num
  class d1,d2,d3,d4,d5,e1,e2,e3,e4 step
  class f1,f2,f3,f4 win
  style S1 fill:#FFFFFF,stroke:#B4B2A9
  style S2 fill:#FFFFFF,stroke:#B4B2A9
  style S3 fill:#FFFFFF,stroke:#B4B2A9
  style S4 fill:#FFFFFF,stroke:#B4B2A9
  style S5 fill:#FFFFFF,stroke:#B4B2A9
  style S6 fill:#FFFFFF,stroke:#B4B2A9
  style S7 fill:#FFFFFF,stroke:#B4B2A9
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.