MMTMMT Docs
Built-in catalog

ta reference

Technical-analysis indicators for rolling, recursive, offset, and OHLCV kinds; each call site keeps its own independent state.

Technical-analysis indicators for rolling, recursive, offset, and OHLCV kinds; each call site keeps its own independent state.

ta.sma

ta.sma(source: float?, len: int) -> float?

Returns the simple moving average of the last len non-null samples.

source supplies the next sample. len chooses how many non-null samples to include; values outside its allowed range stop the script with an error.

Returns null until len non-null samples are available. A null source returns null and leaves the sample history unchanged.

Contracts

  • len: Accepted range: 1 to MAX_ARRAY_LEN. Invalid values are rejected.

Example

script "SMA"
data chart = subscribe(data.ohlcv)
plot ma = plot.line(title: "SMA")

on chart.update {
  ma.plot(ta.sma(chart.close, 20))
}

ta.ema

ta.ema(source: float?, len: int) -> float?

Returns the exponential moving average.

source supplies the next sample. len sets the smoothing weight to 2 / (len + 1); values outside its allowed range stop the script with an error.

The first non-null sample starts the average and is returned immediately. A null source returns null and leaves the average unchanged.

Contracts

  • len: Accepted range: 1 to MAX_ARRAY_LEN. Invalid values are rejected.

Example

script "EMA"
data chart = subscribe(data.ohlcv)
plot ma = plot.line(title: "EMA")

on chart.update {
  ma.plot(ta.ema(chart.close, 20))
}

ta.rma

ta.rma(source: float?, len: int) -> float?

Returns the Wilder's smoothed moving average.

source supplies the next sample. len sets the smoothing weight to 1 / len; values outside its allowed range stop the script with an error.

The first non-null sample starts the average and is returned immediately. A null source returns null and leaves the average unchanged.

Contracts

  • len: Accepted range: 1 to MAX_ARRAY_LEN. Invalid values are rejected.

Example

script "RMA"
data chart = subscribe(data.ohlcv)
plot smooth = plot.line(title: "RMA")

on chart.update {
  smooth.plot(ta.rma(chart.close, 14))
}

ta.sum

ta.sum(source: float?, len: int) -> float?

Returns the sum of the last len non-null samples.

source supplies the next sample. len chooses how many non-null samples to include; values outside its allowed range stop the script with an error. Null samples do not count as zero.

Returns null until len non-null samples are available. A null source returns null and leaves the sample history unchanged.

Contracts

  • len: Accepted range: 1 to MAX_ARRAY_LEN. Invalid values are rejected.

Example

script "Window Sum"
data chart = subscribe(data.ohlcv)
plot vol = plot.line(title: "Volume Sum")

on chart.update {
  vol.plot(ta.sum(chart.volume, 20))
}

ta.stdev

ta.stdev(source: float?, len: int) -> float?

Returns the population standard deviation of the last len non-null samples.

source supplies the next sample. len chooses how many non-null samples to include; values outside its allowed range stop the script with an error. The calculation divides by len.

Returns null until len non-null samples are available. A null source returns null and leaves the sample history unchanged.

Contracts

  • len: Accepted range: 1 to MAX_ARRAY_LEN. Invalid values are rejected.

Example

script "Bands"
data chart = subscribe(data.ohlcv)
plot upper = plot.line(title: "Upper")

on chart.update {
  upper.plot(ta.sma(chart.close, 20) + 2.0 * ta.stdev(chart.close, 20))
}

ta.variance

ta.variance(source: float?, len: int) -> float?

Returns the population variance of the last len non-null samples.

source supplies the next sample. len chooses how many non-null samples to include; values outside its allowed range stop the script with an error. The calculation divides by len.

Returns null until len non-null samples are available. A null source returns null and leaves the sample history unchanged.

Contracts

  • len: Accepted range: 1 to MAX_ARRAY_LEN. Invalid values are rejected.

Example

script "Variance"
data chart = subscribe(data.ohlcv)
plot v = plot.line(title: "Variance")

on chart.update {
  v.plot(ta.variance(chart.close, 20))
}

ta.wma

ta.wma(source: float?, len: int) -> float?

Returns the linearly weighted moving average of the last len non-null samples.

source supplies the next sample. len chooses how many non-null samples to include; values outside its allowed range stop the script with an error. Newer samples receive larger weights.

Returns null until len non-null samples are available. A null source returns null and leaves the sample history unchanged.

Contracts

  • len: Accepted range: 1 to MAX_ARRAY_LEN. Invalid values are rejected.

Example

script "WMA"
data chart = subscribe(data.ohlcv)
plot ma = plot.line(title: "WMA")

on chart.update {
  ma.plot(ta.wma(chart.close, 20))
}

ta.highest

ta.highest(source: float?, len: int) -> float?

Returns the highest value of the last len non-null samples.

source supplies the next sample. len chooses how many non-null samples to include; values outside its allowed range stop the script with an error. Infinities compare normally.

Returns null until len non-null samples are available. A null source returns null and leaves the sample history unchanged.

Contracts

  • len: Accepted range: 1 to MAX_ARRAY_LEN. Invalid values are rejected.

Example

script "Highest"
data chart = subscribe(data.ohlcv)
plot hi = plot.line(title: "High")

on chart.update {
  hi.plot(ta.highest(chart.high, 50))
}

ta.lowest

ta.lowest(source: float?, len: int) -> float?

Returns the lowest value of the last len non-null samples.

source supplies the next sample. len chooses how many non-null samples to include; values outside its allowed range stop the script with an error. Infinities compare normally.

Returns null until len non-null samples are available. A null source returns null and leaves the sample history unchanged.

Contracts

  • len: Accepted range: 1 to MAX_ARRAY_LEN. Invalid values are rejected.

Example

script "Lowest"
data chart = subscribe(data.ohlcv)
plot lo = plot.line(title: "Low")

on chart.update {
  lo.plot(ta.lowest(chart.low, 50))
}

ta.rsi

ta.rsi(source: float?, len: int) -> float?

Returns the relative strength index over valid source changes.

source supplies one sample each time the calculation runs. len sets the Wilder smoothing window; values outside its allowed range stop the script with an error. The result stays null until len + 1 valid samples provide enough changes. Null source samples output null and leave the average gain and loss unchanged.

Contracts

  • len: Accepted range: 1 to MAX_ARRAY_LEN. Invalid values are rejected.

Example

script "RSI"
data chart = subscribe(data.ohlcv)
plot r = plot.line(title: "RSI")

on chart.update {
  r.plot(ta.rsi(chart.close, 14))
}

ta.barsSince

ta.barsSince(condition: bool) -> int?

Returns the number of bars since condition was last true.

condition is checked each time the calculation runs. A true bar returns 0, and later false bars increment the count. The result is null until the first true bar has been observed.

Example

script "Bars Since"
data chart = subscribe(data.ohlcv)
plot recent = plot.line(title: "Recent")

on chart.update {
  recent.plot(float(ta.barsSince(chart.close > chart.open) ?? 0))
}

ta.change

ta.change(source: float?, len: int = 1) -> float?

Returns the change from source to a prior valid sample.

source supplies one sample each time the calculation runs. len sets how many valid samples to look back, defaults to 1, and values outside its allowed range stop the script with an error. Null source samples output null and do not advance the lookback. The result stays null until the referenced prior valid sample exists.

Contracts

  • len: Accepted range: 1 to MAX_ARRAY_LEN. Invalid values are rejected.

Example

script "Change"
data chart = subscribe(data.ohlcv)
plot mom = plot.line(title: "Momentum")

on chart.update {
  mom.plot(ta.change(chart.close, 10))
}

ta.roc

ta.roc(source: float?, len: int) -> float?

Returns the rate of change from a prior valid sample.

source supplies one sample each time the calculation runs. len sets how many valid samples to look back; values outside its allowed range stop the script with an error. Null source samples output null and do not advance the lookback. The result stays null until the referenced prior sample exists, and is null when that prior value is zero.

Contracts

  • len: Accepted range: 1 to MAX_ARRAY_LEN. Invalid values are rejected.

Example

script "ROC"
data chart = subscribe(data.ohlcv)
plot roc = plot.line(title: "ROC")

on chart.update {
  roc.plot(ta.roc(chart.close, 12))
}

ta.dev

ta.dev(source: float?, len: int) -> float?

Returns the mean absolute deviation of the last len non-null samples.

source supplies the next sample. len chooses how many non-null samples to include; values outside its allowed range stop the script with an error. This is the average absolute distance from the mean.

Returns null until len non-null samples are available. A null source returns null and leaves the sample history unchanged.

Contracts

  • len: Accepted range: 1 to MAX_ARRAY_LEN. Invalid values are rejected.

Example

script "Deviation"
data chart = subscribe(data.ohlcv)
plot dev = plot.line(title: "Deviation")

on chart.update {
  dev.plot(ta.dev(chart.close, 20))
}

ta.falling

ta.falling(source: float?, len: int) -> bool

Reports whether source is below every earlier sample in the window.

source supplies the next sample. len sets the number of samples, including the current one; values outside its allowed range stop the script with an error.

Returns true when the current sample is strictly less than each of the len - 1 earlier non-null samples. Returns false until enough samples are available or when source is null. Null samples do not enter the history.

Contracts

  • len: Accepted range: 1 to MAX_ARRAY_LEN. Invalid values are rejected.

Example

script "Falling"
data chart = subscribe(data.ohlcv)

on chart.update {
  let weak = ta.falling(chart.close, 3)
}

ta.rising

ta.rising(source: float?, len: int) -> bool

Reports whether source is above every earlier sample in the window.

source supplies the next sample. len sets the number of samples, including the current one; values outside its allowed range stop the script with an error.

Returns true when the current sample is strictly greater than each of the len - 1 earlier non-null samples. Returns false until enough samples are available or when source is null. Null samples do not enter the history.

Contracts

  • len: Accepted range: 1 to MAX_ARRAY_LEN. Invalid values are rejected.

Example

script "Rising"
data chart = subscribe(data.ohlcv)

on chart.update {
  let strong = ta.rising(chart.close, 3)
}

ta.max

ta.max(source: float?, reset: bool = false) -> float?

Returns the running maximum of valid source samples.

source supplies one sample each time the calculation runs. The first valid sample starts the running maximum. Null source samples output null and leave the running maximum unchanged. reset true forgets the running maximum before this bar's sample is compared, even when that sample is null.

Example

script "Running Max"
data chart = subscribe(data.ohlcv)
plot m = plot.line(title: "Max")

on chart.update {
  m.plot(ta.max(chart.high))
}

ta.min

ta.min(source: float?, reset: bool = false) -> float?

Returns the running minimum of valid source samples.

source supplies one sample each time the calculation runs. The first valid sample starts the running minimum. Null source samples output null and leave the running minimum unchanged. reset true forgets the running minimum before this bar's sample is compared, even when that sample is null.

Example

script "Running Min"
data chart = subscribe(data.ohlcv)
plot m = plot.line(title: "Min")

on chart.update {
  m.plot(ta.min(chart.low))
}

ta.median

ta.median(source: float?, len: int) -> float?

Returns the median of the last len non-null samples.

source supplies the next sample. len chooses how many non-null samples to include; values outside its allowed range stop the script with an error.

Returns null until len non-null samples are available. A null source returns null and leaves the sample history unchanged.

Contracts

  • len: Accepted range: 1 to MAX_ARRAY_LEN. Invalid values are rejected.

Example

script "Median"
data chart = subscribe(data.ohlcv)
plot med = plot.line(title: "Median")

on chart.update {
  med.plot(ta.median(chart.close, 20))
}

ta.mode

ta.mode(source: float?, len: int) -> float?

Returns the most frequent value of the last len non-null samples.

source supplies the next sample. len chooses how many non-null samples to include; values outside its allowed range stop the script with an error. If several values tie, returns the smallest of them.

Returns null until len non-null samples are available. A null source returns null and leaves the sample history unchanged.

Contracts

  • len: Accepted range: 1 to MAX_ARRAY_LEN. Invalid values are rejected.

Example

script "Mode"
data chart = subscribe(data.ohlcv)
plot mode = plot.line(title: "Mode")

on chart.update {
  mode.plot(ta.mode(chart.close, 20))
}

ta.linreg

ta.linreg(source: float?, len: int, offset: int = 0) -> float?

Returns a linear-regression value over the last len valid samples.

source supplies one sample each time the calculation runs. len sets the valid-sample window; values outside its allowed range stop the script with an error. offset selects where the fitted line is evaluated and defaults to 0. Null source samples output null and do not enter the window. The result stays null until len valid samples are available.

Contracts

  • len: Accepted range: 1 to MAX_ARRAY_LEN. Invalid values are rejected.

Example

script "Linear Regression"
data chart = subscribe(data.ohlcv)
plot lr = plot.line(title: "LR")

on chart.update {
  lr.plot(ta.linreg(chart.close, 50, 0))
}

ta.stoch

ta.stoch(source: float?, high: float?, low: float?, len: int) -> float?

Returns the stochastic position of source within the recent range.

source, high, and low supply the current values. len sets how many non-null samples to include; values outside its allowed range stop the script with an error.

Returns the percentage position of source between the lowest low and highest high in that window. If any input is null, returns null and does not add a sample.

Contracts

  • len: Accepted range: 1 to MAX_ARRAY_LEN. Invalid values are rejected.

Example

script "Stochastic"
data chart = subscribe(data.ohlcv)
plot k = plot.line(title: "K")

on chart.update {
  k.plot(ta.stoch(chart.close, chart.high, chart.low, 14))
}

ta.alma

ta.alma(source: float?, len: int, offset: float, sigma: float) -> float?

Returns the Arnaud Legoux moving average of the last len valid samples.

source supplies one sample each time the calculation runs. len sets the valid-sample window; values outside its allowed range stop the script with an error. Sample i (0 = oldest, len - 1 = newest) carries Gaussian weight exp(-(i - m)^2 / (2 * s^2)) with m = offset * (len - 1) and s = len / sigma; the result is the weight-normalized sum (weights divided by their total). Null source samples output null and do not enter the window. The result stays null until len valid samples are available.

Contracts

  • len: Accepted range: 1 to MAX_ARRAY_LEN. Invalid values are rejected.

Example

script "ALMA"
data chart = subscribe(data.ohlcv)
plot ma = plot.line(title: "ALMA")

on chart.update {
  ma.plot(ta.alma(chart.close, 20, 0.85, 6.0))
}

ta.swma

ta.swma(source: float?) -> float?

Returns a four-sample symmetrically weighted moving average.

source supplies one sample each time the calculation runs. The fixed window uses four valid samples with symmetric weights 1, 2, 2, 1 (oldest to newest) divided by 6. Null source samples output null and do not enter the window. The result stays null until four valid samples are available.

Example

script "SWMA"
data chart = subscribe(data.ohlcv)
plot ma = plot.line(title: "SWMA")

on chart.update {
  ma.plot(ta.swma(chart.close))
}

ta.hma

ta.hma(source: float?, len: int) -> float?

Returns the Hull moving average of source.

source supplies one sample each time the calculation runs. len sets the weighted-average window; values outside its allowed range stop the script with an error. The calculation combines weighted moving averages and a final sqrt(len) smoothing window. The result stays null until len + sqrt(len) - 1 valid samples are available; null source samples output null and do not advance the windows.

Contracts

  • len: Accepted range: 1 to MAX_ARRAY_LEN. Invalid values are rejected.

Example

script "HMA"
data chart = subscribe(data.ohlcv)
plot ma = plot.line(title: "HMA")

on chart.update {
  ma.plot(ta.hma(chart.close, 20))
}

ta.pivotHigh

ta.pivotHigh(source: float?, left: int, right: int) -> float?

Returns a pivot high once later samples confirm it.

source supplies the next sample. left sets how many earlier samples to check, and right sets how many later samples must confirm the pivot. Values outside either allowed range stop the script with an error.

A sample qualifies when none of the other left + right samples is higher. Equal values do not disqualify it. The pivot is returned right non-null samples after it occurs, on the confirmation bar. Otherwise the result is null.

A null source returns null and does not enter the sample history.

Contracts

  • left: Accepted range: 1 to MAX_ARRAY_LEN. Invalid values are rejected.
  • right: Accepted range: 1 to MAX_ARRAY_LEN. Invalid values are rejected.

Example

script "Pivot High"
data chart = subscribe(data.ohlcv)
plot ph = plot.line(title: "Pivot High")

on chart.update {
  ph.plot(ta.pivotHigh(chart.high, 3, 3))
}

ta.pivotLow

ta.pivotLow(source: float?, left: int, right: int) -> float?

Returns a pivot low once later samples confirm it.

source supplies the next sample. left sets how many earlier samples to check, and right sets how many later samples must confirm the pivot. Values outside either allowed range stop the script with an error.

A sample qualifies when none of the other left + right samples is lower. Equal values do not disqualify it. The pivot is returned right non-null samples after it occurs, on the confirmation bar. Otherwise the result is null.

A null source returns null and does not enter the sample history.

Contracts

  • left: Accepted range: 1 to MAX_ARRAY_LEN. Invalid values are rejected.
  • right: Accepted range: 1 to MAX_ARRAY_LEN. Invalid values are rejected.

Example

script "Pivot Low"
data chart = subscribe(data.ohlcv)
plot pl = plot.line(title: "Pivot Low")

on chart.update {
  pl.plot(ta.pivotLow(chart.low, 3, 3))
}

ta.cum

ta.cum(source: float?, reset: bool = false) -> float?

Returns the cumulative sum of valid source samples.

source supplies one sample each time the calculation runs. The first valid sample starts the running total. Null source samples output null and leave the running total unchanged.

Example

script "Cumulative"
data chart = subscribe(data.ohlcv)
plot total = plot.line(title: "Total")

on chart.update {
  total.plot(ta.cum(chart.volume))
}

ta.cross

ta.cross(left: float?, right: float?) -> bool

Returns true when left and right cross in either direction.

left and right are compared each time the calculation runs. Returns false during warmup or when either operand is null. Null operands do not update the stored comparison pair.

Example

script "Cross"
data chart = subscribe(data.ohlcv)

on chart.update {
  let hit = ta.cross(ta.ema(chart.close, 12), ta.ema(chart.close, 26))
}

ta.crossover

ta.crossover(left: float?, right: float?) -> bool

Returns true when left crosses above right.

left and right are compared each time the calculation runs. Returns true when left > right now and left <= right on the previous valid comparison pair. Returns false during warmup or when either operand is null. Null operands do not update the stored comparison pair.

Example

script "Crossover"
data chart = subscribe(data.ohlcv)

on chart.update {
  let bull = ta.crossover(ta.ema(chart.close, 12), ta.ema(chart.close, 26))
}

ta.crossunder

ta.crossunder(left: float?, right: float?) -> bool

Returns true when left crosses below right.

left and right are compared each time the calculation runs. Returns true when left < right now and left >= right on the previous valid comparison pair. Returns false during warmup or when either operand is null. Null operands do not update the stored comparison pair.

Example

script "Crossunder"
data chart = subscribe(data.ohlcv)

on chart.update {
  let bear = ta.crossunder(ta.ema(chart.close, 12), ta.ema(chart.close, 26))
}

ta.mfi

ta.mfi(len: int) -> float?

Returns the money flow index over valid OHLCV samples.

len sets the valid-sample window; values outside its allowed range stop the script with an error. The subscription supplies high, low, close, and volume. Null input fields make the bar output null and do not update the window. The result stays null until len + 1 valid samples are available.

Contracts

  • len: Accepted range: 1 to MAX_ARRAY_LEN. Invalid values are rejected.

Example

script "MFI"
data chart = subscribe(data.ohlcv)
plot mfi = plot.line(title: "MFI")

on chart.update {
  mfi.plot(chart.mfi(14))
}

ta.vwap

ta.vwap(reset: bool = false, source: float? = null) -> float?

Returns the volume-weighted average price since the last reset.

The subscription supplies price and volume.

  • reset starts a new average before adding the current sample, even when that sample is null. The first valid sample starts the new average.
  • source optionally replaces the close price. Omitting it uses close; a supplied expression that returns null counts as a missing sample.

A null input returns null and leaves the totals unchanged. The result is also null while accumulated volume is zero. A non-null reset sample with volume returns its own price. During a forming candle, resets are temporary and are recalculated on the next update.

Example

script "Daily VWAP"
data chart = subscribe(data.ohlcv)
plot v = plot.line(title: "VWAP")

on chart.update {
  let ts = chart.time ?? time.fromUnixSeconds(0)
  let newDay = time.isNewPeriod(ts, chart.time[1], 1D)
  v.plot(chart.vwap(reset: newDay))
}

ta.vwapDev

ta.vwapDev(reset: bool = false, source: float? = null) -> float?

Returns the volume-weighted standard deviation around VWAP.

The subscription supplies price and volume. Use vwap + mult * vwapDev and vwap - mult * vwapDev for upper and lower bands.

  • reset starts new totals before adding the current sample, even when it is null.
  • source optionally replaces the close price. Omitting it uses close; a supplied expression that returns null counts as a missing sample.

This is a population standard deviation over the bars since the last reset. The first valid sample with volume returns 0. A null input returns null and leaves the totals unchanged. The result is also null while accumulated volume is zero.

Use the same reset and source as the matching vwap call so both cover the same bars.

Example

script "VWAP Bands"
data chart = subscribe(data.ohlcv)
plot upper = plot.line(title: "Upper")
plot lower = plot.line(title: "Lower")

on chart.update {
  let ts = chart.time ?? time.fromUnixSeconds(0)
  let newDay = time.isNewPeriod(ts, chart.time[1], 1D)
  let v = chart.vwap(reset: newDay)
  let d = chart.vwapDev(reset: newDay)
  upper.plot(v + d)
  lower.plot(v - d)
}

ta.accDist

ta.accDist() -> float?

Returns the accumulation/distribution line for the subscription.

Uses the subscription's high, low, close, and volume. The first non-null sample starts the cumulative line. A null input returns null and leaves the running total unchanged.

Example

script "Accumulation Distribution"
data chart = subscribe(data.ohlcv)
plot ad = plot.line(title: "AD")

on chart.update {
  ad.plot(chart.accDist())
}

ta.vwma

ta.vwma(len: int) -> float?

Returns a volume-weighted moving average over valid OHLCV samples.

len sets the valid-sample window; values outside its allowed range stop the script with an error. The subscription supplies close and volume. Null input fields output null and do not enter the window. The result stays null until len valid samples are available.

Contracts

  • len: Accepted range: 1 to MAX_ARRAY_LEN. Invalid values are rejected.

Example

script "VWMA"
data chart = subscribe(data.ohlcv)
plot ma = plot.line(title: "VWMA")

on chart.update {
  ma.plot(chart.vwma(20))
}

ta.atr

ta.atr(len: int) -> float?

Returns the average true range over valid OHLCV samples.

len sets Wilder smoothing; values outside its allowed range stop the script with an error. Uses the subscription's high, low, and close. True range uses the current high and low and the previous close.

The first non-null sample returns a result immediately. A null input returns null and leaves the average unchanged.

Contracts

  • len: Accepted range: 1 to MAX_ARRAY_LEN. Invalid values are rejected.

Example

script "ATR"
data chart = subscribe(data.ohlcv)
plot stop = plot.line(title: "Stop")

on chart.update {
  stop.plot(chart.close - 2.0 * chart.atr(14))
}

ta.tr

ta.tr() -> float?

Returns true range for the subscription's current bar.

Uses the current high and low and the previous close. The first non-null sample returns a result immediately. A null input returns null and leaves the previous close unchanged.

Example

script "True Range"
data chart = subscribe(data.ohlcv)
plot tr = plot.line(title: "TR")

on chart.update {
  tr.plot(chart.tr())
}

On this page