job.py is the Spark plumbing; signals.py is the logic for one stock, written in plain Python so it can be tested without Spark.
The big idea
Each micro-batch:- Parse the messages with the
mastercontract (rows that do not fit fail loudly:FAILFAST), and set event time to the trading day. - Watermark of 7 days: Spark may wait up to 7 days for late messages, then drops anything older, which also lets it forget old state.
- Join each stock’s row to SPY’s row for the same day (a stream-stream join), to get the market return.
- Per ticker, with state (
applyInPandasWithState):signals.updateholds rows until the watermark passes them (so days come out in order), re-fits the market model every day on the 120 days ending 10 days earlier, scores the abnormal return and the attention z-score, opens a window on each event’s day 0 and closes it on day +5 with its CAR. The state (recent history, open windows) is kept as JSON between batches. - Write with
foreachBatch: each micro-batch goes to its ownbatch_id=Nfolder, so a batch that is re-run after a crash overwrites itself instead of duplicating rows (idempotent output). If a trained model exists, it also scores tomorrow’s big-move chance.
scripts/check_streaming.py then recomputes every daily value and CAR directly with numpy, with none of the streaming machinery, and checks they match.
Words you will meet
Micro-batch
Micro-batch
A small slice of the stream that Spark processes as one batch job. Here, a few trading days’ messages.
Watermark
Watermark
How late data may be. With 7 days, after Spark has seen day D, it drops messages for days before D − 7 and can discard state older than that.
Stateful processing
Stateful processing
Remembering things between micro-batches.
applyInPandasWithState gives each ticker its own state and calls our function with that ticker’s new rows.Checkpoint
Checkpoint
Where Spark records its progress (offsets and state), so a restarted job continues where it stopped.
z-score
z-score
How many standard deviations a value is from its usual level.
z_ar is the abnormal return divided by its usual size; z_views is views against their recent mean.The picture: Inside the streaming job (src/streaming/job.py + signals.py)
The full diagram and its guide explain every box.Read these files in this order
1
src/streaming/init.py
See the file. Read it
2
src/streaming/job.py
Structured Streaming job: flag abnormal returns and attention spikes as the master table is replayed, and compute each event’s CAR the moment its window closes. Read it
3
src/streaming/signals.py
Per-ticker logic of the streaming job, in plain Python so it can be tested without Spark. src/streaming/job.py calls
update once per ticker per micro-batch (through applyInPandasWithState) and keeps the returned state until the next batch. Read it4
scripts/check_streaming.py
Check the streaming job’s output against a direct computation of the same numbers. Read it
Questions you should be able to answer
Why not just compute everything at the end?
Why not just compute everything at the end?
Because the point is to know as soon as the data arrives: an alert on the day it happens, a CAR on the day its window closes. The batch study does the “at the end” version, and the two must agree.
Why hold rows until the watermark passes?
Why hold rows until the watermark passes?
The rolling market model needs days in order. Late messages can arrive after later days; holding each row until the watermark guarantees nothing earlier can still come, so rows are released in date order.
What makes the output safe to re-run?
What makes the output safe to re-run?
Each micro-batch writes its own
batch_id=N folder and replaces it if the batch runs again, and the checkpoint records which batches finished. Readers (the dashboard) only read folders with Spark’s _SUCCESS marker.Where does the model run?
Where does the model run?
Inside the JVM: the saved MLlib pipeline transforms each micro-batch’s rows, so no Python is needed on the workers for scoring.
Ideas to improve it
- Replace the per-ticker pandas state with
transformWithState(Spark 4) for typed state and timers. - Stream intraday prices: the same logic with minutes instead of days.