双移动平均线反转策略(Dual Moving Average Reversal Strategy)是一个利用双移动平均线来识别短期和长期趋势的量化交易策略。该策略结合10日简单移动平均线和200日简单移动平均线,在长期看涨的大趋势下,捕捉短期回调买入机会。同时,该策略还具有一定的趋势跟踪和损失控制机制。
The Dual Moving Average Reversal Strategy is a quantitative trading strategy that utilizes dual moving averages to identify short-term and long-term trends. The strategy combines the 10-day simple moving average (SMA) and the 200-day SMA to capitalize on short-term pullbacks within an underlying long-term uptrend. It also features trend-following and risk management mechanisms.
## Strategy Logic
The Dual Moving Average Reversal Strategy is based on the following assumptions:
1. The 200-day SMA identifies the prevailing long-term trend of the market. When the price is above the 200-day line, it signals that the market is in a long-term uptrend.
2. The 10-day SMA pinpoints short-term pullbacks in price. When the price falls below the 10-day line, it indicates a temporary pullback has occurred.
3. In an ongoing bull market uptrend, any short-term pullback can be viewed as a buying opportunity to efficiently catch the upside rebound.
Based on the above assumptions, the trade signals are generated as follows:
1. When the closing price crosses above the 200-day SMA and simultaneously crosses below the 10-day SMA, it triggers a buy signal as it shows the long-term trend remains positive but a short-term pullback has occurred.
2. If the price recrosses above the 10-day SMA when in a long position, the short-term trend has reversed so the position will be closed immediately. Also, if the market falls substantially leading to a stop loss breach, the position will close.
3. Whenever there is a major downturn (exceeding a predefined threshold), it presents an opportunity to buy the dip as a contrarian signal.
With this design, the strategy aims to efficiently capitalize on upside snapbacks during sustained uptrends while controlling risk using stop losses.
## Advantages
The Dual Moving Average Reversal Strategy has these key advantages:
1. The strategy logic is straightforward and easily understandable.
2. The dual moving average filters effectively identify short and long-term trends.
3. It offers good time efficiency by capitalizing on short-term reversals.
4. The built-in stop loss mechanism tightly controls risk on individual positions.
5. Flexible parametrization makes this strategy widely applicable for indexes and stocks.
## Risks
While being generally effective, the strategy has these limitations:
1. Whipsaws and false signals may occur if the market is range-bound. The strategy should be deactivated during extended consolidations.
2. Reliance solely on moving averages has signal accuracy limitations. More indicators could augment performance.
3. The fixed stop loss methodology lacks flexibility. Other stop loss techniques could be tested.
4. Optimal parameters need to be calibrated for different markets. Suboptimal settings reduce reliability.
## Enhancement Opportunities
Further improvements for this strategy include:
1. Testing other moving average lengths to find the optimal combination.
2. Adding supporting indicators to generate more reliable signals e.g. volume, volatility metrics.
3. Exploring other stop loss techniques like trailing stop loss, time-based stop loss.
4. Building adaptive capabilities into entry rules and stop loss parameters enabling adjustment to changing market dynamics.
5. Incorporating machine learning algorithms to further optimize parameters leveraging more historical data.
## Conclusion
In summary, the Dual Moving Average Reversal Strategy is a highly practical approach. It enables profitable pullback fading during sustained uptrends using moving average analysis paired with stop losses. It also offers market regime detection capabilities and risk control. With continual enhancement, the strategy offers strong potential to deliver differentiated performance.
i_stopPercent = input.float(title="Stop Loss Percent", defval=0.15, step=0.01, group="Strategy Parameters", tooltip="Hard stop loss of 10%")
i_startTime = input(title="Start filter", defval=timestamp("01 Jan 2013 13:30 +0000"), group="Time filter", tooltip="Start date and time to begin")
i_endTime = input(title="End filter", defval=timestamp("01 Jan 2099 19:30 +0000"), group="Time filter", tooltip="End date and time to stop")
i_lowerClose = input.bool(title="Exit on lower close", defval=true, group="Strategy Parameters", tooltip="Wait for lower close after above 10SMA before exiting") // optimise exit strat, boolean type creates tickbox type inputs
i_contrarianBuyTheDip = input.bool(title="Buy whenever more than x% drawdown", defval=true, group="Strategy Parameters", tooltip="Buy the dip! Whenever x% or more drawdown on SPY")
i_contrarianTrigger = input.int(title="Trigger % drop to buy the dip", defval=14, step=1, group="Strategy Parameters", tooltip="% drop to trigger contrarian Buy the Dip!")
//14% to be best for SPY 1D
//20% best for AMZN 1D
i_stopByCrossover_MA2_3 = input.bool(title="Include stop condition using MA crossover", defval=false, group="Strategy Parameters", tooltip="Sell when crossover of MA2/1 happens")
// Get indicator values
ma1 = ta.sma(close,i_ma1) //param 1
ma2 = ta.sma(close,i_ma2) //param 2
ma3 = ta.sma(close,i_ma3) //param 3
ma_9 = ta.ema(close,9) //param 2
ma_20 = ta.ema(close,20) //param 3
// Check filter(s)
f_dateFilter = true //make sure date entries are within acceptable range
// Highest price of the prev 52 days: https://www.tradingcode.net/tradingview/largest-maximum-value/#:~:text=()%20versus%20ta.-,highest(),max()%20and%20ta.
var float buyPrice = 0 //intialise buyPrice, this will change when we enter a trade ; float = decimal number data type 0.0
buyCondition = (close > ma1 and close < ma2 and strategy.position_size == 0 and f_dateFilter) or (strategy.position_size == 0 and i_contrarianBuyTheDip==true and overall_change > i_contrarianTrigger and f_dateFilter) // higher than 200sma, lower than short term ma (pullback) + avoid pyramiding positions
sellCondition = close > ma2 and strategy.position_size > 0 and (not i_lowerClose or close < low[1]) //check if we already in trade + close above 10MA;
// third condition: EITHER i_lowerClose not turned on OR closing price has to be < previous candle's LOW [1]
stopDistance = strategy.position_size > 0 ? ((buyPrice - close)/close) : na // check if in trade > calc % drop dist from entry, if not na
stopPrice = strategy.position_size > 0 ? (buyPrice - (buyPrice * i_stopPercent)) : na // calc SL price if in trade, if not, na
stopCondition = (strategy.position_size > 0 and stopDistance > i_stopPercent) or (strategy.position_size > 0 and (i_stopByCrossover_MA2_3==true and ma3 < ma1))
// Enter positions
if buyCondition
strategy.entry(id="Long", direction=strategy.long) //long only
if buyCondition[1] // if buyCondition is true prev candle
buyPrice := open // entry price = current bar opening price
// Exit position
if sellCondition or stopCondition
strategy.close(id="Long", comment = "Exit" + (stopCondition ? "Stop loss=true" : "")) // if condition? "Value for true" : "value for false"