0bps

Strategy vault / Trend following

5EMA Trend Following Strategy with Dynamic Stop Loss and Take Profit

This article introduces a trend-following strategy based on the 5-period Exponential Moving Average (5EMA). The strategy is designed to identify short-term trend reversal opportunities and manage risk through dynamic…

Author
ChaoZhang
Complexity
Starter · 48 lines
Parameters
0
Backtest bar
2h · intraday
Tested on
BTC/USDT · futures

IMG

Overview

This article introduces a trend-following strategy based on the 5-period Exponential Moving Average (5EMA). The strategy is designed to identify short-term trend reversal opportunities and manage risk through dynamic stop-loss and take-profit levels. The core idea is to enter short positions when the price breaks below the 5EMA and set corresponding stop-loss and profit targets based on the entry point. This approach aims to capture short-term downward market trends while protecting trading capital through strict risk management.

Strategy Principles

  1. Indicator Setup: The strategy uses a 5-period Exponential Moving Average (5EMA) as the primary technical indicator.

  2. Entry Signals:

    • Alert Candle: A candle is marked as an alert candle when its low is completely above the 5EMA line.
    • Entry Condition: A short entry signal is triggered if the low of the next candle is lower than or equal to the low of the alert candle.
  3. Trade Execution:

    • Entry Price: The low of the alert candle serves as the entry price.
    • Stop-Loss: Set at the high of the alert candle.
    • Take-Profit: Uses a 1:3 risk-reward ratio, setting the profit target at 3 times the stop-loss distance.
  4. Risk Management:

    • Employs a percentage risk model, risking a fixed percentage of capital on each trade.
    • Utilizes dynamic stop-loss and take-profit levels, automatically adjusting based on each trade's specifics.
  5. Trading Costs: Incorporates a 0.1% trading commission, reflecting a more realistic trading environment.

Strategy Advantages

  1. Trend Following: Effectively captures short-term trend changes using the 5EMA indicator, improving entry timing accuracy.

  2. Risk Control: Implements a dynamic stop-loss mechanism, automatically adjusting stop-loss positions based on market volatility, effectively controlling risk for each trade.

  3. Profit-Loss Ratio Optimization: Utilizes a 1:3 risk-reward ratio, pursuing higher profit potential while controlling risk.

  4. Automated Execution: The strategy can be fully automated on the TradingView platform, reducing human intervention and emotional influence.

  5. High Adaptability: Through parameterized design, the strategy can adapt to different market environments and trading instruments.

  6. Cost Consideration: Incorporation of trading commissions makes backtesting results closer to actual trading scenarios.

Strategy Risks

  1. False Breakout Risk: In ranging markets, frequent false breakout signals may lead to consecutive losses.

  2. Trend Reversal Risk: Frequent short positions in strong upward trends may face significant losses.

  3. Slippage Risk: Actual trading slippage may cause entry prices to deviate from ideal positions, affecting strategy performance.

  4. Overtrading: High volatility markets may generate excessive trading signals, increasing transaction costs.

  5. Parameter Sensitivity: Strategy performance may be sensitive to parameter settings such as EMA period and risk-reward ratio.

Strategy Optimization Directions

  1. Multi-Period Confirmation: Incorporate longer-term trend indicators, such as 20EMA or 50EMA, to reduce false breakout signals.

  2. Volatility Filtering: Introduce the ATR indicator to pause trading during high volatility periods, reducing risk.

  3. Market State Classification: Develop a market state identification module to adjust strategy parameters or pause trading in different market environments.

  4. Dynamic Risk Management: Dynamically adjust risk exposure for each trade based on account profit and loss, achieving more flexible capital management.

  5. Multi-Instrument Application: Test strategy performance across different trading instruments to achieve cross-instrument diversification.

  6. Machine Learning Optimization: Utilize machine learning algorithms to dynamically optimize parameters such as EMA period and risk-reward ratio.

  7. Fundamental Integration: Incorporate important economic data releases and other fundamental factors to adjust strategy behavior during specific periods.

Conclusion

The 5EMA Trend Following Strategy with Dynamic Stop-Loss and Take-Profit is a concise and effective quantitative trading method. It captures short-term trend reversal opportunities using the 5EMA indicator and manages risk through dynamic stop-losses and a fixed risk-reward ratio. The strategy's advantages lie in its simplicity, high degree of automation, and effective risk management. However, traders need to be aware of potential risks such as false breakouts and trend reversals.

To further enhance the strategy's robustness and profitability, consider introducing multi-period confirmation, volatility filtering, and market state classification. Additionally, exploring the use of machine learning techniques for dynamic parameter optimization and testing the strategy across multiple trading instruments are worthwhile directions.

Overall, this strategy provides a good starting point for short-term trend trading. Through continuous optimization and risk management, it has the potential to become a reliable quantitative trading system. However, before applying it to live trading, it is recommended to conduct thorough backtesting and paper trading to ensure the strategy's stability and reliability under various market conditions.

Source Pine Script

/*backtest
start: 2024-05-28 00:00:00
end: 2024-06-27 00:00:00
period: 2h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=5
strategy("5 EMA Short", overlay=true)

// Input
emaLength = input.int(5, "EMA Length", minval=1)
riskRewardRatio = input.float(3.0, "Risk-Reward Ratio", minval=1.0, step=0.1)

// Calculate 5 EMA
ema5 = ta.ema(close, emaLength)

// Identify alert candle
isAlertCandle = low > ema5 and low[1] > ema5[1]

// Entry condition
entryCondition = isAlertCandle[1] and low <= low[1]

// Calculate stop loss and take profit
stopLoss = high[1]
entryPrice = low[1]  // Entry price is the low of the alert candle
target = entryPrice - (stopLoss - entryPrice) * riskRewardRatio

// Variables to store trade information
var float tradeEntry = na
var float tradeSL = na
var float tradeTarget = na

// Execute strategy and store trade information
if (entryCondition)
    strategy.entry("Short", strategy.short, stop=stopLoss, limit=target)
    tradeEntry := entryPrice
    tradeSL := stopLoss
    tradeTarget := target

// Plot 5 EMA
plot(ema5, color=color.blue, linewidth=1, title="5 EMA")

// Plot entry, stop loss, and target only when a trade is triggered
plotshape(series=tradeEntry, title="Entry", location=location.absolute, color=color.yellow, style=shape.circle, size=size.tiny)
plotshape(series=tradeSL, title="Stop Loss", location=location.absolute, color=color.red, style=shape.circle, size=size.tiny)
plotshape(series=tradeTarget, title="Target", location=location.absolute, color=color.green, style=shape.circle, size=size.tiny)

Published by ChaoZhang on the FMZ strategy square · last modified 2024-06-28. Collected in the Quant Trading Vault. Unvalidated research: re-test with your own data and costs before trusting it.

Build this with AgenKit

A spec is not a trading system. Paste this into your coding agent and AgenKit runs a specialist team — architecture, test-first build, risk gates, review — with paper trading on by default.

in Claude Code, Cursor, Codex & 4 more
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/5EMA-Trend-Following-Strategy-with-Dynamic-Stop-Loss-and-Take-Profit.md
  1. npx agenkit install engineering-kit in your project
  2. Paste the command into your agent and approve each phase
  3. Get tests, backtest, risk controls and a paper-trading switch

Understand it first

Before you run this, read the lesson behind the idea and how to backtest it honestly. Specs like this one are the survivors of an unknown number of trials; deflate their results accordingly.

Related specs

esc

Try , , or .

↑↓ move↵ open