Vectorized backtests compute every signal at once over a full price array, while event-driven backtests process one bar or tick at a time and update state as the market unfolds. The difference matters because a vectorized loop can accidentally let a decision at time t see prices from t+1, producing results that cannot be reproduced live. This article explains how the two designs differ, how lookahead bias enters synthetic-index strategies, and how to refactor a strategy into an event-based simulator whose trade decisions use only data available at the moment of the decision.
Trading involves significant risk and you can lose your entire stake. Test every strategy on a demo account first.
Vectorized and event-driven loops
A vectorized backtest treats the price series as an array and evaluates the strategy as array operations. A moving average becomes a rolling mean over the whole column; a crossover signal becomes a boolean column; the equity curve becomes a cumulative product. This is fast and convenient for research, but it hides an assumption: that the value of any row is a pure function of rows at or before it. When that assumption is violated, the backtest silently reads the future.
An event-driven backtest makes the assumption explicit. The simulator holds a clock and a state object. On each event — a new tick, a closed candle, a settlement — it appends the new data to the history, recomputes only what depends on the new data, asks the strategy for a decision using the current state, and then applies the outcome. Nothing after the current event exists yet, so nothing after it can leak in.
For Deriv synthetic indices the distinction is concrete. Ticks arrive continuously and candles are derived from them. A strategy that decides on candle close must see the completed candle and the ones before it, not the forming candle’s final value. A strategy that decides on ticks must see the tick that just arrived, not the next one. Both are easy to get right in an event loop and easy to get wrong in a vectorized frame.
Where lookahead bias enters
Lookahead bias has several common sources, and most of them are structural rather than mathematical.
- Centering and full-series statistics. A z-score computed over the whole series uses the mean and standard deviation of future data. The same applies to min-max scaling, percentile ranks and any normalization fitted on the full sample.
- Shift errors. A signal computed from a candle’s close and applied to that same candle’s open is impossible in live trading. The decision must be applied to the next available price.
- Forming candles. If the simulator stores a candle that is still open, the strategy can read a close that has not happened.
- Settlement timing. On fixed-stake contracts the outcome is known at expiry, not at entry. A backtest that books the result at entry time will misstate both the equity path and any drawdown limit that depends on realized balance.
- Survivorship of parameters. Tuning a threshold on the same data used to report performance is not lookahead in the strict sense, but it produces the same optimistic illusion. Out-of-sample testing is the remedy.
The general rule is simple: at the moment of a decision, the strategy may read only events with a timestamp less than or equal to the current clock, and only fields that were final at that timestamp.
Refactoring into an event-based simulator
The refactor is mechanical. Start from the vectorized version, list every input the strategy reads, and classify each as either a finalized past event or a derived quantity. Then rebuild the loop so that derived quantities are recomputed incrementally from the history buffer.
A minimal simulator in pseudocode looks like this. It is generic pseudocode, not a Deriv Bot block structure; DBot does not expose user-defined mutable lists or a programmable per-tick loop, and the mapping to DBot is described separately below.
state = {history: [], position: null, balance: initial, equity: [initial]}
for event in stream: # ticks or closed candles, in time order
state.history.append(event) # only finalized data enters history
features = compute(state.history) # uses history only, no future rows
if state.position is None:
signal = strategy_entry(features)
if signal:
stake = fixed_stake(state.balance)
state.position = open_contract(signal, stake, event.time)
else:
if event.time >= state.position.expiry:
pnl = settle(state.position, event.price)
state.balance += pnl
state.equity.append(state.balance)
state.position = None
if limits_breached(state): break
Three properties make this loop honest. History receives only finalized events, so a forming candle never enters. Features are computed from history alone, so no future row is visible. Settlement happens at expiry, so the balance path matches what a live account would show.
The same logic maps onto Deriv Bot, but only through its actual blocks. DBot provides trade parameters, purchase conditions, sell conditions, restart and stop conditions, and built-in stop-loss and take-profit settings. It exposes built-in series such as the last digit, tick and candle lists, but not user-defined mutable lists or a free-form per-tick loop. A DBot version of the strategy therefore uses purchase conditions evaluated on each tick against those built-in lists, sell conditions where the contract type supports selling, and the built-in session limits for stopping. Any state the strategy needs beyond that must be expressed through the contract’s own parameters, not through an arbitrary log variable.
Worked example. Suppose a Rise/Fall strategy uses a fixed stake of 1.00 per contract and a payout of 0.95 on a win. Over a demo sample of 1000 contracts it wins 540 and loses 460. Gross return is 540 × 0.95 = 513.00 and gross loss is 460 × 1.00 = 460.00, so net is 513.00 − 460.00 = 53.00. Expectancy per contract is 53.00 / 1000 = 0.053. The break-even win rate solves p × 0.95 = (1 − p) × 1.00, giving p = 1 / 1.95 ≈ 0.5128, so the observed 0.540 is above break-even on this sample. Now suppose the same strategy is run with a stake of 1.00 but a leaked future signal inflates the win count to 600 and cuts losses to 400. Net becomes 600 × 0.95 − 400 = 570.00 − 400.00 = 170.00, and expectancy rises to 0.17. The gap between 0.053 and 0.17 is the size of the leak, and it is exactly the kind of error an event loop prevents.
Verifying replay parity
Once the event simulator exists, verify it against a reference. Run the same finalized event stream through the simulator twice with different batch sizes; the equity curve must be identical. Then compare the simulator’s decisions against a live demo run of the same rules. On Deriv this comparison is done through the Deriv API and the DBot interface, not by importing arbitrary event streams into DBot itself. The API can stream ticks and candles, and DBot can run the same rule set on a demo account; parity means the entry and exit decisions agree event by event. Any divergence points to a state bug, a timing assumption, or a difference in how the platform finalizes candles.
Limitations
This approach does not fix a bad hypothesis. It does not remove execution differences between demo and live, such as latency, slippage on Multipliers, or contract availability. It does not validate the sample size; a clean loop over 200 trades is still a small sample. It does not replace out-of-sample testing, and it does not tell you how many configurations were tried before the reported one. It also does not make a strategy profitable; it only makes the backtest honest about what the strategy would have known at each moment.
Frequently Asked Questions
Does an event-driven backtest guarantee live results will match? No. It guarantees only that the backtest does not use future data; live execution, latency and platform differences remain.
Can I build the same loop directly in Deriv Bot? No. DBot uses a fixed set of blocks and built-in series; the generic loop above is pseudocode, and the DBot version must be expressed through purchase, sell and restart conditions plus the built-in limits.
How do I know my vectorized backtest has lookahead bias? Compare it to an event-driven version on the same data; a large drop in expectancy usually indicates leakage, as in the worked example.
Key Takeaways
- Vectorized backtests assume each row depends only on past rows; event loops enforce that assumption explicitly.
- Lookahead enters through centering, shift errors, forming candles and settlement timing.
- Refactor by appending only finalized events, computing features from history, and settling at expiry.
- DBot does not expose user-defined lists or a free-form loop; map the logic to its real blocks and built-in limits.
- Verify parity by replaying the same stream and comparing decisions, using the Deriv API and DBot for the demo comparison.
The event-driven design is the difference between a backtest that describes a strategy and one that describes a fantasy. Open-source bots and simulator patterns are available in the ORSTAC repository, and the strategies can be tested on Deriv, where a free demo account is available. Discussion and review of implementations take place in the ORSTAC Telegram group.
Trading involves significant risk and you can lose your entire stake. Test every strategy on a demo account first.
