Returns the Supertrend line and its direction: a trailing stop built from the average true range that flips when price closes through it.
ta.supertrend places bands at hl2 plus and minus factor times ta.atr(atrPeriod). The lower band only ratchets up while price stays above it, and the upper band only ratchets down while price stays below it. When the close breaks through the active band, the trend flips.
It returns [supertrend, direction]. direction is -1 in an uptrend, where the line sits below price, and 1 in a downtrend, where the line sits above it. The first line value appears on bar number atrPeriod.
Syntax
ta.supertrend(factor: series int/float, atrPeriod: simple int) -> [series float, series float]Arguments
| Name | Type | Default | Description |
|---|---|---|---|
| factor | series int/ | required | Multiplier of the ATR that sets the band distance from hl2. |
| atrPeriod | simple int | required | Length of the ATR. A whole number of 1 or more. |
Returns
[series float, series float]: the Supertrend price level and the direction, -1 for up and 1 for down.
Remarks
- Split the line by direction to colour the two trends differently: plot the value only where direction matches.
- The line trails price, so it always sits on the same side of the close as the current trend.
Example
//@version=6
indicator("Supertrend", overlay = true)
[st, dir] = ta.supertrend(3, 10)
plot(dir < 0 ? st : na, "Up trend", color = color.green, linewidth = 2, style = plot.style_linebr)
plot(dir > 0 ? st : na, "Down trend", color = color.red, linewidth = 2, style = plot.style_linebr)See also
Runs on Sidechair