StrategyBuilt inTrend followingChartScript

Supertrend strategy

Use on chart

Loading the chart…

Live chart with the strategy tester open. Change the symbol and timeframe inside the chart. 00 uses6 views0

About this strategy

Goes long when Supertrend flips up and short when it flips down, so it is always in the market in the direction of the trend.

Supertrend is a trailing line built from the average true range (ATR). It sits below the price in an uptrend and above it in a downtrend, and it flips sides when the close breaks through it. This strategy simply follows the flips: long when Supertrend turns up, short when it turns down.

Because it is always in a position, it catches the whole of every big trend, but it also takes every false flip in choppy markets. The ATR length and the multiplier control that balance. A larger multiplier keeps the line further from the price, so it flips less often and gives back more before it exits.

Supertrend is popular for Nifty and Bank Nifty because it is easy to read on any timeframe. Backtest it on the symbol and timeframe you actually trade, and look at the drawdown as well as the net result.

How to use it

  1. Click Use on chart to open it in the strategy tester.
  2. Open it in the script editor or visual builder to change the ATR length or multiplier: a larger multiplier flips less often.
  3. Look at the maximum drawdown, not only the net profit.

Settings and what it draws

Settings

No settings: it works the same on every chart.

Draws on the price chart

  • SupertrendLine

Source code

strategy("Supertrend", overlay=true, initial_capital=100000, default_qty_type=strategy.percent_of_equity, default_qty_value=10, pyramiding=1)

// Calculations
[supertrend, direction] = ta.supertrend(3, 10)
isBelow = direction < 0
isAbove = direction > 0
wasTrue = isBelow[1]
wasTrue2 = isAbove[1]
inverted = not wasTrue
inverted2 = not wasTrue2
bothTrue = isBelow and inverted
bothTrue2 = isAbove and inverted2

// Plots and signals
plot(supertrend, "Supertrend", color=#26A69A, linewidth=2)

// Orders
if bothTrue
    strategy.entry("Long", strategy.long)
if bothTrue2
    strategy.entry("Short", strategy.short)

// @builder-graph v1 f85f21e3
// Visual builder data. Edit the code freely; it will then open as plain code.
// eyJuIjoiU3VwZXJ0cmVuZCBzdHJhdGVneSIsIm5vZGVzIjpbeyJpZCI6Im4xIiwidCI6ImRlY2xhcmUuc3RyYXRlZ3kiLCJ4Ijo0
// MCwieSI6NDAsInAiOnsidGl0bGUiOiJTdXBlcnRyZW5kIiwib3ZlcmxheSI6dHJ1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widCI6InN0cmF0LnNob3J0IiwieCI6MTM2MCwieSI6MTg0LCJwIjp7ImlkIjoiU2hvcnQiLCJxdHkiOjB9fSx7ImlkIjoi
// bjE0IiwidCI6Im91dC5wbG90IiwieCI6MzA0LCJ5IjoyNjQsInAiOnsidGl0bGUiOiJTdXBlcnRyZW5kIiwid2lkdGgiOjIsInN0
// eWxlIjoibGluZSIsImNvbG9yIjoiIzI2QTY5QSJ9fV0sImVkZ2VzIjpbWyJlMSIsIm4yIiwiZGlyIiwibjQiLCJhIl0sWyJlMiIs
// Im4zIiwib3V0IiwibjQiLCJiIl0sWyJlMyIsIm4yIiwiZGlyIiwibjUiLCJhIl0sWyJlNCIsIm4zIiwib3V0IiwibjUiLCJiIl0s
// WyJlNSIsIm40Iiwib3V0IiwibjYiLCJ4Il0sWyJlNiIsIm41Iiwib3V0IiwibjciLCJ4Il0sWyJlNyIsIm42Iiwib3V0Iiwibjgi
// LCJhIl0sWyJlOCIsIm43Iiwib3V0IiwibjkiLCJhIl0sWyJlOSIsIm40Iiwib3V0IiwibjEwIiwiYSJdLFsiZTEwIiwibjgiLCJv
// dXQiLCJuMTAiLCJiIl0sWyJlMTEiLCJuNSIsIm91dCIsIm4xMSIsImEiXSxbImUxMiIsIm45Iiwib3V0IiwibjExIiwiYiJdLFsi
// ZTEzIiwibjEwIiwib3V0IiwibjEyIiwid2hlbiJdLFsiZTE0IiwibjExIiwib3V0IiwibjEzIiwid2hlbiJdLFsiZTE1IiwibjIi
// LCJsaW5lIiwibjE0IiwidmFsdWUiXV19

Comments (0)

    No comments yet. Be the first to say how it worked for you.

    More built-in tools

    See all
    • Majority Rule

      Majority Rule shows the percentage of the last bars that closed higher than the bar before. Majority Rule is a simple measure of how often price has gone up over a recent window, as a percentage of…

      Other 101
    • Historical Volatility

      Shows the annualised standard deviation of log price returns over the last N bars, as a percentage. Historical Volatility measures how much price has actually moved. It takes the logarithm of the…

      Volatility 100
    • Double EMA

      The double exponential moving average: an EMA built to remove part of the lag of a normal EMA. The Double Exponential Moving Average (DEMA) was introduced by Patrick Mulloy. It takes an EMA and an…

      Trend 010