Architecture
Built for correctness first
The first version is a modular monolith: one deployable application with strict module boundaries, enforced by automated architecture tests. Services are only extracted when a measurable requirement justifies it.
Module flow
Risk is a gate between strategy and execution
Step 1
Market data
Bars bounded at the decision time
Step 2
Strategy
Emits target weights only
Step 3
Portfolio construction
Turns weights into whole-share orders
Step 4
Risk gate
Approves or rejects with a named rule
Step 5
Order management
Explicit order state machine
Step 6
Execution (simulated)
Next-open fills, slippage, commission
Step 7
Ledger
Append-only journal, reconciled
Step 8
Analytics
Documented formulas, nulls when undefined
Backtest timeline
Decide at the close, fill at the next open
1. Open
Apply corporate actions
Splits effective today adjust positions before trading.
2. Open
Fill yesterday’s approved orders
Sells fill before buys, at the open plus adverse slippage.
3. Close
Mark to market
Positions are valued at today’s closing price.
4. After close
Strategy decides
Uses bars up to and including today’s close, never later.
5. After close
Build orders and run risk
Each order is approved or rejected with a reason.
6. Next open
Approved orders wait
Nothing fills at a price the strategy already saw.
Look-ahead guard
Future data is unreachable, not just discouraged
Order management
Orders move through an explicit state machine
Main path
- Created
- Pending validation
- Approved
- Pending submission
- Submitted
- Acknowledged
- Partially filled
- Filled
The simulator in v0.1 fills orders in full, so Acknowledged and Partially filled exist in the model but are not yet exercised by the simulator.
Terminal exits
- Rejected
- A risk rule or validation fails, or a buy is unaffordable at the fill price.
- Cancelled
- A cancellation is confirmed (via Pending cancel).
- Expired
- The order is still pending after the last session.
- Failed
- An unexpected execution error occurs.
Portfolio ledger
Balances are derived from an append-only journal
Journal (append-only)
- DepositInitial capital
- BuyExecution id, quantity, price
- FeeCommission for that execution
- Split2-for-1, lot cost basis unchanged
- SellFIFO lots, realized P&L
Projection
- Cash balance
- Positions by instrument
- FIFO tax lots and cost basis
- Realized P&L
Reconcile
Rebuild from the journal and compare with the running state. Duplicate execution ids are ignored, so replaying a fill never creates a second position.
Financial precision
Policies, written down
Money and prices
Decimal arithmetic, never binary floating point, for balances. Cash rounds to the cent per transaction. Fill prices round to four decimal places.
Identifiers
Instruments have permanent internal identifiers. A ticker is an attribute that can change, not a primary key.
Prices and adjustments
Raw prices and corporate actions are stored separately. Adjusted and unadjusted prices are never silently substituted for each other.
Current simplifications are documented rather than hidden: a single currency (USD), whole shares only, and same-day settlement in simulation.