Strategy vault / Trend following
Long Term Trading Strategy Based on SuperTrend
This strategy identifies long opportunities using the SuperTrend indicator. It uses ATR and a multiplier to determine dynamic support levels for long entry. The focus is on long trades.
Overview
This strategy identifies long opportunities using the SuperTrend indicator. It uses ATR and a multiplier to determine dynamic support levels for long entry. The focus is on long trades.
Strategy Logic
The upper and lower bands are calculated based on ATR period, multiplier. Breaking upper band indicates uptrend, breaking lower band indicates downtrend.
The current trend is tracked, with 1 for uptrend and -1 for downtrend. Price breaking above upper band switches trend from down to up, generating buy signal. Breaking below lower band switches from up to down, generating sell signal.
A moving average is added as a trend filter. Buy only if price is above MA when breaking above upper band. Sell only if price is below MA when breaking below lower band. This avoids fake breakouts.
Visual helpers highlight trends, signals etc to assist with decision making.
Advantage Analysis
The advantages of this strategy:
SuperTrend dynamically tracks price changes and reflects trend shifts timely.
The ATR stop loss adjusts stops based on market volatility, helping lock in profits.
The MA filter eliminates false signals from noise in ranging markets.
Visual design intuitively presents strategy mechanics and market situation.
Only trading trend reversals makes it suitable for long-term holding.
Risk Analysis
The main risks are:
SuperTrend is sensitive to parameters. Frequent band adjustments may lead to over trading.
In choppy markets, stops can get triggered frequently.
No consideration of trading costs. Small accounts suffer greater impact.
No stop loss means high drawdown risk.
Trend filter may miss some opportunities.
Risks can be reduced by:
Optimizing ATR parameters to lower band adjustment frequency.
Adding equivalent bar filtering to avoid stops by high frequency minor swings.
Implementing stop loss and take profit to protect profits.
Tuning moving average period to balance filtering effect.
Optimizing money management to lower trading cost impacts.
Optimization Directions
The strategy can be enhanced in the following aspects:
Test different price sources like close, high etc.
Try other dynamic stop loss indicators like Chandelier Exit.
Add position sizing to optimize capital utilization.
Incorporate volatility indicators to refine entries.
Implement stop loss and take profit to control risks.
Adjust parameters for different markets.
Explore machine learning for parameter optimization.
Combine other indicators to improve filter accuracy.
Conclusion
This strategy uses SuperTrend with dynamic stops to determine trends, and adds an MA filter to identify long entries. Visual design simplifies operations. With optimized parameters and added features, it can become a robust long-term trading strategy.
Parameters
| Name | Default | What it controls |
|---|---|---|
| v_input_1 | 10 | ATR Period |
| v_input_2_hl2 | 0 | Source: hl2 | high | low | open | close | hlc3 | hlcc4 | ohlc4 |
| v_input_3 | 3 | ATR Multiplier |
| v_input_4 | true | Change ATR Calculation Method ? |
| v_input_5 | false | Show Buy/Sell Signals ? |
| v_input_6 | true | Highlighter On/Off ? |
| v_input_7 | true | Bar Coloring On/Off ? |
| v_input_8 | 20 | Moving Average Period |
| v_input_9_close | 0 | Moving Average Source: close | high | low | open | hl2 | hlc3 | hlcc4 | ohlc4 |
| v_input_10 | 9 | From Month |
| v_input_11 | true | From Day |
| v_input_12 | 2018 | From Year |
| v_input_13 | true | To Month |
| v_input_14 | true | To Day |
| v_input_15 | 9999 | To Year |
Source Pine Script
/*backtest
start: 2020-09-13 00:00:00
end: 2023-09-19 00:00:00
period: 3d
basePeriod: 1d
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=4
strategy("SuperTrend Long Strategy", overlay=true, initial_capital=50000, currency=currency.USD, default_qty_type=strategy.cash, default_qty_value=50000)
Periods = input(title="ATR Period", type=input.integer, defval=10)
src = input(hl2, title="Source")
Multiplier = input(title="ATR Multiplier", type=input.float, step=0.1, defval=3.0)
changeATR = input(title="Change ATR Calculation Method ?", type=input.bool, defval=true)
showsignals = input(title="Show Buy/Sell Signals ?", type=input.bool, defval=false)
highlighting = input(title="Highlighter On/Off ?", type=input.bool, defval=true)
barcoloring = input(title="Bar Coloring On/Off ?", type=input.bool, defval=true)
atr2 = sma(tr, Periods)
atr = changeATR ? atr(Periods) : atr2
up = src - (Multiplier * atr)
up1 = nz(up[1], up)
up := close[1] > up1 ? max(up, up1) : up
dn = src + (Multiplier * atr)
dn1 = nz(dn[1], dn)
dn := close[1] < dn1 ? min(dn, dn1) : dn
trend = 1
trend := nz(trend[1], trend)
trend := trend == -1 and close > dn1 ? 1 : trend == 1 and close < up1 ? -1 : trend
// Moving Average as Trend Filter
periodes_ma = input(title="Moving Average Period", type=input.integer, defval=20)
src_ma = input(title="Moving Average Source", type=input.source, defval=close)
ma = sma(src_ma, periodes_ma)
upPlot = plot(trend == 1 ? up : na, title="Up Trend", style=plot.style_linebr, linewidth=2, color=color.green)
buySignal = trend == 1 and trend[1] == -1 and close > ma
plotshape(buySignal ? up : na, title="UpTrend Begins", location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(color.green, 0))
plotshape(buySignal and showsignals ? up : na, title="Buy", text="Buy", location=location.absolute, style=shape.labelup, size=size.tiny, color=color.new(color.green, 0))
dnPlot = plot(trend == 1 ? na : dn, title="Down Trend", style=plot.style_linebr, linewidth=2, color=color.red)
sellSignal = trend == -1 and trend[1] == 1 and close < ma
plotshape(sellSignal ? dn : na, title="DownTrend Begins", location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(color.red, 0))
plotshape(sellSignal and showsignals ? dn : na, title="Sell", text="Sell", location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.new(color.red, 0))
mPlot = plot(ohlc4, title="", style=plot.style_circles, linewidth=0)
longFillColor = highlighting ? (trend == 1 ? color.new(color.green, 70) : color.white) : color.white
shortFillColor = highlighting ? (trend == -1 ? color.new(color.red, 70) : color.white) : color.white
fill(mPlot, upPlot, title="UpTrend Highlighter", color=longFillColor)
fill(mPlot, dnPlot, title="DownTrend Highlighter", color=shortFillColor)
FromMonth = input(defval = 9, title = "From Month", minval = 1, maxval = 12)
FromDay = input(defval = 1, title = "From Day", minval = 1, maxval = 31)
FromYear = input(defval = 2018, title = "From Year", minval = 999)
ToMonth = input(defval = 1, title = "To Month", minval = 1, maxval = 12)
ToDay = input(defval = 1, title = "To Day", minval = 1, maxval = 31)
ToYear = input(defval = 9999, title = "To Year", minval = 999)
start = timestamp(FromYear, FromMonth, FromDay, 00, 00)
finish = timestamp(ToYear, ToMonth, ToDay, 23, 59)
window() => time >= start and time <= finish ? true : false
longCondition = buySignal
if (longCondition)
strategy.entry("BUY", strategy.long, when = window())
shortCondition = sellSignal
if (shortCondition)
strategy.close("BUY")
strategy.entry("SELL", strategy.short, when = window())
buy1 = barssince(buySignal)
sell1 = barssince(sellSignal)
color1 = buy1[1] < sell1[1] ? color.green : buy1[1] > sell1[1] ? color.red : na
barcolor(barcoloring ? color1 : na)
Published by ChaoZhang on the FMZ strategy square · last modified 2023-09-20. Collected in the Quant Trading Vault. Unvalidated research: re-test with your own data and costs before trusting it.
Build this with AgenKit
A spec is not a trading system. Paste this into your coding agent and AgenKit runs a specialist team — architecture, test-first build, risk gates, review — with paper trading on by default.
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/Long-Term-Trading-Strategy-Based-on-SuperTrend.md- npx agenkit install engineering-kit in your project
- Paste the command into your agent and approve each phase
- Get tests, backtest, risk controls and a paper-trading switch
Understand it first
Before you run this, read the lesson behind the idea and how to backtest it honestly. Specs like this one are the survivors of an unknown number of trials; deflate their results accordingly.
Related specs
- Multi Indicator Dynamic Trend Following Strategy Based on EMA and SMAPine Script · Trend following · 1d
- Super trend BPine Script · Trend following · 4h
- SuperTrend Dual Direction StrategyPine Script · Trend following · 3d
- Supertrend Advance StrategyPine Script · Trend following · 5m
- Supertrend and Moving Average Crossover StrategyPine Script · Trend following · 2h
- Trading the Equity Curve Position Sizing ExamplePine Script · Trend following · 15m