4 EMA Trend Strategy

Author: ChaoZhang, Date: 2023-12-26 11:10:39
Tags:

img

Overview

This strategy is based on the comparison of four EMA lines with different periods to implement trend-following trading. It goes long when the fast EMA line crosses above the medium EMA line, the medium EMA line crosses above the slow EMA line, and the slow EMA line crosses above the slowest EMA line. It goes short when the opposite crossing relationships happen. The strategy also incorporates date filter conditions, only trading within the specified date range.

Strategy Logic

The core logic of this strategy is based on the comparison of four EMA lines. The EMA lines can effectively smooth the price data and highlight the major trends. The fast EMA line reflects price change fastest, while the medium EMA has some lag, the slow EMA has more lag, and the slowest EMA has the most lag. When the fast EMA crosses above the medium EMA, the medium EMA crosses above the slow EMA, and the slow EMA crosses above the slowest EMA, it signals an uptrend, and the strategy will go long. When the opposite crossing sequence happens, it signals a downtrend and the strategy will go short.

The strategy also uses a date filter condition, only trading within the specified date range between 2018-06-01 and 2019-12-31. This avoids abnormal volatility outside this period affecting the strategy.

Specifically, the periods of the four EMA lines are 8, 13, 21, and 34 days respectively. They are relatively short-term to capture short-term and medium-term trends. The strategy will only generate trade signals when price data satisfy the EMA crossing relationships within the specified date range.

Advantage Analysis

The advantages of this 4-EMA trend strategy are:

  1. Using multiple EMA lines to identify trends with higher accuracy and effectively follow market trends.
  2. The short EMA periods can quickly respond to price changes and capture short-term and medium-term trends.
  3. The date filter avoids the impact of anomalous market moves and improves strategy stability.
  4. The strategy logic is simple and clear, easy to understand and backtest.
  5. The EMA parameters can be flexibly adjusted to adapt to different products and market conditions.

Risk Analysis

There are also some risks of this strategy:

  1. The inherent lag of EMA lines may miss short-term reversal opportunities.
  2. If the date range filter is set improperly, the sample size could be too small and backtest results unstable.
  3. The strategy relies solely on EMA relationship without other factors, which may generate false signals.
  4. There is no stop loss mechanism, leading to high capital risk.

To reduce the above risks, some optimization directions are:

  1. Combine other indicators like MACD, KD to confirm signal validity and avoid false signals.
  2. Add proper stop loss mechanisms to control per trade and total risk.
  3. Test more products and periods to adjust EMA parameters for better adaptation.

Optimization Directions

The main optimization directions are:

  1. Parameter Optimization: Adjust EMA periods to fit different cycles and products for better trend judgment.

  2. Risk Control: Add reasonable stop loss like ATR or trend-based stop loss to control per trade and total risk.

  3. Signal Filtering: Add other auxiliary indicators to avoid signals without a clear trend, e.g. RSI and MACD filters.

  4. Profit Taking: Set proper profit taking rules to lock in profits and avoid retracements.

  5. Automated Trading: Parameterize the strategy and integrate with auto-trading systems to expand applicability.

Conclusion

This is a simple and practical trend-following strategy based on 4-EMA line comparisons. It responds quickly and tracks short-term & medium-term trends effectively with good backtest results. We can optimize it by adjusting parameters, adding filters and stop losses to reduce risk and increase efficiency. Parameterization and automation are also important directions enabling wider applicability. In conclusion, the 4-EMA strategy is a robust and versatile quant trading strategy worthy of further research and optimization.


/*backtest
start: 2022-12-19 00:00:00
end: 2023-12-25 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=3
strategy("4 EMA TREND Strategy ", overlay=true)


length1 = input(8, minval=1)
outFAST = ema(close,length1)
plot(outFAST, color=green ,linewidth=3)

length2 = input(13, minval=1)
outM = ema(close, length2)
plot(outM, color=yellow,linewidth=3)

length3 = input(21, minval=1)
outSLOW = ema(close, length3)
plot(outSLOW, color=red,linewidth=3)

length4 = input(34, minval=1)
outSLOWEST = ema(close, length4)
plot(outSLOWEST, color=black,linewidth=3)

price = close 



    
yearfrom = input(2018)
yearuntil =input(2019)
monthfrom =input(6)
monthuntil =input(12)
dayfrom=input(1)
dayuntil=input(31)


if (  (outFAST>outM) and (outM > outSLOW) and(outSLOW>outSLOWEST)) 
    strategy.entry("BUY", strategy.long, stop=close, oca_name="TREND", comment="BUY")
    
else
    strategy.cancel(id="BUY")


if   (  (outFAST<outM) and (outM<outSLOW) and (outSLOW <outSLOWEST)) 

    strategy.entry("SELL", strategy.short,stop=close, oca_name="TREND", comment="SELL")
else
    strategy.cancel(id="SELL")
    
    
    
    
    
    
    
    


More