Calculate the Supertrend indicator for a given price series.

The Supertrend indicator plots a single trailing line that flips between sitting below price (in an uptrend) and above price (in a downtrend). The line is built from two bands offset from the median price ((High + Low) / 2) by a multiple of the Average True Range, which are then “ratcheted” period over period - each band can only move in the direction that tightens around price - so that the active band only flips to the other side once the closing price actually crosses it. This makes Supertrend both a trend filter (the flip direction signals a trend change) and a trailing stop-loss level.

The formula is as follows:

\[\text{Basic Upper Band} = (\text{High} + \text{Low}) / 2 + \text{multiplier} \cdot \operatorname{ATR}(\text{window})\] \[\text{Basic Lower Band} = (\text{High} + \text{Low}) / 2 - \text{multiplier} \cdot \operatorname{ATR}(\text{window})\] \[\text{Final Upper Band} (t) = \text{Basic Upper Band} (t) \text{if Basic Upper Band} (t) < \text{Final Upper Band} (t-1) \text{or Close} (t-1) > \text{Final Upper Band} (t-1),\;\; \text{else Final Upper Band(t-1)}\] \[\text{Final Lower Band} (t) = \text{Basic Lower Band} (t) \text{if Basic Lower Band} (t) > \text{Final Lower Band} (t-1) \text{or Close} (t-1) < \text{Final Lower Band} (t-1),\;\; \text{else Final Lower Band(t-1)}\] \[\text{While in an uptrend},\;\; \text{Supertrend} = \text{Final Lower Band},\;\; \text{until Close crosses below it},\;\; \text{at which point the trend flips to} a \text{downtrend and Supertrend} = \text{Final Upper Band} (\text{and vice versa})\]

Also known as: Supertrend, SuperTrend.

Supertrend in Python

get_supertrend is part of the Technicals module of the open-source Finance Toolkit. Install it with:

pip install financetoolkit -U

Then call get_supertrend as shown below.

from financetoolkit import Toolkit

toolkit = Toolkit(tickers=["AAPL", "MSFT"])

toolkit.technicals.get_supertrend().xs("AAPL", level=1, axis="columns")

Parameters

get_supertrend accepts the following parameters:

  • period (str, optional): The time period to consider for historical data. Can be “daily”, “weekly”, “quarterly”, or “yearly”. Defaults to “daily”.
  • close_column (str, optional): The column name for closing prices in the historical data. Defaults to “Adj Close”.
  • window (int, optional): Number of periods for the underlying Average True Range calculation. Defaults to 10.
  • multiplier (float, optional): Multiplier applied to the Average True Range to determine how far the bands sit from the median price. Defaults to 3.0.
  • rounding (int | None, optional): The number of decimals to round the results to. Defaults to 4.
  • growth (bool, optional): Whether to calculate the growth of the Supertrend and Trend Direction values. Defaults to False.
  • lag (int | list[int], optional): The lag to use for the growth calculation.
  • standardize (bool, optional): Whether to standardize (Z-Score) the result. When combined with growth=True, standardizes the growth values instead of the raw values. Defaults to False. Defaults to 1.
Share