Multi-Temporal Trend Identification Adaptive Position Management Strategy

FMZQuant - Feb 26 - - Dev Community

Image description

Image description

Overview
This strategy is a trend-following trading system based on Supertrend and Double Exponential Moving Average (DEMA). It combines Supertrend's trend direction identification capability with DEMA's trend confirmation function to build a reliable trading decision framework. The system supports bilateral trading and features a dynamic position adjustment mechanism that can flexibly switch between long and short positions according to market conditions.

Strategy Principles
The core logic of the strategy is based on the following key components:

  1. Supertrend indicator: Uses ATR period of 10 and factor value of 3.0 to capture price trend turning points.
  2. DEMA indicator: Employs a 100-period double exponential moving average to filter market noise and confirm trend reliability.
  3. Trading signal generation mechanism:
  • Long signal: Triggered when price crosses above Supertrend and closing price is above DEMA

  • Short signal: Triggered when price crosses below Supertrend and closing price is below DEMA

  1. Position management: System supports flexible position adjustment, including direct opening, reversal, and closing operations.

Strategy Advantages

  1. Multiple confirmation mechanism: Combining Supertrend and DEMA indicators significantly improves trading signal reliability.
  2. Flexible position management: Supports bilateral trading and can dynamically adjust position direction based on market conditions.
  3. Comprehensive risk control: Features rapid response mechanism at trend turning points, enabling timely stop-loss and capture of new trend opportunities.
  4. Strong parameter adaptability: Key parameters such as ATR period, Supertrend factor, and DEMA period can be optimized according to different market characteristics.

Strategy Risks

  1. Poor performance in sideways markets: May generate frequent false breakout signals in markets without clear trends.
  2. Lag risk: Using DEMA as a filter may cause slight delays in entry timing, affecting some profit potential.
  3. Parameter sensitivity: Strategy effectiveness is sensitive to parameter settings, different market environments may require different parameter combinations.

Strategy Optimization Directions

  1. Introduce volatility adaptive mechanism:
    Dynamically adjust Supertrend factor values based on market volatility
    Increase filtering threshold during high volatility periods and appropriately relax conditions during low volatility periods

  2. Add market environment recognition module:
    Add trend strength indicators to reduce trading frequency in sideways markets
    Introduce volume indicators to assist in confirming breakout validity

  3. Improve stop-loss mechanism:
    Implement ATR-based dynamic stop-loss
    Add trailing stop-loss function to protect profits

Summary
The strategy builds a robust trend-following system by cleverly combining Supertrend and DEMA indicators. Its advantages lie in high signal reliability and comprehensive risk control, but traders still need to optimize parameters based on specific market characteristics. Through the proposed optimization directions, the strategy's adaptability and stability can be further enhanced.

Strategy source code

/*backtest
start: 2025-02-16 00:00:00
end: 2025-02-23 00:00:00
period: 5m
basePeriod: 5m
exchanges: [{"eid":"Binance","currency":"SOL_USDT"}]
*/

//@version=6
strategy("Supertrend with DEMA Strategy (Reversal Enabled)", overlay=true)

// ===== Parameters for Supertrend =====
atrPeriod = input.int(10, "ATR Length", minval=1)
factor    = input.float(3.0, "Factor", minval=0.01, step=0.01)

// ===== Parameters for resolving trade directions =====
allowLong  = input.bool(true,  "Разрешить LONG")
allowShort = input.bool(true,  "Разрешить SHORT")

// Supertrend Calculation
[supertrend, direction] = ta.supertrend(factor, atrPeriod)
// For the first bar, we set the value na to avoid false signals
supertrend := barstate.isfirst ? na : supertrend

// Supertrend Lines Display
plot(direction < 0 ? supertrend : na, "Up Trend",   color=color.green, style=plot.style_linebr)
plot(direction < 0 ? na : supertrend, "Down Trend", color=color.red,   style=plot.style_linebr)

// ===== Parameters and calculation of DEMA =====
demaLength = input.int(100, "DEMA Length", minval=1)
e1 = ta.ema(close, demaLength)
e2 = ta.ema(e1, demaLength)
dema = 2 * e1 - e2

// DEMA display
plot(dema, "DEMA", color=#43A047)

// ===== Signal Definition =====
// Basic Trend Change Signals by Supertrend
trendUp   = ta.crossover(close, supertrend)
trendDown = ta.crossunder(close, supertrend)

// Entry signals considering DEMA
longSignal  = trendUp and (close > dema)
shortSignal = trendDown and (close < dema)

// ===== Position Entry/Reversal Logic =====

// LONG signal
if (longSignal)
    // If a SHORT position is open, we turn it into LONG, if allowed
    if (strategy.position_size < 0)
        if (allowLong)
            strategy.close("Short")
            strategy.entry("Long", strategy.long)
        else
            // If reversal in LONG is not allowed – just close SHORT
            strategy.close("Short")
    // If there is no position, we open LONG, if allowed
    else if (strategy.position_size == 0)
        if (allowLong)
            strategy.entry("Long", strategy.long)

// SHORT signal
if (shortSignal)
    // If a LONG position is open, we turn it into a SHORT, if allowed
    if (strategy.position_size > 0)
        if (allowShort)
            strategy.close("Long")
            strategy.entry("Short", strategy.short)
        else
            // If reversal in SHORT is not allowed – just close LONG
            strategy.close("Long")
    // If there is no position, we open SHORT, if allowed
    else if (strategy.position_size == 0)
        if (allowShort)
            strategy.entry("Short", strategy.short)

// ===== Additional closing of a position when the trend changes without entry =====
// If a Supertrend crossover occurs (trend change), but the DEMA conditions are not met,
// then, if the opposite position is open, we simply close it.
if (trendUp and not longSignal and strategy.position_size < 0)
    strategy.close("Short")

if (trendDown and not shortSignal and strategy.position_size > 0)
    strategy.close("Long")
Enter fullscreen mode Exit fullscreen mode

Strategy Parameters

Image description

The original address: Multi-Temporal Trend Identification Adaptive Position Management Strategy

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .