strategy · on price · open source
Supertrend Strategy
Running…
Loading chart…
About
ATR-based Supertrend trend-following: long when the Supertrend flips bullish (green line under price), exit when it flips bearish.
- 10/3 is the canonical setting; raise factor for fewer, longer trends.
- Combine with an ADX > 25 confirmation for strongest setups.
- Plot the Supertrend line on the chart for visual confirmation.
Source code
//@version=5
strategy("Supertrend Strategy", overlay=true, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=10, commission_type=strategy.commission.percent, commission_value=0.05)
atrLen = input.int(10, "ATR Length", minval=5, maxval=50, step=1)
factor = input.float(3, "Factor", minval=1, maxval=10, step=0.1)
stopPct = input.float(2, "Stop Loss %", minval=0.5, maxval=10, step=0.1)
tpPct = input.float(6, "Take Profit %", minval=0, maxval=20, step=0.1)
[st, dir] = ta.supertrend(factor, atrLen)
up = dir < 0
longSignal = strategy.position_size == 0 and up
exitSignal = strategy.position_size > 0 and not up
if longSignal
strategy.entry("Long", strategy.long)
if strategy.position_size > 0
strategy.exit("Risk", stop=stopPct > 0 ? strategy.position_avg_price * (1 - stopPct / 100) : na, limit=tpPct > 0 ? strategy.position_avg_price * (1 + tpPct / 100) : na)
if exitSignal
strategy.close("Long", comment="Supertrend flip")
p_Up_Trend = plot(up ? st : na, "Up Trend", color=color.green, style=plot.style_linebr, linewidth=2)
p_Down_Trend = plot(up ? na : st, "Down Trend", color=color.red, style=plot.style_linebr, linewidth=2)
plotshape(longSignal, "Long", style=shape.triangleup, location=location.belowbar, color=color.green, text="L")
plotshape(exitSignal, "Exit", style=shape.triangledown, location=location.abovebar, color=color.red, text="X")
alertcondition(longSignal, "Supertrend long entry", "Supertrend flipped bullish")
alertcondition(exitSignal, "Supertrend exit", "Supertrend flipped bearish")
No comments yet.
Sign in to comment.