Black Friday 30% off Basic and ProBlack Friday −30%Ends in––d––h––m––sSee the deal

ta

Functions on series: averages, oscillators, volatility, volume, order flow, windows, events and candlestick patterns.

  • Each function returns lists as long as the input; multiple outputs are a tuple, like middle, upper, lower = ta.bb(…).
  • length is an integer greater than 0. Windows give nan until they are full, or if they contain a nan.
  • In crossover, crossunder and cross the second argument can be a number, like ta.crossover(rsi, 70).

Averages

ta.sma(src, length)

Simple average of the last length values.

nan until the window is full, or if it contains a nan.

ta.ema(src, length)

Exponential moving average.

Each value weighs alpha = 2 / (length + 1). It starts from the first value. A nan gives nan, and the average restarts from the next value.

ta.rma(src, length)

Wilder’s moving average.

It starts from the simple average of the first length values, then each value weighs 1 / length. A nan gives nan, and the average starts over with the next length values.

ta.wma(src, length)

Weighted average: the most recent value weighs length, the previous one length - 1, and so on.

nan until the window is full, or if it contains a nan.

ta.vwma(src, volume, length)

Volume-weighted average: sma(src * volume) / sma(volume). nan if volume is zero.

ta.hma(src, length)

Hull moving average: wma(2 * wma(src, length // 2) - wma(src, length), floor(sqrt(length))).

length must be at least 2, because the first average uses length // 2.

ta.alma(src, length, offset=0.85, sigma=6)

Arnaud Legoux moving average: bell-shaped weights over the window.

offset moves the center of the bell, from 0 (the oldest value) to 1 (the most recent); sigma narrows it.

ta.swma(src)

Symmetric average of the last 4 values, with weights 1/6, 2/6, 2/6, 1/6.

ta.linreg(src, length, offset=0)

Linear regression of the last length values.

The least squares line, read offset candles before the latest: with offset 0 it’s its value on the current candle.

Oscillators

ta.rsi(src, length)

Relative strength index, from 0 to 100.

Wilder averages of gains and losses; with no losses it’s 100.

ta.macd(src, fast_length, slow_length, signal_length)

(macd, signal, hist): difference between two emas, its ema and the difference between the two.

ta.stoch(src, high, low, length)

Stochastic: where src sits between the low and the high of the last length, from 0 to 100.

With the low equal to the high the ratio doesn’t exist, and the previous value remains.

ta.cci(src, length)

Commodity channel index: (src - sma) / (0.015 * mean deviation).

With zero deviation, that is a flat window, the previous value remains.

ta.mfi(src, volume, length)

Money flow index, from 0 to 100: an rsi that weighs each price by its volume.

On the first candle the change is nan, and a comparison with nan is false, so the flow ends up in both sums. With no outflow it’s 100.

ta.cmo(src, length)

Chande momentum oscillator, from -100 to 100: gains minus losses, over their sum.

The first change is nan and ends up only among the losses, so the first value arrives at candle length. A flat window gives nan.

ta.roc(src, length)

Rate of change: percentage change from length candles before. nan if that value is 0.

ta.tsi(src, short_length, long_length)

True strength index, from -1 to 1.

Changes smoothed twice with ema (first long_length, then short_length), divided by the absolute changes smoothed the same way. The TSI indicator multiplies it by 100.

ta.wpr(high, low, close, length)

Williams %R, from -100 to 0: distance of the close from the high of the last length.

ta.uo(high, low, close, fast_length, middle_length, slow_length)

Larry Williams’ Ultimate Oscillator, from 0 to 100.

Buying pressure, that is the close minus the lower of the low and the previous close, is summed over three windows and divided by the sum of the true ranges; the three averages weigh 4, 2 and 1. nan on the first candle and where a window has zero range.

ta.dmi(high, low, close, di_length, adx_length)

(plus_di, minus_di, adx): directional movement, like the DMI indicator.

The DIs are the Wilder averages of the up and down moves, divided by that of the true range; a DI that can’t be calculated keeps the previous value. The ADX is the Wilder average of |plus - minus| / (plus + minus).

Volatility

ta.tr(high, low, close)

True range: the largest of h - l, |h - previous close| and |l - previous close|.

Without a previous close, as on the first candle, it’s h - l.

ta.atr(high, low, close, length)

Average true range: the Wilder average of the true range.

ta.variance(src, length)

Population variance of the last length values (divided by length).

ta.stdev(src, length)

Population standard deviation of the last length values.

ta.dev(src, length)

Mean absolute deviation: the average of |value - sma| over the last length values.

ta.bb(src, length, mult)

Bollinger Bands (middle, upper, lower): sma and sma ± mult standard deviations.

ta.bbw(src, length, mult)

Bollinger Bands width as a percentage of the average.

(upper - lower) / middle * 100; nan if the average is zero.

ta.kc(src, high, low, close, length, mult, use_true_range=True)

Keltner Channels (middle, upper, lower): ema of src ± mult times the ema of the range.

The range is the true range, or h - l with use_true_range=False.

ta.supertrend(high, low, close, factor, atr_length)

(line, direction): Supertrend.

direction is 1 in an uptrend, with the line below the price, and -1 in a downtrend. The bands are hl2 ± factor * atr, and each one can only tighten until the close crosses it. Where the atr is nan, line and direction are nan.

ta.sar(high, low, close, start=0.02, increment=0.02, maximum=0.2)

Parabolic SAR.

It starts from the second candle: in an uptrend if the close rises. The point moves towards the trend extreme with an acceleration that grows by increment at each new extreme, up to maximum; when the price touches it, the trend reverses. If a nan makes the point nan, it starts again as from the second candle.

Volume

ta.vwap(src, volume, reset=None)

(vwap, stdev): volume-weighted average price, and its standard deviation.

The sums start from the first candle and restart where reset is True, for example with bars.new_period("D") for the session; without reset they never restart. Candles with src or volume nan don’t count. nan as long as the summed volume is zero.

ta.obv(close, volume)

On balance volume: running sum of volume, with the sign of the change.

ta.accdist(high, low, close, volume)

Accumulation/distribution: running sum of volume, weighted by the close.

The weight is the position of the close within the candle’s range: -1 at the low, 1 at the high.

ta.pvt(close, volume)

Price volume trend: running sum of volume times the relative change.

The change is that of the close over the previous one; a previous close of zero doesn’t count.

ta.signed_volume(open, close, volume)

The volume of each candle with the sign of its direction: positive if it rises, negative if it falls.

It rises if it closes above the open and falls if it closes below; on a tie the previous close decides, and if that’s equal too the direction of the candle before remains. The first candle rises. It’s the Delta volume rule, which applies it to the intrabar candles.

Order flow

ta.poc(levels)

The price with the most volume in each candle; on a tie the lowest. nan without volume.

ta.value_area(levels, percent=70)

(high, low): the value area of each candle, that is the prices that hold percent% of the volume.

From the POC, each step adds the adjacent level, above or below, with more volume (on a tie the one above), until the sum reaches percent% of the candle’s volume. nan without volume.

ta.imbalances(levels, tick, percent=300)

(buy, sell): for each candle, the lists of prices with a diagonal imbalance.

Buy at p if the buys at p are at least percent% of the sells at p - tick; sell if the sells at p are at least percent% of the buys at p + tick. The adjacent level must exist, and volume at p must be greater than zero. tick is bars.symbol.tick_size.

Missing values

ta.nz(src, value=0)

src with nans replaced by value.

ta.fixnan(src)

src with each nan replaced by the last valid value; nan before the first.

Windows

ta.cum(src, reset=None)

Running sum; restarts from zero where reset is True. nans count as 0.

ta.sum(src, length)

Sum of the last length values.

nan until the window is full, or if it contains a nan.

ta.highest(src, length)

The highest value of the last length.

ta.lowest(src, length)

The lowest value of the last length.

ta.highestbars(src, length)

How many candles ago the highest value of the last length was: 0 is the current candle.

Positive; on a tie, the most recent candle.

ta.lowestbars(src, length)

How many candles ago the lowest value of the last length was, like highestbars.

ta.median(src, length)

The median of the last length values: with an even length, the average of the two middle ones.

ta.percentile(src, length, percent)

The percent percentile (from 0 to 100) of the last length values.

In the sorted values the position is percent / 100 * length - 0.5, counting from 0, with linear interpolation between the two neighbors; outside the values the first or the last is taken. nan if the window contains a nan.

ta.percentrank(src, length)

Percentage of the previous length values that are less than or equal to the current value.

From 0 to 100; nan if the current value or one of the previous ones is nan.

ta.correlation(a, b, length)

Pearson correlation coefficient between a and b over the last length values, from -1 to 1.

If one of the two series is constant in the window the correlation doesn’t exist, and it’s 0.

ta.rci(src, length)

Rank Correlation Index, from -100 to 100.

Over the last length values it compares the rank in time (1 the most recent) with the rank in price (1 the highest): 100 if the price rose at every candle, -100 if it fell. Equal prices take the average of the ranks they occupy. nan until the window is full, or if it contains a nan.

Events

ta.shift(src, offset)

The series shifted by offset candles, to the right if positive.

Shifted to the right it loses the last offset values: the chart doesn’t draw them beyond the latest candle. Shifted to the left, each candle receives the value of a later one.

ta.change(src, length=1)

Difference between each value and the one length candles before; nan on the first length.

ta.crossover(a, b)

True where a crosses above b: now a > b, before a <= b. b can be a number.

ta.crossunder(a, b)

True where a crosses below b: now a < b, before a >= b. b can be a number.

ta.cross(a, b)

True where a and b cross, in either direction. b can be a number.

ta.rising(src, length)

True if src rose at every candle of the last length.

ta.falling(src, length)

True if src fell at every candle of the last length.

ta.barssince(cond)

Candles passed since the last one where cond was true: 0 on that same one. nan before.

ta.valuewhen(cond, src, occurrence=0)

The value of src on the candle where cond was true, occurrence times ago (0: the latest).

nan until cond has been true enough times.

ta.pivothigh(src, left, right)

Local highs: the pivot value on the candle that confirms it, nan elsewhere.

The pivot is right candles before: greater than or equal to the left values on its left and greater than the right ones on its right. The value arrives when the right following candles exist.

ta.pivotlow(src, left, right)

Local lows, like pivothigh: less than or equal on the left, less on the right.

ta.zigzag(high, low, deviation, depth)

The Zig Zag points: a list of (index, price, high), from the first.

A high is a pivot if it exceeds the depth // 2 highs before and none of the depth // 2 after exceeds it; a low the same way. Each pivot is read on the candle that confirms it, the high before the low. If it goes in the same direction as the last point, it moves it when it’s more extreme; if it goes the opposite way, it becomes a new point when it moves away by at least deviation percent, a number or a series read on the confirming candle. The others are ignored: the points alternate highs (high True) and lows.

It isn’t a series, but the points as seen from the latest candle: the last one can still move.

Candles

ta.heikinashi(open, high, low, close)

(open, high, low, close) of the Heikin Ashi candles.

The close is the average of the four prices; the open is the average of the previous Heikin Ashi open and close, and on the first candle, or after a nan, of the real open and close.

ta.candle_trend(close, kind)

(up, down): the trend that precedes candlestick patterns, as bool series.

kind "sma50": uptrend with the close above the 50-candle simple average, downtrend below. "sma50_sma200": in addition the 50 average above the 200 one in an uptrend, below in a downtrend. None: no trend, that is (None, None), which the patterns don’t check.

ta.doji(open, high, low, close)

Doji: open and close almost equal, with the body within 5% of the candle.

ta.dragonfly_doji(open, high, low, close, trend=None)

Dragonfly doji: a doji at the high, with the long lower shadow. After a downtrend.

ta.gravestone_doji(open, high, low, close, trend=None)

Gravestone doji: a doji at the low, with the long upper shadow. After an uptrend.

ta.hammer(open, high, low, close, trend=None)

Hammer: small body at the top and long lower shadow. After a downtrend.

ta.hanging_man(open, high, low, close, trend=None)

Hanging man: the hammer’s shape, but after an uptrend.

ta.inverted_hammer(open, high, low, close, trend=None)

Inverted hammer: small body at the bottom and long upper shadow. After a downtrend.

ta.shooting_star(open, high, low, close, trend=None)

Shooting star: the inverted hammer’s shape, but after an uptrend.

ta.long_lower_shadow(open, high, low, close)

Long lower shadow: at least two thirds of the candle.

ta.long_upper_shadow(open, high, low, close)

Long upper shadow: at least two thirds of the candle.

ta.marubozu_white(open, high, low, close)

White marubozu: a long white body, with no shadows.

ta.marubozu_black(open, high, low, close)

Black marubozu: a long black body, with no shadows.

ta.spinning_top_white(open, high, low, close)

White spinning top: a small white body between two shadows longer than it.

ta.spinning_top_black(open, high, low, close)

Black spinning top: a small black body between two shadows longer than it.

ta.engulfing_bullish(open, high, low, close, trend=None)

Bullish engulfing: a white body engulfs the previous black body. After a downtrend.

ta.engulfing_bearish(open, high, low, close, trend=None)

Bearish engulfing: a black body engulfs the previous white body. After an uptrend.

ta.harami_bullish(open, high, low, close, trend=None)

Bullish harami: a white body inside the previous long black body. After a downtrend.

ta.harami_bearish(open, high, low, close, trend=None)

Bearish harami: a black body inside the previous long white body. After an uptrend.

ta.harami_cross_bullish(open, high, low, close, trend=None)

Bullish harami cross: a doji inside the previous long black body. After a downtrend.

ta.harami_cross_bearish(open, high, low, close, trend=None)

Bearish harami cross: a doji inside the previous long white body. After an uptrend.

ta.doji_star_bullish(open, high, low, close, trend=None)

Bullish doji star: a doji gapping below a long black body. After a downtrend.

ta.doji_star_bearish(open, high, low, close, trend=None)

Bearish doji star: a doji gapping above a long white body. After an uptrend.

ta.piercing(open, high, low, close, trend=None)

Piercing: a white candle that opens below and climbs back past the middle of the long black body.

After a downtrend.

ta.dark_cloud_cover(open, high, low, close, trend=None)

Dark cloud cover: a black candle that opens above and falls past the middle of the long white body.

After an uptrend.

ta.on_neck(open, high, low, close, trend=None)

On-neck: a white candle that opens below the low of the previous long black candle and closes there.

After a downtrend, which continues.

ta.kicking_bullish(open, high, low, close)

Bullish kicking: a black marubozu and then a white one, gapping up.

ta.kicking_bearish(open, high, low, close)

Bearish kicking: a white marubozu and then a black one, gapping down.

ta.tweezer_bottom(open, high, low, close, trend=None)

Tweezer bottom: a black and a white candle with the same low. After a downtrend.

ta.tweezer_top(open, high, low, close, trend=None)

Tweezer top: a white and a black candle with the same high. After an uptrend.

ta.rising_window(open, high, low, close, trend=None)

Rising window: a gap up between the shadows. After an uptrend, which continues.

ta.falling_window(open, high, low, close, trend=None)

Falling window: a gap down between the shadows. After a downtrend, which continues.

ta.morning_star(open, high, low, close, trend=None)

Morning star: long black body, small star gapping below, white candle climbing back.

After a downtrend.

ta.evening_star(open, high, low, close, trend=None)

Evening star: long white body, small star gapping above, black candle falling.

After an uptrend.

ta.morning_doji_star(open, high, low, close, trend=None)

Morning doji star: the morning star with a doji in the middle. After a downtrend.

ta.evening_doji_star(open, high, low, close, trend=None)

Evening doji star: the evening star with a doji in the middle. After an uptrend.

ta.abandoned_baby_bullish(open, high, low, close, trend=None)

Bullish abandoned baby: a doji isolated by two gaps between a long black candle and a white one.

After a downtrend.

ta.abandoned_baby_bearish(open, high, low, close, trend=None)

Bearish abandoned baby: a doji isolated by two gaps between a long white candle and a black one.

After an uptrend.

ta.tri_star_bullish(open, high, low, close, trend=None)

Bullish tri-star: three dojis, with the middle one gapping below the others. After a downtrend.

ta.tri_star_bearish(open, high, low, close, trend=None)

Bearish tri-star: three dojis, with the middle one gapping above the others. After an uptrend.

ta.three_white_soldiers(open, high, low, close, trend=None)

Three white soldiers: three long white candles, each one higher. After a downtrend.

ta.three_black_crows(open, high, low, close, trend=None)

Three black crows: three long black candles, each one lower. After an uptrend.

ta.upside_tasuki_gap(open, high, low, close, trend=None)

Upside Tasuki gap: two white candles with a gap and a black one that doesn’t close the gap.

After an uptrend, which continues.

ta.downside_tasuki_gap(open, high, low, close, trend=None)

Downside Tasuki gap: two black candles with a gap and a white one that doesn’t close the gap.

After a downtrend, which continues.

ta.rising_three_methods(open, high, low, close, trend=None)

Rising three methods: long white candle, three small black candles within its range, white candle higher up.

After an uptrend, which continues.

ta.falling_three_methods(open, high, low, close, trend=None)

Falling three methods: long black candle, three small white candles within its range, black candle lower down.

After a downtrend, which continues.

Input choices

ta.source(open, high, low, close, name)

The series chosen with a "Source" input.

name is "close", "open", "high", "low", "hl2", "hlc3", "ohlc4" or "hlcc4": the averages are calculated candle by candle.

ta.ma(src, length, kind, volume=None)

The average chosen with an "MA type" input.

kind is "sma", "ema", "rma" (the smoothed average, SMMA), "wma", "vwma" or "hma"; "vwma" also needs the volume.

ta.smoothing(src, kind, length, mult, volume=None)

The "Smoothing" group: (average, upper band, lower band).

kind is None (nothing), an ma type, or "bb": the SMA with Bollinger Bands at mult standard deviations. Whatever the chosen type doesn’t show is all nan.