Skip to content

Built-in Functions

All functions are called via this inside calculate(). Functions that accept a source parameter take either a string series name ('close', 'hl2', etc.) or a direct numeric value.

Moving Averages

FunctionSignatureDescription
smathis.sma(source, period)Simple Moving Average
emathis.ema(source, period)Exponential Moving Average
wmathis.wma(source, period)Weighted Moving Average
rmathis.rma(source, period)Running Moving Average (Wilder's smoothing)
smmathis.smma(source, period)Smoothed Moving Average (alias for rma)
demathis.dema(source, period)Double EMA: 2 * EMA - EMA(EMA)
temathis.tema(source, period)Triple EMA: 3*EMA - 3*EMA(EMA) + EMA(EMA(EMA))
hmathis.hma(source, period)Hull MA: WMA(2*WMA(n/2) - WMA(n), sqrt(n)) — minimal lag
vwmathis.vwma(source, period)Volume-Weighted MA
linregthis.linreg(source, period)Linear Regression end value

Example: Comparing Moving Averages

typescript
const smaVal = this.sma('close', 20);    // Smoothest, most lag
const emaVal = this.ema('close', 20);    // Less lag than SMA
const hmaVal = this.hma('close', 20);    // Least lag, may overshoot
const vwmaVal = this.vwma('close', 20);  // Weighted by volume

Compound Indicators

These return objects with multiple values. Destructure or access properties directly.

macd(source, fast, slow, signal)

Returns { macd: number, signal: number, histogram: number }

typescript
const m = this.macd('close', 12, 26, 9);
plot('MACD', m.macd, { color: color.blue, width: 2 });
plot('Signal', m.signal, { color: color.orange });
plotHistogram('Histogram', m.histogram, {
  color: m.histogram >= 0 ? color.green : color.red
});

bbands(source, period, multiplier)

Returns { upper: number, middle: number, lower: number }

typescript
const bb = this.bbands('close', 20, 2);
plot('Upper', bb.upper, { color: color.blue, overlay: true });
plot('Middle', bb.middle, { color: color.gray, overlay: true });
plot('Lower', bb.lower, { color: color.blue, overlay: true });

stoch(kPeriod, kSmooth, dSmooth)

Returns { k: number, d: number } (0-100 range)

typescript
const s = this.stoch(14, 3, 3);
plot('K', s.k, { color: color.blue, width: 2 });
plot('D', s.d, { color: color.orange });
hline('OB', 80, { color: color.red, style: 'dashed' });
hline('OS', 20, { color: color.green, style: 'dashed' });

supertrend(period, factor)

Returns { value: number, direction: number } where direction is 1 (uptrend) or -1 (downtrend).

typescript
const st = this.supertrend(10, 3);
plotColor('SuperTrend', st.value,
  [color.green, color.red],
  st.direction === 1 ? 0 : 1,
  { width: 2 }
);

Single-Value Indicators

FunctionSignatureReturnsDescription
rsithis.rsi(source, period)0-100Relative Strength Index
atrthis.atr(period)numberAverage True Range (uses H/L/C internally)
ccithis.cci(source, period)numberCommodity Channel Index (use 'hlc3' for classic)
adxthis.adx(period)0-100Average Directional Index (trend strength)
mfithis.mfi(period)0-100Money Flow Index (volume-weighted RSI)
obvthis.obv()numberOn Balance Volume (cumulative)
stdevthis.stdev(source, period)numberStandard Deviation

Range & Volatility

FunctionSignatureReturnsDescription
highestthis.highest(source, period)numberHighest value over N bars
lowestthis.lowest(source, period)numberLowest value over N bars
sumthis.sum(source, period)numberSum of values over N bars
trueRangethis.trueRange()numberTrue Range: `max(H-L,
changethis.change(source, length?)numberCurrent value minus value N bars ago (default: 1)
rocthis.roc(source, period)numberRate of Change %: (curr - prev) / prev * 100
percentRankthis.percentRank(source, period)0-100Percentile rank over lookback period

Utility Functions

Cross Detection

Important

crossOver and crossUnder require 4 arguments: current AND previous values for both series.

typescript
const fast = this.ema('close', 9);
const slow = this.ema('close', 21);
plot('Fast', fast); plot('Slow', slow);

// Use getPlot to get previous bar's values
const prevFast = this.getPlot('Fast', 1);
const prevSlow = this.getPlot('Slow', 1);

if (this.crossOver(fast, slow, prevFast, prevSlow)) {
  this.log('Bullish crossover!');
}

Condition Helpers

FunctionSignatureReturnsDescription
barsSincethis.barsSince(condition)numberBars since condition was true (-1 if never)
valuewhenthis.valuewhen(condition, source, occurrence?)numberValue of source when condition was last true
risingthis.rising(source, length)booleanTrue if source increased for N consecutive bars
fallingthis.falling(source, length)booleanTrue if source decreased for N consecutive bars
typescript
// How many bars since RSI crossed above 70?
const rsiVal = this.rsi('close', 14);
const barsSinceOB = this.barsSince(rsiVal > 70);

// What was the close when RSI last crossed 70?
const closeAtOB = this.valuewhen(rsiVal > 70, 'close');

// Is price rising for 3 consecutive bars?
const isRising = this.rising('close', 3);

Math Functions

FunctionDescription
this.abs(value)Absolute value
this.max(a, b, ...)Maximum of values
this.min(a, b, ...)Minimum of values
this.round(value, decimals?)Round to N decimal places

Source Parameter

Functions that take a source parameter accept two forms:

String series names (from input.source() or directly):

  • 'open', 'high', 'low', 'close', 'volume'
  • 'hl2' = (high + low) / 2
  • 'hlc3' = (high + low + close) / 3
  • 'ohlc4' = (open + high + low + close) / 4
  • Plot names (reference your own plotted values)
  • State keys (reference your own stored state)

Direct numeric values: this.close, this.high, or any computed number.

Both forms work correctly. Use input.source() when you want the user to choose the source in settings.

ChartLabs Documentation