Dual Exponential Moving Average Crossover Strategy Optimizer
FMZQuant

FMZQuant @fmzquant

Joined:
Apr 25, 2024

Dual Exponential Moving Average Crossover Strategy Optimizer

Publish Date: Apr 29
1 1

Image description

Image description

Overview
The Dual Exponential Moving Average Crossover Strategy Optimizer is a quantitative trading strategy based on crossover signals between two exponential moving averages with different periods. This strategy uses the crossover relationship between a fast EMA and a slow EMA to determine market trend direction and executes both long and short trades when specific conditions are met. The core of the strategy lies in parameterized EMA settings, allowing users to flexibly adjust strategy parameters according to different market environments, while also maximizing returns with profit target functionality. The strategy also supports a complete backtest date selection function, contributing to more accurate historical performance evaluation.

Strategy Principles
The core principle of this strategy is based on the classic moving average crossover theory in technical analysis, mainly including the following key components:

  1. Dual EMA Crossover Signals: The strategy uses two exponential moving averages (EMAs) with different periods, specifically a fast EMA with a default parameter of 6 and a slow EMA with a default parameter of 16. When the fast EMA crosses above the slow EMA, a long signal is generated; when the fast EMA crosses below the slow EMA, a short signal is generated.

  2. Direction Filtering: The strategy allows users to choose trading direction (long, short, or both) through input parameters, increasing strategy flexibility. The system controls whether to execute corresponding directional trades through the longOK and shortOK variables.

  3. Candlestick Pattern Confirmation: The strategy introduces an additional price confirmation mechanism, requiring that when a long signal appears, the current candle's closing price must be higher than the opening price (bullish candle); when a short signal appears, the current candle's closing price must be lower than the opening price (bearish candle). This design effectively filters out some false signals.

  4. Profit Target Mechanism: The strategy sets profit percentage targets for both long and short positions (default is 4% for both), automatically closing positions when prices reach the preset profit targets, locking in profits.

  5. Crossover Reversal Exit: When a short signal occurs while holding a long position, or a long signal occurs while holding a short position, the strategy triggers an exit operation, effectively controlling loss expansion.

Strategy Advantages
Deep analysis of the strategy code reveals the following advantages:

  1. Parameter Flexibility: The strategy allows users to customize fast and slow EMA periods, trading direction, and profit target percentages, enabling the strategy to adapt to different market environments and personal risk preferences.

  2. Dual Confirmation Mechanism: The strategy not only relies on EMA crossover signals but also combines candlestick patterns (bullish/bearish) as additional confirmation, improving signal reliability and reducing losses from false breakouts.

  3. Comprehensive Trading: Supports both long and short trading, capable of capturing opportunities in different market trends, not limited to single-direction market conditions.

  4. Profit Optimization: Through preset profit targets, the strategy can automatically lock in profits when prices reach expected targets, avoiding profit giveback due to market reversals.

  5. Reversal Signal Exit: When the market trend may reverse (indicated by opposite crossover signals), the strategy exits positions promptly, effectively controlling risk.

  6. Calculation Efficiency: The strategy uses built-in ta.ema, ta.crossover, and ta.crossunder functions to calculate signals, providing high computational efficiency for real-time execution.

  7. Visualization Support: The strategy plots fast and slow EMA lines, as well as profit target levels on the chart, allowing users to intuitively understand strategy execution.

Strategy Risks
Despite the reasonable design, the strategy still has the following potential risks:

  1. Moving Average Lag: EMAs are inherently lagging indicators, which may produce delayed signals in rapidly changing markets, leading to suboptimal entry and exit timing.

  2. Range-Bound Market Risk: In range-bound markets, EMA crossover signals appear frequently but lack sustainability, potentially leading to frequent trading and consecutive losses.

  3. Lack of Stop-Loss Mechanism: The current strategy only sets profit targets without a clear stop-loss mechanism, which may face significant losses under extreme market conditions.

  4. Candlestick Confirmation Limitation: Requiring candlestick pattern confirmation may cause missing some valid signals, especially during rapid trend changes.

  5. Fixed Profit Target Risk: Preset fixed profit percentages may not be suitable for all market environments; in strong trending markets, they might result in early profit-taking, missing larger gains.

  6. Lack of Volatility Adaptation Mechanism: The strategy does not have functionality to dynamically adjust parameters based on market volatility, potentially performing poorly in high or low volatility environments.

Strategy Optimization Directions
Addressing the above risks, the strategy can be optimized in the following directions:

  1. Introduce Adaptive Parameters: Parameters can be dynamically adjusted based on ATR (Average True Range) or historical volatility, enabling the strategy to better adapt to different market volatility environments. This is beneficial because fixed parameters perform differently across markets with varying volatility.

  2. Add Stop-Loss Mechanism: It is advisable to introduce a stop-loss mechanism based on ATR or fixed percentage, automatically exiting positions when prices move significantly against the trade, effectively controlling per-trade losses.

  3. Add Trend Filter: A longer-period trend determination indicator (such as 50-day EMA) can be added to execute trades only in the direction of the main trend, avoiding frequent trading in range-bound markets.

  4. Optimize Entry Timing: Other technical indicators like RSI, MACD can be incorporated as auxiliary confirmation to improve signal quality.

  5. Dynamic Profit Targets: Implement dynamic profit targets based on market volatility, or adopt trailing stop mechanisms, allowing profits to grow while protecting gains.

  6. Add Volume Filter: Consider volume factors when generating signals, executing trades only when supported by volume, improving signal reliability.

  7. Time Filter: Add trading time window settings to avoid trading during periods of low or irregular market volatility.

  8. Money Management Optimization: Introduce dynamic position sizing mechanisms, adjusting the proportion of funds for each trade based on signal strength, market volatility, and historical win rate.

Summary
The Dual Exponential Moving Average Crossover Strategy Optimizer is a well-designed quantitative trading system that achieves bidirectional trading functionality through the crossover relationship between fast and slow EMAs, combined with candlestick pattern confirmation and profit target mechanisms. The strategy's strengths lie in parameter flexibility, dual confirmation mechanisms, and comprehensive trading capabilities, but it also faces issues such as moving average lag, range-bound market risk, and lack of stop-loss mechanisms.

Through improvements in adaptive parameters, adding stop-loss mechanisms, incorporating trend filters, and optimizing money management, the strategy's stability and profitability can be significantly enhanced. Particularly, combining dynamic parameter adjustment with risk management mechanisms can maintain relatively stable performance across different market environments.

For traders applying this strategy in practice, it is recommended to combine macro market analysis, select market environments with clear trends, and conduct thorough historical backtesting and parameter optimization to find the optimal parameter combinations for specific trading instruments. Additionally, continuously monitoring strategy performance and timely adjusting parameters according to market changes are key to maintaining the strategy's long-term effectiveness.

Strategy source code

/*backtest
start: 2025-01-01 00:00:00
end: 2025-03-31 00:00:00
period: 1h
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"SOL_USDT"}]
*/

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// This strategy has been created for illustration purposes only and should not be relied upon as a basis for buying, selling, or holding any asset or security.
// © kirilov

//@version=6
strategy(
     "gosho bot Strategy",
     overlay=true,
     calc_on_every_tick=true,
     currency=currency.USD
     )

// INPUT:

// Options to enter fast and slow Exponential Moving Average (EMA) values
emaFast = input.int(title="Fast EMA",  defval=6, minval=1, maxval=9999)
emaSlow = input.int(title="Slow EMA",  defval=16, minval=1, maxval=9999)

// Option to select trade directions
tradeDirection = input.string(title="Trade Direction", defval="Both", options=["Long", "Short", "Both"])




// CALCULATIONS:

// Use the built-in function to calculate two EMA lines
fastEMA = ta.ema(close, emaFast)
slowEMA = ta.ema(close, emaSlow)


// PLOT:

// Draw the EMA lines on the chart
plot(series=fastEMA, color=color.orange, linewidth=2)
plot(series=slowEMA, color=color.blue, linewidth=2)
percentageDiff = (fastEMA - slowEMA) / slowEMA * 100







// Translate input into trading conditions
longOK  = (tradeDirection == "Long") or (tradeDirection == "Both")
shortOK = (tradeDirection == "Short") or (tradeDirection == "Both")

// Decide if we should go long or short using the built-in functions
longCondition = ta.crossover(fastEMA, slowEMA)
shortCondition = ta.crossunder(fastEMA, slowEMA)


profit_long = input.float(4, "Profit_long %", minval=0.0, step=0.1) * 0.01
profit_short = input.float(4, "Profit_short %", minval=0.0, step=0.1) * 0.01
short_stop_profit = strategy.position_avg_price * (1 - profit_short)
long_stop_profit = strategy.position_avg_price * (1 + profit_long)






// ORDERS:

// Submit entry (or reverse) orders
if (longCondition and close > open )
    strategy.entry(" Long ", strategy.long)
if (shortCondition and close < open )
    strategy.entry(" Short ", strategy.short)

// Submit exit orders in the cases where we trade only long or only short
if (strategy.position_size > 0 and shortCondition   )
    strategy.exit(id="exit long", stop=close)
if (strategy.position_size < 0 and longCondition         )
    strategy.exit(id="exit short", stop=close)


plot(short_stop_profit)
Enter fullscreen mode Exit fullscreen mode

Strategy parameters

Image description

The original address: Dual Exponential Moving Average Crossover Strategy Optimizer

Comments 1 total

  • Raxrb Kuech
    Raxrb KuechJul 15, 2025

    Ah yes, the good old dual EMA crossover—now with an optimizer upgrade! It’s like taking a classic car and giving it a turbo engine. 🚗💨 Love how this keeps things simple but still packs some serious punch. Now if only it could also optimize my life decisions... 😅 Great work, this one’s definitely chart-worthy!

Add comment