CODESCRIPT

Multi-timeframe

15 entries. What each one takes, what it returns, and a working example.

htf(timeframe, source)

Higher-timeframe value (stable — it does not change on later bars).

Samples the source to the higher timeframe: once each HTF bar COMPLETES, its last value is carried forward (constant within the bar).

timeframe
string
Higher timeframe (e.g. "60", "1D", "1W").
source
series
The series to sample to the higher timeframe (e.g. close).

Note

There is NO look-ahead — the value appears only after the HTF bar closes, so it does not peek into the future (repaint-safe). A simpler shortcut than `security` for same-symbol higher timeframes.

When to use

To use a higher-timeframe trend/level (e.g. daily close, weekly average) as a filter on a lower-timeframe chart.

Limits

Until the HTF bar closes the value comes from the previous completed bar (it does not show the currently forming HTF bar). timeframe must be a valid duration.

CODESCRIPT
daily_close = htf("1D", close)
plot(daily_close, "Daily Close")

Carries the daily close onto the lower-TF chart as a step line.

CODESCRIPT
plot(close > htf("1D", sma(close, 20)) ? 1 : 0, "Above daily SMA")

1 when price is above the daily 20-SMA (a higher-TF trend filter).

quandl(code,...)

External Quandl data (discontinued → empty).

Always returns an empty (na) series — no data is provided because the source (NASDAQ Data Link) was discontinued.

code
string
Dataset code.

Note

Present for backward compatibility; it returns no functional data because the source was shut down (the data provider itself has closed).

Limits

Always na. Use `seed` instead.

CODESCRIPT
plot(quandl("FRED/GDP"), "Quandl")

Returns an empty (na) series — source is closed.

security(symbol, timeframe, source, gaps?, lookahead?, ignore_invalid_symbol?)

Series from another symbol/higher timeframe (source text=field name, or a series). Parameter names are English: symbol, timeframe, source. the 4th argument, gaps (barmerge.gaps_on/off), 5th lookahead (barmerge.lookahead_on/off). ignore_invalid_symbol is named-argument only: true → returns na instead of erroring on an invalid/unloaded symbol. (currency and calc_bars_count are not supported in this version.)

The series of the expression computed on the higher timeframe and aligned to the current bar without looking ahead.

symbol
string
Symbol; usually syminfo.tickerid (the current symbol) or a fixed code.
timeframe
string
Timeframe (e.g. "60", "240", "D"). Empty = chart timeframe.
source
series
Expression to compute on the higher timeframe (e.g. close, sma(close,20)).
gaps
string
Optional (4th argument in order). barmerge.gaps_on = leave gaps (na) on in-between bars; barmerge.gaps_off (default) = carry the last value forward.
lookahead
string
Optional (5th argument in order). barmerge.lookahead_off (default) = no look-ahead (causal). barmerge.lookahead_on introduces look-ahead leakage — NOT recommended in backtests.
ignore_invalid_symbol
bool
Optional, given by name ONLY (not in order). If true, an invalid/unloaded symbol returns na (empty) instead of raising an error. Default false (raises an error).

Note

The higher-timeframe value is aligned after that bar CLOSES (it does not look ahead and does not change afterward). The `currency` (FX conversion) and `calc_bars_count` parameters are NOT supported in this version — if given, they are silently ignored. The higher-timeframe value aligns only after that bar closes; the symbol and timeframe must stay constant throughout the run.

When to use

For a multi-timeframe filter (for example, trading on the 5-minute according to the 1-hour trend direction).

Limits

The symbol and timeframe must stay fixed through the run (a symbol that changes bar by bar is not supported). Until the higher-timeframe bar closes, the last closed higher-timeframe value is carried forward — so it looks flat across the forming higher-timeframe bar.

Not for

Not for a symbol or timeframe that changes bar by bar — both must stay constant through the run. If you want the same data as the chart's own timeframe, you do not need security; use close/sma(...) directly. It is not for dropping to a lower (shorter) timeframe to read data early; a higher-timeframe value aligns only after that bar closes.

Tip

The higher-timeframe value aligns only after that bar closes; the symbol and timeframe must stay constant throughout the run.

The higher-timeframe value is aligned after that bar CLOSES (it does not look ahead and does not change afterward). The `currency` (FX conversion) and `calc_bars_count` parameters are NOT supported in this version — if given, they are silently ignored.

The higher-timeframe value aligns only after that bar closes; the symbol and timeframe must stay constant throughout the run.

CODESCRIPT
plot(security(syminfo.tickerid, "60", close), "1h close", "#ff9800")

A stepped line of the 1-hour close on a 5m chart (updates every hour).

CODESCRIPT
htf = security(syminfo.tickerid, "60", sma(close,20))
enterLong(crossover(close, htf))
exit(crossunder(close, htf))

BUY when price crosses above the 1-hour SMA20 — a higher-timeframe trend filter.

securityLowerTf(symbol, tf, source)

Intrabar data from a lower timeframe (LTF): for each bar it returns the lower-TF values inside that bar as an array. The request.security_lower_tf namespace form is equivalent.

For each main-chart bar, an array of the lower-TF values that fall inside that bar, in chronological order.

symbol
string
A literal symbol string (PRIMARY or empty is not supported).
tf
string
Must be a timeframe LOWER than the main chart's (LTF).
source
series
The expression to evaluate in the LTF context (e.g. `close`).

Note

Equivalent to `request.security_lower_tf(symbol, tf, source)` (the namespace form). The `source` expression may only reference built-ins, user functions (`fn`), and independent top-level variables.

Limits

Only accepts a LOWER timeframe — passing the same or a higher TF raises a clear error (use `request.security` for that). There is a depth limit on nested `request.security*` calls; exceeding it raises an error.

CODESCRIPT
ltfCloses = securityLowerTf("BTCUSDT", "1", close)
plot(ltfCloses.size() > 0 ? ltfCloses.get(ltfCloses.size() - 1) : na)

Returns an array of 1-minute lower-TF closes for each main-chart bar; plots the last element.

seed(source, symbol)

External (seed) data series. na if no data.

Returns a symbol's series from a custom seed dataset, aligned to the bars.

source
string
Seed dataset/source name.
symbol
string
Symbol within that source.

Note

Populated only if the terminal supplies the relevant seed data; otherwise na.

Limits

If this data is not supplied by the terminal, the series is empty (na). Fundamental-data coverage varies by symbol/market.

CODESCRIPT
plot(seed("seed_crypto", "BTC_ACTIVITY"), "Seed series")

Plots the series from the seed source (if data exists).

sym(symbol)

Another symbol's series (.open/.high/.low/.close/.volume), aligned to this chart's time. For pair-RV.

The series of another symbol (.open/.high/.low/.close/.volume), aligned to this chart's time.

symbol
string
Symbol code, e.g. "LTCUSDT".

Note

For pair/ratio strategies: ratio = close / sym("LTCUSDT").close. The returned object's fields are used in calculations like any series.

When to use

To compare two instruments on one chart, build a ratio/spread, or measure relative strength or correlation (market-neutral pairs).

Limits

At most 4 additional symbols. The data must be supplied by the terminal; if it is not loaded the call raises. Series are aligned to this chart's bar times (the secondary symbol's own gaps are carried onto the aligned grid).

Not for

Not for a higher timeframe of the same symbol — use htf or security for that.

CODESCRIPT
ratio = close / sym("LTCUSDT").close
plot(ratio, "Ratio")

Plots the price ratio of the two symbols (the basis of a pair/ratio strategy).

CODESCRIPT
plot(correlation(close, sym("LTCUSDT").close, 30), "Correlation")

Plots the 30-bar correlation between this symbol and LTCUSDT.

syminfoPrefix(symbol)

Exchange prefix (before ':').

Returns the prefix before ":" in the ticker; an empty string if there is no ":".

symbol
string
Full ticker (exchange prefix and symbol, separated by ':').

Note

To extract which exchange/provider a symbol belongs to.

When to use

To decide or label by the exchange part of a ticker (e.g. applying a rule only to the symbols of one exchange).

Limits

Splits on the first ":"; returns an empty string for a symbol without a ":".

CODESCRIPT
label(close, syminfoPrefix(syminfo.tickerid))

Labels the exchange prefix of the chart's symbol.

syminfoTicker(symbol)

Ticker id (after ':').

With an argument, returns the symbol part after ":" (e.g. "BTCUSDT"); called with no argument, returns the currently processed symbol.

symbol
string
Optional. Full ticker (exchange prefix and symbol, separated by ':'). If omitted, the active context symbol.

Note

Bare syminfoTicker() yields the active symbol in multi-symbol scans (useful inside request.security loops and securityLowerTf expressions).

When to use

To get the bare symbol code without a prefix, or to learn the currently processed symbol inside a scan for labeling/logging.

Limits

The argument form takes what follows the first ":" (or the string as-is if there is no ":"). The bare form returns an empty string when no active symbol is set.

CODESCRIPT
label(close, syminfoTicker(syminfo.tickerid))

Labels the chart symbol's code (e.g. BTCUSDT).

tickerHeikinashi(symbol)

Heikin-Ashi 'virtual ticker' marker — computes nothing by itself; pass it as the SYMBOL of security(tickerHeikinashi(symbol), tf, source) and source (close/open/high/low) then returns that symbol's Heikin-Ashi-transformed value. Empty symbol or syminfo.ticker means the current chart itself — only at the chart's OWN timeframe (tf empty/matching); a different tf errors out.

Computes nothing — returns a 'virtual ticker' marker (text) meant for security()'s SYMBOL argument. The actual Heikin-Ashi transform is applied when security() runs.

symbol
string
The symbol to take the Heikin-Ashi of. Empty, or syminfo.ticker, means the current chart itself.

Note

Mirrors the common idiom: security(tickerHeikinashi(syminfo.ticker), "" instead of timeframe.period, close). The close/open/high/low inside that call are NOT standard bars — they are the Heikin-Ashi formula (close=(o+h+l+c)/4, open=average of the previous HA-open/HA-close, high/low=max/min of the real value and HA-open/HA-close).

When to use

When you want an HA-based source (e.g. a crossover against HA-close) without switching the main chart to Heikin-Ashi.

Limits

Only works at the chart's OWN timeframe — a different timeframe (e.g. "1D" on a 1h chart) raises a clear error (it does NOT produce an approximate/wrong result). For another symbol's Heikin-Ashi, that symbol must already be loaded elsewhere in the code via sym()/security().

CodeScript operates on standard (time) bars; Heikin-Ashi bars are not produced. Instead of silently returning wrong (standard-bar) results, it raises a clear error.

CODESCRIPT
haClose = security(tickerHeikinashi(syminfo.ticker), "", close)

Takes the close from the symbol's Heikin-Ashi bars; the chart still shows normal bars while the calculation runs on smoothed ones.

tickerInherit(symbol, source)

Inherits symbol settings. Example result: BTCUSDT

Returns the first ticker UNCHANGED (identity).

symbol
string
Ticker string.
source
string
Source ticker to inherit settings from.

Note

Intended for a symbol to inherit another symbol's settings; it has no effect on standard bars (a placeholder).

When to use

For syntax compatibility when porting code where one symbol inherits another's settings.

Limits

Returns the first ticker unchanged; no real setting is carried over from the source ticker.

CODESCRIPT
label(close, tickerInherit("BINANCE:ETHUSDT", syminfo.tickerid))

Returns the first ticker as-is (ETHUSDT).

CodeScript operates on standard (time) bars; Kagi bars are not produced. Instead of silently returning wrong (standard-bar) results, it raises a clear error.

CodeScript operates on standard (time) bars; Line-break bars are not produced. Instead of silently returning wrong (standard-bar) results, it raises a clear error.

tickerModify(symbol,...)

Modifies the ticker id. Example result: BTCUSDT

Returns the ticker UNCHANGED (identity).

symbol
string
Ticker string.

Note

Intended to attach session/adjustment settings to a ticker; since CodeScript works on standard bars, this transform leaves the symbol as-is (a placeholder).

When to use

Present for syntax compatibility when porting code that attach session/adjustment settings.

Limits

Returns the ticker unchanged; no session/adjustment transform is applied.

Not for

Not for changing the bar type (renko/heikin-ashi, etc.) — those transforms raise an error here.

CODESCRIPT
label(close, tickerModify(syminfo.tickerid))

Returns the chart symbol's ticker as-is.

tickerNew(prefix, symbol)

Combined 'PREFIX:SYMBOL' ticker id.

Returns a full ticker string in "PREFIX:SYMBOL" form (just the symbol if the prefix is empty).

prefix
string
Exchange prefix (the part of the ticker before ':').
symbol
string
Symbol code (e.g. "BTCUSDT").

Note

Used to build a full ticker for security or sym. The result is a string, not a series.

When to use

To assemble a fully-qualified ticker from an exchange prefix + symbol to pass to security/sym.

Limits

It only concatenates text (prefix + ":" + symbol); it does not fetch data by itself. If the prefix is empty the result is just the symbol.

CODESCRIPT
label(close, tickerNew("BINANCE", "BTCUSDT"))

Builds the full ticker; use it in security.

CodeScript operates on standard (time) bars; Point & Figure bars are not produced. Instead of silently returning wrong (standard-bar) results, it raises a clear error.

CodeScript operates on standard (time) bars; Renko bars are not produced. Instead of silently returning wrong (standard-bar) results, it raises a clear error.

tickerStandard(symbol)

Standard ticker id. Example result: BTCUSDT

Returns the ticker UNCHANGED (identity).

symbol
string
Ticker string.

Note

Intended to convert a symbol to a standard (untransformed) ticker; since CodeScript works on standard bars, this transform leaves the symbol as-is (a placeholder).

When to use

To explicitly state that a symbol should use the standard bar type (behavior is standard already).

Limits

Returns the ticker unchanged; it has no effect since CodeScript already produces standard bars.

CODESCRIPT
label(close, tickerStandard(syminfo.tickerid))

Returns the chart symbol's ticker as-is.

trade(series)

Instrument the backtest computes P&L on (instead of close). Pair/spread mode.

Does not return a value; the backtest's profit/loss is computed over this series.

series
series
The instrument whose profit/loss the backtest computes (e.g. a ratio/spread). The named argument can also be given as `price` (equivalent).

Note

For simulating trades on a ratio/spread instead of close (pair mode). If not called, the backtest uses close.

When to use

When signals are generated on a pair/ratio and P&L should be computed on that same pair (market-neutral backtest).

Limits

It may be called only once in the same code (a second call raises). The argument must be a series.

Not for

It draws nothing; use plot for visual output. It is not needed when you trade the chart symbol itself (close is the default instrument).

CODESCRIPT
x = sym("LTCUSDT")
ratio = close / x.close
trade(ratio)
enterLong(crossover(ema(ratio,5), ema(ratio,13)))
enterShort(crossunder(ema(ratio,5), ema(ratio,13)))

Trades EMA crossovers on the ratio; P&L is computed on the ratio instead of close.

securityLast(symbol, timeframe, source, ignore_invalid_symbol?)

The LAST known value of another symbol/timeframe — a single number. The value is the SAME on every bar, so it is meant for panels and labels that show one figure. Do not use it in a calculation that looks back: that would leak the future — use security() there.

The last known value (a single number). Identical on every bar.

symbol
Symbol to read, e.g. "BTCUSDT". Leave "" for the chart's own symbol.
timeframe
Which timeframe, e.g. "1D" or "60". Empty uses the chart's timeframe.
source
The series or field to read: close, high, low, volume.
ignore_invalid_symbol
bool
Optional, given by name ONLY (not in order). If true, an invalid/unloaded symbol returns na (empty) instead of raising an error. Default false (raises an error).

Note

security() aligns the secondary series to EVERY chart bar and pulls the whole range. For a single figure in a panel this form pulls far less: measured on a 3,270-bar chart, 4,200 bars sufficed instead of ~1,360,000, with identical output.

CODESCRIPT
plot(securityLast(syminfo.tickerid, "", close))

Fetches and plots the last close from another timeframe, giving the completed value rather than the bar still forming.