Momentum Tracking Dual-EMA Crossover Strategy

Author: ChaoZhang, Date: 2024-02-26 16:40:29



This strategy is a trend-following algorithmic trading strategy. It calculates two EMA lines with different parameters and generates trading signals when the Golden Cross and Death Cross occur between the two EMAs. The strategy also combines multiple EMA lines for profit exit and sets stop loss points to control risks.

Strategy Principle

The strategy uses 4 EMA lines, including a fast EMA and a slow EMA, whose crossover is used to generate buy and sell signals. In addition, two EMA lines with parameters between fast and slow EMAs are used to partially or completely exit positions in advance to lock in profits.

Specifically, when the fast EMA crosses above the slow EMA, a buy signal is generated. When the fast EMA crosses below the slow EMA, a sell signal is generated. This is a typical dual-EMA crossover strategy. To better track trends and increase profitability, after entering a position, the strategy will selectively exit part or all of the position when the fast EMA crosses above the second EMA line or when the fast EMA crosses below the third EMA line.

In addition, the strategy sets both long and short stop loss points to prevent excessive losses. Specifically, the stop loss for long positions is set at 6% of the entry price, and 3% for short positions.

Advantage Analysis

Compared with a typical dual-EMA crossover strategy, the main advantages of this strategy include:

  1. Setting up multiple EMA lines for profit exit can better lock in profits and prevent profit shrinkage during subsequent pullbacks.

  2. The short position has a smaller stop loss, which can withstand greater normal market fluctuations and prevent frequent stop loss.

  3. Setting EMA lines with different parameters for profit exit allows choosing the optimal exit point based on market conditions.

  4. The overall strategy has good trend-following capability to capture greater profits from mid- to long-term trends.

Risk Analysis

The main risks of this strategy include:

  1. In range-bound markets, the trading signals generated by the EMA lines are frequent, which can lead to over-trading.

  2. The short stop loss can only prevent extreme market conditions and cannot prevent significant drawdowns in the strategy account.

  3. The risk of drawdowns still exists. Profits may shrink significantly when long-term adjustment occurs.

  4. The strategy is sensitive to parameter tuning. Improper configuration may cause strategy failure.


In view of the above risks, the strategy can be optimized in the following aspects:

  1. Increase machine learning algorithms to assist in trend judgment and reduce mis-trading probabilities.

  2. Increase adaptive stop loss mechanism to dynamically adjust stop loss based on market volatility.

  3. Set capital utilization to avoid excessive capital occupation and increase position management mechanism.

  4. Select trading products with obvious trends and high fluctuations.

  5. Increase parameter optimization module to achieve automatic optimization and update of parameters.


Overall, the dual-EMA crossover strategy is a cost-effective trend-following strategy. It has advantages like multiple EMA lines for profit taking, small short stops, and good trend-following capability. However, there are still some risks with this strategy. It needs further parameter tuning optimization and incorporation of machine learning algorithms to improve stability. In general, this strategy is suitable for investors with some trading experience to carry out algorithm trading.

start: 2023-02-19 00:00:00
end: 2024-02-25 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]

// This Pine Scriptâ„¢ code is subject to the terms of the Mozilla Public License 2.0 at
// © RealTraderAkeme

strategy("AKEME_EMA_CROSS_V6", overlay=true)

emaFast_op = input(title="Fast_EMA", defval=6)
emaSlow_op = input(title="Slow_EMA", defval=26)
emaExit_op = input(title="Sell_EMA_Exit",defval=10)
emabuyExit_op = input(title="Buy_EMA_Exit",defval=20)
Order_Value = input(defval=1000, title="Order_Value in Pounds") 
Direction_Of_Trade = input(title="Trade Direction", defval="Both")


fastEMA = ta.ema(close, emaFast_op)
slowEMA = ta.ema(close,emaSlow_op)
emaExit = ta.ema(close,emaExit_op)
emabuyExit = ta.ema(close,emabuyExit_op)
Entry_Ratio = strategy.openprofit/Order_Value


plot(fastEMA,, linewidth = 2)
plot(slowEMA,color =, linewidth = 2)
plot(emaExit,color = color.gray, linewidth = 2)
plot(series=emabuyExit, color= color.rgb(210, 74, 235), linewidth=2)

longOK  = (Direction_Of_Trade == "Long") or (Direction_Of_Trade == "Both")
shortOK = (Direction_Of_Trade == "Short") or (Direction_Of_Trade == "Both")

longCondition = ta.crossover(fastEMA, slowEMA) and longOK 
if (longCondition)  
    strategy.entry("Buy", strategy.long) 

shortCondition = ta.crossunder(fastEMA, slowEMA) and shortOK
if (shortCondition)
    strategy.entry("Sell", strategy.short)

if (strategy.position_size > 0 and shortCondition)
    strategy.exit(id="exit Buy", stop=close)
if (strategy.position_size < 0 and longCondition)
    strategy.exit(id="exit Sell", stop=close)

/////////////////////////////////////////////////////TAKE PROFIT CONDITIONS////////////////////////////////////////////////////////

if  ta.crossunder(fastEMA, emabuyExit) and Entry_Ratio > 0.08333
    strategy.close("Buy",comment = "Exit")

if  ta.crossover(fastEMA, emaExit) and Entry_Ratio > 0.016666
    strategy.close("Sell",comment = "Exit")

if Entry_Ratio > 0.4166666 //0.4166666 
    strategy.close("Buy",comment = "Exit", qty_percent = 100)

if Entry_Ratio > 0.0833333//0.0833333
    strategy.close("Sell",comment = "Exit")//50

if Entry_Ratio > 0.1111111//4000
    strategy.close("Sell",comment = "Exit", qty_percent = 50)

if ta.crossover(fastEMA, emaExit) and Entry_Ratio > 0.278 //Percentage 
    strategy.close("Sell",comment = "Exit")

////////////////////////////////////////////STOP LOSS AS PERCENTAGE OF ENTRY CONDITIONS///////////////////////////////////////////

if Entry_Ratio < -0.05555555555
    strategy.close("Buy",comment = "Exit")
if Entry_Ratio < -0.027777777777
    strategy.close("Sell",comment = "Exit")// The Sell Stoloss is half the buying stoploss.