Triple EMA Crossover Trading System with Smart R2R-based Stop Loss Management

FMZQuant - Jan 24 - - Dev Community

Image description

Overview
This is a trend-following trading system based on triple exponential moving average (EMA) crossover signals. The system combines the three moving averages of EMA8, EMA21 and EMA89, generates trading signals through moving average crossovers, and integrates an intelligent moving stop loss function based on the risk-return ratio to achieve automated risk management.

Strategy Principle
The system mainly includes the following core functional modules:

  1. Signal generation module: Use the crossover of fast EMA8 and medium EMA21 to determine the trading direction, and require the price to be above or below the slow EMA89 to confirm the general trend
  2. Trading execution module: automatically open a position when the long or short conditions are met, and set the initial stop loss and target position
  3. Risk management module: When the price movement reaches a 1:1 risk-return ratio, the stop loss is automatically moved to the cost position to lock in risk-free returns
  4. Visualization module: draw three moving averages, entry points and trailing stop loss markers on the chart

Strategy Advantages

  1. Multiple time frame verification: Confirm the trend through three moving averages of different periods to improve the reliability of transactions
  2. Intelligent risk management: a moving stop loss mechanism based on risk-return ratio, protecting profits while reducing drawdowns
  3. Highly automated: The entire process from signal generation to position management is automatically executed, reducing human intervention
  4. Parameters can be adjusted: key parameters such as moving average period, stop loss ratio, etc. can be optimized according to different market characteristics

Strategy Risks

  1. Risk of volatile market: Frequent false breakout signals may occur in a sideways market.
  2. Slippage risk: There may be slippage when executing a moving stop loss in a fast market.
  3. Systemic risk: Sudden and large market fluctuations may cause stop loss to fail

Solution:

  • Add trend filter to identify volatile markets
  • Set a reasonable stop loss buffer
  • Introducing a volatility adaptive mechanism

Strategy Optimization Direction

  1. Introducing volume indicators: Add volume confirmation based on moving average crossover signals to improve signal quality
  2. Develop dynamic stop loss: dynamically adjust the stop loss distance according to market volatility to improve strategy adaptability
  3. Optimize the trailing stop mechanism: use trailing stop after reaching the target profit ratio to obtain more potential profits
  4. Add market environment filtering: design trend strength indicators and adjust strategy parameters in different market environments

Summary
This strategy realizes a complete trend-following trading system by combining the classic moving average crossover system with modern risk management methods. The advantages of the system lie in its reliable signal generation mechanism and intelligent risk control method, but in actual application, parameter optimization and function expansion are still required according to specific market characteristics. Through continuous improvement and optimization, this strategy is expected to maintain stable performance in various market environments.

Strategy source code

/*backtest
start: 2024-12-06 00:00:00
end: 2025-01-04 08:00:00
period: 4h
basePeriod: 4h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=5
strategy("EMA Crossover with SL to BE", shorttitle="OmegaGalsky", overlay=true)

// Input parameters
ema8_period = input.int(8, title="EMA 8 Period")
ema21_period = input.int(21, title="EMA 21 Period")
ema89_period = input.int(89, title="EMA 89 Period")
fixed_risk_reward = input.float(1.0, title="Risk/Reward Ratio (R2R)")
sl_percentage = input.float(0.001, title="Stop Loss Percentage", step=0.0001)
tp_percentage = input.float(0.0025, title="Take Profit Percentage", step=0.0001)

// Calculating the EMA
ema8 = ta.ema(close, ema8_period)
ema21 = ta.ema(close, ema21_period)
ema89 = ta.ema(close, ema89_period)

// BUY conditions
buy_condition = ta.crossover(ema8, ema21) and close > ema89 and close > open

// Условия за SELL
sell_condition = ta.crossunder(ema8, ema21) and close < ema89 and close < open

// Entering a BUY position
if (buy_condition)
    stop_loss = close * (1 - sl_percentage)
    take_profit = close * (1 + tp_percentage)
    strategy.entry("BUY", strategy.long)
    strategy.exit("TP/SL", from_entry="BUY", stop=stop_loss, limit=take_profit)

// Entering a SELL position
if (sell_condition)
    stop_loss = close * (1 + sl_percentage)
    take_profit = close * (1 - tp_percentage)
    strategy.entry("SELL", strategy.short)
    strategy.exit("TP/SL", from_entry="SELL", stop=stop_loss, limit=take_profit)

// Logic for moving stop to BE
if (strategy.position_size > 0)
    entry_price = strategy.position_avg_price
    // For LONG position
    if (strategy.position_size > 0 and high  >= entry_price + (entry_price * sl_percentage * fixed_risk_reward))
        strategy.exit("SL to BE", from_entry="BUY", stop=entry_price)
        label.new(bar_index, high, "SL moved to BE", color=color.green)
    // For SHORT position
    if (strategy.position_size < 0 and low <= entry_price - (entry_price * sl_percentage * fixed_risk_reward))
        strategy.exit("SL to BE", from_entry="SELL", stop=entry_price)
        label.new(bar_index, low, "SL moved to BE", color=color.red)

// EMA drawing
plot(ema8, color=color.orange, title="EMA 8")
plot(ema21, color=color.blue, title="EMA 21")
plot(ema89, color=color.purple, title="EMA 89")
Enter fullscreen mode Exit fullscreen mode

Strategy Parameters

Image description

The original address: Triple EMA Crossover Trading System with Smart R2R-based Stop Loss Management

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .