Raxx · internal docs

internal · gated

ADR 0052 — Broker adapter interface: BrokerAdapter ABC with registry, not extending alpaca_integration.py

Status: Proposed
Date: 2026-05-05
Deciders: software-architect, operator
Context doc: fidelity-broker-integration.md §5
Related ADRs: 0014, 0050


Context

Raxx currently has one broker integration — Alpaca — implemented as a collection of procedural functions in backend_v2/api/services/alpaca_integration.py and trading_runtime.py. There is no interface or base class; the Alpaca-specific implementation is wired directly into the trading routes.

Adding Fidelity as a second broker forces a choice: extend the Alpaca-specific code with conditional branches, or introduce a proper abstraction. This ADR records that choice.


Decision

Introduce a BrokerAdapter abstract base class (ABC) and a BrokerAdapterRegistry service locator.

Do not extend alpaca_integration.py with Fidelity-specific branches. The file stays as the Alpaca implementation. The refactor into AlpacaBrokerAdapter is a migration sub-card (SC-3) that does not break the public API of the trading routes — it moves the internals behind the interface.


Consequences

Positive

Negative

Neutral


Alternatives considered

Extend alpaca_integration.py with Fidelity branches (if broker == 'fidelity': ...)

Rejected. This path produces a file that grows unbounded with each new broker and is impossible to test cleanly. The broker-specific logic for OAuth token management, rate limiting, and order-spec translation is materially different between adapters. Conditional branching would produce an unreadable module.

One file per broker, no ABC (duck typing)

Considered. Python duck typing means the ABC is not strictly enforced at runtime. The ABC is retained for: 1. Explicitness — feature-developer implementing a new adapter gets a clear contract via @abstractmethod. 2. CI enforcement — mypy or pyright can verify all methods are implemented. 3. Documentation — the ABC is the canonical interface specification, not implicit convention.

External broker aggregator (SnapTrade / Plaid Investments) as the abstraction layer

Deferred. The BYOB strategy includes an aggregator slot (existing design). The BrokerAdapter interface is compatible with an aggregator adapter (the aggregator becomes one adapter, routing to multiple downstream brokers). This ADR does not rule out an aggregator adapter — it only establishes the interface that the aggregator adapter would also implement.


Revisit when