Advanced Multi-functional SuperTrend Trend Following Strategy with Automated Take Profit and Stop Loss System

ATR supertrend TP/SL 趋势跟踪 价格回撤 动态调整 风险管理
Created on: 2025-04-02 15:30:36 Modified on: 2025-04-02 15:30:36
Copy: 4 Number of hits: 490
avatar of ianzeng123 ianzeng123
2
Follow
319
Followers

 Advanced Multi-functional SuperTrend Trend Following Strategy with Automated Take Profit and Stop Loss System  Advanced Multi-functional SuperTrend Trend Following Strategy with Automated Take Profit and Stop Loss System

Strategy Overview

The “Advanced Multi-functional SuperTrend Trend Following Strategy with Automated Take Profit and Stop Loss System” is a quantitative trading strategy based on the SuperTrend indicator, combined with automated take profit/stop loss (TP/SL) mechanisms and entry optimization logic. The core of the strategy utilizes the SuperTrend indicator to identify market trends, generates trading signals at trend reversal points, adjusts the actual entry points through percentage offsets, and sets predefined take profit and stop loss levels to manage risk and secure profits. This approach allows traders to enter at more favorable prices after trend confirmation while having clear risk management parameters.

Strategy Principles

This strategy is based on the calculation and application of the SuperTrend indicator, with the following fundamental principles:

  1. SuperTrend Calculation: First, the strategy calculates the ATR (Average True Range) to measure market volatility. Then, it determines the upper and lower bands using the ATR and a user-defined multiplier. The SuperTrend determines the current trend based on the price position relative to these bands.

  2. Trend Identification: When the price breaks through the upper band, the trend turns downward (-1); when the price breaks through the lower band, the trend turns upward (1). The strategy tracks trend changes, generating buy signals when the trend changes from -1 to 1, and sell signals when the trend changes from 1 to -1.

  3. Entry Optimization: The strategy doesn’t enter immediately at the trend reversal point but calculates an offset price instead. For buy signals, the entry point is set lower by a certain percentage (entryOffsetPerc) from the signal price; for sell signals, the entry point is set higher by a certain percentage.

  4. Automated Take Profit and Stop Loss: Once entered, the strategy automatically sets take profit (TP) and stop loss (SL) levels based on the entry price and user-defined percentage parameters. For long trades, the TP is set a certain percentage above the entry price, and the SL a certain percentage below; for short trades, the opposite applies.

  5. Trigger Mechanism: The strategy monitors whether the price hits the TP or SL levels. Once hit, the trade is closed, and corresponding markers are displayed on the chart.

Strategy Advantages

  1. Optimized Entry After Trend Confirmation: Unlike traditional SuperTrend strategies that enter immediately at trend line crossovers, this strategy allows waiting for price pullbacks to more favorable positions after signal generation, improving entry price favorability and reducing risks from false breakouts.

  2. Automated Risk Management: The strategy has built-in TP/SL mechanisms, automatically setting clear exit conditions for each trade, avoiding the problem of traders failing to exit unfavorable trades timely due to subjective emotions.

  3. Visualization of Trade Information: The strategy marks entry points, TP and SL trigger points on the chart, allowing traders to intuitively understand trade execution, which helps with subsequent analysis and strategy optimization.

  4. Parameter Adjustability: The strategy provides multiple adjustable parameters, including ATR period, ATR multiplier, TP/SL percentages, and entry offset percentage, allowing traders to adjust according to different market environments and personal risk preferences.

  5. Bidirectional Trading: The strategy supports both long and short trading, enabling profit in both upward and downward trends, maximizing market opportunities.

Strategy Risks

  1. False Breakout Risk: Although the strategy mitigates this problem through entry offset, the market may still exhibit short-term fluctuations that break the trend and then resume the original trend, leading to unnecessary trading signals and losses.

  2. Limitations of Fixed Percentage Stop Loss: The strategy uses fixed percentages for stop losses, which may not always be optimal. In highly volatile markets, fixed percentage stops may be too small, causing frequent triggers; in low volatility markets, they may be too large, resulting in excessive losses.

  3. Parameter Optimization Challenges: Finding the optimal ATR period, multiplier, and TP/SL ratios requires extensive historical data testing and optimization, and the optimal parameters may need adjustment as market conditions change.

  4. Gap Risk: In cases of price gaps, the actual stop loss price may be far lower or higher than the set stop loss level, causing actual losses to exceed expectations.

  5. Liquidity Risk: In markets with insufficient liquidity, entry or exit orders may not be executed at expected prices, leading to increased slippage and decreased strategy performance.

Solutions: - Combine other indicators or conditions for trend confirmation to reduce false breakout risk - Adopt dynamic stop loss methods, such as setting stops based on ATR rather than fixed percentages - Continuously test and adjust parameters for different market cycles and environments - Manual monitoring and intervention may be required for highly volatile markets - When used in low liquidity markets, increase slippage considerations or avoid trading

Strategy Optimization Directions

  1. Adaptive Parameter Adjustment: Currently, the strategy uses fixed ATR periods and multipliers. It can be optimized to automatically adjust these parameters based on market volatility. For example, increasing the ATR period or decreasing the multiplier when volatility increases, and vice versa when volatility decreases, to adapt to different market environments.

  2. Multi-timeframe Analysis: Introducing multi-timeframe confirmation mechanisms, executing trades only when the higher timeframe trend direction aligns with the current signal, can reduce counter-trend trading and false breakout risks.

  3. Dynamic Take Profit and Stop Loss Settings: Changing fixed percentage TP/SL to dynamic values based on ATR or volatility better reflects current market conditions, avoiding stops that are too tight in high volatility environments or too wide in low volatility environments.

  4. Adding Volume Confirmation: Incorporating volume analysis to confirm trend validity, for example, confirming signals only when price breakouts are accompanied by sufficient volume, can reduce false breakout risks.

  5. Partial Profit Locking Mechanism: Adding partial position closing functionality, closing part of the position when the price reaches specific profit levels, both secures partial profits and gives remaining positions space to follow the trend, improving overall risk-reward ratio.

These optimization directions are important because they enable the strategy to better adapt to market changes, reduce false signals, and enhance the robustness and long-term profitability of the strategy. Especially adaptive parameters and dynamic TP/SL settings can significantly improve the strategy’s performance consistency across different market environments.

Conclusion

The Advanced Multi-functional SuperTrend Trend Following Strategy combines the advantages of classic trend following with modern risk management techniques, identifying market trends through the SuperTrend indicator while enhancing trading results through optimized entry points and automated TP/SL mechanisms. Its most notable feature is seeking optimal entry points after trend signal confirmation while setting clear profit and risk control parameters for each trade.

The strategy’s strength lies in its complete trading system architecture, with clear specifications from signal generation, entry optimization to risk management, suitable for traders seeking to profit in trending markets while controlling risk. However, like all trend following strategies, it may generate more false signals and losing trades in oscillating markets.

To further enhance strategy performance, traders should consider adding adaptive parameter adjustments, multi-timeframe confirmation, and dynamic risk management mechanisms to better adapt to different market environments. Ultimately, this strategy provides a good foundation framework that traders can customize and optimize according to their own needs and market characteristics.

Strategy source code
/*backtest
start: 2024-04-02 00:00:00
end: 2025-04-01 00:00:00
period: 3d
basePeriod: 3d
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=5
strategy("SuperTrend STRATEGY w/ TP & SL", overlay=true)

// === Girdiler ===
atrPeriod = input.int(10, title="ATR Period")
mult = input.float(3.0, title="ATR Multiplier")
src = input.source(hl2, title="Kaynak")
tpPerc = input.float(2.5, title="Take Profit (%)")
slPerc = input.float(1.0, title="Stop Loss (%)")
entryOffsetPerc = input.float(0.2, title="Giriş Noktası Yüzdesel Geriden (%)")

// === ATR ve SuperTrend Hesabı ===
atr = ta.atr(atrPeriod)
upperBand = src - mult * atr
lowerBand = src + mult * atr

prevUpper = nz(upperBand[1], upperBand)
prevLower = nz(lowerBand[1], lowerBand)

upperBand := close[1] > prevUpper ? math.max(upperBand, prevUpper) : upperBand
lowerBand := close[1] < prevLower ? math.min(lowerBand, prevLower) : lowerBand

var trend = 1
trend := close > prevLower ? 1 : close < prevUpper ? -1 : nz(trend[1], 1)

buySignal = trend == 1 and trend[1] == -1
sellSignal = trend == -1 and trend[1] == 1

// === Giriş Noktası Takibi ===
var float lastBuyPrice = na
var float lastSellPrice = na
var int lastBuyBarIndex = na
var int lastSellBarIndex = na
var bool buyEntryPlotted = false
var bool sellEntryPlotted = false

if buySignal
    lastBuyPrice := close * (1 - entryOffsetPerc / 100)
    lastBuyBarIndex := bar_index
    buyEntryPlotted := false

if sellSignal
    lastSellPrice := close * (1 + entryOffsetPerc / 100)
    lastSellBarIndex := bar_index
    sellEntryPlotted := false

// === TP ve SL Hesapları ===
long_tp = lastBuyPrice * (1 + tpPerc / 100)
long_sl = lastBuyPrice * (1 - slPerc / 100)
short_tp = lastSellPrice * (1 - tpPerc / 100)
short_sl = lastSellPrice * (1 + slPerc / 100)

// === TP / SL Temasları (yalnızca işlemden sonra ilk gelen hedef) ===
hitLongTP = false
hitLongSL = false
hitShortTP = false
hitShortSL = false

// === Giriş Etiketleri ===
var label buyLabel = na
var label sellLabel = na

if not na(lastBuyPrice) and not buyEntryPlotted and close <= lastBuyPrice and bar_index > lastBuyBarIndex
    buyLabel := label.new(x=bar_index, y=low, text="Giriş", style=label.style_label_up, color=color.fuchsia, textcolor=color.white)
    buyEntryPlotted := true
    lastBuyBarIndex := bar_index

if not na(lastSellPrice) and not sellEntryPlotted and close >= lastSellPrice and bar_index > lastSellBarIndex
    sellLabel := label.new(x=bar_index, y=high, text="Giriş", style=label.style_label_down, color=color.fuchsia, textcolor=color.white)
    sellEntryPlotted := true
    lastSellBarIndex := bar_index

if not na(lastBuyPrice) and buyEntryPlotted and bar_index > lastBuyBarIndex
    if high >= long_tp
        hitLongTP := true
        lastBuyPrice := na
    else if low <= long_sl
        hitLongSL := true
        lastBuyPrice := na

if not na(lastSellPrice) and sellEntryPlotted and bar_index > lastSellBarIndex
    if low <= short_tp
        hitShortTP := true
        lastSellPrice := na
    else if high >= short_sl
        hitShortSL := true
        lastSellPrice := na

// === Strateji Girişleri ===
if buySignal
    strategy.entry("BUY", strategy.long)
if sellSignal
    strategy.entry("SELL", strategy.short)

// === SuperTrend Plot ===
upPlot = plot(trend == 1 ? upperBand : na, title="Up Trend", style=plot.style_linebr, linewidth=2, color=color.green)
dnPlot = plot(trend == -1 ? lowerBand : na, title="Down Trend", style=plot.style_linebr, linewidth=2, color=color.red)

// === TP & SL Plot ===
plotshape(hitLongTP, location=location.abovebar, style=shape.labeldown, color=color.green, text="TP", textcolor=color.white, title="TP Long")
plotshape(hitLongSL, location=location.abovebar, style=shape.labeldown, color=color.red, text="Stop", textcolor=color.white, title="SL Long")
plotshape(hitShortTP, location=location.belowbar, style=shape.labelup, color=color.green, text="TP", textcolor=color.white, title="TP Short")
plotshape(hitShortSL, location=location.belowbar, style=shape.labelup, color=color.red, text="Stop", textcolor=color.white, title="SL Short")