SimulatedExecutor¶
SimulatedExecutor simulates order execution against historical market data for backtesting. It supports slippage models on market-style fills and optional queue simulation for resting limit orders.
class SimulatedExecutor : public IOrderExecutor
{
public:
static constexpr size_t kMaxSymbols = 256;
static constexpr size_t kDefaultOrderCapacity = 64;
static constexpr size_t kDefaultFillCapacity = 4096;
using OrderEventCallback = std::function<void(const OrderEvent&)>;
explicit SimulatedExecutor(IClock& clock);
void setOrderEventCallback(OrderEventCallback cb);
// Where each symbol's tick is read. Without a registry nothing is checked.
void setSymbolRegistry(const SymbolRegistry* registry);
// For a registry that is a copy of another's entries: the log of the
// registry it was copied from, whose applied ticks and lots win.
void setVenueTickLog(std::shared_ptr<const VenueTickLog> log);
int64_t orderTickRaw(SymbolId symbol) const; // 0 when no tick is known
int64_t orderLotRaw(SymbolId symbol) const; // 0 when no lot is declared
// Apply slippage and queue-simulation settings from a BacktestConfig.
void applyConfig(const BacktestConfig& config);
// Convenience setters for callers without a full BacktestConfig.
void setDefaultSlippage(const SlippageProfile& profile);
void setSymbolSlippage(SymbolId symbol, const SlippageProfile& profile);
void setQueueModel(QueueModel model, size_t depth);
void start() override;
void stop() override;
void submitOrder(const Order& order) override;
void cancelOrder(OrderId orderId) override;
void cancelAllOrders(SymbolId symbol) override;
void replaceOrder(OrderId oldOrderId, const Order& newOrder) override;
void submitOCO(const OCOParams& params) override;
ExchangeCapabilities capabilities() const override;
void onBookUpdate(SymbolId symbol, const std::pmr::vector<BookLevel>& bids,
const std::pmr::vector<BookLevel>& asks);
void onTrade(SymbolId symbol, Price price, bool isBuy);
void onTrade(SymbolId symbol, Price price, Quantity qty, bool isBuy);
void onBar(SymbolId symbol, Price close);
void onBar(SymbolId symbol, Price high, Price low, Price close);
void onBar(SymbolId symbol, Price open, Price high, Price low, Price close);
// Bar-callback window: orders arriving while it is open are held for the
// next bar's open instead of being matched against a bar already walked.
// BarCallbackScope is the RAII pairing; prefer it to the raw calls.
class BarCallbackScope { explicit BarCallbackScope(SimulatedExecutor&); };
void beginBarCallbackWindow() noexcept;
void endBarCallbackWindow() noexcept;
bool barCallbackWindowOpen() const noexcept;
size_t heldOrderCount() const noexcept;
void releaseHeldOrders(SymbolId symbol);
// Drop the state of a finished run, keep the configuration.
void reset();
const std::vector<Fill>& fills() const;
std::vector<Fill> extractFills();
const std::vector<Order>& conditionalOrders() const;
// The live order with this id (held, resting or conditional), or nullptr.
const Order* findOrder(OrderId orderId) const;
// Top-of-book accessors. Return 0 when the side is empty.
Price bestBidPrice(SymbolId symbol) const;
Price bestAskPrice(SymbolId symbol) const;
Price bookMidPrice(SymbolId symbol) const; // 0 when either side is empty
CompositeOrderLogic& compositeLogic();
};
start() and stop() are empty overrides. compositeLogic() is not noexcept.
Brackets¶
A single call submits the entry leg and arms a take-profit plus stop pair on entry fill. The simulator
runs the state machine: the first child to fill cancels the other, and cancelling the bracket cancels
every still-live leg. Order ids are derived from the bracket id — entry = bracketId * 3 + 0,
tp = bracketId * 3 + 1, stop = bracketId * 3 + 2.
void submitBracket(const BracketOrder& bracket);
void cancelBracket(uint64_t bracketId);
BracketStatus bracketStatus(uint64_t bracketId) const;
enum class BracketArmMode : uint8_t { OnFullFill = 0, OnPartialFill = 1 };
void setBracketChildArmMode(BracketArmMode mode) noexcept;
BracketArmMode bracketChildArmMode() const noexcept;
OnFullFill (the default) arms the children once, at full entry fill. OnPartialFill arms them at the
running entry-fill quantity on every partial, resizing via replace as more entry quantity fills.
Iceberg¶
Applies to orders of OrderType::ICEBERG.
void setIcebergRefreshLatency(int64_t latencyNs) noexcept;
int64_t icebergRefreshLatencyNs() const noexcept;
void setIcebergSizeRandomisationPct(double pct) noexcept;
double icebergSizeRandomisationPct() const noexcept;
void setIcebergJitterSeed(uint64_t seed) noexcept;
enum class IcebergPriorityMode : uint8_t { Back = 0, Retain = 1 };
void setIcebergPriorityMode(IcebergPriorityMode mode) noexcept;
IcebergPriorityMode icebergPriorityMode() const noexcept;
void setIcebergPriorityModeByName(const std::string& name) noexcept;
int64_t icebergHiddenRemainingRaw(OrderId id) const; // 0 if not an iceberg
| Setting | Description |
|---|---|
setIcebergRefreshLatency |
Delay between a visible tranche filling and the next being exposed. Applies to orders submitted after the call |
setIcebergSizeRandomisationPct |
Per-refresh visible-slice jitter as a fraction. 0.0 is deterministic, 0.10 is +/-10% uniform. Sampled from an internal RNG |
setIcebergJitterSeed |
Reseed that RNG to reproduce a specific draw sequence |
setIcebergPriorityMode |
Back: the refreshed slice goes to the back of the queue, behind whatever is still resting at the level (most crypto venues). Retain: it keeps the prior slice's queue position and trades on the next print (CME options, some Eurex contracts) |
icebergHiddenRemainingRaw |
Diagnostic: remaining hidden quantity, or 0 |
Self-trade prevention¶
STP keys on Order::accountId plus an optional STP group. Two orders share an STP scope when their
accountIds are equal, or when both accounts map to the same non-zero group.
void setSTPMode(STPMode mode) noexcept;
STPMode stpMode() const noexcept;
void setSTPGroupMembership(uint64_t accountId, uint64_t groupId); // groupId 0 removes
uint64_t stpGroupFor(uint64_t accountId) const;
bool sameStpScope(uint64_t a, uint64_t b) const;
See STPMode for the modes.
FOK semantics¶
enum class FokMode : uint8_t { AnyPrice = 0, SinglePrice = 1 };
void setFokMode(FokMode mode) noexcept;
FokMode fokMode() const noexcept;
void setFokModeByName(const std::string& name); // "any_price" | "single_price"
AnyPrice (the default, matching crypto venues) fills when cumulative liquidity at prices crossing
the order's limit is at least the order quantity. SinglePrice (CME, Eurex, most US equities) fills
only when the level at the limit price holds the whole quantity in one trade. The simulator currently
consults top-of-book quantity only. setFokModeByName is case-insensitive and ignores unknown values.
Latency¶
void setSubmitAckLatency(int64_t latencyNs, int64_t jitterNs);
void setCancelAckLatency(int64_t latencyNs, int64_t jitterNs);
void setReplaceAckLatency(int64_t latencyNs, int64_t jitterNs);
void setSubmitAckLatencyDistribution(const LatencyDistribution& dist);
void setCancelAckLatencyDistribution(const LatencyDistribution& dist);
void setReplaceAckLatencyDistribution(const LatencyDistribution& dist);
void applyLatencyProfile(const char* name);
The scalar setters delegate to the distribution setters: zero jitter becomes Constant, non-zero
becomes Uniform over [base - jitter, base + jitter].
Venue availability and rate limits¶
void setVenueAvailability(VenueAvailability* availability); // nullptr disables
VenueAvailability* venueAvailability() noexcept;
void setRateLimitPolicy(const RateLimitPolicy& policy);
void clearRateLimitPolicy();
bool hasRateLimitPolicy() const noexcept;
RateLimitPolicy& rateLimitPolicy();
Submit, cancel and replace issued while the venue is down are buffered and flushed at the recovery
edge in FIFO order. Market-data callbacks (onTrade, onBookUpdate, onBar) are silently dropped
during an outage, so the strategy sees a feed gap.
Submit, cancel and replace consult the rate-limit policy first; an overflow emits
OrderEventStatus::REJECTED_RATE_LIMIT and the action is not committed.
Queue tuning¶
setQueueFifoTopN, setTopPriorityShare, setLmmOrders, setLmmBonusMultiplier,
setOrderPriorityMultiplier and setQueuePositionMinChangeFraction are documented in
Queue simulation.
Execution logic¶
Order prices and the tick¶
A venue refuses an order whose price is not a whole number of ticks, and so does the simulator. With
a registry attached (setSymbolRegistry), each order is checked against its symbol's tick when it
reaches the simulator, and a replace is checked the same way. The rule lives in
include/lrvx/execution/order_price_rule.h and is the only implementation.
Where a runner's registry comes from:
| Runner | Ticks read from |
|---|---|
C++ BacktestRunner |
the registry passed to setSymbolRegistry; without one, the symbol entries the lrvx::Strategy was built with |
C++ BacktestRunner with an IStrategy that is not an lrvx::Strategy |
only a registry passed to setSymbolRegistry |
WalkForwardRunner folds (C++ and every binding) |
the strategy each fold's factory returns, as above |
Python BacktestRunner, lrvx_backtest_runner_create (Node, Codon) |
the registry they are constructed with |
Python SimulatedExecutor |
the registry passed to set_symbol_registry; none by default |
Node SimulatedExecutor, lrvx_simulated_executor_create |
nothing: no registry can be attached |
| Bundle replay | a registry holding the manifest's tick_size |
PaperBroker |
the registry it is given |
Vectorized Engine.run (Python, Node) |
nothing: its symbols are names with no tick |
| Order type | Checked | Reject reason |
|---|---|---|
LIMIT, ICEBERG |
price |
price_off_tick |
STOP_MARKET, TAKE_PROFIT_MARKET |
triggerPrice |
trigger_off_tick |
STOP_LIMIT, TAKE_PROFIT_LIMIT |
triggerPrice, then price |
trigger_off_tick / price_off_tick |
TRAILING_STOP |
trailingOffset (a fixed distance) |
trailing_offset_off_tick |
MARKET |
nothing |
A refused order emits REJECTED with the reason in OrderEvent::rejectReason, before SUBMITTED,
and never reaches the book. A refused replace emits REPLACE_REJECTED with the reason and leaves the
original order as it was. A replacement trailing stop that names a triggerPrice has that checked too
(trigger_off_tick). An order held from a bar callback is checked when the next bar's open
releases it.
Which tick applies:
| Symbol | Checked against |
|---|---|
Registered with a tick (tickSizeKnown, or any tickSize other than the 0.01 default) |
that tick |
Registered without a tick (the 0.01 default, tickSizeKnown false) |
nothing |
| Tick of zero | nothing |
| No registry attached | nothing |
The flag exists because the 0.01 default is also a common real tick: a SymbolInfo holding 0.01
cannot say by itself whether someone declared it. Any other tick can only have been set on purpose,
so it counts as declared without the flag, and C++ code that sets tickSize to a venue's tick is
checked without knowing the flag exists. Every binding sets the flag whenever a tick is passed:
Python add_symbol(tick_size=0.01), Node addSymbol(exchange, name, 0.01) and
lrvx_registry_add_symbol with 0.01 (which Codon calls) are all checked. In C++ an explicit 0.01
is checked only with tickSizeKnown = true.
The flag is part of the registry entry: registerSymbol(SymbolInfo), getSymbolInfo,
getAllSymbols, saveToFile / loadFromFile and serialize / deserialize all keep it. A
registry written before the flag was persisted loads with it clear; see
SymbolRegistry.
Prices the simulator computes itself are snapped onto the tick on the side that is worse for the
order's owner: a buy is moved up, a sell down. This covers a market-style fill moved by a slippage
model (see Slippage) and the trigger of a percentage trailing
stop (trailingCallbackRate). A volume-weighted fill over several book levels is an average, not a
traded price, and is left as it is.
Order quantities and the lot¶
A venue also refuses an order whose quantity is not a whole number of its quantity step. The
simulator does the same for a symbol that declares one (SymbolInfo::lotSize greater than zero),
read from the same registry as the tick:
| Checked | Reject reason |
|---|---|
quantity, on every order type, MARKET included |
quantity_off_lot |
visibleQuantity of an iceberg |
quantity_off_lot |
The check runs after the price check, so an order off both reports its price. A refused order
emits REJECTED and never reaches the book; a replace whose new quantity is off the lot emits
REPLACE_REJECTED and leaves the original as it was.
| Symbol | Checked against |
|---|---|
Registered with a lot (lotSize positive) |
that lot |
Registered without one (lotSize zero, the default) |
nothing |
| No registry attached | nothing |
There is no default lot and no flag: a lot is declared by being positive. It is set through
SymbolInfo::lotSize in C++, add_symbol(..., lot_size=) in Python,
addSymbol(exchange, name, tickSize, lotSize) in Node and
lrvx_registry_add_symbol_with_lot in the C API (which Codon calls). A QuickJS strategy registers
its symbols without a tick and without a lot. A lot is the step only; a venue's minimum order size
is a separate rule the simulator does not model. Fills the simulator sizes itself (a partial fill
against a book level, a queue model's share) are not snapped to the lot.
A tick or lot applied after the executor read the symbol¶
The executor caches each symbol's tick and lot on first use. The cache follows the registry: a
connector that applies the venue's tick or lot to an entry afterwards
(SymbolRegistry::applyVenueTick / applyVenueLot) moves the revision of the registry's venue
tick log, the executor reads that revision on every check (one atomic load) and drops both caches
when it has moved, so the next order is checked against the applied value. Before, the first
answer stuck, "nothing declared" included, and an executor that had seen one order on a symbol
never checked that symbol again.
A C++ BacktestRunner that was not given a registry builds one from the strategy's entries
(Strategy::symbolInfos()), which is a copy. It hands the executor the venue tick log of the
registry the strategy was built from (setVenueTickLog), and a tick or lot that log holds is used
in place of the copied entry's, so such a runner follows applied values too.
Market orders¶
A taker walks the visible ladder. onBookUpdate keeps every level it reports, not just the touch,
and a crossing order consumes them in order until its size is covered:
| Case | Fill price |
|---|---|
| Size covered by the visible ladder | Volume-weighted price of the levels consumed |
| Size past the visible ladder | The deepest level the walk reached, for the whole order |
| No ladder for the symbol (bar or trade-only feed) | Best ask / best bid, or the last trade when neither side is known |
Three consequences worth knowing:
- Depth is consumed. What one order ate is gone for the next order in the same step, and the
touch left behind is republished:
bestAskPrice/bestBidPricereport what is still there, and a side eaten to the last lot reports no touch at all. The next book snapshot restores the ladder — consumption is within a step, not permanent. - Size past the ladder is not free. The feed says nothing past the deepest visible level, so the simulator refuses to invent liquidity there and prices the whole order at that level. The order still fills in full; it just stops being cheap. Sweeping a book you could not really sweep is a modelling error either way — size the orders to the book you replayed.
- A maker takes nothing. A resting limit provides the liquidity, so it consumes no depth.
The walk is one fill per order, reported at the volume-weighted price, not one fill per level.
Slippage is applied on top of the walk for market orders, from the active SlippageProfile — it is
an independent friction, and a configured model stays configured whether or not there was depth to
walk. VOLUME_IMPACT and a full ladder both charge for size, so use one or the other. See
Slippage.
The ladder is whatever the last onBookUpdate call reported. The executor is not told whether an
update was a snapshot or a delta, so a feed that publishes only changed levels leaves a partial
ladder behind and a large order will clear against the deepest level of that delta. Replay snapshots
when the depth of the walk matters.
Limit orders¶
Without queue simulation (QueueModel::NONE, the default), a limit order fills as soon as the book crosses its price:
| Side | Condition to fill |
|---|---|
| BUY | orderPrice >= bestAsk |
| SELL | orderPrice <= bestBid |
With queue simulation enabled, non-crossing limits are registered in an OrderQueueTracker and fill only when trades at the level consume the queue ahead of them. Crossing ("marketable") limits still fill immediately, walking the ladder up to their own limit price, without slippage. A limit the tracker never registered — one submitted before the model was configured, or a stop-limit that rested when it triggered — has no queue to wait behind and falls back to the crossing rule above. See Queue simulation.
Conditional orders¶
Stop, take-profit, and trailing-stop orders are stored separately and checked on each market update.
| Type | Trigger condition |
|---|---|
| STOP_MARKET / STOP_LIMIT | SELL: price ≤ trigger, BUY: price ≥ trigger |
| TAKE_PROFIT_MARKET / TAKE_PROFIT_LIMIT | SELL: price ≥ trigger, BUY: price ≤ trigger |
| TRAILING_STOP | Trigger follows price; executes on reversal |
When triggered, conditional orders convert to market or limit and execute through the usual path
(slippage applies to the market leg), with one bound: a triggered order may fill worse than the
price that armed it, never better. A sell cannot print above its trigger, a buy cannot print below
it. On bar data the trigger is tested against a synthetic bid = ask = extreme, so without the
bound a take-profit sell armed at 105 would book the bar's 110 high — a price it had no claim on,
since it only exists because the bar is over. The bound is one-sided, so a protective stop still
pays the adverse extreme: a sell stop armed at 95 on a bar that traded down to 90 books 90.
replaceOrder on an order still waiting for its trigger rewrites it in place: it keeps waiting, with
the replacement's trigger, limit and quantity. The replacement must keep the order's type
(replace_changes_order_type otherwise). For a trailing stop a non-zero triggerPrice on the
replacement becomes the current trigger, and trailing continues from there. replaceOrder on an id
that is neither resting nor waiting emits REPLACE_REJECTED with unknown_order.
A trailing stop is bounded by its moving trigger, which is written onto the order as it fires, so
the TRIGGERED event reports the arming price. A stop-limit is additionally capped by its own limit;
one armed at a price past that limit cannot trade there and rests instead, the way a venue treats a
stop-limit triggered on a gap.
OCO orders¶
When one order fills, the other is canceled automatically.
Trailing stop¶
struct TrailingState
{
Price activationPrice{}; // price when trailing stop was activated
Price currentTrigger{}; // current trigger price (moves with price)
};
SELL trailing: trigger follows price up (never down). BUY trailing: trigger follows price down (never up). A percentage trailing trigger is snapped onto the symbol's tick, down for a sell and up for a buy.
Feeding market data¶
| Call | When to use |
|---|---|
onBookUpdate(symbol, bids, asks) |
Full L2 snapshot. Replaces the visible ladder, updates top-of-book state and drives the queue tracker's level-update heuristic. |
onTopOfBook(symbol, bids, asks) |
Touch-only frame (BookUpdateType::TOP_OF_BOOK): moves the touch, drops levels more aggressive than it, keeps deeper depth from the last onBookUpdate, then the same matching pass. An empty side means an empty side. |
onTrade(symbol, price, isBuy) |
Trade event without quantity. Keeps legacy behavior but does not drive queue-simulated fills. |
onTrade(symbol, price, qty, isBuy) |
Trade event with quantity. Required for queue simulation. |
onBar(symbol, close) |
Bar close shortcut. Sets best bid, best ask, and last trade to the close price. Intrabar stops and targets that would have triggered against the wick are missed. |
onBar(symbol, high, low, close) |
Walks low, then high, then close, so wick-triggered stops and take-profits match. Carries no open, so it releases no held orders. |
onBar(symbol, open, high, low, close) |
The form a bar-driven run should use: moves the market to the open, releases the orders held from the previous bar's callback there, then walks low, high and close. |
A bar reports no depth, so a bar step drops the symbol's ladder: on bar data any size trades at the step price, and a stale book from an earlier update is not walked at prices the bar has left behind.
The binding-level one-level feeds — the C API's lrvx_simulated_executor_on_best_levels, Python's
on_best_levels, QuickJS's onBestLevels — land on onTopOfBook, not onBookUpdate: depth from
the last snapshot survives a one-level quote instead of being replaced by it. Callers that mixed
on_best_levels with snapshot feeds and relied on the quote wiping the ladder get the touch
semantics instead.
Bar data and look-ahead¶
A bar reaches the strategy only after the simulator has walked it, which means the whole bar — open, high, low and close — is in the market state by the time the callback runs. Matching an order submitted from there against that state is look-ahead: the order prints at a price that exists only because the bar is over.
beginBarCallbackWindow() / endBarCallbackWindow() mark that callback. While the window is open
an arriving order is held rather than submitted; the next onBar(symbol, open, high, low, close)
for that symbol releases it at the open, through the full submit path, so rate limits, reduce-only
and self-trade prevention are evaluated where the order actually reaches the venue. The deferral
does not resize or split it.
Hold a SimulatedExecutor::BarCallbackScope for the span of the callback rather than calling the
two by hand. The window is a counter, and an early return or a callback that throws leaves it up;
a window that never closes holds every order submitted after it for the rest of the run, and says
nothing while it does. The raw calls remain public for drivers that cannot put the callback inside
a scope.
- An order held when the run ends never reached the venue: it neither fills nor cancels.
heldOrderCount()reports how many are waiting. - Cancel and replace reach a held order where it waits, so an order submitted and pulled inside one callback cannot leak into the next bar.
- The hold is per symbol. An order for a symbol whose bar never comes back is never released.
- Window depth is counted, so a re-entrant callback cannot close a window it did not open.
BacktestRunner::runBars opens and closes the window around the strategy callback for you.
Reset¶
reset() drops everything a finished run left behind — fills, live, held and conditional orders,
market state, ladder, net positions, queue positions, brackets, icebergs and in-flight acks — and
keeps the configuration: slippage, queue model, latency distributions, rate limits, STP, the order
event callback and the attached VenueAvailability. Seeded generators go back to their configured
seeds, so a repeated run repeats. BacktestRunner calls it at the start of every run.
Order events¶
| Event | When |
|---|---|
SUBMITTED |
Order received |
ACCEPTED |
Order validated |
PENDING_TRIGGER |
Conditional order waiting for trigger |
TRIGGERED |
Conditional order triggered |
FILLED |
Fully executed |
PARTIALLY_FILLED |
Partial execution (common with queue simulation) |
CANCELED |
Order canceled |
REPLACED |
Order modified |
REJECTED |
Order refused; rejectReason says why (for example price_off_tick, quantity_off_lot, reduce_only) |
REPLACE_REJECTED |
Replace refused; the original order is unchanged. rejectReason says why (price_off_tick, trigger_off_tick, quantity_off_lot, unknown_order, replace_changes_order_type, late_replace_after_fill, late_replace_after_trigger) |
Trailing stop updates emit TRAILING_UPDATED events with the new trigger price.
Market state¶
Per-symbol best bid, best ask, last trade and level quantities are updated via onBookUpdate() and
onTrade(), and by the taker walk, which republishes the touch it leaves behind. The state struct
itself is private; read it through the public accessors:
Price bestBidPrice(SymbolId symbol) const; // 0 when the side is empty
Price bestAskPrice(SymbolId symbol) const;
Price bookMidPrice(SymbolId symbol) const; // 0 when either side is empty
The tracked level quantities are consumed internally by VOLUME_IMPACT slippage and by the queue
tracker; they are not exposed.
Performance¶
- Fixed-size array for symbols 0-255 (fast path)
- Overflow vector for symbol IDs >= 256
- O(n) pending order scan on each market update
- Pre-allocated fill vector (default 4096)
See also¶
- BacktestRunner — Run backtests with strategies
- BacktestResult — Performance statistics
- Slippage
- Queue simulation