joshriang
← Back to portfolio

Quant research & trading·Python·Published

Trading Bot

This is the skeleton I use when I want to prove an execution path before any real broker is involved, and its whole design is about making the unsafe path hard to take. A strategy emits a signal, the engine routes it through a RiskGate that holds the KillSwitch, and only then does anything reach Broker.submit_order, so there is no code path from signal to order ticket that skips the risk check. The KillSwitch halts on max position size, max daily loss or max drawdown, all three configurable from the CLI. Orders move through an explicit state machine, PENDING to SUBMITTED to FILLED, CANCELLED or REJECTED, with a legal-transition table in state.py so an illegal transition raises instead of silently corrupting state. Every state change and order event goes out as structured JSON through structlog, which means the log is queryable rather than something I read with my eyes. A PaperBroker fills instantly at the last quote so the whole loop runs end to end with no money and no network, and adding a real broker means implementing the Broker ABC's five methods and registering it.

Date
2026
Main stack
Python
Code
github ↗
01
02
  • Putting the risk gate between the strategy and the broker, rather than inside the strategy, is what makes the kill switch unbypassable; a risk check the strategy can skip is not a risk check.
  • An explicit legal-transition table for order state turns a whole class of concurrency bugs into an exception at the moment of the bad transition instead of a wrong position an hour later.
  • Structured JSON logging from the first commit means the order log is a dataset, so reconstructing what happened does not depend on having written the right log line at the right time.
03

Scale 0–100: how much this project relied on each skill.