|
Category
General-Purpose Functions
Description
Evaluate expression as of the most recent available bar when one is missing on the current date
Syntax
MostRecent(expr)
Parameters
expr - any expression (typically with Extern)
Notes
To evaluate an expression for a symbol on a given date, that symbol must have a bar on that date. The evaluation date is the date of the current symbol's current bar at the moment of evaluation. When the symbol has no bar on that date, the lookup cannot be anchored and the result is NaN. This arises in two common situations:
•Cross-symbol references on a holiday. Suppose the current symbol is AUDUSD and the date is April 3, 2026 (Good Friday). US and AU equity markets are closed that day, so Extern($SPY, Close) returns NaN even though AUDUSD itself has a bar.
•Higher-timeframe scripts reading a lower timeframe. RealTest labels each weekly bar with the Friday of that week whenever any symbol in the data file traded that Friday — the "global calendar." A symbol that was closed that Friday has no daily bar on the weekly bar's label date, so Extern(~Daily, MA(C, 10)) returns NaN for that symbol even though its weekly bar exists.
MostRecent(expr) resolves both cases. When the bar for the current context is missing, it evaluates expr as of the most recent earlier bar instead of returning NaN. The expression is evaluated normally as of that bar — MostRecent does not fabricate or repeat data, so MA(C, 10), for instance, is the average of the ten real bars ending on that earlier date. (This differs from Padding: AllMarketDays, which inserts a synthetic bar that repeats the prior close and can distort range-based indicators such as ATR.)
Using the examples above, MostRecent(Extern($SPY, Close)) returns SPY's Thursday close, and MostRecent(Extern(~Daily, MA(C, 10))) returns the 10-day average close as of Thursday.
MostRecent must enclose the Extern, not the reverse. Extern($SPY, MostRecent(Close)) still returns NaN on a holiday, because Extern performs its bar lookup — and fails — before its argument is evaluated, so the inner MostRecent never takes effect. As a rule, place MostRecent around whatever is returning NaN.
The same rule applies to barsize-specific Data arrays. Reading a lower-timeframe Data item from a higher-timeframe context performs the same cross-barsize lookup as Extern(~Barsize, …) — an implied Extern at the point of reference. Apply MostRecent where you reference the item, not inside its definition (where there is no Extern to enclose and it has no effect). Alternatively, define the item with an explicit Extern(~Barsize, …) and wrap that with MostRecent, so the fallback value is computed once and every reference uses it.
Examples
Cross-symbol reference that survives a holiday:
Data:
spyClose: MostRecent(Extern($SPY, Close))
Daily indicator in a weekly-barsize script — preferred form, fallback stored once:
Settings:
BarSize: Weekly
Data:
dailyMA10: MostRecent(Extern(~Daily, MA(C, 10)))
Same result using a barsize-specific Data item — wrap the reference, not the definition:
Settings:
BarSize: Weekly
Data:
BarSize: Daily
ma10: MA(C, 10) // calculated on daily bars
Scan:
x: MostRecent(ma10) // apply MostRecent where the weekly context reads it
The following does not work — MostRecent inside a barsize-specific definition has no Extern to enclose:
Data:
BarSize: Daily
ma10: MostRecent(MA(C, 10)) // returns NaN on the missing date
|