ProBacktest/probacktest · proorder

TIMEFRAME (PB/PO)

TIMEFRAME in ProBacktest and ProOrder runs strategy code on another timeframe. Syntax, updateonclose vs default modes, and multi-timeframe order examples.

Syntax

probuilder
TIMEFRAME(TF, mode)
probuilder
TIMEFRAME(default)

Parameters

NameTypeDefaultDescription
TFtimeframe keywordnoneTarget timeframe for the following lines, for example 1 minute, 15 minutes, 1 hour, 4 hours, daily, weekly. Must be greater than or equal to the timeframe the strategy runs on.
modeupdateonclose or defaultdefaultupdateonclose refreshes the section's values only when a TF bar closes. default recalculates them on every bar of the smallest timeframe, using the developing TF bar.

How it works

A strategy is attached to one base timeframe when it is launched, and that base must be the smallest timeframe the code uses. Every line after a TIMEFRAME(TF, mode) call is evaluated against bars of TF until the next TIMEFRAME call. TIMEFRAME(default) switches back to the base timeframe, which is where order management code usually lives.

The mode argument controls when the higher-timeframe section updates. With updateonclose, variables in that section only change when the higher-timeframe bar completes, so a 1-hour RSI holds its value for the full hour. With default, the section recalculates on every bar of the base timeframe using the current, still-forming higher-timeframe bar, so values move intrabar and can differ from the final closed-bar reading.

Variables keep their values across TIMEFRAME sections. A boolean computed in a 1 hour block can be read in the default block on the same run, which is the standard pattern: derive the signal on the higher timeframe, act on it at the finer one.

Examples

Example 1, Hourly signal with 1-minute order management (ProOrder)

probuilder
defparam cumulateorders = false

// Compute the entry signal on the 1-hour timeframe
TIMEFRAME(1 hour, updateonclose)
myrsi = rsi[14]
buycondition = myrsi crosses over 50

// Manage orders on the 1-minute base timeframe
TIMEFRAME(1 minute, default)
if buycondition then
  buy 10 contract at market
endif

if not onmarket then
  breakeven = 0
endif

if longonmarket and close - tradeprice >= 15 * pointsize then
  breakeven = 1
endif

if breakeven = 1 then
  sell at tradeprice + 5 * pointsize stop
endif

graph breakeven

The RSI cross is detected once per closed hourly bar, while the exit stop is evaluated minute by minute. This is the canonical example from the official reference.

Example 2, Daily trend filter for intraday entries (ProBacktest)

probuilder
// Trend filter computed on closed daily bars
TIMEFRAME(daily, updateonclose)
dailyMA = Average[200](close)
uptrend = close > dailyMA

// Entries and exits on the base timeframe
TIMEFRAME(default)
IF NOT onmarket AND uptrend AND close CROSSES OVER Average[20](close) THEN
  BUY 1 CONTRACT AT MARKET
ENDIF

IF longonmarket AND close CROSSES UNDER Average[20](close) THEN
  SELL AT MARKET
ENDIF

The 200-period daily average acts as a regime filter. Entries only fire on the base timeframe while the daily close sits above it.

Example 3, Intrabar reading of a 4-hour indicator (ProOrder)

probuilder
// The 4-hour RSI updates on every base-timeframe bar (default mode)
TIMEFRAME(4 hours, default)
h4rsi = RSI[14](close)

// Act on the base timeframe
TIMEFRAME(default)
IF NOT onmarket AND h4rsi < 30 THEN
  BUY 1 CONTRACT AT MARKET
ENDIF

IF longonmarket AND h4rsi > 70 THEN
  SELL AT MARKET
ENDIF

Because the 4-hour section uses default mode, the RSI reflects the developing 4-hour bar, so the strategy can react before the bar closes.

Common errors and gotchas

  • Base timeframe not the smallest. Every TIMEFRAME(TF) call must reference a timeframe greater than or equal to the one the strategy is launched on. Requesting a smaller timeframe than the base is rejected.
  • The two meanings of default. As the sole argument, TIMEFRAME(default) returns to the base timeframe. As the second argument, default is an update mode. TIMEFRAME(4 hours, default) and TIMEFRAME(default) do different things.
  • Intrabar values change under default mode. A condition that is true mid-bar can be false once the higher-timeframe bar closes. Use updateonclose when the logic should only see confirmed bars, and expect backtest results to differ between the two modes.
  • Wrong TIMEFRAME variant. ProScreener accepts a different form of this instruction. Screener syntax pasted into ProBacktest or ProOrder code, or the reverse, does not compile. See TIMEFRAME (PS).
  • TIMEFRAME (PS), the ProScreener variant of this instruction.
  • DEFPARAM, strategy-level parameters such as CumulateOrders.
  • CumulateOrders, controls whether repeated entries stack into one position.
  • ONMARKET, true while any position is open.
  • LONGONMARKET, true while a long position is open.
  • TRADEPRICE, the execution price of a past trade.
  • BUY, opens a long position.
  • SELL, closes a long position.