The Mini-Bonsi Terminal is a high-performance miniature Limit Order Book (LOB) matching engine, complete with an interactive REPL and a custom domain-specific language (DSL) for placing trading rules.
Originally built as a monolithic OCaml prototype, it has now evolved into a modern, decoupled Client-Server architecture:
- The Backend (
bonsi-engine): A headless OCaml gRPC microservice that handles advanced functional ADT patterns, concurrent Lwt streams, and formal Menhir/OCamllex language parsing. - The Frontend (
bonsi-tui): A sleek, modern TypeScript dashboard powered by Bun and OpenTUI (a custom React Reconciler for terminal UIs).
The easiest way to install OCaml on a Mac is via Homebrew by installing opam (the OCaml package manager).
# Install opam
brew install opam
# Initialize opam (this might take a few minutes)
opam init
# Follow the prompts to update your shell environment, or run:
eval $(opam env)Once opam is initialized, install the required packages for the OCaml backend:
brew install pkgconf protobuf
opam install -y dune lwt lwt_ppx menhir grpc-lwt ocaml-protoc-plugin h2-lwt-unixYou will also need to install the Bun runtime to serve the OpenTUI frontend:
curl -fsSL https://bun.sh/install | bashFor the best experience writing and reading OCaml code:
- Open VS Code or Antigravity.
- Go to the Extensions tab (
Cmd+Shift+X). - Search for and install the OCaml Platform extension. This provides excellent syntax highlighting, type inference on hover, and auto-completion.
The Mini-Bonsi Terminal uses a decoupled architecture. You need to run the OCaml matching engine and the TypeScript OpenTUI dashboard in two separate terminal windows.
The backend runs an HTTP/2 gRPC server that streams market data.
cd bonsi-engine
eval $(opam env)
dune build
dune exec -- ./bin/main.exe(The server will start silently on port 50051)
The dashboard connects to the engine and provides a rich terminal UI.
cd bonsi-tui
bun run src/index.tsxInside the dashboard REPL, you can interactively type DSL rules (e.g., ON AAPL > 100 DO BUY 10 AAPL) to submit them to the engine over gRPC! To exit the application, press Ctrl-C.
To execute the inline unit tests for the core engine's limit order matching logic, run:
dune runtestFor a detailed breakdown of the architecture, implementation phases, and exactly how the engine and DSL work under the hood, please check out the ocaml-engine-learning-docs directory!