Inspiration

A-share retail traders only get free Level-1 data, and it has a dirty secret: the "tick-by-tick" feed is not tick-by-tick at all — the exchange/vendor aggregates everything into ~3-second windows. Inside one window you can't tell whether 666 lots were a single institutional order or 200 retail fills.

Traders in China read integer lot counts as signals ("XXX", "YYY" — the so-called 盘口暗号). But no free tool lets you capture, search, and alert on them. And because free sources only serve the latest trading day, a day you don't capture is a day that is gone forever.

So I built astock: a zero-dependency, runs-on-a-laptop pipeline that captures the feed before it evaporates, indexes it by lot size, and taps you on the shoulder the moment a XXX-lot buy prints.

What it does

Capture — polls Level-2-style order-book snapshots every 3 s and pulls incremental trade details by sequence cursor every 15 s during market hours, plus 4 scheduled daily jobs (morning full backfill, afternoon increment, daily-bar backfill, feature snapshot). Search — find every print with lot count ∈ {XX, YY, XXX,YYY, …}, or ≥ a threshold, by amount, by side, by natural-language query (astock ask "今天 XXX 手的买单"). Alert — a rule engine (lots_in / lots_gte / amount_gte / shares_in / shares_gte) with per-rule cooldowns, macOS/terminal/webhook notifications. Research — daily feature snapshots with T+1 / T+5 forward-return labels $r_{T+1} = P_{T+1}/P_T - 1$, an event-study module, and a paper-trading engine with pool-based capital allocation.

How we built it

Pure Python standard library — no pip, no npm. The web UI is http.server.ThreadingHTTPServer serving a single hand-written HTML file; storage is SQLite; scheduling is an in-process thread pinned by a cross-process singleton lock in the database (so the launchd daemon and the web server can never run the same job twice).

The trickiest architectural seam: the paper-trading logic layer is plain JavaScript, built by tools/build_sim.py into a single file, and the Python scheduler drives it through a Node subprocess — one source of truth for both backtest and live simulation.

Security: HMAC-signed session cookies, constant-time comparison, per-IP login rate limiting, Origin-based CSRF checks, and a default bind to 127.0.0.1.

Challenges we ran into

The 3-second window. A "XXX-lot trade" may be an aggregate. We tag every record with a confidence level — confirmed_single only when the source reports the window contained exactly $n=1$ trades — and never let the rule engine overclaim. Perishability. Miss a day and it's unrecoverable. Every scheduled slot is persisted in schedule_runs; a run only "counts" if executed after its planned time, so a 00:33 manual catch-up can never silently cancel the real 15:40 job (that exact bug once made one slot fire 15+ times in a day). Time-of-day vs. calendar. Comparing start_time < slot_time as wall-clock times breaks the day after a long-running process crosses midnight — the fix is date-aware comparison, learned the hard way. Two writers, one truth. The same lot-size config once lived under two key names; divergence caused duplicate alerts. Now normalization at load time keeps a single authoritative schema with legacy aliases synced.

Accomplishments that we're proud of

  • 1,756 unit tests, all green** — in a project with zero third-party deps.
  • 20,815 trade records re-captured in 92 s during a live recovery drill.
  • Weeks of unattended 3-second polling with order-book frame counts reconciled against the vendor's own sequence numbers.
  • A UI honest about epistemics: "no data yet" and "no matches" are rendered as different states, because they are different statements.

What we learned

Snapshots and increments have opposite failure modes: a missed order-book frame is gone forever (poll tightly), while trade details are cursor-based (poll lazily, lose nothing). More generally: in a data tool, "we never got it" and "we got it and it matched nothing" must never share a code path — most silent-failure bugs are exactly that confusion.

What's next for astock

  • Real Level-2 (Shenzhen order-by-order feed) to resolve the 3-second ambiguity for good.
  • Failover across the four free quote sources when one is rate-limited.
  • Pattern mining over lot-count sequences, not just single prints.
  • A proper public release with a one-command installer.

Built With

Share this project:

Updates

Submission history