Quant Python: Architecting Autonomous Trading Systems

Quant Python: Architecting Autonomous Trading Systems

Day 65 — Persistence & Storage: The Complete AppendOnlyCSVWriter

Python Quant's avatar
Python Quant
Jun 29, 2026
∙ Paid

This workspace delivers a trade-log writer that survives the two failure modes the free post named but didn’t fix: concurrent coroutines racing for the same file, and a crash leaving a corrupted trailing row that the loader must detect rather than trust.

What you’re building

AppendOnlyCSVWriter and its companion CorruptionQuarantine boundary. The writer serializes every append behind an asyncio.Lock, builds each row as one complete string (fields plus newline) before writing it, and calls os.fsync() after every write so a row is durable on disk before the coroutine returns. On the read side, load_fills() walks the file once, and any trailing line that doesn’t end in \n — the signature of a crash mid-write — is pulled out into a quarantine list instead of being handed to the caller as valid data. This sits directly downstream of Day 64’s PathStorage, which resolves and creates the fills/ directory this writer appends into, and directly upstream of FillAuditLog, which now only ever sees rows that passed the newline check.

The free post showed the shape of a safe append — build the row as one string, write it, flush it. This workspace is what that shape grows into once it has to survive the conditions a live engine actually runs under: multiple coroutines appending fills from different symbols’ WebSocket streams in the same event-loop tick, a process that gets killed without warning, and a loader that has to assume every file it opens might contain exactly one bad row from the last time that happened.

User's avatar

Continue reading this post for free, courtesy of Python Quant.

Or purchase a paid subscription.
© 2026 Python Quant · Privacy ∙ Terms ∙ Collection notice
Start your SubstackGet the app
Substack is the home for great culture