Read many intervals of one or several markets cut at one instant with bars_window — which candles are closed, which one is still forming — and a short test that shows no price from the future can reach an observation.
An agent that reads the one-minute chart together with the hourly and the four-hour chart has a problem a single-timeframe agent does not: at 07:40 the 07:00 hourly candle is not finished. Its final high, low and close will only exist at 08:00. A program that reads "the latest hourly candle" from a finished history file reads those final values twenty minutes early. That mistake is called look-ahead, and a model trained on it learns from prices it will never have when it trades.
In Bentrix the mistake cannot be made by accident, for one reason: every read is cut at the workbench's clock. Nothing after the clock exists for a reader. This page shows how to read several timeframes, and then proves the cut.
01One call, many timeframes
bars_window answers the candles of several intervals — and several markets — cut at one instant:
got = engine.call("bars_window", bench=bench, symbol="XAUUSD",
frames=["M1", "M5", "M12", "H1", "H4"], limit=3, include_forming=True)
frame = got["markets"]["XAUUSD"]["H1"]
For each market and frame it answers:
| Field | Meaning |
|---|---|
rows | Closed candles only, oldest first, at most limit (200 when omitted, 5,000 at most). Each row is time_ms, open, high, low, close, tick_volume, spread_points; time_ms is the candle's opening time. |
count | How many rows. Fewer than limit near the start of the workbench's days. |
last_closed_open_ms | When the newest closed candle opened; null when none has closed yet. |
forming | With include_forming: the candle still being built, as far as the clock has seen it. It is never one of the rows. |
and, for the whole answer, cursor_ms (the clock) and at_ms (the instant of the cut).
Which intervals. The standard ones — M1, M5, M15, M30, H1, H4, D1, W1 — and custom ones in the same form: M2, M3, M4, M6, M10, M12, H2, H3, H6, H8. Up to 16 frames and 64 markets in one call.
Three states of a candle follow directly:
- CompleteIt is in
rows. - FormingIt is
forming: its open is final; its high, low and close are only "so far". - UnavailableIt would open after the clock. It is nowhere in the answer.
Aligning to a slower interval. aligned_to="H1" cuts every frame at the last hourly close instead of at the clock, so every frame ends at the same finished hour. Use it when the agent decides once an hour and should see nothing newer than that close.
Daily candles follow the workbench's time zone: a D1 candle opens at the workbench's own midnight and a W1 on Monday, as on its charts. zone="…" cuts the frames in another zone for this call only.
02The proof
The program stands at 07:40, reads five timeframes, then moves two hours on and reads again. If the first read had held anything from the future, the two would disagree.
import tempfile
from bentrix import _native
from bentrix.local import Local
workspace, store = _native.make_tape(tempfile.mkdtemp(prefix="bentrix-practice-"))
engine = Local(workspace=workspace, store=store)
bench = engine.call("get_state")["laboratories"][0]["benches"][0]["uri"]
engine.arm(bench)
symbol = "XAUUSD"
FRAMES = ["M1", "M5", "M12", "H1", "H4"]
def window(limit=3):
return engine.call("bars_window", bench=bench, symbol=symbol, frames=FRAMES, limit=limit, include_forming=True)
# Stand in the middle of an hour: 07:40 on the first day.
start = engine.state(bench)["bounds"]["from_ms"]
engine.advance(bench, start + 7 * 3_600_000 + 40 * 60_000)
seen = window()
print("the clock:", seen["cursor_ms"])
for frame in FRAMES:
f = seen["markets"][symbol][frame]
closed = [row[0] for row in f["rows"]]
print(f"{frame:>3}: {f['count']} closed, the newest opened at {f['last_closed_open_ms']}; forming since {f['forming'][0]}")
# The proof: go forward two hours and read again. Every candle that was closed then is the same row
# now, and the candle that was forming then has since closed with a different high, low and close.
before = {frame: seen["markets"][symbol][frame] for frame in FRAMES}
engine.advance(bench, seen["cursor_ms"] + 2 * 3_600_000)
later = engine.call("bars_window", bench=bench, symbol=symbol, frames=FRAMES, limit=200)
for frame in FRAMES:
rows_now = {row[0]: row for row in later["markets"][symbol][frame]["rows"]}
unchanged = all(rows_now[row[0]] == row for row in before[frame]["rows"])
forming_then = before[frame]["forming"]
closed_since = rows_now.get(forming_then[0])
ohlc = lambda row: [round(x, 3) for x in row[1:5]] # gold has 3 decimals here
print(f"{frame:>3}: closed rows unchanged: {unchanged}; the forming candle then {ohlc(forming_then)} closed as "
f"{ohlc(closed_since) if closed_since else 'not yet closed'}")
engine.close()
the clock: 1709538000000
M1: 3 closed, the newest opened at 1709537940000; forming since 1709538000000
M5: 3 closed, the newest opened at 1709537700000; forming since 1709538000000
M12: 3 closed, the newest opened at 1709537040000; forming since 1709537760000
H1: 3 closed, the newest opened at 1709532000000; forming since 1709535600000
H4: 1 closed, the newest opened at 1709510400000; forming since 1709524800000
M1: closed rows unchanged: True; the forming candle then [2107.717, 2107.717, 2107.717, 2107.717] closed as [2107.717, 2107.767, 2107.553, 2107.603]
M5: closed rows unchanged: True; the forming candle then [2107.717, 2107.717, 2107.717, 2107.717] closed as [2107.717, 2107.767, 2107.058, 2107.108]
M12: closed rows unchanged: True; the forming candle then [2108.072, 2108.122, 2107.667, 2107.717] closed as [2108.072, 2108.122, 2106.71, 2106.76]
H1: closed rows unchanged: True; the forming candle then [2106.165, 2108.236, 2105.86, 2107.717] closed as [2106.165, 2108.236, 2105.86, 2106.407]
H4: closed rows unchanged: True; the forming candle then [2108.217, 2108.267, 2102.694, 2107.717] closed as [2108.217, 2108.267, 2102.694, 2106.407]
Read the result:
- Every candle that was closed at 07:40 is byte-for-byte the same row two hours later. A closed candle never changes.
- Every candle that was forming at 07:40 closed differently. The hourly candle stood at a close of 2107.717 at 07:40 and finished the hour at 2106.407. The first read did not know that — it could not.
- The forming candle at 07:40 opened exactly on the clock for
M1andM5: one tick old, its four prices all equal.
So an observation built from rows alone holds only finished candles. One built from rows plus forming holds what a trader watching the chart at that instant would have seen — also free of the future, but different from what a history file shows for that candle. Decide which your agent trains on, and use the same when it trades.
03Rules that keep an agent honest
- Build observations from
rows. Useformingonly on purpose, knowing its high, low and close are provisional. - Read at the clock, never from a file prepared earlier. A table of hourly candles computed once for the whole history, then indexed by time, is where look-ahead comes from.
bars_windowat each step cannot leak. - Indicators too.
bentrix.data.indicatorandstudy_valuescompute over candles up to the clock. An indicator computed in your own code overrowshas the same property; one computed over a whole downloaded history does not. - Orders fill after the decision. An order sent at a close fills on the next tick — the engine in your own program shows it — so a decision can never be filled at a price that came before it.
04Good to know
bars_getreads one market and one interval, 5,000 candles a page, and is the door for long histories;bentrix.data.barspages through it into NumPy columns.- Ticks are cut the same way: the
data_ticksresource answers one day's ticks that have happened by the clock, never one past it. - The verification battery runs a test of the same idea on any recording: a strategy run over half the days must trade exactly as it did in that half of the whole run.