σμimplied vol · KKmax(S−K,0)udβρᵢⱼfat tailsPnL · drawdownyield · TdSₜ = μSₜ dt + σSₜ dWₜC = S·N(d₁) − K·e⁻ʳᵀ·N(d₂)σₚ² = wᵀΣwd₁ = [ln(S/K) + (r + σ²⁄2)T] ⁄ σ√T∂V/∂t + ½σ²S²·∂²V/∂S² + rS·∂V/∂S − rV = 0Sharpe = (Rₚ − R_f) ⁄ σₚVaR₉₉ = μ − 2.33σρ = Cov(X,Y) ⁄ σₓσᵧE[Rₚ] = Σ wᵢ·E[Rᵢ]φ(x) = e^(−x²/2) ⁄ √2πE[Rᵢ] = R_f + βᵢ(E[Rₘ] − R_f)dXₜ = θ(μ − Xₜ)dt + σ dWₜσ²ₜ = ω + α·ε²ₜ₋₁ + β·σ²ₜ₋₁C − P = S − K·e⁻ʳᵀΔ = ∂V/∂S Γ = ∂²V/∂S²p = (e^(rΔt) − d) ⁄ (u − d)rₜ = ln(Pₜ ⁄ Pₜ₋₁)E[Xₜ₊₁ | ℱₜ] = Xₜf* = (bp − q) ⁄ bz = (x − μ) ⁄ σP(N=k) = λᵏe^(−λ) ⁄ k!Θ = −∂V/∂Tν = S·φ(d₁)·√Tmin wᵀΣw s.t. wᵀμ = μ*∫Σ∂∞λ
uantCraft AcademyRoadmapsSkills
Ctrl K
AO
+1.2k Learners
LoginJoin ↗
RoadmapsSkills
AO
+1.2k Learners
Login
  • English
  • Français
  • Español
  • العربية
  • Português
  • Deutsch
  • 日本語
  • 中文
σμimplied vol · KKmax(S−K,0)udβρᵢⱼfat tailsPnL · drawdownyield · TdSₜ = μSₜ dt + σSₜ dWₜC = S·N(d₁) − K·e⁻ʳᵀ·N(d₂)σₚ² = wᵀΣwd₁ = [ln(S/K) + (r + σ²⁄2)T] ⁄ σ√T∂V/∂t + ½σ²S²·∂²V/∂S² + rS·∂V/∂S − rV = 0Sharpe = (Rₚ − R_f) ⁄ σₚVaR₉₉ = μ − 2.33σρ = Cov(X,Y) ⁄ σₓσᵧE[Rₚ] = Σ wᵢ·E[Rᵢ]φ(x) = e^(−x²/2) ⁄ √2πE[Rᵢ] = R_f + βᵢ(E[Rₘ] − R_f)dXₜ = θ(μ − Xₜ)dt + σ dWₜσ²ₜ = ω + α·ε²ₜ₋₁ + β·σ²ₜ₋₁C − P = S − K·e⁻ʳᵀΔ = ∂V/∂S Γ = ∂²V/∂S²p = (e^(rΔt) − d) ⁄ (u − d)rₜ = ln(Pₜ ⁄ Pₜ₋₁)E[Xₜ₊₁ | ℱₜ] = Xₜf* = (bp − q) ⁄ bz = (x − μ) ⁄ σP(N=k) = λᵏe^(−λ) ⁄ k!Θ = −∂V/∂Tν = S·φ(d₁)·√Tmin wᵀΣw s.t. wᵀμ = μ*∫Σ∂∞λ
uantCraft AcademyRoadmapsSkills
Ctrl K
AO
+1.2k Learners
LoginJoin ↗
RoadmapsSkills
AO
+1.2k Learners
Login
  • English
  • Français
  • Español
  • العربية
  • Português
  • Deutsch
  • 日本語
  • 中文
Apache Arrow skill›Module 3 · Between processes›Topic 3.2·~1 hr 20 minAdvanced

Capstone: a zero-copy tick pipeline

Skill positionModule 3 of 3 · Topic 2 of 2
3.2 Capstone: a zero-copy tick pipeline
—Lab score— / 100Total XP—Streak—
Lecture0% read

3 min readCode examples: 3

  1. 01The idea in plain words
  2. 02Rows in, batches out, a column computed
  3. 03Write a stream, then total it batch by batch
  4. 04What the tasks ask

A pipeline is where the earlier topics earn their keep. Ticks arrive as rows; you want per-symbol notional by the end of the day without ever loading the whole day. With Arrow, that is five small steps over record batches: build batches, compute on whole columns, write a stream, read it back one batch at a time, fold the totals. Memory never holds more than one batch, and the data keeps its columnar layout the whole way.

01

The idea in plain words

Where we are. The last topic of the Arrow skill. It uses batches (topic 2), kernels (topic 3), IPC streams (topic 6) and the stream-reading pattern of topic 2 again; timestamps (topic 7) appear in the schema. The same pipeline shape, with Parquet files in place of the stream and DuckDB or Polars in place of the fold, is the data-pipelines skill's subject.

DefinitionPipeline

A chain of stages where each takes the previous stage's output as input, here a batch at a time. Because only a small piece is in flight, memory stays flat however much data passes through, and a slow stage can be improved without touching the others.

02

Rows in, batches out, a column computed

rows_to_batches is the doorway from Python into Arrow: each batch is built from at most batch_rows rows, column by column, with the shared TICK_SCHEMA, so every later stage can rely on the types. add_notional is one kernel call on the whole batch: price times size, and a null on either side gives a null, no special case to write.

main.pyPython
# The ticks as rows, then as record batches of 5,000 rows, then one batch with its notional columnprint(len(rows), "rows; first:", rows[0])batches = rows_to_batches(rows, 5000)print(len(batches), "batches:", [b.num_rows for b in batches])enriched = add_notional(batches[0])print(enriched.schema.names)print("first notional:", f"{enriched.column('notional')[0].as_py():,.0f}", "= price", rows[0][2], "x size", f"{rows[0][3]:,}")
33493 rows; first: ('AAPL', datetime.datetime(2000, 1, 3, 0, 0, tzinfo=datetime.timezone.utc), 0.83826, 638640787)
7 batches: [5000, 5000, 5000, 5000, 5000, 5000, 3493]
['symbol', 'ts', 'price', 'size', 'notional']
first notional: 535,347,026 = price 0.83826 x size 638,640,787
03

Write a stream, then total it batch by batch

write_ticks adds the notional to each batch and writes it into an IPC stream file as soon as it is ready, so the file grows while only one batch is in memory. notional_by_symbol reads that file the same way: one batch, one small group-by, add to a running dictionary, next batch. The result matches summing everything at once, but the whole day is never in memory. The same code works whether the file holds a day or a decade.

main.pyPython
# Write the stream, then total the notional per symbol while reading it back one batch at a timeimport oswritten = write_ticks(batches, "ticks.stream")print(written, "rows written;", os.path.getsize("ticks.stream"), "bytes on disk")for symbol, total in notional_by_symbol("ticks.stream").items():    print(f"{symbol:5} {total:>22,.0f}")
33493 rows written; 1309120 bytes on disk
AAPL      40,972,234,013,659
JNJ        5,791,663,141,548
JPM        8,498,489,347,331
KO         3,955,468,216,505
XOM        9,008,363,956,774
main.pyPython
# Check against adding everything up in plain Python, which needs the whole list at oncecheck = {}for symbol, ts, price, size in rows:    check[symbol] = check.get(symbol, 0.0) + price * sizetotals = notional_by_symbol("ticks.stream")print("same symbols:", sorted(check) == list(totals))print("largest relative difference:", f"{max(abs(totals[k] - check[k]) / check[k] for k in check):.1e}")
same symbols: True
largest relative difference: 2.5e-15
Where to go from here

Swap any stage without touching the others. Write Parquet instead of the stream when the data is for keeping (Parquet skill), hand a batch to DuckDB or Polars for a heavier query (DuckDB skill, zero-copy as in topic 5), or send the batches with Flight (topic 8). The batch is the contract between stages, and that is why Arrow suits pipelines.

04

What the tasks ask

  1. 1
    `rows_to_batches`For each slice of batch_rows rows, transpose with zip(*chunk), pa.array(column, type=field.type) for each field of TICK_SCHEMA, then pa.RecordBatch.from_arrays(arrays, schema=TICK_SCHEMA).
  2. 2
    `add_notional`pc.multiply(price, pc.cast(size, pa.float64())), then pa.RecordBatch.from_arrays(batch.columns + [notional], names=...).
  3. 3
    `write_ticks` and `notional_by_symbol`Open ipc.new_stream(sink, enriched.schema) with the first batch and write_batch each one; read with ipc.open_stream(source), group each batch and add into a running dict.

Key takeaways

  • The batch is the contract Every stage takes and gives record batches with a shared schema.
  • Memory stays at one batch Write as you go, read batch by batch, fold the totals.
  • Compute in kernels, convert at the end Python objects only for the small final answer. That completes the Arrow skill.
Quick check
Why does notional_by_symbol read the stream batch by batch instead of loading it with read_all()?
Pick an answer.

Lab · what's graded

4 tasks · 100 pts
  • Rows into batches: rows_to_batches25 pts
  • One kernel for notional: add_notional25 pts
  • Write the stream: write_ticks25 pts
  • Total it batch by batch: notional_by_symbol25 pts
Explorer · lab-9-1
▾ lab-9-1
▾ data
…
bash · in-browser
learner@quantcraft:~/lab-9-1$
Python not loaded yetPythonLn 1, Col 1Spaces: 4UTF-8

Reset this topic?

Everything in lab-9-1 goes back to the starter files and the terminal is cleared. Your current solution will be lost — this can't be undone.

Will be reset
  • main.py → starter code
  • Terminal history and test output
  • Unsubmitted changes
Will be kept
  • Best lab score
  • Your XP and streak
  • Lecture progress and notes