This strategy is a dynamic trend following system based on dual moving average channels, combined with risk management mechanisms. It utilizes two Simple Moving Averages (SMA) to construct a trading channel, with the upper band calculated using the high price and the lower band using the low price. The system generates entry signals when the closing price remains above the upper band for five consecutive bars, and exit signals when either the price falls below the lower band for five consecutive bars or retraces 25% from the highest point, achieving dynamic trend tracking and risk control.
The core principles involve capturing price trends through dual moving average channels and establishing strict entry and exit mechanisms: 1. Entry Mechanism: Requires price to maintain above the upper band for five consecutive days, ensuring trend continuity and validity 2. Exit Mechanism: Operates on two levels - Trend Deviation Exit: Triggered when price falls below the lower band for five consecutive days, indicating potential trend reversal - Stop-Loss Exit: Activated when price retraces 25% from the highest point, preventing excessive losses 3. Position Management: Uses a fixed percentage of account equity for position sizing, ensuring effective capital allocation
This strategy constructs a complete trend following trading system through dual moving average channels, combining strict entry confirmation and dual exit mechanisms to achieve effective trend tracking and risk control. The strategy’s strengths lie in its clear execution logic and comprehensive risk control, though it requires parameter optimization for different market environments and can be further improved through market environment filtering and multiple timeframe confirmation. Overall, it represents a structurally complete and logically rigorous quantitative trading strategy, suitable for application in markets with clear trends.
/*backtest start: 2025-01-02 00:00:00 end: 2025-01-09 00:00:00 period: 10m basePeriod: 10m exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT","balance":49999}] */ //@version=5 strategy("Moving Average Channel (MAC)", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100) // Parameters for Moving Averages upperMALength = input.int(10, title="Upper MA Length") lowerMALength = input.int(8, title="Lower MA Length") stopLossPercent = input.float(25.0, title="Stop Loss (%)", minval=0.1) / 100 // Calculate Moving Averages upperMA = ta.sma(high, upperMALength) lowerMA = ta.sma(low, lowerMALength) // Plot Moving Averages plot(upperMA, color=color.red, title="Upper Moving Average") plot(lowerMA, color=color.green, title="Lower Moving Average") // Initialize variables var int upperCounter = 0 var int lowerCounter = 0 var float entryPrice = na var float highestPrice = na // Update counters based on conditions if (low <= upperMA) upperCounter := 0 else upperCounter += 1 if (high >= lowerMA) lowerCounter := 0 else lowerCounter += 1 // Entry condition: 5 consecutive bars above the Upper MA if (upperCounter == 5 and strategy.position_size == 0) strategy.entry("Long", strategy.long) highestPrice := high // Initialize highest price // Update the highest price after entry if (strategy.position_size > 0) highestPrice := na(highestPrice) ? high : math.max(highestPrice, high) // Exit condition: 5 consecutive bars below the Lower MA if (lowerCounter == 5 and strategy.position_size > 0) strategy.close("Long", comment="Exit: 5 bars below Lower MA") // Stop-loss condition: Exit if market closes below 25% of the highest price since entry stopLossCondition = low < highestPrice * (1 - stopLossPercent) if (stopLossCondition and strategy.position_size > 0) strategy.close("Long", comment="Exit: Stop Loss")