strategy · on price · open source

EMA crossover strategy (9 / 21)

9/21 EMA crossover strategy: buy the bullish cross, exit the bearish cross, with a 2%/6% bracket. Plain-English rules, code and a costed backtest. Free to run. Deepwick Script runs a subset of Pine Script.

v1Updated 3 Oct 2026▲ 0 users
Running…
Loading chart…

What the EMA Crossover strategy does

Long-only EMA crossover: enter when the fast EMA crosses above the slow EMA, exit on the reverse cross. The two EMAs are plotted on the chart for visual confirmation.

  • 9/21 is the classic short-term pair; 20/50 swings between days.
  • Raise the gap for fewer, larger signals; tighten it for faster flips.
  • Combine with an ATR-based stop or a higher-timeframe trend filter.

Entry and exit rules

    Language reference: Deepwick Script.

    Default inputs

    InputDefaultMinMax
    Fast EMA9250
    Slow EMA2110200
    Stop Loss %20.510
    Take Profit %6020

    How Deepwick fills this strategy

    • Market orders fill at the next bar's open.
    • Stops and limits follow the bar's path: open → nearer extreme → farther extreme → close.
    • Gaps fill at the open.
    • Commission is charged on every fill, and slippage applies to market and stop fills, as set in strategy().

    More on how backtests work

    Source code

    Deepwick Script, a subset of Pine Script · read-only

    //@version=6
    strategy("EMA Crossover", overlay=true, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=10, commission_type=strategy.commission.percent, commission_value=0.05)
    fast = input.int(9, "Fast EMA", minval=2, maxval=50, step=1)
    slow = input.int(21, "Slow EMA", minval=10, maxval=200, step=1)
    stopPct = input.float(2, "Stop Loss %", minval=0.5, maxval=10, step=0.1)
    tpPct = input.float(6, "Take Profit %", minval=0, maxval=20, step=0.1)
    colorMid = input.color("color.orange", "Color  Mid")
    colorLine = input.color("color.blue", "Color  Line")
    colorUp = input.color("color.green", "Color  Up")
    colorDown = input.color("color.red", "Color  Down")
    fastE = ta.ema(close, fast)
    slowE = ta.ema(close, slow)
    longSignal = strategy.position_size == 0 and ta.crossover(fastE, slowE)
    exitSignal = strategy.position_size > 0 and ta.crossunder(fastE, slowE)
    if longSignal
        strategy.entry("Long", strategy.long)
    if strategy.position_size > 0
        strategy.exit("Risk", stop=stopPct > 0 ? strategy.position_avg_price * (1 - stopPct / 100) : na, limit=tpPct > 0 ? strategy.position_avg_price * (1 + tpPct / 100) : na)
    if exitSignal
        strategy.close("Long", comment="EMA cross down")
    p_Fast_EMA = plot(fastE, "Fast EMA", color=colorMid, linewidth=2)
    p_Slow_EMA = plot(slowE, "Slow EMA", color=colorLine, linewidth=2)
    plotshape(longSignal, "Long", style=shape.triangleup, location=location.belowbar, color=colorUp, text="L")
    plotshape(exitSignal, "Exit", style=shape.triangledown, location=location.abovebar, color=colorDown, text="X")
    alertcondition(longSignal, "EMA long entry", "Fast EMA crossed above slow EMA")
    alertcondition(exitSignal, "EMA exit", "Fast EMA crossed below slow EMA")
    

    Run it in Deepwick

    1. Open a chart of any crypto pair or CFD.
    2. Click “Scripts” and add this strategy.
    3. Press Run and read the trades in the Strategy Tester.
    Start free

    FAQ

    Which EMA crossover is best, 9/21 or 50/200?

    Neither is best in general. 9/21 trades often and suits intraday or swing tests; 50/200 is the slow golden-cross style (see the SMA crossover page). Compare both on the same symbol, period and costs and look at trades, drawdown and profit factor, not only net profit.

    Why does the EMA crossover lose money in ranges?

    In sideways markets the two EMAs cross back and forth, and each cross pays commission and spread. That is why the backtest table shows costs and the number of trades next to net profit.

    Can I add a trend filter?

    Yes. A common change is to only take crosses when price is above a longer average, e.g. close > ta.ema(close, 200). Add it to the entry condition and rerun the backtest with the same costs.

    Comments

    No comments yet.

    Sign in to comment.