Skip to main content

ta.stdev#

Sample standard deviation of values over the trailing length window ending at i.

SYNTAX#

function ta.stdev(values: (number | null)[], length: number, i: number): number | null

ARGUMENTS#

  • values ((number | null)[]) — Series of input values, oldest-first.
  • length (number) — Window length. Must be > 1 (a one-bar window has zero degrees of freedom).
  • i (number) — Current bar index.

RETURNS#

number | null — sample standard deviation over the window.

REMARKS#

Pair with ta.sma on the same series and length to construct Bollinger Bands: upper = sma + 2 * stdev, lower = sma - 2 * stdev.

EXAMPLE#

const closes = [];
function onBar(i, ctx) {
  closes[i] = candles[i].close;
  const ma  = ta.sma(closes, 20, i);
  const sd  = ta.stdev(closes, 20, i);
  if (ma !== null && sd !== null) {
    plot('mid',   ma,            { color: '#fbbf24' });
    plot('upper', ma + 2 * sd,   { color: '#26a69a' });
    plot('lower', ma - 2 * sd,   { color: '#ef5350' });
  }
}

SEE ALSO#

ta.sma, ta.atr