TheDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/theonetrade/backtest-kit/llms.txt
Use this file to discover all available pages before exploring further.
Broker singleton is the bridge between Backtest Kit’s internal position state and a real exchange. Every commit operation (open, close, partial, trailing, breakeven, DCA) is intercepted before the corresponding internal state mutation. If the adapter method throws — for example, because an exchange order was rejected, the network timed out, or the fill poll exceeded the maximum attempts — the mutation is skipped and the framework retries automatically on the next tick. This gives the adapter full transactional control: the internal state is never ahead of what the exchange has actually confirmed.
All broker calls are silently skipped in backtest mode (payload.backtest === true).
Registration and Lifecycle
Broker.useBrokerAdapter
Register a class constructor or plain object implementingPartial<IBroker>.
A class constructor (called with
new) or an already-instantiated object. Only the methods you need to implement are required — the rest are no-ops via BrokerBase defaults.Broker.enable().
Broker.enable
Subscribe the broker to the internalsyncSubject event bus. This wires signal-open and signal-close events automatically. Call once at startup.
Broker.disable(). Protected by a singleshot guard — calling it multiple times is a no-op.
Broker.disable
Unsubscribe fromsyncSubject and reset the singleshot guard. Safe to call multiple times.
IBroker Interface
Implement any subset of these methods.BrokerBase provides default no-op implementations that log each event.
waitForInit
Called once before the first event. Use to authenticate with the exchange, load API keys, or establish connections.onSignalOpenCommit
Called when a scheduled limit order is confirmed filled and the position becomes active. Routed automatically viasyncSubject when Broker.enable() is active.
BrokerSignalOpenPayload:
| Field | Type | Description |
|---|---|---|
symbol | string | Trading pair symbol |
cost | number | Dollar cost of entry |
position | "long" | "short" | Trade direction |
priceOpen | number | Entry price |
priceTakeProfit | number | Take-profit price |
priceStopLoss | number | Stop-loss price |
pnl | IStrategyPnL | Current PNL snapshot |
backtest | boolean | Always false when adapter is invoked |
context | { strategyName, exchangeName, frameName? } | Routing context |
onSignalCloseCommit
Called when a position is closed (TP/SL/manual). Routed automatically viasyncSubject.
currentPrice, totalEntries, totalPartials, closeReason.
onPartialProfitCommit
Called explicitly beforecommitPartialProfit mutates state.
percentToClose, cost (dollar value of the portion), currentPrice, priceTakeProfit, priceStopLoss, position.
onPartialLossCommit
Called explicitly beforecommitPartialLoss mutates state.
BrokerPartialProfitPayload.
onTrailingStopCommit
Called beforecommitTrailingStop / commitTrailingStopCost mutates state.
percentShift, currentPrice, newStopLossPrice (absolute), takeProfitPrice, position.
onTrailingTakeCommit
Called beforecommitTrailingTake / commitTrailingTakeCost mutates state.
percentShift, currentPrice, newTakeProfitPrice (absolute), takeProfitPrice, position.
onBreakevenCommit
Called beforecommitBreakeven mutates state. newStopLossPrice equals effectivePriceOpen.
currentPrice, newStopLossPrice, newTakeProfitPrice, position.
onAverageBuyCommit
Called beforecommitAverageBuy mutates state.
currentPrice, cost, priceTakeProfit, priceStopLoss, position.
BrokerBase
ExtendBrokerBase instead of implementing IBroker from scratch. It provides default no-op implementations that log every event, so you only need to override the methods you care about.
Transactional Guarantee
If any adapter method throws, the corresponding internal state mutation is not applied. Backtest Kit will call the same adapter method again on the next tick, effectively retrying the operation until it succeeds. This means:- Exchange rejections do not corrupt internal position state.
- Network timeouts cause automatic retry rather than stale state.
- Partial fills can be detected, rolled back, and the operation retried cleanly.