
This strategy is a trend following system based on the crossover of dual moving averages, utilizing the intersection of short-term and long-term simple moving averages (SMA) to generate clear long and short trading signals. The design is concise and easy to understand and implement, particularly suitable for traders seeking to master the basic principles of moving average crossovers. The core idea is that when the short-term moving average crosses above the long-term moving average, the system generates a long signal; when the short-term moving average crosses below the long-term moving average, the system generates a short signal. This trading method automatically reverses positions at the closing price when signals appear, ensuring traders can adjust market direction in a timely manner.
The core of the strategy is based on the interaction of two simple moving averages (SMA): 1. Short-term moving average: Default setting is 9 periods, reflecting more recent price movements 2. Long-term moving average: Default setting is 21 periods, reflecting longer-term price trends
Trade signal generation logic: - Long condition: When the short-term MA crosses above the long-term MA (ta.crossover function), the system generates a long signal - Short condition: When the short-term MA crosses below the long-term MA (ta.crossunder function), the system generates a short signal
Trading execution process: - When a long signal is triggered, the system first immediately closes any existing short positions, then opens a new long position - When a short signal is triggered, the system first immediately closes any existing long positions, then opens a new short position - The system clearly labels entry prices on the chart, with long labels displayed above the candlesticks and short labels below
The strategy also allows users to customize the price source (default is opening price) and moving average period lengths to adapt to different market environments or trading styles.
Through in-depth analysis of the strategy code, we can summarize the following distinct advantages:
Despite the strategy’s concise and effective design, there are still the following potential risks:
Frequent trading in oscillating markets: In sideways consolidation or oscillating markets, short-term and long-term moving averages may frequently cross, leading to excessive trading signals and unnecessary trading costs
Lag issues: Moving averages are inherently lagging indicators, signals may be generated only after trends have developed or are about to end
False breakout risk: Prices may briefly cross the moving average and then return to the original trend, causing false signals
Lack of stop-loss mechanism: The current strategy has no explicit stop-loss settings, which may lead to significant losses in strong reversal markets
Parameter sensitivity: Strategy performance is relatively sensitive to the choice of moving average period lengths, inappropriate parameters may cause significant changes in strategy effectiveness
Based on an in-depth analysis of the code, I propose the following optimization directions:
Add trend filters: Introduce ADX, trend strength indicators, or relative position judgments of price and moving averages, generating signals only in confirmed trend environments to avoid frequent trading in oscillating markets
Implement dynamic stop-loss mechanisms: Set dynamic stop-loss levels based on ATR or other volatility indicators to protect profits and limit maximum risk per trade
Optimize entry timing: Consider using smaller timeframes for confirmation after signal generation or waiting for pullbacks before entering, to obtain better execution prices
Add volume filtering: Add volume confirmation on top of crossover signals, executing trades only when volume also supports directional changes
Implement adaptive moving average periods: Automatically adjust moving average period lengths based on market volatility, using longer periods in high-volatility environments and shorter periods in low-volatility environments
Add phased position building and closing mechanisms: Instead of establishing full positions at once, build and close positions in steps to reduce timing risk
The Dual Moving Average Crossover Trend Following Strategy is a concise yet powerful quantitative trading system that generates clear trading signals through the crossover of short-term and long-term moving averages. Its main advantages lie in simple operation, visual intuitiveness, and automatic reversal mechanisms, allowing traders to objectively follow market trends. However, the strategy also has inherent risks such as frequent trading in oscillating markets and signal lag.
This basic strategy can be significantly enhanced by adding trend filters, implementing dynamic stop-loss mechanisms, optimizing entry timing, and adding volume confirmation. Particularly, combining other technical indicators to filter signals and optimize risk management will help improve strategy performance across various market environments.
For beginners hoping to start quantitative trading, this is an ideal starting point; for experienced traders, it provides a solid foundation that can be further customized and optimized. Importantly, whatever improvements are adopted should be evaluated through rigorous backtesting and forward validation to ensure strategy improvements truly add long-term value.
/*backtest
start: 2025-01-01 00:00:00
end: 2025-03-24 00:00:00
period: 2d
basePeriod: 2d
exchanges: [{"eid":"Futures_Binance","currency":"ETH_USDT"}]
*/
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//@version=6
//
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// @author = Da_mENIZ
// © denis_zvegelj
// last change 20.Mar.2025
//
// Simple MA Crossover strategy that shows on the chart with Long/Short indicators. Feel free to use it to suit
// your needs
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
strategy("DZ Simple MA Crossover Strategy", shorttitle="DZ_MACross", overlay=true, calc_on_every_tick=true)
// Define the moving average lengths
i_src_price = input.source (open, "Price source", group="Main Settings")
i_shMA_len = input.int (9, "Short MA Length", minval=1, group="Main Settings")
i_loMA_len = input.int (21, "Long MA Length", minval=6, group="Main Settings")
// Calculate the moving averages
short_MA = ta.sma(i_src_price, i_shMA_len)
long_MA = ta.sma(i_src_price, i_loMA_len)
// Plot the moving averages on the chart
plot(short_MA, color=color.red, linewidth=2, title="Short MA")
plot(long_MA, color=color.blue, linewidth=2, title="Long MA")
// Generate the buy and sell signals
long_Cond = ta.crossover(short_MA, long_MA)
short_Cond = ta.crossunder(short_MA, long_MA)
// Place the orders based on conditions
if (long_Cond)
strategy.close("Short", immediately = true, comment = "Close")
strategy.entry("Long", strategy.long, comment = "Enter")
label.new(bar_index+1, open, "Long\n" + str.tostring(open), style=label.style_label_down, color=color.blue, textcolor=color.white, yloc=yloc.abovebar)
if (short_Cond)
strategy.close("Long", immediately = true, comment = "Close")
// strategy.entry("Short", strategy.short, comment = "Short\n" + str.tostring(open))
strategy.entry("Short", strategy.short, comment = "Enter")
label.new(bar_index+1, open, "Short\n" + str.tostring(open), style=label.style_label_up, color=color.red, textcolor=color.white, yloc=yloc.belowbar)