๐ŸŽ“ Tutorials

TradingView Pine Script SuperTrend Strategy: Build a Custom Indicator Step by Step (2026)

โš ๏ธ Disclosure: Some links on this page are affiliate links. If you sign up through them, I may earn a commission โ€” at no extra cost to you. I only review tools I actually use.
The SuperTrend indicator is one of the most effective trend-following tools in a trader's arsenal โ€” and building your own version in Pine Script gives you complete control over the logic, signals, and risk management. This tutorial walks you through creating a custom SuperTrend strategy indicator from scratch in Pine Script v6, with working code you can copy-paste and adapt to any market.

I use TradingView daily for my USDJPY momentum trading, and the SuperTrend concept underpins a lot of how I think about trend direction. The default built-in SuperTrend is fine for quick checks, but a custom implementation lets you add filters, alerts, and multi-timeframe confirmation that the built-in version simply can't do.

What Is the SuperTrend Indicator?

The SuperTrend is an ATR-based (Average True Range) trend-following indicator that plots a dynamic line above or below price. When price is above the line, the trend is bullish. When price is below, the trend is bearish. Flip points generate buy and sell signals.

The math is straightforward:

The indicator switches between bands based on price action: There are two inputs that control sensitivity:
ParameterDefaultEffect
ATR Period10Number of bars used to calculate volatility. Higher = smoother, fewer signals
Multiplier3.0How far the bands sit from the midpoint. Higher = wider bands, fewer false signals
The beauty of SuperTrend is its simplicity. Unlike oscillators that bounce between overbought and oversold, SuperTrend gives you a clear binary signal: you're either in an uptrend or a downtrend.

Why Build a Custom SuperTrend Instead of Using the Built-In?

TradingView includes a built-in ta.supertrend() function and a default SuperTrend indicator. So why bother writing your own?

1. Custom signal logic. The built-in indicator shows the line and color changes โ€” but doesn't let you define exactly when to enter, how to filter signals, or when to exit. A custom strategy version lets you add conditions like "only take long signals when RSI is above 50" or "skip signals during low-volume sessions." 2. Strategy backtesting. The built-in indicator is just an indicator โ€” it can't generate backtesting statistics. A strategy() script gives you net profit, win rate, max drawdown, profit factor, and trade-by-trade results in TradingView's Strategy Tester. 3. Alert automation. Custom scripts let you create alerts that fire on your exact conditions, which you can pipe to webhooks for automated execution on exchanges like OKX or through your broker's API. 4. Multi-timeframe confirmation. You can request the SuperTrend value from a higher timeframe and only take signals that align with the bigger picture โ€” something the built-in indicator doesn't support natively. 5. Visual customization. Add buy/sell labels, background coloring, dashboard panels, or combine SuperTrend with other indicators in a single script (important for free-tier users who are limited to 3 indicators per chart on TradingView).

Step 1: Basic SuperTrend Indicator in Pine Script v6

Let's start with a clean SuperTrend indicator. This version uses TradingView's built-in ta.supertrend() function for the core calculation, then adds visual signals on top.

//@version=6
indicator("Custom SuperTrend", overlay=true)

// โ”€โ”€โ”€ INPUTS โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
atrPeriod  = input.int(10, "ATR Period", minval=1)
multiplier = input.float(3.0, "Multiplier", minval=0.1, step=0.1)

// โ”€โ”€โ”€ SUPERTREND CALCULATION โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
[supertrend, direction] = ta.supertrend(multiplier, atrPeriod)

// direction: -1 = bullish (price above), +1 = bearish (price below)
isBullish = direction < 0
isBearish = direction > 0

// โ”€โ”€โ”€ PLOT โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
plot(supertrend, "SuperTrend", 
     color=isBullish ? color.green : color.red, 
     linewidth=2, style=plot.style_linebr)

// โ”€โ”€โ”€ BUY / SELL SIGNALS โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
buySignal  = isBullish and not isBullish[1]
sellSignal = isBearish and not isBearish[1]

plotshape(buySignal, "Buy", shape.triangleup, 
          location.belowbar, color.green, size=size.small)
plotshape(sellSignal, "Sell", shape.triangledown, 
          location.abovebar, color.red, size=size.small)

// โ”€โ”€โ”€ BACKGROUND โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
bgcolor(isBullish ? color.new(color.green, 93) : color.new(color.red, 93))

What this does:

Save this script, add it to any chart, and you'll immediately see buy/sell signals wherever the SuperTrend flips.

Step 2: Convert to a Backtestable Strategy

Now let's turn this into a strategy() so you can see actual backtest performance numbers in TradingView's Strategy Tester.

//@version=6
strategy("SuperTrend Strategy", overlay=true, 
         default_qty_type=strategy.percent_of_equity, 
         default_qty_value=100, 
         initial_capital=10000, 
         commission_type=strategy.commission.percent, 
         commission_value=0.1)

// โ”€โ”€โ”€ INPUTS โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
atrPeriod  = input.int(10, "ATR Period", minval=1)
multiplier = input.float(3.0, "Multiplier", minval=0.1, step=0.1)

// โ”€โ”€โ”€ SUPERTREND CALCULATION โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
[supertrend, direction] = ta.supertrend(multiplier, atrPeriod)

isBullish = direction < 0
isBearish = direction > 0

buySignal  = isBullish and not isBullish[1]
sellSignal = isBearish and not isBearish[1]

// โ”€โ”€โ”€ STRATEGY ENTRIES โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
if buySignal
    strategy.entry("Long", strategy.long)

if sellSignal
    strategy.close("Long")

// โ”€โ”€โ”€ PLOT โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
plot(supertrend, "SuperTrend", 
     color=isBullish ? color.green : color.red, 
     linewidth=2, style=plot.style_linebr)
plotshape(buySignal, "Buy", shape.triangleup, 
          location.belowbar, color.green, size=size.small)
plotshape(sellSignal, "Sell", shape.triangledown, 
          location.abovebar, color.red, size=size.small)

After adding this to your chart, click the Strategy Tester tab at the bottom of TradingView to see performance metrics. Pay attention to:

A common beginner mistake: running backtests with 0% commission. The commission_value=0.1 line above adds a realistic 0.1% per trade. Always include commission โ€” it's the difference between a profitable backtest and a real-world loss.

> Tip: The Strategy Tester is available on all TradingView plans, but the number of bars it processes differs by plan. Essential and above give you more historical data for longer backtests.

Step 3: Add an RSI Filter to Reduce False Signals

The SuperTrend alone generates plenty of whipsaw signals during sideways markets. Adding an RSI filter is one of the simplest ways to cut the noise: only take long entries when RSI confirms momentum is behind the move.

//@version=6
strategy("SuperTrend + RSI Filter", overlay=true, 
         default_qty_type=strategy.percent_of_equity, 
         default_qty_value=100, 
         initial_capital=10000, 
         commission_type=strategy.commission.percent, 
         commission_value=0.1)

// โ”€โ”€โ”€ INPUTS โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
atrPeriod   = input.int(10, "ATR Period", minval=1)
multiplier  = input.float(3.0, "Multiplier", minval=0.1, step=0.1)
rsiLength   = input.int(14, "RSI Length", minval=1)
rsiLongMin  = input.int(50, "RSI Min for Long", minval=1, maxval=100)
rsiShortMax = input.int(50, "RSI Max for Short", minval=1, maxval=100)

// โ”€โ”€โ”€ CALCULATIONS โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
[supertrend, direction] = ta.supertrend(multiplier, atrPeriod)
rsi = ta.rsi(close, rsiLength)

isBullish = direction < 0
isBearish = direction > 0

buySignal  = isBullish and not isBullish[1] and rsi > rsiLongMin
sellSignal = isBearish and not isBearish[1] and rsi < rsiShortMax

// โ”€โ”€โ”€ STRATEGY ENTRIES โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
if buySignal
    strategy.entry("Long", strategy.long)

if sellSignal
    strategy.close("Long")
    strategy.entry("Short", strategy.short)

// Exit short on next buy signal
if isBullish and not isBullish[1]
    strategy.close("Short")

// โ”€โ”€โ”€ PLOTS โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
plot(supertrend, "SuperTrend", 
     color=isBullish ? color.green : color.red, 
     linewidth=2, style=plot.style_linebr)
plotshape(buySignal, "Buy", shape.triangleup, 
          location.belowbar, color.green, size=size.small)
plotshape(sellSignal, "Sell", shape.triangledown, 
          location.abovebar, color.red, size=size.small)

// โ”€โ”€โ”€ RSI PANEL (optional) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
// Uncomment these lines to see RSI in a separate panel:
// plot(rsi, "RSI", color.purple)
// hline(rsiLongMin, "RSI Filter Level", color.gray)

What the RSI filter does:

In my experience with USDJPY, adding the RSI filter reduced the number of trades by about 30% while improving the profit factor. Fewer trades, but better ones.

Step 4: Multi-Timeframe SuperTrend Confirmation

One of the most powerful upgrades you can make to any trend indicator is adding a higher-timeframe filter. The idea: only trade in the direction of the bigger trend. If the daily SuperTrend is bullish, only take long signals on the 4-hour chart.

//@version=6
strategy("SuperTrend MTF Strategy", overlay=true, 
         default_qty_type=strategy.percent_of_equity, 
         default_qty_value=100, 
         initial_capital=10000, 
         commission_type=strategy.commission.percent, 
         commission_value=0.1)

// โ”€โ”€โ”€ INPUTS โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
atrPeriod  = input.int(10, "ATR Period", minval=1)
multiplier = input.float(3.0, "Multiplier", minval=0.1, step=0.1)
htfInput   = input.timeframe("D", "Higher Timeframe")

// โ”€โ”€โ”€ CURRENT TIMEFRAME SUPERTREND โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
[supertrend, direction] = ta.supertrend(multiplier, atrPeriod)
isBullish = direction < 0
isBearish = direction > 0

// โ”€โ”€โ”€ HIGHER TIMEFRAME SUPERTREND โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
htfDirection = request.security(syminfo.tickerid, htfInput, 
               ta.supertrend(multiplier, atrPeriod)[1])

// [1] prevents lookahead bias โ€” uses the CONFIRMED bar value
htfBullish = htfDirection < 0

// โ”€โ”€โ”€ FILTERED SIGNALS โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
buySignal  = isBullish and not isBullish[1] and htfBullish
sellSignal = isBearish and not isBearish[1] and not htfBullish

if buySignal
    strategy.entry("Long", strategy.long)

if sellSignal
    strategy.close("Long")

// โ”€โ”€โ”€ PLOTS โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
plot(supertrend, "SuperTrend", 
     color=isBullish ? color.green : color.red, 
     linewidth=2, style=plot.style_linebr)
plotshape(buySignal, "Buy (MTF Confirmed)", shape.triangleup, 
          location.belowbar, color.green, size=size.normal)
plotshape(sellSignal, "Sell", shape.triangledown, 
          location.abovebar, color.red, size=size.small)

// HTF trend background
bgcolor(htfBullish ? color.new(color.green, 95) : color.new(color.red, 95))

Important: The [1] offset on the request.security() call is critical. Without it, you get lookahead bias โ€” the strategy would "cheat" by using higher-timeframe data that wasn't available yet in real time. Always offset by 1 bar for confirmed data.

The subtle green/red background shows the higher-timeframe trend direction at a glance. Large green triangles mark buy signals that have multi-timeframe confluence โ€” these are the highest-conviction setups.

> Note: Multi-timeframe data requests require TradingView Essential or higher. Free plan users are limited in their request.security() calls.

Step 5: Add Stop Loss and Take Profit

No strategy is complete without risk management. Here's how to add a percentage-based stop loss and take profit to the SuperTrend strategy:

//@version=6
strategy("SuperTrend + Risk Management", overlay=true, 
         default_qty_type=strategy.percent_of_equity, 
         default_qty_value=100, 
         initial_capital=10000, 
         commission_type=strategy.commission.percent, 
         commission_value=0.1)

// โ”€โ”€โ”€ INPUTS โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
atrPeriod  = input.int(10, "ATR Period", minval=1)
multiplier = input.float(3.0, "Multiplier", minval=0.1, step=0.1)
slPercent  = input.float(2.0, "Stop Loss %", minval=0.1, step=0.1)
tpPercent  = input.float(4.0, "Take Profit %", minval=0.1, step=0.1)
useTrailSL = input.bool(false, "Use Trailing Stop")
trailPercent = input.float(1.5, "Trail %", minval=0.1, step=0.1)

// โ”€โ”€โ”€ SUPERTREND โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
[supertrend, direction] = ta.supertrend(multiplier, atrPeriod)
isBullish = direction < 0
isBearish = direction > 0

buySignal  = isBullish and not isBullish[1]
sellSignal = isBearish and not isBearish[1]

// โ”€โ”€โ”€ ENTRIES WITH SL/TP โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
if buySignal
    strategy.entry("Long", strategy.long)
    if useTrailSL
        strategy.exit("Trail", "Long", 
                       trail_points=close * trailPercent / 100 / syminfo.mintick, 
                       trail_offset=close * trailPercent / 100 / syminfo.mintick)
    else
        strategy.exit("SL/TP", "Long", 
                       stop=close * (1 - slPercent / 100), 
                       limit=close * (1 + tpPercent / 100))

if sellSignal
    strategy.close("Long")

// โ”€โ”€โ”€ PLOTS โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
plot(supertrend, "SuperTrend", 
     color=isBullish ? color.green : color.red, 
     linewidth=2, style=plot.style_linebr)
plotshape(buySignal, "Buy", shape.triangleup, 
          location.belowbar, color.green, size=size.small)
plotshape(sellSignal, "Sell", shape.triangledown, 
          location.abovebar, color.red, size=size.small)

Risk management options in this script:

The stop loss and take profit values are set as percentages from the entry price. For volatile markets like crypto, you might want wider stops (3-5%). For forex pairs like USDJPY, 1-2% is often enough.

Step 6: Full Production Strategy โ€” Complete Code

Here's the complete strategy that combines everything: SuperTrend + RSI filter + multi-timeframe confirmation + risk management + a dashboard panel showing key statistics.

//@version=6
strategy("SuperTrend Pro Strategy", overlay=true, 
         default_qty_type=strategy.percent_of_equity, 
         default_qty_value=100, 
         initial_capital=10000, 
         commission_type=strategy.commission.percent, 
         commission_value=0.1,
         max_bars_back=5000)

// โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
// INPUTS
// โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
grpST     = "SuperTrend Settings"
atrPeriod  = input.int(10, "ATR Period", minval=1, group=grpST)
multiplier = input.float(3.0, "Multiplier", minval=0.1, step=0.1, group=grpST)

grpFilter = "Filters"
useRSI     = input.bool(true, "Use RSI Filter", group=grpFilter)
rsiLength  = input.int(14, "RSI Length", minval=1, group=grpFilter)
rsiMin     = input.int(50, "RSI Min for Long", group=grpFilter)
useMTF     = input.bool(false, "Use Multi-Timeframe Filter", group=grpFilter)
htfInput   = input.timeframe("D", "Higher Timeframe", group=grpFilter)

grpRisk   = "Risk Management"
slPercent  = input.float(2.0, "Stop Loss %", minval=0.1, step=0.1, group=grpRisk)
tpPercent  = input.float(4.0, "Take Profit %", minval=0.1, step=0.1, group=grpRisk)
allowShort = input.bool(false, "Allow Short Trades", group=grpRisk)

// โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
// CALCULATIONS
// โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
[supertrend, direction] = ta.supertrend(multiplier, atrPeriod)
rsi = ta.rsi(close, rsiLength)

isBullish = direction < 0
isBearish = direction > 0

// Higher timeframe filter
htfBullish = true
if useMTF
    htfDir = request.security(syminfo.tickerid, htfInput, 
             ta.supertrend(multiplier, atrPeriod)[1])
    htfBullish := htfDir < 0

// Signal generation
rawBuy  = isBullish and not isBullish[1]
rawSell = isBearish and not isBearish[1]

rsiOK = useRSI ? rsi > rsiMin : true
mtfOK = useMTF ? htfBullish : true

buySignal  = rawBuy and rsiOK and mtfOK
sellSignal = rawSell

// โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
// STRATEGY LOGIC
// โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
if buySignal
    strategy.entry("Long", strategy.long)
    strategy.exit("SL/TP Long", "Long", 
                   stop=close * (1 - slPercent / 100), 
                   limit=close * (1 + tpPercent / 100))

if sellSignal
    strategy.close("Long")
    if allowShort
        strategy.entry("Short", strategy.short)
        strategy.exit("SL/TP Short", "Short", 
                       stop=close * (1 + slPercent / 100), 
                       limit=close * (1 - tpPercent / 100))

if buySignal and strategy.position_size < 0
    strategy.close("Short")

// โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
// VISUALS
// โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
plot(supertrend, "SuperTrend", 
     color=isBullish ? color.green : color.red, 
     linewidth=2, style=plot.style_linebr)

plotshape(buySignal, "Buy", shape.triangleup, 
          location.belowbar, color.green, size=size.normal)
plotshape(sellSignal, "Sell", shape.triangledown, 
          location.abovebar, color.red, size=size.small)

bgcolor(useMTF and not htfBullish ? color.new(color.red, 95) : na)

// โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
// DASHBOARD
// โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
var table dash = table.new(position.top_right, 2, 5, 
                           bgcolor=color.new(color.black, 80), 
                           border_width=1)
if barstate.islast
    table.cell(dash, 0, 0, "SuperTrend Pro", text_color=color.white, 
               text_size=size.small)
    table.cell(dash, 1, 0, "", text_color=color.white)
    table.cell(dash, 0, 1, "Trend", text_color=color.gray, 
               text_size=size.small)
    table.cell(dash, 1, 1, isBullish ? "โ–ฒ BULL" : "โ–ผ BEAR", 
               text_color=isBullish ? color.green : color.red, 
               text_size=size.small)
    table.cell(dash, 0, 2, "RSI", text_color=color.gray, 
               text_size=size.small)
    table.cell(dash, 1, 2, str.tostring(rsi, "#.0"), 
               text_color=rsi > 50 ? color.green : color.red, 
               text_size=size.small)
    table.cell(dash, 0, 3, "SuperTrend", text_color=color.gray, 
               text_size=size.small)
    table.cell(dash, 1, 3, str.tostring(supertrend, "#.##"), 
               text_color=color.white, text_size=size.small)
    table.cell(dash, 0, 4, "ATR Period", text_color=color.gray, 
               text_size=size.small)
    table.cell(dash, 1, 4, str.tostring(atrPeriod), 
               text_color=color.white, text_size=size.small)

This is the full production-ready script. Add it to your chart, open the Strategy Tester, and start experimenting with the inputs.

Best SuperTrend Settings for Different Markets

There's no universal "best" setting โ€” it depends on what you're trading and your timeframe. Here are starting points based on common configurations:

MarketTimeframeATR PeriodMultiplierNotes
Forex (USDJPY, EURUSD)4H103.0Default works well for major pairs
Forex (GBPJPY, volatile)4H103.5Wider multiplier for volatile crosses
Crypto (BTC, ETH)1H102.0Tighter settings catch faster crypto moves
Crypto (altcoins)1H72.0Shorter ATR period for faster reactions
Stocks (large cap)Daily143.0Longer ATR period smooths daily noise
Indices (S&P 500)4H103.0Standard settings work on liquid indices
How to optimize settings:

1. Apply the strategy to your target instrument and timeframe

2. Open Strategy Tester โ†’ click the Optimization icon (gear with sparkle) 3. Set ATR Period range: 5 to 20, step 1 4. Set Multiplier range: 1.5 to 5.0, step 0.5 5. Run the optimization and sort by Profit Factor (not just Net Profit)

A word of caution: don't over-optimize. If your best settings only work on the last 100 bars, they'll fall apart on live data. Look for parameter ranges where performance is consistently good, not just the single best configuration.

Setting Up Alerts for Live Trading

Once you're satisfied with the backtest results, set up alerts to trade live. Here's how to add alert conditions to the strategy:

// Add these lines to your strategy script:
alertcondition(buySignal, "SuperTrend Buy", "SuperTrend flipped BULLISH on {{ticker}} ({{interval}})")
alertcondition(sellSignal, "SuperTrend Sell", "SuperTrend flipped BEARISH on {{ticker}} ({{interval}})")

To create an alert:

1. Right-click the indicator name on your chart 2. Select Add Alert 3. Choose the condition (SuperTrend Buy or SuperTrend Sell) 4. Set the frequency โ€” "Once Per Bar Close" is usually what you want (it waits for the bar to close before firing, preventing false signals from intra-bar price spikes) 5. Configure notifications: push, email, webhook, or all three

For automated execution, use the Webhook URL field to send alert data to your exchange. If you're trading on OKX, check out their Signal Bot which accepts TradingView webhooks directly.

For a detailed walkthrough on alert frequencies and troubleshooting, see our guide on TradingView alert "Once Per Bar Close" explained.

Common Mistakes and How to Avoid Them

After building and testing dozens of Pine Script strategies, here are the mistakes I see most often:

1. No Commission in Backtests

If your strategy shows 200% profit with 0% commission, it might actually lose money in real trading. Always set commission_value to at least 0.05% for stocks, 0.1% for crypto, or 0.02% for forex.

2. Lookahead Bias in Multi-Timeframe Requests

If you use request.security() without the [1] offset, your strategy "sees the future" by using the current higher-timeframe bar's value before it closes. Always offset:

// WRONG โ€” lookahead bias
htfValue = request.security(syminfo.tickerid, "D", ta.supertrend(3, 10))

// RIGHT โ€” uses confirmed previous bar
htfValue = request.security(syminfo.tickerid, "D", ta.supertrend(3, 10)[1])

3. Over-Optimization

Finding the "perfect" ATR period and multiplier on historical data guarantees nothing about the future. Instead of optimizing to a single value, look for robust ranges โ€” if your strategy is profitable with multipliers from 2.5 to 3.5, you're on solid ground. If it only works at exactly 2.73, you've curve-fitted.

4. Ignoring Trending vs. Ranging Markets

The SuperTrend is a trend-following indicator. By definition, it underperforms in range-bound markets. Adding a filter like ADX (Average Directional Index) can help:

adx = ta.adx(14)
isStrong = adx > 25  // Only trade when trend strength is sufficient

buySignal = isBullish and not isBullish[1] and isStrong

5. Trading Every Signal

Not every SuperTrend flip is worth trading. Combine it with volume, RSI, or higher-timeframe direction to filter for the best setups. The multi-timeframe version in Step 4 above is a good starting point.

SuperTrend vs. Other Trend Indicators

How does SuperTrend compare to other popular trend-following tools?

FeatureSuperTrendMoving Average CrossoverParabolic SARIchimoku Cloud
Simplicityโ˜…โ˜…โ˜…โ˜…โ˜…โ˜…โ˜…โ˜…โ˜…โ˜…โ˜…โ˜…โ˜…โ˜…
Signal clarityClear buy/sellCrossover pointsDot flipComplex multi-signal
LagMediumHigh (depends on MA period)LowMedium-High
False signals in rangesModerateHighVery HighLow
Customization2 parameters2 MA periods2 parameters5+ parameters
Pine Script complexitySimpleSimpleSimpleComplex
SuperTrend hits a sweet spot: clearer signals than moving average crossovers, fewer false signals than Parabolic SAR, and far simpler than Ichimoku. That's why it's one of the most popular indicators on TradingView, with thousands of community scripts built around it.

What's Next

You now have a production-ready SuperTrend strategy in Pine Script v6. Here's how to take it further:

1. Backtest on your target market โ€” apply the script, run the Strategy Tester, and evaluate performance before risking real capital

2. Experiment with filters โ€” try different RSI thresholds, add volume confirmation, or combine with our RSI Divergence indicator 3. Set up multi-timeframe confirmation โ€” toggle on the MTF filter to align with the daily or weekly trend 4. Automate with alerts โ€” configure webhook alerts to execute trades automatically 5. Combine with other scripts โ€” merge SuperTrend with EMA or MACD into a single all-in-one indicator to save indicator slots on your TradingView plan

If you're migrating Pine Script v5 code to v6, check our Pine Script v5 to v6 migration checklist โ€” there are breaking changes that can silently alter your strategy's behavior.

For live trading execution, you can connect TradingView alerts to Interactive Brokers via their API or to OKX using their Signal Bot feature. See our guide on TradingView webhook to Google Sheets for a free trade logging setup.

---

*Disclaimer: This tutorial is for educational purposes only. Past backtest performance does not guarantee future results. Always test strategies on paper trading before using real capital. The author uses TradingView and Interactive Brokers for personal trading.*

๐Ÿ“ˆ

About the author

I'm a systematic trader running live strategies on IB (USDJPY momentum) and Hyperliquid (crypto perps). Every tool reviewed here is something I've used with real capital. Questions? Reach out.

๐Ÿ“š Related Articles

๐ŸŽ“ Tutorials

TradingView Free Plan Indicator Limit: How to Combine RSI, EMA, and MACD Into One Pine Script (2026)

Hit TradingView's 2-indicator limit on the free plan? Learn how to combine RSI, EMA, and MACD into a single all-in-one Pine Script v6 indicator โ€” with full copy-paste code, visual customization tips, and a clear upgrade path when you outgrow the workaround.

March 23, 2026 โฑ 10 min read
๐ŸŽ“ Tutorials

TradingView Webhook to Telegram Bot: Get Real-Time Alerts on Your Phone (2026 Setup Guide)

Learn how to send TradingView alerts directly to Telegram using webhooks. Step-by-step guide covering BotFather setup, free relay options, JSON payload formatting, and real trading alert examples โ€” no coding experience required.

March 22, 2026 โฑ 14 min read
๐ŸŽ“ Tutorials

TradingView Pine Script Multi-Chart Layout: Display Multiple Panels from One Indicator

Build a single Pine Script indicator that displays data across multiple chart panels using force_overlay, plot splits, and request.security.

March 20, 2026 โฑ 10 min read

๐Ÿ“ฌ Get weekly trading insights

Real trades, honest reviews, no fluff. One email per week.