Navigation: Realtest Script Language > Bar Offsets

When a bar field such as Close or Volume is referenced in a formula, there is always a "current bar" context.

In a test, for example, the backtest engine is looping through the dates of the test range and evaluating formulas such as EntrySetup for each stock that has a bar for that date.

So when a formula uses a simple term like Close, it is inferred to be the closing price of the current stock being evaluated for the current date in the test.

When you need to refer to a different bar of the current stock, you do so using an offset.

Some software, including MetaStock and AmiBroker, use a function called "Ref" when an offset is required, and express the offset as a negative number of bars. Yesterday's close would be Ref(C,-1), the prior day is Ref(C, -2), and so on.

Other software, including TradeStation and ThinkOrSwim, use square brackets to express an offset, and express it as a positive number of bars. Yesterday's close is C[1], the prior day is C[2], and so on.

RealTest uses the square bracket syntax, but with some important enhancements:

The value inside the brackets can be any expression. Say you wanted some random close between 1 and 10 bars ago. That would be C[Random(1,10)].

Brackets can be applied to any expression. The difference between yesterday's high and yesterday's close could be expressed as H[1]-C[1], or as (H-C)[1]. The 10-day average close as of 3 days ago would be MA(C,10)[3].

Though not generally advisable, negative offsets can be used to look into the future. Tomorrow's close is C[-1], the day after tomorrow is C[-2], and so on.

One example of a valid use of negative offsets to obtain future values is with date elements such as BarDate, DayOfWeek, EndOfMonth, etc.