Single source of truth: useTimestamp
useTimestamp is the hook you should use whenever you need “now” inside strategy or adapter code that runs in both real environments and replay.
- Live and paper - returns wall-clock time (
Date.now()), aligned with real execution. - Replay (backtest) - returns the current replay clock: the timestamp advanced by the replay scheduler as historical samples are processed, aligned to your backtest window and event order.
useTimestamp does not return the real wall clock. It returns the simulated time for the sample currently being handled, so logging, correlation IDs, and any logic that branches on “current time” stay coherent with the replayed data.
Why not use Date.now() directly?
Using Date.now() or new Date() for strategy decisions or event tagging breaks replay:
- In replay, events happened in the past; wall clock is unrelated to the backtest period.
- You get inconsistent ordering, misleading logs, and logic that behaves differently in replay vs live.
Date.now() for code that truly must know the wall clock (for example diagnostics outside the strategy pipeline). For anything that participates in strategy semantics or event correlation, use useTimestamp.
What Core already does
Several helpers attach timestamps usinguseTimestamp so downstream code stays consistent:
useLoggerprefixes log lines with a time derived fromuseTimestampwithRequestand WebSocket helpers tag responses withuseTimestamp()
Practical pattern
When you emit or transform events, prefer tagging with the replay-aware clock when the event is produced in your pipeline:useTimestamp for “when this was observed in the run” if you need both.
Summary
| Context | What useTimestamp returns |
|---|---|
| Live / paper | Wall-clock milliseconds (Date.now()) |
| Replay | Current replay time, aligned to the backtest stream |
useTimestamp everywhere strategy-relevant time matters so live and replay stay comparable and your backtest period is reflected correctly in time-based logic.
Replay workflow
See how replay windows and storage interact with scheduled time.