DEV Community

FMZQuant
FMZQuant

Posted on

Dual EMA Crossover Strategy with Smart Risk-Reward Control

Image description

Overview
This is a trading strategy based on the crossover of 15-period and 50-period Exponential Moving Averages (EMA). The strategy implements intelligent stop-loss and take-profit levels to optimize risk-reward control. It not only captures trend reversal signals but also automatically adjusts trading parameters based on market volatility, thereby improving strategy stability and profitability.

Strategy Principle
The core logic is based on crossover signals between the fast EMA (15-period) and slow EMA (50-period). A long signal is generated when the fast line crosses above the slow line, and a short signal when the fast line crosses below. For risk management optimization, the strategy employs a dynamic stop-loss setting method, using the lowest opening price of the previous 2 candles as the long stop-loss and the highest opening price as the short stop-loss. The profit target is set at twice the risk, ensuring a favorable risk-reward ratio. The strategy uses 30% of the account equity for trading, which helps control risk exposure.

Strategy Advantages

  1. Dynamic Risk Management: The strategy automatically adjusts risk parameters based on market volatility through real-time stop-loss calculations.
  2. Optimized Risk-Reward Ratio: Setting profit targets at twice the stop-loss distance ensures reasonable profit potential for each trade.
  3. Robust Money Management: Using 30% of account equity for trading maintains a balance between profit potential and risk control.
  4. Bi-directional Trading Opportunities: The strategy captures both long and short trading opportunities, increasing trading frequency and profit potential.
  5. Visual Assistance: Stop-loss and take-profit levels are marked on the chart, allowing traders to monitor trade status intuitively.

Strategy Risks

  1. Volatile Market Risk: During sideways markets, EMA crossover signals may generate false signals leading to consecutive losses.
  2. Slippage Risk: During rapid market movements, actual execution prices may significantly deviate from intended prices.
  3. Money Management Risk: Using a fixed 30% of equity might be too aggressive under certain market conditions.
  4. Stop-Loss Setting Risk: Stop-losses based on the previous 2 candles might not be flexible enough in extreme market conditions.

Strategy Optimization Directions

  1. Implement Trend Filters: Add additional trend confirmation indicators like ADX or trend strength indicators to filter weak signals.
  2. Dynamic Position Sizing: Automatically adjust position size based on market volatility for better adaptability.
  3. Optimize Stop-Loss Method: Consider incorporating ATR indicator for stop-loss settings to better reflect market volatility characteristics.
  4. Add Time Filters: Implement trading time filters to avoid periods of high volatility or low liquidity.
  5. Include Volume Confirmation: Use volume as a confirmation indicator to improve signal reliability.

Summary
This is a well-structured EMA crossover strategy with clear logic. By combining classical technical analysis methods with modern risk management techniques, the strategy achieves favorable risk-reward characteristics. While there is room for optimization, the basic framework demonstrates good practicality and extensibility. Through the suggested optimization directions, the strategy's performance can be further enhanced.

Strategy source code

/*backtest
start: 2019-12-23 08:00:00
end: 2024-12-11 08:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=5
strategy("EMA Cross - Any Direction", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=30)

// Input for EMAs
ema_short_length = input(15, title="Short EMA Length")
ema_long_length = input(50, title="Long EMA Length")

// Calculate EMAs
ema_short = ta.ema(close, ema_short_length)
ema_long = ta.ema(close, ema_long_length)

// Plot EMAs
plot(ema_short, color=color.blue, title="15 EMA")
plot(ema_long, color=color.red, title="50 EMA")

// Entry Conditions (Any EMA Cross)
cross_condition = ta.crossover(ema_short, ema_long) or ta.crossunder(ema_short, ema_long)

// Determine Trade Direction
is_long = ta.crossover(ema_short, ema_long)
is_short = ta.crossunder(ema_short, ema_long)

// Stop Loss and Take Profit
long_stop_loss = ta.lowest(open[1], 2)  // Lowest open of the last 2 candles
short_stop_loss = ta.highest(open[1], 2) // Highest open of the last 2 candles
long_take_profit = close + 2 * (close - long_stop_loss)
short_take_profit = close - 2 * (short_stop_loss - close)

// Execute Trades
if (cross_condition)
    if (is_long)
        strategy.entry("Long", strategy.long)
        strategy.exit("Exit Long", "Long", stop=long_stop_loss, limit=long_take_profit)
    else if (is_short)
        strategy.entry("Short", strategy.short)
        strategy.exit("Exit Short", "Short", stop=short_stop_loss, limit=short_take_profit)

// Plot Stop Loss and Take Profit Levels
plot(long_stop_loss, color=color.orange, title="Long Stop Loss", style=plot.style_circles, linewidth=2)
plot(long_take_profit, color=color.green, title="Long Take Profit", style=plot.style_circles, linewidth=2)
plot(short_stop_loss, color=color.orange, title="Short Stop Loss", style=plot.style_circles, linewidth=2)
plot(short_take_profit, color=color.red, title="Short Take Profit", style=plot.style_circles, linewidth=2)
Enter fullscreen mode Exit fullscreen mode

Strategy parameters

Image description

The original address: https://www.fmz.com/strategy/474958

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

Imagine monitoring actually built for developers

Billboard image

Join Vercel, CrowdStrike, and thousands of other teams that trust Checkly to streamline monitor creation and configuration with Monitoring as Code.

Start Monitoring

đź‘‹ Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay