Documentation

This directory contains the design and architecture documentation for an algorithmic-trading system written in OCaml. The project streams market data, runs indicator-and-strategy pipelines, and routes signals through a broker-agnostic order layer to live brokers (Finam, BCS) or a paper simulator. For installation, quickstart and CLI reference see the top-level README.md.

Why OCaml

OCaml is a practical, industrially-proven functional language designed by researchers and used where correctness matters more than hype. Three things drove the choice for this system:

The language is developed by INRIA (French national research institute) and stewarded by the OCaml Software Foundation; fintech companies like Jane Street drive much of the industrial tooling. It's "a language by scientists for scientists" that also pays professional bills.

For a broader personal take see Why I chose OCaml as my primary language by Xavier Van de Woestyne.

Why OCaml pairs well with LLM-assisted development

LLMs are stochastic code generators — every line is a guess shaped by training, and the only way to converge on correct output is a fast, specific feedback loop. OCaml gives exactly that, more than most languages:

The net effect: the model-level reasoning the type system demands nudges LLM output toward well-factored designs, and the compiler's fast, specific feedback catches the rest.

Architecture

Long-form essays on how the system is structured and why. Read in order for a tour; read individually for a specific concern.

  1. Overview — hexagonal layers, module map, data flow from bar to order.
  2. Domain model — core types (Instrument, Candle, Order, Signal, Portfolio) and the invariants they carry.
  3. State machine: Step + Pipeline — the shared transducer Backtest and Live_engine both drive.
  4. Streams — functional pipeline over Seq.t, with an Eio.Stream adapter as the only push/pull boundary.
  5. Reservations ledger — cash and position accounting across the broker-latency gap: reserve → commit_fill with available_cash gating Risk checks.
  6. Live engine — how streaming bars become orders; Paper wiring; reconciliation with the broker.
  7. Testing strategy — unit, component, differential; mirroring lib/ in test/unit/.
  8. Order flow — footprint analysis on the public trade tape: the order_flow BC, its aggregate lifecycle, the per-venue trade relay, tick-replay backtest, and what is proved versus tested.
  9. Gradient-boosted trees — pure-OCaml inference over LightGBM text-dump models; training pipeline; how Gbt_strategy plugs into the engine.
  10. Logistic regression — lightweight classifier as a gating function for the Composite.Learned policy; SGD + L2 in ~70 lines.
  11. Triple-barrier labelling — path-sensitive, volatility-adaptive label derivation for supervised training; the intended alternative to "sign of forward return" when the strategy will trade TP/SL brackets.

How-to guides

Step-by-step walkthroughs for common tasks. Unlike the architecture docs (which explain why), these focus on how: concrete commands, expected output, troubleshooting.

Decision records (ADR)

Short, dated notes capturing architecture decisions and their rationale. See adr/README.md for the template and chronological index.

Module reference

Auto-generated API documentation (from .mli files via odoc) lives at dune build @doc output, typically published to GitHub Pages. This repository uses .mli files as the primary guardrail against leaking implementation details — every domain module has one.

Conventions