Built for Quant Research · Python · FastAPI · NumPy · pandas
Deterministic Quantitative Routing
A prototype of the routing and orchestration layer of a 'thin agent, fat platform' design for market-regime analysis. A plain-English query becomes an execution plan; the plan runs frozen, stateless indicator functions (six technical indicators, each turning a price series into a 0 or 1 signal) and composes them with AND, OR, XOR, SUM or AVERAGE, optionally smoothing the result until it settles. A FastAPI service with strict input limits. The router is a keyword stand-in for a language model, and the data is a mock random walk.
Text Query → Keyword Router → Orchestrator → Regime Output

Role
AI/ML Engineer & Systems Architect
Duration
6 weeks
Tech Stack
The Problem
A language model is good at understanding a request such as 'is the trend turning?' and poor at arithmetic it cannot show its working for. If the model computes an indicator itself, the number cannot be audited or reproduced.
The premise of this design is a thin agent on a fat platform: the model only chooses which pre-written functions to run, and fixed, tested code does all the maths, so the same plan always gives the same answer. This repository is a working prototype of the routing and orchestration layer of that design.
My Approach
- Router. A SemanticRouter interface turns a query into a blueprint: steps, a composition rule, assets and a timeframe. Only a keyword-matching stand-in exists (trend, volatility or squeeze, momentum or RSI, combine); the interface is where a language-model router would plug in.
- Function library. Six technical indicators (SMA crossover, price above SMA, Bollinger squeeze, ATR expansion, RSI overbought and oversold). Each is a pure function from a price series to an array of 0 and 1, and each validates its input, its arguments and its output.
- Orchestrator. Runs the plan's steps and composes the results across steps and across assets with AND, OR, XOR, SUM or AVERAGE, including synthetic positions such as A-B.
- Recursive stability. An optional loop smooths the regime by majority vote until it stops changing (Hamming distance of at most 1% of the length). A circuit breaker catches two-cycle oscillation, and the response says why it stopped: stable, oscillation or max_iterations.
- API. FastAPI with GET /health and POST /query. The query is 1 to 2000 characters, unknown fields are refused, max_iterations is 1 to 100, a plan is limited to 20 steps and 50 assets, a bad plan is a 422 with a plain message and any other failure is a generic 500. A two-stage Docker image runs as a non-root user.
- Review. The first version turned every error into a 500 carrying the exception text, let a caller set max_iterations without limit, accepted plans of any size, listened on all interfaces with no authentication, and had indicators that gave misleading signals (a Bollinger squeeze for any spread whose average is negative, 'price above average' true everywhere when the window was longer than the data). All were fixed and covered by tests.
Results & Impact
Re-run on 2026-11-14 in a scratch copy: uv run pytest reports 254 passed (8 before the hardening pass) and ruff check reports all checks passed. The API was also run from that copy and driven through its Swagger UI: 'is there a trend?' with the recursive loop settled after 1 iteration, 'combine' produced a two-step AND plan, and max_iterations of 1000 was refused with a 422.
Recorded in the repository's work-done.md (2026-11-10): - A mutation check that put 242 deliberate bugs back one at a time; the tests failed for all 242. - uv audit: no known vulnerabilities in 28 packages. - The Docker image built and ran: it answered /health and /query, refused 10^9 iterations, an empty query and an unknown field with 422, ran as uid 10001, reported healthy, and was 408 MB.
Not verified: behaviour against real market data (the loader is a mock random walk, the same series for every symbol); a language-model router (none exists); behaviour under load; the GitHub Actions workflow, which has not run.