A strategy can decide to buy after reading a bar’s close. The simulator still has to decide when that order could have filled. If the strategy also books the fill, a test can pass while quietly using a price from before the decision was possible.
The strategy pattern gives those two decisions different owners. In Trader, the strategy returns the orders it wants. The runner submits them and the simulated broker decides what fills. That split lets you test a trading rule with a few bars and test the execution model separately.
Why this matters
The boundary lets a reviewer ask two questions independently: did the strategy request the expected order, and could the broker have filled it at that price and time? When both answers come from one component, a change to signal logic can also change the assumptions behind its results.
Trader provides the worked example. The source links below are pinned to the same revision. That revision supports backtesting with a simulated broker. Its README lists paper trading as planned and real-money execution as unavailable.
The strategy interface
Trader’s strategy contract is internal to the Go package. This is the interface declaration from the source:
type strategy interface {
name() string
init() error
onBars(ctx context.Context, bars []marketdata.Bar) ([]*order, error)
}
init gives a strategy a place to validate its inputs and initialize
state. A call to onBars receives the bars that became available at one
instant, in instrument order. A single-instrument call uses the same
interface with one bar. A symbol without a new bar at that instant is
absent from the group. The slice is valid only for that call.
The return value distinguishes three outcomes: orders to submit, no orders, or an error that stops the run before any returned order is submitted. Returning an order expresses a request. An order can reserve margin when submitted. It opens or closes a position only if it fills.
Process the bars before submitting new orders
Trader’s runner advances the simulated broker through the frame before calling the strategy. The strategy sees the entire group before any of its new orders is submitted. The runner submits orders in the returned order. That order matters when two requests compete for margin: the second is sized after the first has reserved margin. In the market-order test, the order fills at the next bar’s open. The decision bar has already been processed.
Availability matters when several instruments are involved. Group by when each bar can be observed. Grouping by candle open time can hand a strategy a completed candle before its close was knowable. The same rule makes a single-symbol replay easier to audit: you can trace which data was visible at each decision and which bar the broker used for execution.
Make warm-up and state explicit
Some strategies need history before they can make a decision. Trader
handles that with a separate optional warmer interface. The backtest
passes bars whose opening time precedes the trading start to strategies
that support warm-up and keeps those bars away from the broker. That gives an indicator history without also
giving the account a position before the run begins.
Keep indicator state, signal state, and account state distinct. The EMA crossover implementation, for example, records the last entry signal it consumed. That field can differ from the actual position after a simulated stop. A test of the signal and a test of the account should therefore assert different things. Trader also supports optional broker feedback before the next strategy decision for strategies that need execution events. That feedback is an explicit input, separate from submitting an order.
Test the boundary at two levels
- Feed an initialized strategy known bars and assert on the orders it returns. Cover insufficient history, the first actionable input, and repeated runs with fresh state.
- Run the strategy through the backtest runner and assert on submissions and fills. Cover warm-up bars that never trade, simultaneous bars delivered in one call, and a market order filling on the next bar.
Trader has strategy tests, backtest warm-up tests, and a frame-ordering test that illustrate these checks. A fixture with just a few bars can expose an ordering change that a comparison of aggregate returns would miss.
Common pitfalls
- Letting the strategy call a broker. A decision test now needs an execution environment and can accidentally depend on a broker’s behavior.
- Giving replay and live code different decision inputs. If a future live adapter supplies account or time data the replay cannot reproduce, sharing the strategy code won’t make the decisions comparable. Define those inputs explicitly and test equivalent event sequences.
- Hiding run state. A shared indicator or an implicit clock makes the next run depend on the previous one. Control their lifetimes and inputs in tests.
- Treating a request as a fill. Rejections, delays and fill prices belong to execution. A strategy test that asserts only its request tells you nothing about the account after that request.
Where this fits
A narrow strategy interface makes hypothetical live execution easier to reason about. It cannot establish how another broker will handle latency, gaps, rejections, or partial fills. Those are separate contracts with their own tests.
For the rest of the system, see the Trader project. The same boundary discipline applies to fixed-point money types in Go and to testing expected behavior.