Simple Moving Average Envelope Dynamic Trading Strategy

SMA 移动平均线 包络线 均值回归 趋势追踪 技术分析 量化交易 止损策略 资金管理
Created on: 2025-06-12 13:24:25 Modified on: 2025-06-12 13:24:25
Copy: 0 Number of hits: 296
avatar of ianzeng123 ianzeng123
2
Follow
319
Followers

 Simple Moving Average Envelope Dynamic Trading Strategy  Simple Moving Average Envelope Dynamic Trading Strategy

Overview

The Simple Moving Average Envelope Dynamic Trading Strategy is a trading approach based on the relationship between price and the SMA200. The core concept leverages market mean-reversion characteristics by entering long positions when price drops below a specific percentage of the SMA200, and exiting when price returns to the SMA200 or reaches the upper envelope band. This strategy primarily targets large-cap stocks, utilizes a daily timeframe, sets a 14% envelope bandwidth, and offers two different exit mechanisms to accommodate various market environments and trader risk preferences.

Strategy Principle

The core principle of this strategy is based on the relationship between price and the SMA200 (200-day Simple Moving Average):

  1. Entry Conditions:

    • A buy signal is triggered when the price low falls below 85%-90% of the SMA200
    • The strategy assumes that large-cap stocks tend to revert to the mean after significant declines
  2. Exit Conditions (two optional modes):

    • Mode One (SMA200 Exit): Exit when the price high reaches or exceeds the SMA200 or 103% of the SMA200
    • Mode Two (Upper Band Exit): Exit when the price high reaches 110%-114% of the SMA200
  3. Technical Parameters:

    • Uses the 200-day Simple Moving Average as the baseline
    • Envelope bands set at 14% above and below the SMA200
    • Applicable only to daily timeframes
  4. Backtesting Time Settings:

    • The strategy allows users to define backtesting start and end dates
    • Default backtesting range is from 2000-01-01 to 2099-01-01

The execution logic is clear: boolean input parameters determine which exit strategy to use, ensuring that the two modes are not enabled simultaneously, and trading signals are generated based on whether price meets the entry and exit conditions.

Strategy Advantages

  1. Clear Entry and Exit Points: The strategy provides objective, clear entry and exit signals, reducing emotional interference from subjective judgment.

  2. Mean Reversion Principle: Effectively utilizes the market’s mean reversion characteristics, particularly suitable for relatively stable assets like large-cap stocks.

  3. Flexible Exit Mechanisms: Offers two exit strategy options, allowing traders to adjust according to their risk preferences and market conditions:

    • SMA200 exit strategy is suitable for traders seeking stable returns
    • Upper envelope band exit strategy is suitable for those looking to capture larger price movements
  4. Parameter Optimization Space: The strategy allows adjustment of entry and exit percentage levels, providing adaptability for different market environments.

  5. Customizable Backtesting Dates: Allows users to specify the time range for backtesting, facilitating strategy evaluation for specific market phases.

  6. Price Extremes Signal Triggering: Uses price highs and lows as signal trigger conditions rather than closing prices, better capturing intraday fluctuations.

Strategy Risks

  1. False Breakout Risk: Prices may temporarily break through the set thresholds and then retreat, leading to false signals. A solution could be adding confirmation indicators or setting time filters.

  2. Market Trend Shift Risk: Mean reversion strategies may underperform in strongly trending markets. It’s advisable to assess the current market environment before use or add trend filters.

  3. Parameter Sensitivity: The choice of SMA period and envelope percentage significantly impacts strategy performance. Different parameter combinations should be backtested to find optimal settings.

  4. Long-Only Limitation: The current strategy only implements long logic, potentially missing opportunities in continuously declining markets. Consider adding short logic to address various market environments.

  5. Timeframe Limitation: The strategy explicitly specifies use only on daily charts, with performance on other timeframes unverified.

  6. Lack of Risk Management Mechanism: The code does not include stop-loss settings, potentially leading to significant losses in extreme market conditions. Adding appropriate stop-loss mechanisms is recommended.

Optimization Directions

  1. Add Short Strategy: The current strategy only implements long positions; adding short logic would make the strategy adaptable to more market environments. This can be implemented by adding reverse condition triggers for short entries and exits.

  2. Incorporate Stop-Loss Mechanism: To avoid substantial losses in extreme market conditions, add ATR-based or fixed percentage stop-loss settings.

  3. Combine with Other Technical Indicators: Additional filtering conditions can be added, such as RSI, MACD, or volume indicators, to improve signal quality. For example, require RSI to be in an oversold region when price meets entry conditions.

  4. Dynamic Parameter Adjustment: Envelope width can be dynamically adjusted based on market volatility, widening the envelope range when volatility increases and narrowing it when volatility decreases.

  5. Enhanced Position Management: Implement partial position closing functionality rather than closing all positions, to optimize capital utilization and risk control.

  6. Add Time Filters: Include filtering conditions based on market sessions to avoid generating signals during unfavorable trading periods.

  7. Implement Position Sizing: Dynamically adjust the size of each trade based on volatility or risk indicators, rather than using fixed positions.

  8. Multi-Timeframe Analysis: Combine analyses from longer and shorter time periods to improve the accuracy of entry and exit timing.

Summary

The Simple Moving Average Envelope Dynamic Trading Strategy is a quantitative trading approach based on technical analysis, capitalizing on trading opportunities by leveraging the relationship between price and the long-term moving average. This strategy is particularly suitable for daily trading of large-cap stocks, entering positions when prices significantly deviate from the SMA200 and exiting when prices revert to or exceed specific levels to capture profits.

The main advantages of the strategy lie in its clear rules, objective signals, and provision of two different exit mechanisms to accommodate different trading styles. However, the strategy also has limitations such as high parameter sensitivity, lack of stop-loss mechanisms, and long-only trading restrictions.

By adding short logic, stop-loss mechanisms, additional technical indicator filtering, and dynamic parameter adjustments, this strategy has the potential to achieve more stable performance across different market environments. For quantitative traders, this provides a solid foundation framework that can be further customized and improved according to individual needs and market characteristics.

Strategy source code
/*backtest
start: 2024-06-11 00:00:00
end: 2025-06-11 00:00:00
period: 1h
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"ETH_USDT"}]
*/

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © prasathalye

//@version=6
strategy("Upper or Lower Band Env Entry and Exit", overlay=true, fill_orders_on_standard_ohlc = true)

//Get User Input for LONG  or SHORT ENVELOPE STRATEGY

//@version=6
//indicator("Input in an expression`", "", true)
//bool input_strategy = input.string("SHORT", "Strategy", options = ["SHORT", "LONG"]) == "SHORT"
//plot(plotDisplayInput ? close : na)

//string StrategySelection= input.text_area("SELECT EITHER LONG or SHORT")
bool strategyShortEnvelope=input.bool(true,"Exit at SMA200  STRATEGY")
bool strategyLongEnvelope=input.bool(false,"Exit at Upper Band  STRATEGY")

if strategyShortEnvelope== true
    strategyLongEnvelope == false
if strategyLongEnvelope == true
    strategyShortEnvelope==false

//Basis = 20 Period SMA
//Upper Envelope = 20 Period SMA + (20 Period SMA x 0.1)
//Lower Envelope = 20 Period SMA - (20 Period SMA x 0.1)



// strategy for the exit at UPPER band of Envelope
if strategyLongEnvelope == true 
    conditionForBuy_1= low <= ta.sma(close,200) * 0.86
    conditionForBuy_2= low <= ta.sma(close,200) * 0.9
    conditionForSell_1= high >= ta.sma(close,200) * 1.1
    conditionForSell_2 = high >= ta.sma(close,200) * 1.14

    if conditionForBuy_1 or conditionForBuy_2
        strategy.entry("My Long Entry Id", strategy.long)
    
    if conditionForSell_1 or conditionForSell_2
        strategy.close_all("Target Price Reached")

// strategy for the exit at LOWER band of Envelope
if strategyShortEnvelope == true 
    conditionForBuy_1= low <= ta.sma(close,200) * 0.85
    conditionForBuy_2= low <= ta.sma(close,200) * 0.9
    conditionForSell_1= high >= ta.sma(close,200) * 1.03
    conditionForSell_2 = high >= ta.sma(close,200)

    if conditionForBuy_1 or conditionForBuy_2
        strategy.entry("My Long Entry Id", strategy.long)
    
    if conditionForSell_1 or conditionForSell_2
        strategy.close_all("Target Price Reached")